Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]Add e2e test case for VM creation #1000

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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) })
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)
}
23 changes: 22 additions & 1 deletion 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 @@ -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"
Expand Down
Loading