Skip to content

Commit

Permalink
Nutanix ensure VMs cleaned up before and after tests (#7955)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinavmpandey08 authored Apr 10, 2024
1 parent 1fdea5e commit aa9b312
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/eks-a-tool/cmd/nutanixrmvms.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var nutanixRmVmsCmd = &cobra.Command{
if viper.IsSet(insecureFlag) {
insecure = true
}
err = cleanup.NutanixTestResourcesCleanup(cmd.Context(), clusterName, viper.GetString(endpointFlag), viper.GetString(portFlag), insecure, viper.GetBool(ignoreErrorsFlag))
err = cleanup.NutanixTestResources(clusterName, viper.GetString(endpointFlag), viper.GetString(portFlag), insecure, viper.GetBool(ignoreErrorsFlag))
if err != nil {
log.Fatalf("Error removing vms: %v", err)
}
Expand Down
18 changes: 18 additions & 0 deletions cmd/integration_test/build/buildspecs/nutanix-test-eks-a-cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ phases:
if ! [[ ${CODEBUILD_INITIATOR} =~ "codepipeline" ]]; then
make build-eks-a-for-e2e build-integration-test-binary e2e-tests-binary E2E_TAGS="e2e nutanix" E2E_OUTPUT_FILE=bin/nutanix/e2e.test
fi
- >
./bin/test e2e cleanup nutanix
-n ${CLUSTER_NAME_PREFIX}
-e ${T_NUTANIX_ENDPOINT}
-p ${T_NUTANIX_PORT}
--insecure
--ignoreErrors
-v 4
build:
commands:
- export JOB_ID=$CODEBUILD_BUILD_ID
Expand All @@ -85,6 +93,16 @@ phases:
--test-report-folder=reports
--branch-name=${BRANCH_NAME}
--baremetal-branch=${BAREMETAL_BRANCH}
post_build:
commands:
- >
./bin/test e2e cleanup nutanix
-n ${CLUSTER_NAME_PREFIX}
-e ${T_NUTANIX_ENDPOINT}
-p ${T_NUTANIX_PORT}
--insecure
--ignoreErrors
-v 4
reports:
e2e-reports:
files:
Expand Down
16 changes: 16 additions & 0 deletions cmd/integration_test/build/buildspecs/quick-test-eks-a-cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ phases:
-n ${CLUSTER_NAME_PREFIX}
--delete-duplicate-networks
-v 6
- >
./bin/test e2e cleanup nutanix
-n ${CLUSTER_NAME_PREFIX}
-e ${T_NUTANIX_ENDPOINT}
-p ${T_NUTANIX_PORT}
--insecure
--ignoreErrors
-v 4
build:
commands:
- export JOB_ID=$CODEBUILD_BUILD_ID
Expand Down Expand Up @@ -219,6 +227,14 @@ phases:
./bin/test e2e cleanup cloudstack
-n ${CLUSTER_NAME_PREFIX}
-v 4
- >
./bin/test e2e cleanup nutanix
-n ${CLUSTER_NAME_PREFIX}
-e ${T_NUTANIX_ENDPOINT}
-p ${T_NUTANIX_PORT}
--insecure
--ignoreErrors
-v 4
reports:
e2e-reports:
files:
Expand Down
72 changes: 72 additions & 0 deletions cmd/integration_test/cmd/cleanupnutanix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cmd

import (
"fmt"
"log"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/aws/eks-anywhere/internal/test/cleanup"
"github.com/aws/eks-anywhere/pkg/logger"
)

const (
endpointFlag = "endpoint"
portFlag = "port"
insecureFlag = "insecure"
ignoreErrorsFlag = "ignoreErrors"
)

var requiredNutanixCleanUpFlags = []string{clusterNameFlagName, endpointFlag}

var cleanUpNutanixCmd = &cobra.Command{
Use: "nutanix",
Short: "Clean up e2e vms on Nutanix Prism",
Long: "Clean up vms created for e2e testing on Nutanix Prism",
SilenceUsage: true,
PreRun: preRunCleanUpNutanixSetup,
RunE: func(_ *cobra.Command, _ []string) error {
err := cleanUpNutanixTestResources()
if err != nil {
logger.Fatal(err, "Failed to cleanup e2e vms on Nutanix Prism")
}
return nil
},
}

func preRunCleanUpNutanixSetup(cmd *cobra.Command, _ []string) {
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
err := viper.BindPFlag(flag.Name, flag)
if err != nil {
log.Fatalf("Error initializing flags: %v", err)
}
})
}

func init() {
cleanUpInstancesCmd.AddCommand(cleanUpNutanixCmd)

cleanUpNutanixCmd.Flags().StringP(clusterNameFlagName, "n", "", "Cluster name for associated vms")
cleanUpNutanixCmd.Flags().StringP(endpointFlag, "e", "", "Nutanix Prism endpoint")
cleanUpNutanixCmd.Flags().StringP(portFlag, "p", "9440", "Nutanix Prism port")
cleanUpNutanixCmd.Flags().BoolP(insecureFlag, "k", false, "skip TLS when contacting Prism APIs")
cleanUpNutanixCmd.Flags().Bool(ignoreErrorsFlag, true, "ignore APIs errors when deleting VMs")

for _, flag := range requiredNutanixCleanUpFlags {
if err := cleanUpNutanixCmd.MarkFlagRequired(flag); err != nil {
log.Fatalf("Error marking flag %s as required: %v", flag, err)
}
}
}

func cleanUpNutanixTestResources() error {
clusterName := viper.GetString(clusterNameFlagName)
err := cleanup.NutanixTestResources(clusterName, viper.GetString(endpointFlag), viper.GetString(portFlag), viper.IsSet(insecureFlag), viper.IsSet(ignoreErrorsFlag))
if err != nil {
return fmt.Errorf("running cleanup for Nutanix vms: %v", err)
}

return nil
}
4 changes: 2 additions & 2 deletions internal/test/cleanup/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ func cleanupCloudstackDuplicateNetworks(ctx context.Context, cmk *executables.Cm
return nil
}

// NutanixTestResourcesCleanup cleans up any leftover VMs in Nutanix after a test run.
func NutanixTestResourcesCleanup(ctx context.Context, clusterName, endpoint, port string, insecure, ignoreErrors bool) error {
// NutanixTestResources cleans up any leftover VMs in Nutanix after a test run.
func NutanixTestResources(clusterName, endpoint, port string, insecure, ignoreErrors bool) error {
creds := nutanix.GetCredsFromEnv()
nutanixCreds := prismgoclient.Credentials{
URL: fmt.Sprintf("%s:%s", endpoint, port),
Expand Down
2 changes: 1 addition & 1 deletion test/framework/nutanix.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (n *Nutanix) UpdateKubeConfig(content *[]byte, clusterName string) error {

// CleanupVMs satisfies the test framework Provider.
func (n *Nutanix) CleanupVMs(clustername string) error {
return cleanup.NutanixTestResourcesCleanup(context.Background(), clustername, os.Getenv(nutanixEndpoint), os.Getenv(nutanixPort), true, true)
return cleanup.NutanixTestResources(clustername, os.Getenv(nutanixEndpoint), os.Getenv(nutanixPort), true, true)
}

// ClusterConfigUpdates satisfies the test framework Provider.
Expand Down

0 comments on commit aa9b312

Please sign in to comment.