|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "time" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | +) |
| 10 | + |
| 11 | +type Init_Hardware_Parameter struct { |
| 12 | + cpu string |
| 13 | + disk string |
| 14 | + memory string |
| 15 | + expect_memory []string |
| 16 | +} |
| 17 | +type Init_Test_Para []Init_Hardware_Parameter |
| 18 | + |
| 19 | +func init_hardware_test(imagePath string, tests Init_Hardware_Parameter, opts ...string) { |
| 20 | + |
| 21 | + initCMD := []string{} |
| 22 | + startCMD := []string{"start"} |
| 23 | + checkCPU := []string{"ssh", "nproc"} |
| 24 | + checkDisk := []string{"ssh", "lsblk"} |
| 25 | + checkMemory := []string{"ssh", "free", "-h"} |
| 26 | + |
| 27 | + if len(opts) > 0 { |
| 28 | + provider := []string{"--provider", opts[0]} |
| 29 | + initCMD = append(initCMD, provider...) |
| 30 | + startCMD = append(startCMD, provider...) |
| 31 | + checkCPU = append(provider, checkCPU...) |
| 32 | + checkDisk = append(provider, checkDisk...) |
| 33 | + checkMemory = append(provider, checkMemory...) |
| 34 | + } |
| 35 | + |
| 36 | + initCMD = append(initCMD, "init", "--cpus", tests.cpu, "--disk-size", tests.disk, "--memory", tests.memory, imagePath) |
| 37 | + // init a CentOS VM with cpu and disk-size setup |
| 38 | + runCMDsuccess(initCMD) |
| 39 | + |
| 40 | + // check the list command returns one item |
| 41 | + vmNumberCheck(1, opts...) |
| 42 | + |
| 43 | + // start the CentOS VM |
| 44 | + output := runCMDsuccess(startCMD) |
| 45 | + Expect(output).Should(ContainSubstring("started successfully")) |
| 46 | + |
| 47 | + // ssh into the VM and check CPU number |
| 48 | + output = runCMDsuccess(checkCPU) |
| 49 | + Expect(strings.TrimSpace(output)).Should(Equal(tests.cpu)) |
| 50 | + |
| 51 | + // ssh into the VM and check disk |
| 52 | + output = runCMDsuccess(checkDisk) |
| 53 | + Expect(output).Should(ContainSubstring(tests.disk)) |
| 54 | + |
| 55 | + // ssh into the VM and check memory |
| 56 | + output = runCMDsuccess(checkMemory) |
| 57 | + // Verify memory is close to the set memory (allow for system overhead) |
| 58 | + Expect(output).Should(Or(ContainSubstring(tests.expect_memory[0]), ContainSubstring(tests.expect_memory[1]), ContainSubstring(tests.expect_memory[2]), ContainSubstring(tests.expect_memory[3]))) |
| 59 | +} |
| 60 | + |
| 61 | +func init_sshkey_test(imagePath string, opts ...string) { |
| 62 | + initCMD := []string{"init", "--username", "test", "--ssh-identity-path", keypath, imagePath} |
| 63 | + startCMD := []string{"start"} |
| 64 | + usernameCheck := []string{"ssh", "--username", "test", "whoami"} |
| 65 | + |
| 66 | + if len(opts) > 0 { |
| 67 | + provider := []string{"--provider", opts[0]} |
| 68 | + initCMD = append(initCMD, provider...) |
| 69 | + startCMD = append(startCMD, provider...) |
| 70 | + usernameCheck = append(provider, usernameCheck...) |
| 71 | + } |
| 72 | + |
| 73 | + runCMDsuccess(initCMD) |
| 74 | + vmNumberCheck(1, opts...) |
| 75 | + |
| 76 | + output := runCMDsuccess(startCMD) |
| 77 | + Expect(output).Should(ContainSubstring("started successfully")) |
| 78 | + |
| 79 | + output = runCMDsuccess(usernameCheck) |
| 80 | + Expect(strings.TrimSpace(output)).Should(Equal("test")) |
| 81 | +} |
| 82 | + |
| 83 | +func init_vmName_test(imagePath string, opts ...string) { |
| 84 | + initCMD := []string{"init", "--name", "myVM", imagePath} |
| 85 | + startCMD := []string{"start", "myVM"} |
| 86 | + checkUser := []string{"ssh", "myVM", "whoami"} |
| 87 | + |
| 88 | + if len(opts) > 0 { |
| 89 | + provider := []string{"--provider", opts[0]} |
| 90 | + initCMD = append(initCMD, provider...) |
| 91 | + startCMD = append(startCMD, provider...) |
| 92 | + checkUser = append(provider, checkUser...) |
| 93 | + } |
| 94 | + |
| 95 | + runCMDsuccess(initCMD) |
| 96 | + |
| 97 | + // check the list command returns one item |
| 98 | + vmNumberCheck(1, opts...) |
| 99 | + |
| 100 | + // start the CentOS VM with set name |
| 101 | + output := runCMDsuccess(startCMD) |
| 102 | + Expect(output).Should(ContainSubstring("started successfully")) |
| 103 | + |
| 104 | + // ssh into the VM and prints user |
| 105 | + output = runCMDsuccess(checkUser) |
| 106 | + Expect(strings.TrimSpace(output)).Should(Equal("core")) |
| 107 | +} |
| 108 | + |
| 109 | +func init_cloudinit_test(imagePath string, opts ...string) { |
| 110 | + initCMD := []string{"init", "--cloud-init", cloudinitPath, "--username", "macadamtest", "--ssh-identity-path", keypath, imagePath} |
| 111 | + startCMD := []string{"start"} |
| 112 | + checkuser := []string{"ssh", "whoami"} |
| 113 | + check_cloudFinal := []string{"ssh", "systemctl", "status", "cloud-final"} |
| 114 | + check_gitVersion := []string{"ssh", "git", "--version"} |
| 115 | + check_file_exist := []string{"ssh", "ls", "/home/macadamtest/hello.txt"} |
| 116 | + |
| 117 | + if len(opts) > 0 { |
| 118 | + provider := []string{"--provider", opts[0]} |
| 119 | + initCMD = append(initCMD, provider...) |
| 120 | + startCMD = append(startCMD, provider...) |
| 121 | + checkuser = append(provider, checkuser...) |
| 122 | + check_cloudFinal = append(provider, check_cloudFinal...) |
| 123 | + check_gitVersion = append(provider, check_gitVersion...) |
| 124 | + check_file_exist = append(provider, check_file_exist...) |
| 125 | + } |
| 126 | + |
| 127 | + runCMDsuccess(initCMD) |
| 128 | + |
| 129 | + // check the list command returns one item |
| 130 | + vmNumberCheck(1, opts...) |
| 131 | + |
| 132 | + // start the CentOS VM |
| 133 | + output := runCMDsuccess(startCMD) |
| 134 | + Expect(output).Should(ContainSubstring("started successfully")) |
| 135 | + |
| 136 | + // ssh into the VM and prints user |
| 137 | + output = runCMDsuccess(checkuser) |
| 138 | + Expect(strings.TrimSpace(output)).Should(Equal("macadamtest")) |
| 139 | + |
| 140 | + // wait until cloud-init finish |
| 141 | + Eventually(func() string { |
| 142 | + exitcode, output := runCMD(check_cloudFinal) |
| 143 | + |
| 144 | + if exitcode != 0 { |
| 145 | + return "" |
| 146 | + } |
| 147 | + return output |
| 148 | + }, 20*time.Minute, 30*time.Second).Should(ContainSubstring("Active: active (exited)")) |
| 149 | + |
| 150 | + GinkgoWriter.Println("cloud-init has finished") |
| 151 | + |
| 152 | + // ssh into the VM and check installed app |
| 153 | + output = runCMDsuccess(check_gitVersion) |
| 154 | + Expect(output).Should(ContainSubstring("git version")) |
| 155 | + |
| 156 | + // ssh into the VM and check file created |
| 157 | + runCMDsuccess(check_file_exist) |
| 158 | +} |
0 commit comments