Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

Commit

Permalink
fix: reset result when either 404 or 409 is ignored (during deletion) (
Browse files Browse the repository at this point in the history
  • Loading branch information
MatousJobanek authored Jan 23, 2019
1 parent 94f6c55 commit 8e221ab
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 11 deletions.
16 changes: 10 additions & 6 deletions openshift/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ var WhenConflictThenDeleteAndRedo = AfterDoCallback{
// "method": method.action,
// "object": object,
//}, "there was a conflict, trying to delete the object and re-do the operation")
fmt.Println(fmt.Sprintf("WARNING: there was a conflict when doing %s on object %s, trying to delete the object and re-do the operation",
method.action, object.ToString()))
fmt.Println(fmt.Sprintf("WARNING: there was a conflict when doing %s on object %s/%s, trying to delete the object and re-do the operation",
method.action, environment.GetKind(object), environment.GetName(object)))
err := checkHTTPCode(objEndpoints.Apply(client, object, http.MethodDelete))
if err != nil {
return errors.Wrap(err, "delete request failed while removing an object because of a conflict")
Expand Down Expand Up @@ -226,8 +226,12 @@ var IgnoreWhenDoesNotExistOrConflicts = AfterDoCallback{
// "object": object.ToString(),
// "message": result.Body,
//}, "failed to %s the object. Ignoring this error because it probably does not exist or is being removed", method.action)
fmt.Println(fmt.Sprintf("WARNING: failed to %s the object %s - reveived response %s. "+
"Ignoring this error because it probably does not exist or is being removed", method.action, object.ToString(), result.response.Status))
fmt.Println(fmt.Sprintf("WARNING: failed to %s the object %s/%s - reveived response %s. "+
"Ignoring this error because it probably does not exist or is being removed",
method.action, environment.GetKind(object), environment.GetName(object), result.response.Status))
result.err = nil
result.Body = []byte{}
result.response = nil
return nil
}
return checkHTTPCode(result, result.err)
Expand Down Expand Up @@ -270,9 +274,9 @@ var TryToWaitUntilIsGone = AfterDoCallback{
// "cluster": client.MasterURL,
// "error-message": msg,
//}, "unable to finish the action %s for an object as there were %d of unsuccessful retries to completely remove the objects from the cluster", method.action)
fmt.Println(fmt.Sprintf("WARNING: unable to finish the action %s for an object %s as there were %d of unsuccessful retries "+
fmt.Println(fmt.Sprintf("WARNING: unable to finish the action %s for an object %s/%s as there were %d of unsuccessful retries "+
"to completely remove the objects from the cluster %s. The retrieved errors:%s",
method.action, object, retries, client.MasterURL, msg))
method.action, environment.GetKind(object), environment.GetName(object), retries, client.MasterURL, msg))
}
return nil
},
Expand Down
8 changes: 3 additions & 5 deletions openshift/callback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,7 @@ func TestWhenConflictThenDeleteAndRedoAction(t *testing.T) {
defer gock.OffAll()
gock.New("https://starter.com").
Delete("/api/v1/namespaces/john-jenkins/persistentvolumeclaims/jenkins-home").
Reply(404)
gock.New("https://starter.com").
Get("/api/v1/namespaces/john-jenkins/persistentvolumeclaims/jenkins-home").
Reply(404)
Reply(500)
result := openshift.NewResult(&http.Response{StatusCode: http.StatusConflict}, []byte{}, nil)

// when
Expand All @@ -264,7 +261,7 @@ func TestWhenConflictThenDeleteAndRedoAction(t *testing.T) {
// then
test.AssertError(t, err,
test.HasMessageContaining("delete request failed while removing an object because of a conflict"),
test.HasMessageContaining("server responded with status: 404 for the DELETE request"))
test.HasMessageContaining("server responded with status: 500 for the DELETE request"))
})

t.Run("when there is a second conflict while redoing the action, then it return an error and stops redoing", func(t *testing.T) {
Expand Down Expand Up @@ -305,6 +302,7 @@ func TestIgnoreWhenDoesNotExist(t *testing.T) {

// then
assert.NoError(t, err)
assert.Empty(t, result.Body)
})

t.Run("when there is 409, then it ignores it even if there is an error", func(t *testing.T) {
Expand Down
58 changes: 58 additions & 0 deletions openshift/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,64 @@ func (s *ServiceTestSuite) TestDeleteAndGet() {
HasNoNamespace()
}

func (s *ServiceTestSuite) TestCleanAndGet() {
// given
defer gock.OffAll()
config, reset := test.LoadTestConfig(s.T())
defer reset()

testdoubles.MockCleanRequestsToOS(ptr.Int(0), test.ClusterURL)
fxt := tf.FillDB(s.T(), s.DB, tf.AddSpecificTenants(tf.SingleWithName("john")), tf.AddDefaultNamespaces())
tnnt := fxt.Tenants[0]
service := testdoubles.NewOSService(
config,
testdoubles.AddUser("john").WithToken("abc123"),
tenant.NewTenantRepository(s.DB, tnnt.ID))

// when
err := service.Delete(environment.DefaultEnvTypes, fxt.Namespaces, openshift.DeleteOpts().EnableSelfHealing())

// then
require.NoError(s.T(), err)
assertion.AssertTenantFromDB(s.T(), s.DB, tnnt.ID).
Exists()
}

func (s *ServiceTestSuite) TestCleanAndGetWhenReturns404() {
// given
defer gock.OffAll()
config, reset := test.LoadTestConfig(s.T())
defer reset()

gock.New(test.ClusterURL).
Delete("").
SetMatcher(test.ExpectRequest(
test.HasUrlMatching(`.*\/(persistentvolumeclaims|configmaps|services|deploymentconfigs|routes)\/.*`))).
Persist().
Reply(404).
BodyString("{}")
gock.New(test.ClusterURL).
Get("").
SetMatcher(test.ExpectRequest(
test.HasUrlMatching(`.*\/(persistentvolumeclaims)\/.*`))).
Persist().
Reply(404)
fxt := tf.FillDB(s.T(), s.DB, tf.AddSpecificTenants(tf.SingleWithName("john")), tf.AddDefaultNamespaces())
tnnt := fxt.Tenants[0]
service := testdoubles.NewOSService(
config,
testdoubles.AddUser("john").WithToken("abc123"),
tenant.NewTenantRepository(s.DB, tnnt.ID))

// when
err := service.Delete(environment.DefaultEnvTypes, fxt.Namespaces, openshift.DeleteOpts().EnableSelfHealing())

// then
require.NoError(s.T(), err)
assertion.AssertTenantFromDB(s.T(), s.DB, tnnt.ID).
Exists()
}

func (s *ServiceTestSuite) TestNumberOfCallsToCluster() {
// given
defer gock.OffAll()
Expand Down

0 comments on commit 8e221ab

Please sign in to comment.