Skip to content

Commit

Permalink
e2e: improve cloud-init user-data specs
Browse files Browse the repository at this point in the history
it may happen that the spec connects to the vm console too late
thus missing the prompted string from the cloud-init executed script.

with this change, the script creates a file on the fs and the spec
waits until login appears, then checking the file existence.

Signed-off-by: enp0s3 <[email protected]>
  • Loading branch information
enp0s3 committed Feb 21, 2023
1 parent 1c54a9b commit bf22177
Showing 1 changed file with 53 additions and 59 deletions.
112 changes: 53 additions & 59 deletions tests/vmi_cloudinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ import (
)

const (
sshAuthorizedKey = "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkT test-ssh-key"
fedoraPassword = "fedora"
expectedUserData = "printed from cloud-init userdata"
testNetworkData = "#Test networkData"
testUserData = "#cloud-config"
sshAuthorizedKey = "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkT test-ssh-key"
fedoraPassword = "fedora"
expectedUserDataFile = "cloud-init-userdata-executed"
testNetworkData = "#Test networkData"
testUserData = "#cloud-config"
)

var _ = Describe("[rfe_id:151][crit:high][vendor:[email protected]][level:component][sig-compute]CloudInit UserData", decorators.SigCompute, func() {
Expand Down Expand Up @@ -132,15 +132,15 @@ var _ = Describe("[rfe_id:151][crit:high][vendor:[email protected]][level:compon
Describe("[rfe_id:151][crit:medium][vendor:[email protected]][level:component]A new VirtualMachineInstance", func() {
Context("with cloudInitNoCloud userDataBase64 source", func() {
It("[test_id:1615]should have cloud-init data", func() {
userData := fmt.Sprintf("#!/bin/sh\n\necho '%s'\n", expectedUserData)

userData := fmt.Sprintf("#!/bin/sh\n\ntouch /%s\n", expectedUserDataFile)
vmi := tests.NewRandomVMIWithEphemeralDiskAndUserdata(cd.ContainerDiskFor(cd.ContainerDiskCirros), userData)
LaunchVMI(vmi)

vmi = tests.RunVMIAndExpectLaunch(vmi, 60)
libwait.WaitUntilVMIReady(vmi, console.LoginToCirros)
CheckCloudInitIsoSize(vmi, cloudinit.DataSourceNoCloud)
VerifyUserDataVMI(vmi, []expect.Batcher{
&expect.BSnd{S: "\n"},
&expect.BExp{R: expectedUserData},
}, time.Second*120)

By("Checking whether the user-data script had created the file")
Expect(console.RunCommand(vmi, fmt.Sprintf("cat /%s\n", expectedUserDataFile), time.Second*120)).To(Succeed())
})

Context("with injected ssh-key", func() {
Expand All @@ -154,6 +154,7 @@ var _ = Describe("[rfe_id:151][crit:high][vendor:[email protected]][level:compon

LaunchVMI(vmi)
CheckCloudInitIsoSize(vmi, cloudinit.DataSourceNoCloud)

VerifyUserDataVMI(vmi, []expect.Batcher{
&expect.BSnd{S: "\n"},
&expect.BExp{R: "login:"},
Expand All @@ -170,15 +171,15 @@ var _ = Describe("[rfe_id:151][crit:high][vendor:[email protected]][level:compon

Context("with cloudInitConfigDrive userDataBase64 source", func() {
It("[test_id:3178]should have cloud-init data", func() {
userData := fmt.Sprintf("#!/bin/sh\n\necho '%s'\n", expectedUserData)

userData := fmt.Sprintf("#!/bin/sh\n\ntouch /%s\n", expectedUserDataFile)
vmi := tests.NewRandomVMIWithEphemeralDiskAndConfigDriveUserdata(cd.ContainerDiskFor(cd.ContainerDiskCirros), userData)
LaunchVMI(vmi)

vmi = tests.RunVMIAndExpectLaunch(vmi, 60)
libwait.WaitUntilVMIReady(vmi, console.LoginToCirros)
CheckCloudInitIsoSize(vmi, cloudinit.DataSourceConfigDrive)
VerifyUserDataVMI(vmi, []expect.Batcher{
&expect.BSnd{S: "\n"},
&expect.BExp{R: expectedUserData},
}, time.Second*120)

By("Checking whether the user-data script had created the file")
Expect(console.RunCommand(vmi, fmt.Sprintf("cat /%s\n", expectedUserDataFile), time.Second*120)).To(Succeed())
})

Context("with injected ssh-key", func() {
Expand All @@ -192,6 +193,7 @@ var _ = Describe("[rfe_id:151][crit:high][vendor:[email protected]][level:compon

LaunchVMI(vmi)
CheckCloudInitIsoSize(vmi, cloudinit.DataSourceConfigDrive)

VerifyUserDataVMI(vmi, []expect.Batcher{
&expect.BSnd{S: "\n"},
&expect.BExp{R: "login:"},
Expand Down Expand Up @@ -258,54 +260,44 @@ var _ = Describe("[rfe_id:151][crit:high][vendor:[email protected]][level:compon
})
})

Context("with cloudInitNoCloud userData source", func() {
It("[test_id:1617]should process provided cloud-init data", func() {
userData := fmt.Sprintf("#!/bin/sh\n\necho '%s'\n", expectedUserData)
vmi := tests.NewRandomVMIWithEphemeralDiskAndUserdata(cd.ContainerDiskFor(cd.ContainerDiskCirros), userData)

vmi = LaunchVMI(vmi)
CheckCloudInitIsoSize(vmi, cloudinit.DataSourceNoCloud)
By("executing a user-data script")
VerifyUserDataVMI(vmi, []expect.Batcher{
&expect.BSnd{S: "\n"},
&expect.BExp{R: expectedUserData},
}, time.Second*120)
Context("should process provided cloud-init data", func() {
userData := fmt.Sprintf("#!/bin/sh\n\ntouch /%s\n", expectedUserDataFile)

By("applying the hostname from meta-data")
Expect(console.LoginToCirros(vmi)).To(Succeed())
runTest := func(vmi *v1.VirtualMachineInstance, dsType cloudinit.DataSourceType) {
vmi = tests.RunVMIAndExpectLaunch(vmi, 60)

Expect(console.SafeExpectBatch(vmi, []expect.Batcher{
&expect.BSnd{S: "hostname\n"},
&expect.BExp{R: dns.SanitizeHostname(vmi)},
}, 10)).To(Succeed())
})
})

Context("with cloudInitConfigDrive userData source", func() {
It("[test_id:3180]should process provided cloud-init data", func() {
userData := fmt.Sprintf("#!/bin/sh\n\necho '%s'\n", expectedUserData)
vmi := tests.NewRandomVMIWithEphemeralDiskAndConfigDriveUserdata(cd.ContainerDiskFor(cd.ContainerDiskCirros), userData)
By("waiting until login appears")
libwait.WaitUntilVMIReady(vmi, console.LoginToCirros)

vmi = LaunchVMI(vmi)
CheckCloudInitIsoSize(vmi, cloudinit.DataSourceConfigDrive)
By("executing a user-data script")
VerifyUserDataVMI(vmi, []expect.Batcher{
&expect.BSnd{S: "\n"},
&expect.BExp{R: expectedUserData},
}, time.Second*120)
By("validating cloud-init disk is 4k aligned")
CheckCloudInitIsoSize(vmi, dsType)

By("applying the hostname from meta-data")
Expect(console.LoginToCirros(vmi)).To(Succeed())
By("Checking whether the user-data script had created the file")
Expect(console.RunCommand(vmi, fmt.Sprintf("cat /%s\n", expectedUserDataFile), time.Second*120)).To(Succeed())

By("validating the hostname matches meta-data")
Expect(console.SafeExpectBatch(vmi, []expect.Batcher{
&expect.BSnd{S: "hostname\n"},
&expect.BExp{R: dns.SanitizeHostname(vmi)},
}, 10)).To(Succeed())
}

It("[test_id:1617] with cloudInitNoCloud userData source", func() {
vmi := tests.NewRandomVMIWithEphemeralDiskAndUserdata(
cd.ContainerDiskFor(cd.ContainerDiskCirros),
userData)
runTest(vmi, cloudinit.DataSourceNoCloud)
})
It("[test_id:3180] with cloudInitConfigDrive userData source", func() {
vmi := tests.NewRandomVMIWithEphemeralDiskAndConfigDriveUserdata(
cd.ContainerDiskFor(cd.ContainerDiskCirros),
userData)
runTest(vmi, cloudinit.DataSourceConfigDrive)
})
})

It("[test_id:1618]should take user-data from k8s secret", func() {
userData := fmt.Sprintf("#!/bin/sh\n\necho '%s'\n", expectedUserData)
userData := fmt.Sprintf("#!/bin/sh\n\ntouch /%s\n", expectedUserDataFile)
vmi := tests.NewRandomVMIWithEphemeralDiskAndUserdata(cd.ContainerDiskFor(cd.ContainerDiskCirros), "")

idx := 0
Expand Down Expand Up @@ -338,12 +330,14 @@ var _ = Describe("[rfe_id:151][crit:high][vendor:[email protected]][level:compon
Expect(err).ToNot(HaveOccurred())
break
}
LaunchVMI(vmi)

vmi = tests.RunVMIAndExpectLaunch(vmi, 60)
libwait.WaitUntilVMIReady(vmi, console.LoginToCirros)

CheckCloudInitIsoSize(vmi, cloudinit.DataSourceNoCloud)
VerifyUserDataVMI(vmi, []expect.Batcher{
&expect.BSnd{S: "\n"},
&expect.BExp{R: expectedUserData},
}, time.Second*120)

By("Checking whether the user-data script had created the file")
Expect(console.RunCommand(vmi, fmt.Sprintf("cat /%s\n", expectedUserDataFile), time.Second*120)).To(Succeed())

// Expect that the secret is not present on the vmi itself
vmi, err = virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(vmi)).Get(context.Background(), vmi.Name, &metav1.GetOptions{})
Expand Down

0 comments on commit bf22177

Please sign in to comment.