Skip to content

Commit f03cca6

Browse files
authored
Merge pull request #288 from lilyLuLiu/test
add test cases for darwin libkrun
2 parents 1d35ceb + 1c11e75 commit f03cca6

File tree

8 files changed

+317
-211
lines changed

8 files changed

+317
-211
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ test:
3131
@go test -tags "$(BUILDTAGS)" -v ./pkg/...
3232

3333
e2e:
34-
@go test -tags "$(BUILDTAGS)" -v ./test/e2e/...
34+
@go test -tags "$(BUILDTAGS)" -v ./test/e2e/... --ginkgo.label-filter=$(DEFAULT_GOOS)
3535

3636
clean:
3737
@rm -rf bin

test/e2e/common_test.go

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,55 +15,97 @@ import (
1515
)
1616

1717
var (
18-
macadamTest *MacadamTestIntegration
19-
machineResponses []ListReporter
20-
err error
21-
tempDir string
22-
image string
23-
keypath string
24-
cloudinitPath string
18+
macadamTest *MacadamTestIntegration
19+
machineResponses []ListReporter
20+
err error
21+
tempDir string
22+
CENTOS_QCOW2_IMAGE string
23+
keypath string
24+
cloudinitPath string
2525
)
2626

27-
28-
2927
func TestSuite(t *testing.T) {
3028
RegisterFailHandler(Fail)
3129
RunSpecs(t, "Macadam suite")
3230
}
3331

34-
func noVMcheck() {
35-
session := macadamTest.Macadam([]string{"list", "--format", "json"})
32+
func noVMcheck(opts ...string) {
33+
listCMD := []string{"list", "--format", "json"}
34+
if len(opts) > 0 {
35+
listCMD = append(listCMD, "--provider", opts[0])
36+
}
37+
38+
session := macadamTest.Macadam(listCMD)
3639
session.WaitWithDefaultTimeout()
3740
Expect(session).Should(gexec.Exit(0))
3841
err := json.Unmarshal(session.Out.Contents(), &machineResponses)
3942
Expect(err).NotTo(HaveOccurred())
4043
Expect(len(machineResponses)).Should(Equal(0))
4144
}
4245

43-
func removeAllVM() {
44-
session := macadamTest.Macadam([]string{"list", "--format", "json"})
46+
func vmNumberCheck(expect_number int, opts ...string) {
47+
listCMD := []string{"list", "--format", "json"}
48+
if len(opts) > 0 {
49+
listCMD = append(listCMD, "--provider", opts[0])
50+
}
51+
52+
session := macadamTest.Macadam(listCMD)
53+
session.WaitWithDefaultTimeout()
54+
Expect(session).Should(gexec.Exit(0))
55+
err := json.Unmarshal(session.Out.Contents(), &machineResponses)
56+
Expect(err).NotTo(HaveOccurred())
57+
Expect(len(machineResponses)).Should(Equal(expect_number))
58+
}
59+
60+
func runCMDsuccess(cmd []string) string {
61+
GinkgoWriter.Printf("Run cmd: %v\n", cmd)
62+
session := macadamTest.Macadam(cmd)
63+
session.WaitWithDefaultTimeout()
64+
Expect(session).Should(gexec.Exit(0))
65+
return session.OutputToString()
66+
}
67+
68+
func runCMD(cmd []string) (int, string) {
69+
GinkgoWriter.Printf("Run cmd: %v\n", cmd)
70+
session := macadamTest.Macadam(cmd)
71+
session.WaitWithDefaultTimeout()
72+
return session.ExitCode(), session.OutputToString()
73+
}
74+
75+
func removeAllVM(opts ...string) {
76+
listCMD := []string{"list", "--format", "json"}
77+
baseCMD := []string{}
78+
if len(opts) > 0 {
79+
provider := opts[0]
80+
listCMD = append(listCMD, "--provider", provider)
81+
baseCMD = append(baseCMD, "--provider", provider)
82+
}
83+
84+
session := macadamTest.Macadam(listCMD)
4585
session.WaitWithDefaultTimeout()
4686
Expect(session).Should(gexec.Exit(0))
4787
err = json.Unmarshal(session.Out.Contents(), &machineResponses)
4888
Expect(err).NotTo(HaveOccurred())
4989

5090
for _, m := range machineResponses {
51-
stopCmd := "stop " + string(m.Name)
52-
rmCmd := "rm -f " + string(m.Name)
91+
stopCMD := append([]string{"stop"}, baseCMD...)
92+
stopCMD = append(stopCMD, m.Name)
93+
rmCMD := append([]string{"rm", "-f"}, baseCMD...)
94+
rmCMD = append(rmCMD, m.Name)
5395

5496
// stop the CentOS VM
55-
session = macadamTest.Macadam(strings.Fields(stopCmd))
97+
session = macadamTest.Macadam(stopCMD)
5698
session.WaitWithDefaultTimeout()
5799
Expect(session).Should(gexec.Exit(0))
58100
Expect(session.OutputToString()).Should(ContainSubstring("stopped successfully"))
59101

60102
// rm the CentOS VM and verify that "list" does not return any vm
61-
session = macadamTest.Macadam(strings.Fields(rmCmd))
103+
session = macadamTest.Macadam(rmCMD)
62104
session.WaitWithDefaultTimeout()
63105
Expect(session).Should(gexec.Exit(0))
64106
}
65107

66-
session = macadamTest.Macadam([]string{"list", "--format", "json"})
108+
session = macadamTest.Macadam(listCMD)
67109
session.WaitWithDefaultTimeout()
68110
Expect(session).Should(gexec.Exit(0))
69111
err = json.Unmarshal(session.Out.Contents(), &machineResponses)
@@ -77,7 +119,7 @@ var _ = BeforeSuite(func() {
77119

78120
// download CentOS image
79121
centosProvider := osprovider.NewCentosProvider()
80-
image, err = centosProvider.Fetch(tempDir)
122+
CENTOS_QCOW2_IMAGE, err = centosProvider.Fetch(tempDir)
81123
Expect(err).NotTo(HaveOccurred())
82124

83125
keypath = filepath.Join(tempDir, "id_rsa")
@@ -106,5 +148,5 @@ var _ = AfterSuite(func() {
106148
})
107149

108150
var _ = BeforeEach(func() {
109-
macadamTest = MacadamTestCreate()
110-
})
151+
macadamTest = MacadamTestCreate()
152+
})

test/e2e/init_common_test.go

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
}

test/e2e/macProvider_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package e2e
2+
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
)
6+
7+
var _ = Describe("provider libkrun test with centos", Label("libkrun", "darwin"), func() {
8+
BeforeEach(func() {
9+
vmNumberCheck(0, "libkrun")
10+
})
11+
12+
AfterEach(func() {
13+
removeAllVM("libkrun")
14+
})
15+
16+
It("init CentOS VM with cpu, disk and memory setup", Label("hardware"), func() {
17+
testparam := Init_Hardware_Parameter{
18+
cpu: "3",
19+
disk: "30",
20+
memory: "2048",
21+
expect_memory: []string{"1.7G", "1.8G", "1.9G", "2G"},
22+
}
23+
init_hardware_test(CENTOS_QCOW2_IMAGE, testparam, "libkrun")
24+
})
25+
26+
It("init CentOS VM with username and sshkey setup", Label("sshkey"), func() {
27+
init_sshkey_test(CENTOS_QCOW2_IMAGE, "libkrun")
28+
})
29+
30+
It("init CentOS VM with cloud-init setup", Label("cloudinit"), func() {
31+
init_cloudinit_test(CENTOS_QCOW2_IMAGE, "libkrun")
32+
})
33+
34+
It("init CentOS VM with name", Label("name"), func() {
35+
init_vmName_test(CENTOS_QCOW2_IMAGE, "libkrun")
36+
})
37+
38+
})

0 commit comments

Comments
 (0)