Skip to content

Commit

Permalink
fix: ignore category value delete error
Browse files Browse the repository at this point in the history
added basic test which creates 2 clusters with same name and deletes last one
  • Loading branch information
deepakm-ntnx committed Aug 9, 2024
1 parent ce22e6b commit f1de4e2
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 8 deletions.
8 changes: 5 additions & 3 deletions controllers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,11 @@ func deleteCategoryKeyValues(ctx context.Context, client *prismclientv3.Client,

err = client.V3.DeleteCategoryValue(ctx, key, value)
if err != nil {
errorMsg := fmt.Errorf("failed to delete category with key %s. error: %v", key, err)
log.Error(errorMsg, "failed to delete category")
return errorMsg
errorMsg := fmt.Errorf("failed to delete category value with key:value %s:%s. error: %v", key, value, err)
log.Error(errorMsg, "failed to delete category value")

Check warning on line 508 in controllers/helpers.go

View check run for this annotation

Codecov / codecov/patch

controllers/helpers.go#L507-L508

Added lines #L507 - L508 were not covered by tests
// NCN-101935: If the category value still has VMs assigned, do not delete the category key:value
// TODO:deepakmntnx Add a check for specific error mentioned in NCN-101935
return nil

Check warning on line 511 in controllers/helpers.go

View check run for this annotation

Codecov / codecov/patch

controllers/helpers.go#L511

Added line #L511 was not covered by tests
}
}

Expand Down
107 changes: 102 additions & 5 deletions test/e2e/categories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package e2e

import (
"context"
"os"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -41,23 +42,31 @@ var _ = Describe("Nutanix categories", Label("capx-feature-test", "categories"),
)

var (
namespace *corev1.Namespace
clusterName string
clusterResources *clusterctl.ApplyClusterTemplateAndWaitResult
cancelWatches context.CancelFunc
testHelper testHelperInterface
namespace *corev1.Namespace
namespace2 *corev1.Namespace
clusterName string
clusterResources *clusterctl.ApplyClusterTemplateAndWaitResult
clusterResources2 *clusterctl.ApplyClusterTemplateAndWaitResult
cancelWatches context.CancelFunc
cancelWatches2 context.CancelFunc
testHelper testHelperInterface
)

BeforeEach(func() {
testHelper = newTestHelper(e2eConfig)
clusterName = testHelper.generateTestClusterName(specName)
clusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult)
clusterResources2 = new(clusterctl.ApplyClusterTemplateAndWaitResult)
Expect(bootstrapClusterProxy).NotTo(BeNil(), "BootstrapClusterProxy can't be nil")
namespace, cancelWatches = setupSpecNamespace(ctx, specName, bootstrapClusterProxy, artifactFolder)
namespace2, cancelWatches2 = setupSpecNamespace(ctx, specName+"-2", bootstrapClusterProxy, artifactFolder)
})

AfterEach(func() {
dumpSpecResourcesAndCleanup(ctx, specName, bootstrapClusterProxy, artifactFolder, namespace, cancelWatches, clusterResources.Cluster, e2eConfig.GetIntervals, skipCleanup)
if clusterResources2 != nil {
dumpSpecResourcesAndCleanup(ctx, specName+"-2", bootstrapClusterProxy, artifactFolder, namespace2, cancelWatches2, clusterResources2.Cluster, e2eConfig.GetIntervals, skipCleanup)
}
})

It("Create a cluster with default cluster categories (no additional categories)", func() {
Expand Down Expand Up @@ -174,4 +183,92 @@ var _ = Describe("Nutanix categories", Label("capx-feature-test", "categories"),

By("PASSED!")
})

It("Create and delete 2 clusters with same name with default cluster categories and should succeed", Label("same-name-two-cluster-test"), func() {
Expect(namespace).NotTo(BeNil())
flavor := clusterctl.DefaultFlavor
expectedClusterNameCategoryKey := infrav1.DefaultCAPICategoryKeyForName
By("Creating a workload cluster 1", func() {
testHelper.deployClusterAndWait(
deployClusterParams{
clusterName: clusterName,
namespace: namespace,
flavor: flavor,
clusterctlConfigPath: clusterctlConfigPath,
artifactFolder: artifactFolder,
bootstrapClusterProxy: bootstrapClusterProxy,
}, clusterResources)
})

By("Checking cluster category condition is true", func() {
testHelper.verifyConditionOnNutanixCluster(verifyConditionParams{
clusterName: clusterName,
namespace: namespace,
bootstrapClusterProxy: bootstrapClusterProxy,
expectedCondition: clusterv1.Condition{
Type: infrav1.ClusterCategoryCreatedCondition,
Status: corev1.ConditionTrue,
},
})
})

By("Checking if a category was created", func() {
testHelper.verifyCategoryExists(ctx, expectedClusterNameCategoryKey, clusterName)
})

By("Checking if there are VMs assigned to this category", func() {
expectedCategories := map[string]string{
expectedClusterNameCategoryKey: clusterName,
}
testHelper.verifyCategoriesNutanixMachines(ctx, clusterName, namespace.Name, expectedCategories)
})

By("Setting different Control plane endpoint IP for 2nd cluster", func() {
cp2EndpointIP := testHelper.getVariableFromE2eConfig("CONTROL_PLANE_ENDPOINT_IP_2")
if cp2EndpointIP == "" {
cp2EndpointIP = os.Getenv("CONTROL_PLANE_ENDPOINT_IP_2")
if cp2EndpointIP == "" {
Fail("CONTROL_PLANE_ENDPOINT_IP_2 not set")
}
}
testHelper.updateVariableInE2eConfig("CONTROL_PLANE_ENDPOINT_IP", cp2EndpointIP)
})

By("Creating a workload cluster 2 with same name", func() {
testHelper.deployClusterAndWait(
deployClusterParams{
clusterName: clusterName,
namespace: namespace2,
flavor: flavor,
clusterctlConfigPath: clusterctlConfigPath,
artifactFolder: artifactFolder,
bootstrapClusterProxy: bootstrapClusterProxy,
}, clusterResources2)
})

By("Checking cluster category condition is true", func() {
testHelper.verifyConditionOnNutanixCluster(verifyConditionParams{
clusterName: clusterName,
namespace: namespace2,
bootstrapClusterProxy: bootstrapClusterProxy,
expectedCondition: clusterv1.Condition{
Type: infrav1.ClusterCategoryCreatedCondition,
Status: corev1.ConditionTrue,
},
})
})

By("Checking if a category was created", func() {
testHelper.verifyCategoryExists(ctx, expectedClusterNameCategoryKey, clusterName)
})

By("Checking if there are VMs assigned to this category", func() {
expectedCategories := map[string]string{
expectedClusterNameCategoryKey: clusterName,
}
testHelper.verifyCategoriesNutanixMachines(ctx, clusterName, namespace2.Name, expectedCategories)
})

// TODO delete one cluster and verify the other one is still running
})
})
5 changes: 5 additions & 0 deletions test/e2e/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ type testHelperInterface interface {
getNutanixResourceIdentifierFromEnv(envVarKey string) infrav1.NutanixResourceIdentifier
getNutanixResourceIdentifierFromE2eConfig(variableKey string) infrav1.NutanixResourceIdentifier
getVariableFromE2eConfig(variableKey string) string
updateVariableInE2eConfig(variableKey string, variableValue string)
stripNutanixIDFromProviderID(providerID string) string
verifyCategoryExists(ctx context.Context, categoryKey, categoyValue string)
verifyCategoriesNutanixMachines(ctx context.Context, clusterName, namespace string, expectedCategories map[string]string)
Expand Down Expand Up @@ -559,6 +560,10 @@ func (t testHelper) getVariableFromE2eConfig(variableKey string) string {
return variableValue
}

func (t testHelper) updateVariableInE2eConfig(variableKey string, variableValue string) {
t.e2eConfig.Variables[variableKey] = variableValue
}

func (t testHelper) stripNutanixIDFromProviderID(providerID string) string {
return strings.TrimPrefix(providerID, nutanixProviderIDPrefix)
}
Expand Down

0 comments on commit f1de4e2

Please sign in to comment.