Skip to content

Commit

Permalink
add ttr cleanup logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Devtools committed Nov 21, 2024
1 parent bdba663 commit b4f334d
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion testsupport/cleanup/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (c *cleanTask) cleanObject() {
objToClean, ok := c.objToClean.DeepCopyObject().(client.Object)
require.True(c.t, ok)
userSignup, isUserSignup := c.objToClean.(*toolchainv1alpha1.UserSignup)
nsTemplateTier, isNsTemplateTier := c.objToClean.(*toolchainv1alpha1.NSTemplateTier)
kind := objToClean.GetObjectKind().GroupVersionKind().Kind
if kind == "" {
kind = reflect.TypeOf(c.objToClean).Elem().Name()
Expand All @@ -126,10 +127,13 @@ func (c *cleanTask) cleanObject() {
}
}
}
// if the object was NSTemplateTier, then let's check that the TierTemplateRevisions were deleted as well
_, err := c.verifyTierTemplateRevisionsDeleted(isNsTemplateTier, nsTemplateTier, true)
require.NoError(c.t, err)

// wait until deletion is done
c.t.Logf("waiting until %s: %s is completely deleted", kind, objToClean.GetName())
err := wait.PollUntilContextTimeout(context.TODO(), defaultRetryInterval, defaultTimeout, true, func(ctx context.Context) (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), defaultRetryInterval, defaultTimeout, true, func(ctx context.Context) (done bool, err error) {
if err := c.client.Get(context.TODO(), test.NamespacedName(objToClean.GetNamespace(), objToClean.GetName()), objToClean); err != nil {
if errors.IsNotFound(err) {
// if the object was UserSignup, then let's check that the MUR is deleted as well
Expand All @@ -140,6 +144,10 @@ func (c *cleanTask) cleanObject() {
if spaceDeleted, err := c.verifySpaceDeleted(isUserSignup, userSignup, false); !spaceDeleted || err != nil {
return false, err
}
// if the object was NSTemplateTier, then let's check that the TTRs were deleted as well
if ttrsDeleted, err := c.verifyTierTemplateRevisionsDeleted(isNsTemplateTier, nsTemplateTier, false); !ttrsDeleted || err != nil {
return false, err
}
return true, nil
}
c.t.Logf("problem with getting the related %s '%s': %s", kind, objToClean.GetName(), err)
Expand Down Expand Up @@ -240,3 +248,36 @@ func (c *cleanTask) verifySpaceDeleted(isUserSignup bool, userSignup *toolchainv
}
return true, nil
}

func (c *cleanTask) verifyTierTemplateRevisionsDeleted(isNsTemplateTier bool, nsTemplateTier *toolchainv1alpha1.NSTemplateTier, delete bool) (bool, error) {
if !isNsTemplateTier {
return true, nil
}
ttrs := &toolchainv1alpha1.TierTemplateRevisionList{}
if err := c.client.List(context.TODO(), ttrs,
client.InNamespace(nsTemplateTier.GetNamespace()),
client.MatchingLabels{toolchainv1alpha1.TierLabelKey: nsTemplateTier.GetName()}); err != nil {
c.t.Logf("problem with getting the ttrs for tier %s: %s", nsTemplateTier, err)

Check failure on line 260 in testsupport/cleanup/clean.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

printf: (*testing.common).Logf format %s has arg nsTemplateTier of wrong type *github.com/codeready-toolchain/api/api/v1alpha1.NSTemplateTier (govet)

Check failure on line 260 in testsupport/cleanup/clean.go

View workflow job for this annotation

GitHub Actions / Unit Tests

(*testing.common).Logf format %s has arg nsTemplateTier of wrong type *github.com/codeready-toolchain/api/api/v1alpha1.NSTemplateTier
return false, err
}
if len(ttrs.Items) == 0 {
c.t.Logf("the NSTemplateTier %s doesn't have TTRs", nsTemplateTier.GetName())
return true, nil
}
if delete {
for _, ttr := range ttrs.Items {
c.t.Logf("deleting also the related TTR %s", ttr.GetName())
if err := c.client.Delete(context.TODO(), &ttr, propagationPolicyOpts); err != nil {

Check failure on line 270 in testsupport/cleanup/clean.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

G601: Implicit memory aliasing in for loop. (gosec)
if errors.IsNotFound(err) {
continue
}
c.t.Logf("problem with deleting the related TTR %s: %s", ttr.GetName(), err)
return false, err
}
}
} else if len(ttrs.Items) > 0 {
// ttrs are still there
return false, nil
}
return true, nil
}

0 comments on commit b4f334d

Please sign in to comment.