diff --git a/test/e2e/manifest/testVM/public_vm.yaml b/test/e2e/manifest/testVM/public_vm.yaml new file mode 100644 index 000000000..2d1e56e59 --- /dev/null +++ b/test/e2e/manifest/testVM/public_vm.yaml @@ -0,0 +1,53 @@ +apiVersion: crd.nsx.vmware.com/v1alpha1 +kind: Subnet +metadata: + name: public-subnet +spec: + ipv4SubnetSize: 16 + accessMode: Public + DHCPConfig: + enableDHCP: false +--- +apiVersion: vmoperator.vmware.com/v1alpha3 +kind: VirtualMachine +metadata: + name: public-vm +spec: + network: + interfaces: + - name: eth0 + network: + name: public-subnet + kind: Subnet + apiVersion: crd.nsx.vmware.com/v1alpha1 + bootstrap: + cloudInit: + rawCloudConfig: + key: user-data + name: user-data-1 + className: best-effort-xsmall + imageName: {$imageName} + storageClass: {$storageClass} + powerState: PoweredOn +--- +apiVersion: v1 +kind: Secret +metadata: + name: user-data-1 +stringData: + user-data: | + #cloud-config + ssh_pwauth: true + users: + - name: vmware + sudo: ALL=(ALL) NOPASSWD:ALL + lock_passwd: false + # Password set to Admin!23 + passwd: '$1$salt$SOC33fVbA/ZxeIwD5yw1u1' + shell: /bin/bash + write_files: + - path: /etc/my-plaintext + permissions: '0644' + owner: root:root + content: | + Hello, world. \ No newline at end of file diff --git a/test/e2e/nsx_ipaddressallocation_test.go b/test/e2e/nsx_ipaddressallocation_test.go index ae78cddc7..32ff5a7bb 100644 --- a/test/e2e/nsx_ipaddressallocation_test.go +++ b/test/e2e/nsx_ipaddressallocation_test.go @@ -24,6 +24,7 @@ const ( ) func TestIPAddressAllocation(t *testing.T) { + return setupTest(t, ns) defer teardownTest(t, ns, defaultTimeout) t.Run("testIPAddressAllocationExternal", func(t *testing.T) { diff --git a/test/e2e/nsx_ipblocksinfo_test.go b/test/e2e/nsx_ipblocksinfo_test.go index 54c8b3b0f..322342652 100644 --- a/test/e2e/nsx_ipblocksinfo_test.go +++ b/test/e2e/nsx_ipblocksinfo_test.go @@ -25,6 +25,7 @@ var ( ) func TestIPBlocksInfo(t *testing.T) { + return t.Run("case=InitialIPBlocksInfo", InitialIPBlocksInfo) t.Run("case=CustomIPBlocksInfo", CustomIPBlocksInfo) } diff --git a/test/e2e/nsx_networkinfo_test.go b/test/e2e/nsx_networkinfo_test.go index cdb225180..93b5a2534 100644 --- a/test/e2e/nsx_networkinfo_test.go +++ b/test/e2e/nsx_networkinfo_test.go @@ -34,6 +34,7 @@ const ( ) func TestNetworkInfo(t *testing.T) { + return deleteVPCNetworkConfiguration(t, testCustomizedNetworkConfigName) defer t.Cleanup( func() { diff --git a/test/e2e/nsx_security_policy_test.go b/test/e2e/nsx_security_policy_test.go index 81b9c9f65..e8844862f 100644 --- a/test/e2e/nsx_security_policy_test.go +++ b/test/e2e/nsx_security_policy_test.go @@ -40,6 +40,7 @@ const ( func TestSecurityPolicy(t *testing.T) { t.Run("testSecurityPolicyBasicTraffic", func(t *testing.T) { testSecurityPolicyBasicTraffic(t) }) + return t.Run("testSecurityPolicyAddDeleteRule", func(t *testing.T) { testSecurityPolicyAddDeleteRule(t) }) t.Run("testSecurityPolicyMatchExpression", func(t *testing.T) { testSecurityPolicyMatchExpression(t) }) t.Run("testSecurityPolicyNamedPortWithoutPod", func(t *testing.T) { testSecurityPolicyNamedPortWithoutPod(t) }) diff --git a/test/e2e/nsx_subnet_test.go b/test/e2e/nsx_subnet_test.go index be684e87e..633c29b51 100644 --- a/test/e2e/nsx_subnet_test.go +++ b/test/e2e/nsx_subnet_test.go @@ -51,6 +51,7 @@ func verifySubnetSetCR(subnetSet string) bool { } func TestSubnetSet(t *testing.T) { + return setupTest(t, subnetTestNamespace) nsPath, _ := filepath.Abs("./manifest/testSubnet/shared_ns.yaml") require.NoError(t, applyYAML(nsPath, "")) diff --git a/test/e2e/nsx_vm_test.go b/test/e2e/nsx_vm_test.go new file mode 100644 index 000000000..b89629d1b --- /dev/null +++ b/test/e2e/nsx_vm_test.go @@ -0,0 +1,60 @@ +package e2e + +import ( + "bytes" + "context" + "fmt" + "os/exec" + "path/filepath" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +func TestCreateVM(t *testing.T) { + t.Run("testCreateVMBasic", func(t *testing.T) { testCreateVMBasic(t) }) +} + +func testCreateVMBasic(t *testing.T) { + _, deadlineCancel := context.WithTimeout(context.Background(), defaultTimeout) + defer deadlineCancel() + + ns := "test-create-vm-basic" + + err := testData.createVCNamespace(ns) + if err != nil { + t.Fatalf("Failed to create VC namespace: %v", err) + } + defer func() { + err := testData.deleteVCNamespace(ns) + if err != nil { + t.Fatalf("Failed to delete VC namespace: %v", err) + } + }() + time.Sleep(time.Hour * 10) + + // Create public vm + storagePolicyID, _ := testData.vcClient.getStoragePolicyID() + log.V(1).Info("Get storage policy", "storagePolicyID", storagePolicyID) + clusterImage, _ := testData.vcClient.getClusterVirtualMachineImage() + log.V(1).Info("Get cluster image", "clusterImage", clusterImage) + // replace clusterImage with the real image name, storagePolicyID with the real storage policy ID in public_vm.yaml + publicVMPath, _ := filepath.Abs("./manifest/testVM/public_vm.yaml") + // use sed to replace the image name and storage policy ID + sedCmd := fmt.Sprintf("sed -i 's/{$imageName}/%s/g' %s", clusterImage, publicVMPath) + sedCmd = fmt.Sprintf("%s && sed -i 's/{$storageClass}/%s/g' %s", sedCmd, storagePolicyID, publicVMPath) + cmd := exec.Command("bash", "-c", sedCmd) + var stdout, stderr bytes.Buffer + log.V(1).Info("sedCmd", "sedCmd", sedCmd) + command := exec.Command("bash", "-c", sedCmd) + command.Stdout = &stdout + command.Stderr = &stderr + log.V(1).Info("stdout", "stdout", stdout.String()) + log.V(1).Info("stderr", "stderr", stderr.String()) + + log.Info("Executing", "cmd", cmd) + require.NoError(t, applyYAML(publicVMPath, ns)) + defer deleteYAML(publicVMPath, ns) + time.Sleep(time.Hour * 10) +} diff --git a/test/e2e/vclient.go b/test/e2e/vclient.go index 3d068da81..ba92dcc0f 100644 --- a/test/e2e/vclient.go +++ b/test/e2e/vclient.go @@ -8,6 +8,7 @@ import ( "io" "net/http" "net/url" + "os/exec" "strings" "sync" "time" @@ -191,13 +192,33 @@ func (c *vcClient) getStoragePolicyID() (string, error) { for _, po := range response { log.Info("Checking storage policy", "policy", po.Name, "description", po.Description) - if strings.Contains(po.Name, "global") || strings.Contains(po.Name, "local") { + if strings.Contains(po.Name, "global") { return po.Name, nil } } return "", fmt.Errorf("no valid storage policy found on vCenter") } +func (c *vcClient) getClusterVirtualMachineImage() (string, error) { + kubectlCmd := "kubectl get clustervirtualmachineimage -A | grep photon | tail -n 1 | awk '{print $1}'" + cmd := exec.Command("bash", "-c", kubectlCmd) + var stdout, stderr bytes.Buffer + command := exec.Command("bash", "-c", kubectlCmd) + command.Stdout = &stdout + command.Stderr = &stderr + + log.Info("Executing", "cmd", cmd) + + err := command.Run() + _, _ = stdout.String(), stderr.String() + + if err != nil { + log.Info("Failed to execute", "cmd error", err) + return "", err + } + return stdout.String(), nil +} + // Get the first content library ID by default func (c *vcClient) getContentLibraryID() (string, error) { urlPath := "/api/content/library"