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

Commit

Permalink
fix: ignore 403 while deleting namespaces (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatousJobanek authored May 20, 2019
1 parent ace3ccd commit 97b0525
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
31 changes: 30 additions & 1 deletion controller/tenants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (s *TenantsControllerTestSuite) TestSuccessfullyDeleteTenants() {
HasNoNamespace()
})

s.T().Run("ok even if namespace missing", func(t *testing.T) {
s.T().Run("ok even if namespace missing while returning 404", func(t *testing.T) {
// if the namespace record exist in the DB, but the `delete namespace` call on the cluster endpoint fails with a 404
// given
defer gock.OffAll()
Expand Down Expand Up @@ -190,6 +190,35 @@ func (s *TenantsControllerTestSuite) TestSuccessfullyDeleteTenants() {
HasNoNamespace()
})

s.T().Run("ok even if namespace missing while returning 403", func(t *testing.T) {
// if the namespace record exist in the DB, but the `delete namespace` call on the cluster endpoint fails with a 403
// given
defer gock.OffAll()
testdoubles.MockCommunicationWithAuth(test.ClusterURL)
gock.New(test.ClusterURL).
Delete("/oapi/v1/projects/bar-che").
SetMatcher(test.ExpectRequest(test.HasJWTWithSub("devtools-sre"))).
Reply(403).
BodyString(`{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Forbidden"}`)
gock.New(test.ClusterURL).
Delete("/oapi/v1/projects/bar").
SetMatcher(test.ExpectRequest(test.HasJWTWithSub("devtools-sre"))).
Reply(200).
BodyString(`{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Success"}`)

fxt := tf.FillDB(t, s.DB, tf.AddSpecificTenants(tf.SingleWithName("bar")), tf.AddNamespaces(environment.TypeUser, environment.TypeChe))
id := fxt.Tenants[0].ID

svc, ctrl, reset := s.newTestTenantsController()
defer reset()
// when
goatest.DeleteTenantsNoContent(t, createValidSAContext("fabric8-auth"), svc, ctrl, id)
// then
assertion.AssertTenantFromService(t, repo, id).
DoesNotExist().
HasNoNamespace()
})

s.T().Run("ok when unsupported namespaces exist in DB", func(t *testing.T) {
// given
defer gock.OffAll()
Expand Down
2 changes: 1 addition & 1 deletion openshift/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ var IgnoreWhenDoesNotExistOrConflicts = AfterDoCallback{
return func(context CallbackContext) (*Result, error) {
result, err := previousCallback(context)
code := result.Response.StatusCode
if code == http.StatusNotFound || code == http.StatusConflict {
if isNotPresent(code) || code == http.StatusConflict {
// todo investigate why logging here ends with panic: runtime error: index out of range in common logic
logrus.WithFields(map[string]interface{}{
"action": context.Method.action,
Expand Down
12 changes: 12 additions & 0 deletions openshift/callback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,18 @@ func TestIgnoreWhenDoesNotExist(t *testing.T) {
assert.Empty(t, callbackResult)
})

t.Run("when there is 403, then it ignores it even if there is an error", func(t *testing.T) {
// given
result := openshift.NewResult(&http.Response{StatusCode: http.StatusForbidden}, []byte{}, fmt.Errorf("forbidden"))

// when
callbackResult, err := openshift.IgnoreWhenDoesNotExistOrConflicts.Create(newAfterCallbackFunc(result, fmt.Errorf("forbidden")))(callbackContext)

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

t.Run("when there is 409, then it ignores it even if there is an error", func(t *testing.T) {
// given
result := openshift.NewResult(&http.Response{StatusCode: http.StatusConflict}, []byte{}, fmt.Errorf("conflict"))
Expand Down

0 comments on commit 97b0525

Please sign in to comment.