Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(e2e): do not test deletion configmap is true #964

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions tests/e2e/controller_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ func TestOdhOperator(t *testing.T) {

// Run deletion if skipDeletion is not set
if !skipDeletion {
t.Run("delete components", deletionTestSuite)

// This test case recreates entire DSC again and deletes afterward
t.Run("remove components by using labeled configmap", cfgMapDeletionTestSuite)
// this is a negative test case, since by using the positive CM('true'), even CSV gets deleted which leaves no operator pod in prow
t.Run("components should not be removed if labeled is set to 'false' on configmap", cfgMapDeletionTestSuite)

t.Run("delete components", deletionTestSuite)
}
}

Expand Down
85 changes: 23 additions & 62 deletions tests/e2e/dsc_cfmap_deletion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ import (
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"

dsc "github.com/opendatahub-io/opendatahub-operator/v2/apis/datasciencecluster/v1"
dsci "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/upgrade"
)
Expand All @@ -26,83 +23,47 @@ func cfgMapDeletionTestSuite(t *testing.T) {
defer removeDeletionConfigMap(testCtx)

t.Run(testCtx.testDsc.Name, func(t *testing.T) {
t.Run("create data science cluster", func(t *testing.T) {
err = testCtx.testDSCCreation()
require.NoError(t, err, "Error to create DSC instance")
t.Run("create configmap but set to disable deletion", func(t *testing.T) {
err = testCtx.testDSCDeletionUsingConfigMap("false")
require.NoError(t, err, "Configmap should not delete DSC instance")
})

t.Run("ensure all components created", func(t *testing.T) {
err = testCtx.testAllApplicationCreation(t)
require.NoError(t, err, "Error to create DSC instance")
})

t.Run("trigger deletion using labeled config map", func(t *testing.T) {
err = testCtx.testDSCDeletionUsingConfigMap()
require.NoError(t, err, "Error to delete DSC instance")
})

t.Run("owned namespaces should be deleted", func(t *testing.T) {
err = testCtx.testOwnedNamespacesDeletion()
t.Run("owned namespaces should be not deleted", func(t *testing.T) {
err = testCtx.testOwnedNamespacesAllExist()
require.NoError(t, err, "Error while deleting owned namespaces")
})

t.Run("dsci should be deleted", func(t *testing.T) {
err = testCtx.testDSCIDeletion()
require.NoError(t, err, "failed deleting DSCI")
})

t.Run("applications resources should be deleted", func(t *testing.T) {
err = testCtx.testAllApplicationDeletion()
require.NoError(t, err, "Error to delete component")
})
})
}

func (tc *testContext) testDSCIDeletion() error {
dsciInstances := &dsci.DSCInitializationList{}
if err := tc.customClient.List(context.TODO(), dsciInstances); err != nil {
return fmt.Errorf("failed while listing DSCIs: %w", err)
}

if len(dsciInstances.Items) != 0 {
return fmt.Errorf("expected DSCI removal, but got %v", dsciInstances)
}

return nil
}

func (tc *testContext) testDSCDeletionUsingConfigMap() error {
func (tc *testContext) testDSCDeletionUsingConfigMap(enableDeletion string) error {
dscLookupKey := types.NamespacedName{Name: tc.testDsc.Name}
expectedDSC := &dsc.DataScienceCluster{}

if err := createDeletionConfigMap(tc); err != nil {
if err := createDeletionConfigMap(tc, enableDeletion); err != nil {
return err
}

err := tc.customClient.Get(tc.ctx, dscLookupKey, expectedDSC)
if err == nil {
dscerr := tc.customClient.Delete(tc.ctx, expectedDSC, &client.DeleteOptions{})
if dscerr != nil {
return fmt.Errorf("error deleting DSC instance %s: %w", expectedDSC.Name, dscerr)
}
} else if !k8serrors.IsNotFound(err) {
if err != nil {
return fmt.Errorf("error getting DSC instance :%w", err)
// should have DSC instance
if err != nil {
if k8serrors.IsNotFound(err) {
return fmt.Errorf("should have DSC instance in cluster:%w", err)
}
return fmt.Errorf("error getting DSC instance :%w", err)
}

return nil
}
func (tc *testContext) testOwnedNamespacesAllExist() error {
namespaces, err := tc.kubeClient.CoreV1().Namespaces().List(tc.ctx, metav1.ListOptions{
LabelSelector: labels.ODH.OwnedNamespace,
})

func (tc *testContext) testOwnedNamespacesDeletion() error {
if err := wait.PollUntilContextTimeout(tc.ctx, tc.resourceRetryInterval, tc.resourceCreationTimeout, false, func(ctx context.Context) (bool, error) {
namespaces, err := tc.kubeClient.CoreV1().Namespaces().List(ctx, metav1.ListOptions{
LabelSelector: labels.ODH.OwnedNamespace,
})

return len(namespaces.Items) == 0, err
}); err != nil {
return fmt.Errorf("failed waiting for all owned namespaces to be deleted: %w", err)
if err != nil {
return fmt.Errorf("failed getting owned namespaces %w", err)
}
if len(namespaces.Items) == 0 {
return fmt.Errorf("all namespaces are gone")
}

return nil
Expand All @@ -112,13 +73,13 @@ func removeDeletionConfigMap(tc *testContext) {
_ = tc.kubeClient.CoreV1().ConfigMaps(tc.operatorNamespace).Delete(context.TODO(), "delete-self-managed", metav1.DeleteOptions{})
}

func createDeletionConfigMap(tc *testContext) error {
func createDeletionConfigMap(tc *testContext, enableDeletion string) error {
configMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "delete-self-managed",
Namespace: tc.operatorNamespace,
Labels: map[string]string{
upgrade.DeleteConfigMapLabel: "true",
upgrade.DeleteConfigMapLabel: enableDeletion,
},
},
}
Expand Down
37 changes: 35 additions & 2 deletions tests/e2e/dsc_deletion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,36 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

dsc "github.com/opendatahub-io/opendatahub-operator/v2/apis/datasciencecluster/v1"
dsci "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/components"
)

func deletionTestSuite(t *testing.T) {
testCtx, err := NewTestContext()
require.NoError(t, err)

t.Run("ensure all components created", func(t *testing.T) {
err = testCtx.testAllApplicationCreation(t)
require.NoError(t, err, "Error to create DSC instance")
})

t.Run(testCtx.testDsc.Name, func(t *testing.T) {
t.Run("Deletion: DataScienceCluster instance", func(t *testing.T) {
err = testCtx.testDSCDeletion()
err = testCtx.testDeletionExistDSC()
require.NoError(t, err, "Error to delete DSC instance")
})
t.Run("Deletion: Application Resource", func(t *testing.T) {
err = testCtx.testAllApplicationDeletion()
require.NoError(t, err, "Error to delete component")
})
t.Run("Deletion: DSCI instance", func(t *testing.T) {
err = testCtx.testDeletionExistDSCI()
require.NoError(t, err, "Error to delete DSCI instance")
})
})
}

func (tc *testContext) testDSCDeletion() error {
func (tc *testContext) testDeletionExistDSC() error {
// Delete test DataScienceCluster resource if found

dscLookupKey := types.NamespacedName{Name: tc.testDsc.Name}
Expand Down Expand Up @@ -123,3 +133,26 @@ func (tc *testContext) testAllApplicationDeletion() error {
}
return err
}

// To test if DSCI CR is in the cluster and no problem to delete it
// if fail on any of these two conditios, fail test.
func (tc *testContext) testDeletionExistDSCI() error {
// Delete test DSCI resource if found

dsciLookupKey := types.NamespacedName{Name: tc.testDSCI.Name}
expectedDSCI := &dsci.DSCInitialization{}

err := tc.customClient.Get(tc.ctx, dsciLookupKey, expectedDSCI)
if err == nil {
dscierr := tc.customClient.Delete(tc.ctx, expectedDSCI, &client.DeleteOptions{})
if dscierr != nil {
return fmt.Errorf("error deleting DSCI instance %s: %w", expectedDSCI.Name, dscierr)
}
} else if !errors.IsNotFound(err) {
if err != nil {
return fmt.Errorf("error getting DSCI instance :%w", err)
}
}

return nil
}
Loading