Skip to content

Commit

Permalink
Nutanix ensure VMs cleaned up before and after tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinavmpandey08 committed Apr 10, 2024
1 parent 1fdea5e commit f4f0203
Show file tree
Hide file tree
Showing 6 changed files with 111 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(cmd.Context(), 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
73 changes: 73 additions & 0 deletions cmd/integration_test/cmd/cleanupnutanix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cmd

import (
"context"
"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(cmd *cobra.Command, args []string) error {

Check warning on line 31 in cmd/integration_test/cmd/cleanupnutanix.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'args' seems to be unused, consider removing or renaming it as _ (revive)
err := cleanUpNutanixTestResources(cmd.Context())
if err != nil {
logger.Fatal(err, "Failed to cleanup e2e vms on Nutanix Prism")
}
return nil
},
}

func preRunCleanUpNutanixSetup(cmd *cobra.Command, args []string) {

Check warning on line 40 in cmd/integration_test/cmd/cleanupnutanix.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'args' seems to be unused, consider removing or renaming it as _ (revive)
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(ctx context.Context) error {
clusterName := viper.GetString(clusterNameFlagName)
err := cleanup.NutanixTestResources(ctx, clusterName, viper.GetString(endpointFlag), viper.GetString(portFlag), viper.IsSet(insecureFlag), viper.IsSet(ignoreErrorsFlag))
if err != nil {
return fmt.Errorf("running cleanup for vsphere vcenter 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(ctx context.Context, clusterName, endpoint, port string, insecure, ignoreErrors bool) error {

Check warning on line 158 in internal/test/cleanup/cleanup.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
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(context.Background(), clustername, os.Getenv(nutanixEndpoint), os.Getenv(nutanixPort), true, true)
}

// ClusterConfigUpdates satisfies the test framework Provider.
Expand Down

0 comments on commit f4f0203

Please sign in to comment.