Skip to content

Commit

Permalink
Add e2e test case for VM creation
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengxiexie committed Jan 14, 2025
1 parent acc0d70 commit e68acd4
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 0 deletions.
53 changes: 53 additions & 0 deletions test/e2e/manifest/testVM/public_vm.yaml
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions test/e2e/nsx_ipaddressallocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/nsx_ipblocksinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
)

func TestIPBlocksInfo(t *testing.T) {
return
t.Run("case=InitialIPBlocksInfo", InitialIPBlocksInfo)
t.Run("case=CustomIPBlocksInfo", CustomIPBlocksInfo)
}
Expand Down
1 change: 1 addition & 0 deletions test/e2e/nsx_networkinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
)

func TestNetworkInfo(t *testing.T) {
return
deleteVPCNetworkConfiguration(t, testCustomizedNetworkConfigName)
defer t.Cleanup(
func() {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/nsx_security_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
)

func TestSecurityPolicy(t *testing.T) {
return
t.Run("testSecurityPolicyBasicTraffic", func(t *testing.T) { testSecurityPolicyBasicTraffic(t) })
t.Run("testSecurityPolicyAddDeleteRule", func(t *testing.T) { testSecurityPolicyAddDeleteRule(t) })
t.Run("testSecurityPolicyMatchExpression", func(t *testing.T) { testSecurityPolicyMatchExpression(t) })
Expand Down
1 change: 1 addition & 0 deletions test/e2e/nsx_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ""))
Expand Down
60 changes: 60 additions & 0 deletions test/e2e/nsx_vm_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
21 changes: 21 additions & 0 deletions test/e2e/vclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/url"
"os/exec"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -198,6 +199,26 @@ func (c *vcClient) getStoragePolicyID() (string, error) {
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"
Expand Down

0 comments on commit e68acd4

Please sign in to comment.