From bc6352c51d3171358c6e1e11333fd240399d93ae Mon Sep 17 00:00:00 2001 From: Alona Kaplan Date: Sun, 9 Aug 2020 11:42:54 +0300 Subject: [PATCH 1/2] tests: utils.RetValue no need to pass prompt "RetValue" will accept both non root "$ " and root "# " prompts. Signed-off-by: Alona Kaplan --- tests/config_test.go | 16 +++++++------- tests/container_disk_test.go | 4 ++-- tests/pausing_test.go | 2 +- tests/storage_test.go | 8 +++---- tests/utils.go | 27 ++++++++++++++---------- tests/vmi_cloudinit_hook_sidecar_test.go | 2 +- tests/vmi_cloudinit_test.go | 2 +- tests/vmi_configuration_test.go | 24 ++++++++++----------- tests/vmi_gpu_test.go | 2 +- tests/vmi_monitoring_test.go | 2 +- tests/vmi_multus_test.go | 10 ++++----- tests/vmi_networking_test.go | 22 +++++++++---------- 12 files changed, 63 insertions(+), 58 deletions(-) diff --git a/tests/config_test.go b/tests/config_test.go index 64bc4726f32b..83aa4caae534 100644 --- a/tests/config_test.go +++ b/tests/config_test.go @@ -105,7 +105,7 @@ var _ = Describe("[rfe_id:899][crit:medium][vendor:cnv-qe@redhat.com][level:comp // mount iso ConfigMap image &expect.BSnd{S: "mount /dev/sda /mnt\n"}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\#")}, + &expect.BExp{R: tests.RetValue("0")}, &expect.BSnd{S: "cat /mnt/option1 /mnt/option2 /mnt/option3\n"}, &expect.BExp{R: expectedOutput}, }, 200*time.Second) @@ -199,7 +199,7 @@ var _ = Describe("[rfe_id:899][crit:medium][vendor:cnv-qe@redhat.com][level:comp // mount iso Secret image &expect.BSnd{S: "mount /dev/sda /mnt\n"}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\#")}, + &expect.BExp{R: tests.RetValue("0")}, &expect.BSnd{S: "cat /mnt/user /mnt/password\n"}, &expect.BExp{R: expectedOutput}, }, 200*time.Second) @@ -282,7 +282,7 @@ var _ = Describe("[rfe_id:899][crit:medium][vendor:cnv-qe@redhat.com][level:comp // mount service account iso image &expect.BSnd{S: "mount /dev/sda /mnt\n"}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\#")}, + &expect.BExp{R: tests.RetValue("0")}, &expect.BSnd{S: "cat /mnt/namespace\n"}, &expect.BExp{R: tests.NamespaceTestDefault}, &expect.BSnd{S: "tail -c 20 /mnt/token\n"}, @@ -377,7 +377,7 @@ var _ = Describe("[rfe_id:899][crit:medium][vendor:cnv-qe@redhat.com][level:comp &expect.BExp{R: "#"}, &expect.BSnd{S: "mount /dev/vdc /mnt\n"}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\#")}, + &expect.BExp{R: tests.RetValue("0")}, &expect.BSnd{S: "cat /mnt/config1 /mnt/config2 /mnt/config3\n"}, &expect.BExp{R: expectedOutputCfgMap}, }, 200*time.Second) @@ -403,7 +403,7 @@ var _ = Describe("[rfe_id:899][crit:medium][vendor:cnv-qe@redhat.com][level:comp // mount Secret image &expect.BSnd{S: "mount /dev/vdd /mnt\n"}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\#")}, + &expect.BExp{R: tests.RetValue("0")}, &expect.BSnd{S: "cat /mnt/user /mnt/password\n"}, &expect.BExp{R: expectedOutputSecret}, }, 200*time.Second) @@ -501,13 +501,13 @@ var _ = Describe("[rfe_id:899][crit:medium][vendor:cnv-qe@redhat.com][level:comp &expect.BExp{R: "\\#"}, &expect.BSnd{S: "mount /dev/sda /mnt\n"}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\#")}, + &expect.BExp{R: tests.RetValue("0")}, &expect.BSnd{S: "grep \"PRIVATE KEY\" /mnt/ssh-privatekey\n"}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\#")}, + &expect.BExp{R: tests.RetValue("0")}, &expect.BSnd{S: "grep ssh-rsa /mnt/ssh-publickey\n"}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\#")}, + &expect.BExp{R: tests.RetValue("0")}, }, 200*time.Second) log.DefaultLogger().Object(vmi).Infof("%v", res) Expect(err).ToNot(HaveOccurred()) diff --git a/tests/container_disk_test.go b/tests/container_disk_test.go index 77ff79a60ebf..dfeaad79c59e 100644 --- a/tests/container_disk_test.go +++ b/tests/container_disk_test.go @@ -211,11 +211,11 @@ var _ = Describe("[rfe_id:588][crit:medium][vendor:cnv-qe@redhat.com][level:comp // mount virtio cdrom and check files are there &expect.BSnd{S: "mount -t iso9600 /dev/cdrom\n"}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\#")}, + &expect.BExp{R: tests.RetValue("0")}, &expect.BSnd{S: "cd /media/cdrom\n"}, &expect.BSnd{S: "ls virtio-win_license.txt guest-agent\n"}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\#")}, + &expect.BExp{R: tests.RetValue("0")}, }, 200*time.Second) Expect(err).ToNot(HaveOccurred(), "expected virtio files to be mounted properly") }) diff --git a/tests/pausing_test.go b/tests/pausing_test.go index 8b859287bd05..d105b0810bcd 100644 --- a/tests/pausing_test.go +++ b/tests/pausing_test.go @@ -367,7 +367,7 @@ var _ = Describe("[rfe_id:3064][crit:medium][vendor:cnv-qe@redhat.com][level:com grepSleepPid := func(expecter expect.Expecter) string { res, err := tests.ExpectBatchWithValidatedSend(expecter, []expect.Batcher{ &expect.BSnd{S: `pgrep -f "sleep 5"` + "\n"}, - &expect.BExp{R: tests.RetValue("[0-9]+", "\\# ")}, // pid + &expect.BExp{R: tests.RetValue("[0-9]+")}, // pid }, 15*time.Second) log.DefaultLogger().Infof("a:%+v\n", res) Expect(err).ToNot(HaveOccurred()) diff --git a/tests/storage_test.go b/tests/storage_test.go index a017d2dc83d8..626697f7810e 100644 --- a/tests/storage_test.go +++ b/tests/storage_test.go @@ -201,7 +201,7 @@ var _ = Describe("Storage", func() { &expect.BSnd{S: "sudo mkfs.ext4 /dev/vdc\n"}, &expect.BExp{R: "\\$ "}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\$ ")}, + &expect.BExp{R: tests.RetValue("0")}, }, 20*time.Second) log.DefaultLogger().Object(vmi).Infof("%v", res) Expect(err).ToNot(HaveOccurred()) @@ -302,7 +302,7 @@ var _ = Describe("Storage", func() { // Because "/" is mounted on tmpfs, we need something that normally persists writes - /dev/sda2 is the EFI partition formatted as vFAT. &expect.BSnd{S: "mount /dev/sda2 /mnt\n"}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\#")}, + &expect.BExp{R: tests.RetValue("0")}, &expect.BSnd{S: "echo content > /mnt/checkpoint\n"}, // The QEMU process will be killed, therefore the write must be flushed to the disk. &expect.BSnd{S: "sync\n"}, @@ -330,10 +330,10 @@ var _ = Describe("Storage", func() { // Same story as when first starting the VirtualMachineInstance - the checkpoint, if persisted, is located at /dev/sda2. &expect.BSnd{S: "mount /dev/sda2 /mnt\n"}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\#")}, + &expect.BExp{R: tests.RetValue("0")}, &expect.BSnd{S: "cat /mnt/checkpoint &> /dev/null\n"}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("1", "\\#")}, + &expect.BExp{R: tests.RetValue("1")}, }, 200*time.Second) Expect(err).ToNot(HaveOccurred()) }) diff --git a/tests/utils.go b/tests/utils.go index dd2c0f860046..251ecfc515a9 100644 --- a/tests/utils.go +++ b/tests/utils.go @@ -243,6 +243,11 @@ type ObjectEventWatcher struct { dontFailOnMissingEvent bool } +const ( + PromptExpression = `(\$ |\# )` + CRLF = "\r\n" +) + func NewObjectEventWatcher(object runtime.Object) *ObjectEventWatcher { return &ObjectEventWatcher{object: object, startType: invalidWatch} } @@ -2864,11 +2869,11 @@ func configureConsole(expecter expect.Expecter, prompt string, shouldSudo bool) &expect.BSnd{S: "stty cols 500 rows 500\n"}, &expect.BExp{R: prompt}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: RetValue("0", prompt)}, + &expect.BExp{R: RetValue("0")}, &expect.BSnd{S: fmt.Sprintf("%sdmesg -n 1\n", sudoString)}, &expect.BExp{R: prompt}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: RetValue("0", prompt)}}) + &expect.BExp{R: RetValue("0")}}) resp, err := expecter.ExpectBatch(batch, 30*time.Second) if err != nil { log.DefaultLogger().Infof("%v", resp) @@ -2882,7 +2887,7 @@ func configureIPv6OnVMI(vmi *v1.VirtualMachineInstance, expecter expect.Expecter &expect.BSnd{S: "\n"}, &expect.BExp{R: prompt}, &expect.BSnd{S: "ip a | grep -q eth0; echo $?\n"}, - &expect.BExp{R: RetValue("0", prompt)}}) + &expect.BExp{R: RetValue("0")}}) _, err := ExpectBatchWithValidatedSend(expecter, hasNetEth0Batch, 30*time.Second) return err == nil } @@ -2892,7 +2897,7 @@ func configureIPv6OnVMI(vmi *v1.VirtualMachineInstance, expecter expect.Expecter &expect.BSnd{S: "\n"}, &expect.BExp{R: prompt}, &expect.BSnd{S: "ip -6 address show dev eth0 scope global | grep -q inet6; echo $?\n"}, - &expect.BExp{R: RetValue("0", prompt)}}) + &expect.BExp{R: RetValue("0")}}) _, err := ExpectBatchWithValidatedSend(expecter, hasGlobalIPv6Batch, 30*time.Second) return err == nil } @@ -2915,7 +2920,7 @@ func configureIPv6OnVMI(vmi *v1.VirtualMachineInstance, expecter expect.Expecter &expect.BSnd{S: "\n"}, &expect.BExp{R: prompt}, &expect.BSnd{S: "sudo ip -6 addr add fd10:0:2::2/120 dev eth0; echo $?\n"}, - &expect.BExp{R: RetValue("0", prompt)}}) + &expect.BExp{R: RetValue("0")}}) resp, err := ExpectBatchWithValidatedSend(expecter, addIPv6Address, 30*time.Second) if err != nil { log.DefaultLogger().Object(vmi).Infof("addIPv6Address failed: %v", resp) @@ -2928,7 +2933,7 @@ func configureIPv6OnVMI(vmi *v1.VirtualMachineInstance, expecter expect.Expecter &expect.BSnd{S: "\n"}, &expect.BExp{R: prompt}, &expect.BSnd{S: "sudo ip -6 route add default via fd10:0:2::1 src fd10:0:2::2; echo $?\n"}, - &expect.BExp{R: RetValue("0", prompt)}}) + &expect.BExp{R: RetValue("0")}}) resp, err = ExpectBatchWithValidatedSend(expecter, addIPv6DefaultRoute, 30*time.Second) if err != nil { log.DefaultLogger().Object(vmi).Infof("addIPv6DefaultRoute failed: %v", resp) @@ -4263,7 +4268,7 @@ func StartTCPServer(vmi *v1.VirtualMachineInstance, port int) { &expect.BSnd{S: fmt.Sprintf("screen -d -m nc -klp %d -e echo -e \"Hello World!\"\n", port)}, &expect.BExp{R: "\\$ "}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: RetValue("0", "\\$ ")}, + &expect.BExp{R: RetValue("0")}, }, 60*time.Second) log.DefaultLogger().Infof("%v", resp) Expect(err).ToNot(HaveOccurred()) @@ -4294,7 +4299,7 @@ func StartHTTPServer(vmi *v1.VirtualMachineInstance, port int, isFedoraVM bool) &expect.BSnd{S: httpServerMaker}, &expect.BExp{R: prompt}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: RetValue("0", prompt)}, + &expect.BExp{R: RetValue("0")}, }, 60*time.Second) log.DefaultLogger().Infof("%v", resp) Expect(err).ToNot(HaveOccurred()) @@ -4417,7 +4422,7 @@ func GenerateHelloWorldServer(vmi *v1.VirtualMachineInstance, testPort int, prot &expect.BSnd{S: serverCommand}, &expect.BExp{R: "\\$ "}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: RetValue("0", "\\$ ")}, + &expect.BExp{R: RetValue("0")}, }, 60*time.Second) Expect(err).ToNot(HaveOccurred()) } @@ -5035,6 +5040,6 @@ func IsLauncherCapabilityValid(capability k8sv1.Capability) bool { return false } -func RetValue(retcode, prompt string) string { - return "\n" + retcode + "\r\n" + ".*" + prompt +func RetValue(retcode string) string { + return "\n" + retcode + CRLF + ".*" + PromptExpression } diff --git a/tests/vmi_cloudinit_hook_sidecar_test.go b/tests/vmi_cloudinit_hook_sidecar_test.go index 79c610122f83..ccf8ac874fb8 100644 --- a/tests/vmi_cloudinit_hook_sidecar_test.go +++ b/tests/vmi_cloudinit_hook_sidecar_test.go @@ -68,7 +68,7 @@ var _ = Describe("CloudInitHookSidecars", func() { &expect.BSnd{S: cmdCheck}, &expect.BExp{R: prompt}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", prompt)}, + &expect.BExp{R: tests.RetValue("0")}, }, 15) Expect(err).ToNot(HaveOccurred()) } diff --git a/tests/vmi_cloudinit_test.go b/tests/vmi_cloudinit_test.go index c77237a3a901..e833aa4bccfc 100644 --- a/tests/vmi_cloudinit_test.go +++ b/tests/vmi_cloudinit_test.go @@ -100,7 +100,7 @@ var _ = Describe("[rfe_id:151][crit:high][vendor:cnv-qe@redhat.com][level:compon &expect.BSnd{S: cmdCheck}, &expect.BExp{R: prompt}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", prompt)}, + &expect.BExp{R: tests.RetValue("0")}, }, 15) Expect(err).ToNot(HaveOccurred()) } diff --git a/tests/vmi_configuration_test.go b/tests/vmi_configuration_test.go index 8312a0cf8122..91a0a9299671 100644 --- a/tests/vmi_configuration_test.go +++ b/tests/vmi_configuration_test.go @@ -563,11 +563,11 @@ var _ = Describe("Configurations", func() { res, err := tests.ExpectBatchWithValidatedSend(expecter, []expect.Batcher{ &expect.BSnd{S: "[ $(free -m | grep Mem: | tr -s ' ' | cut -d' ' -f2) -gt 200 ] && echo 'pass'\n"}, - &expect.BExp{R: tests.RetValue("pass", "\\$ ")}, + &expect.BExp{R: tests.RetValue("pass")}, &expect.BSnd{S: "swapoff -a && dd if=/dev/zero of=/dev/shm/test bs=1k count=118k\n"}, &expect.BExp{R: "\\$ "}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\$ ")}, + &expect.BExp{R: tests.RetValue("0")}, }, 15*time.Second) log.DefaultLogger().Object(vmi).Infof("%v", res) Expect(err).ToNot(HaveOccurred()) @@ -621,7 +621,7 @@ var _ = Describe("Configurations", func() { // Check on the VM, if the Free memory is roughly what we expected res, err := tests.ExpectBatchWithValidatedSend(expecter, []expect.Batcher{ &expect.BSnd{S: "[ $(free -m | grep Mem: | tr -s ' ' | cut -d' ' -f2) -gt 95 ] && echo 'pass'\n"}, - &expect.BExp{R: tests.RetValue("pass", "\\$ ")}, + &expect.BExp{R: tests.RetValue("pass")}, }, 15*time.Second) log.DefaultLogger().Object(vmi).Infof("%v", res) Expect(err).ToNot(HaveOccurred()) @@ -1984,11 +1984,11 @@ var _ = Describe("Configurations", func() { res, err := tests.ExpectBatchWithValidatedSend(expecter, []expect.Batcher{ &expect.BSnd{S: "[ $(free -m | grep Mem: | tr -s ' ' | cut -d' ' -f2) -lt 80 ] && echo 'pass'\n"}, - &expect.BExp{R: tests.RetValue("pass", "\\$ ")}, + &expect.BExp{R: tests.RetValue("pass")}, &expect.BSnd{S: "swapoff -a && dd if=/dev/zero of=/dev/shm/test bs=1k count=118k\n"}, &expect.BExp{R: "\\$ "}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\$ ")}, + &expect.BExp{R: tests.RetValue("0")}, }, 15*time.Second) log.DefaultLogger().Object(vmi).Infof("%v", res) Expect(err).ToNot(HaveOccurred()) @@ -2333,7 +2333,7 @@ var _ = Describe("Configurations", func() { // Check on the VM, if expected values are there with dmidecode res, err := tests.ExpectBatchWithValidatedSend(expecter, []expect.Batcher{ &expect.BSnd{S: "[ $(sudo dmidecode -s chassis-asset-tag | tr -s ' ') = Test-123 ] && echo 'pass'\n"}, - &expect.BExp{R: tests.RetValue("pass", "\\# ")}, + &expect.BExp{R: tests.RetValue("pass")}, }, 1*time.Second) log.DefaultLogger().Object(vmi).Infof("%v", res) Expect(err).ToNot(HaveOccurred()) @@ -2377,11 +2377,11 @@ var _ = Describe("Configurations", func() { // Check on the VM, if expected values are there with dmidecode res, err := tests.ExpectBatchWithValidatedSend(expecter, []expect.Batcher{ &expect.BSnd{S: "[ $(sudo dmidecode -s system-family | tr -s ' ') = KubeVirt ] && echo 'pass'\n"}, - &expect.BExp{R: tests.RetValue("pass", "\\# ")}, + &expect.BExp{R: tests.RetValue("pass")}, &expect.BSnd{S: "[ $(sudo dmidecode -s system-product-name | tr -s ' ') = None ] && echo 'pass'\n"}, - &expect.BExp{R: tests.RetValue("pass", "\\# ")}, + &expect.BExp{R: tests.RetValue("pass")}, &expect.BSnd{S: "[ $(sudo dmidecode -s system-manufacturer | tr -s ' ') = KubeVirt ] && echo 'pass'\n"}, - &expect.BExp{R: tests.RetValue("pass", "\\# ")}, + &expect.BExp{R: tests.RetValue("pass")}, }, 1*time.Second) log.DefaultLogger().Object(vmi).Infof("%v", res) Expect(err).ToNot(HaveOccurred()) @@ -2417,11 +2417,11 @@ var _ = Describe("Configurations", func() { // Check on the VM, if expected values are there with dmidecode res, err := tests.ExpectBatchWithValidatedSend(expecter, []expect.Batcher{ &expect.BSnd{S: "[ $(sudo dmidecode -s system-family | tr -s ' ') = test ] && echo 'pass'\n"}, - &expect.BExp{R: tests.RetValue("pass", "\\# ")}, + &expect.BExp{R: tests.RetValue("pass")}, &expect.BSnd{S: "[ $(sudo dmidecode -s system-product-name | tr -s ' ') = test ] && echo 'pass'\n"}, - &expect.BExp{R: tests.RetValue("pass", "\\# ")}, + &expect.BExp{R: tests.RetValue("pass")}, &expect.BSnd{S: "[ $(sudo dmidecode -s system-manufacturer | tr -s ' ') = None ] && echo 'pass'\n"}, - &expect.BExp{R: tests.RetValue("pass", "\\# ")}, + &expect.BExp{R: tests.RetValue("pass")}, }, 1*time.Second) log.DefaultLogger().Object(vmi).Infof("%v", res) Expect(err).ToNot(HaveOccurred()) diff --git a/tests/vmi_gpu_test.go b/tests/vmi_gpu_test.go index 152dce9912ff..fd05071956ed 100644 --- a/tests/vmi_gpu_test.go +++ b/tests/vmi_gpu_test.go @@ -44,7 +44,7 @@ func checkGPUDevice(vmi *v1.VirtualMachineInstance, gpuName string, prompt strin &expect.BSnd{S: cmdCheck}, &expect.BExp{R: prompt}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", prompt)}, + &expect.BExp{R: tests.RetValue("0")}, }, 15) Expect(err).ToNot(HaveOccurred(), "GPU device %q was not found in the VMI %s within the given timeout", gpuName, vmi.Name) } diff --git a/tests/vmi_monitoring_test.go b/tests/vmi_monitoring_test.go index c9c31c517f10..e1955a3883fe 100644 --- a/tests/vmi_monitoring_test.go +++ b/tests/vmi_monitoring_test.go @@ -72,7 +72,7 @@ var _ = Describe("Health Monitoring", func() { &expect.BSnd{S: "watchdog -t 2000ms -T 4000ms /dev/watchdog && sleep 5 && killall -9 watchdog\n"}, &expect.BExp{R: "\\#"}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\#")}, + &expect.BExp{R: tests.RetValue("0")}, }, 250*time.Second) Expect(err).ToNot(HaveOccurred()) diff --git a/tests/vmi_multus_test.go b/tests/vmi_multus_test.go index 0dec86e094f2..719e1a2c21a4 100644 --- a/tests/vmi_multus_test.go +++ b/tests/vmi_multus_test.go @@ -214,7 +214,7 @@ var _ = Describe("Multus", func() { &expect.BSnd{S: cmdCheck}, &expect.BExp{R: "\\$ "}, &expect.BSnd{S: "ip addr show eth1 | grep 10.1.1 | wc -l\n"}, - &expect.BExp{R: tests.RetValue("1", "\\$ ")}, + &expect.BExp{R: tests.RetValue("1")}, }, 15) Expect(err).ToNot(HaveOccurred()) @@ -892,7 +892,7 @@ func configInterface(vmi *v1.VirtualMachineInstance, interfaceName, interfaceAdd &expect.BSnd{S: cmdCheck}, &expect.BExp{R: prompt}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", prompt)}, + &expect.BExp{R: tests.RetValue("0")}, }, 15) Expect(err).ToNot(HaveOccurred(), "Failed to configure address %s for interface %s on VMI %s", interfaceAddress, interfaceName, vmi.Name) @@ -903,7 +903,7 @@ func configInterface(vmi *v1.VirtualMachineInstance, interfaceName, interfaceAdd &expect.BSnd{S: cmdCheck}, &expect.BExp{R: prompt}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", prompt)}, + &expect.BExp{R: tests.RetValue("0")}, }, 15) Expect(err).ToNot(HaveOccurred(), "Failed to set interface %s up on VMI %s", interfaceName, vmi.Name) } @@ -916,7 +916,7 @@ func checkInterface(vmi *v1.VirtualMachineInstance, interfaceName, prompt string &expect.BSnd{S: cmdCheck}, &expect.BExp{R: prompt}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", prompt)}, + &expect.BExp{R: tests.RetValue("0")}, }, 15) Expect(err).ToNot(HaveOccurred(), "Interface %q was not found in the VMI %s within the given timeout", interfaceName, vmi.Name) } @@ -930,7 +930,7 @@ func checkMacAddress(vmi *v1.VirtualMachineInstance, interfaceName, macAddress s &expect.BExp{R: macAddress}, &expect.BExp{R: "#"}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\#")}, + &expect.BExp{R: tests.RetValue("0")}, }, 15) Expect(err).ToNot(HaveOccurred(), "MAC %q was not found in the VMI %s within the given timeout", macAddress, vmi.Name) } diff --git a/tests/vmi_networking_test.go b/tests/vmi_networking_test.go index 5fb95859bca9..a634417cf9dd 100644 --- a/tests/vmi_networking_test.go +++ b/tests/vmi_networking_test.go @@ -225,7 +225,7 @@ var _ = Describe("[rfe_id:694][crit:medium][vendor:cnv-qe@redhat.com][level:comp &expect.BSnd{S: addrShow}, &expect.BExp{R: fmt.Sprintf(".*%s.*\n", expectedMtuString)}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\$ ")}, + &expect.BExp{R: tests.RetValue("0")}, }, 180*time.Second) log.Log.Infof("%v", resp) Expect(err).ToNot(HaveOccurred()) @@ -244,7 +244,7 @@ var _ = Describe("[rfe_id:694][crit:medium][vendor:cnv-qe@redhat.com][level:comp &expect.BSnd{S: cmdCheck}, &expect.BExp{R: "\\$ "}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\$ ")}, + &expect.BExp{R: tests.RetValue("0")}, }, 180) Expect(err).ToNot(HaveOccurred()) @@ -255,7 +255,7 @@ var _ = Describe("[rfe_id:694][crit:medium][vendor:cnv-qe@redhat.com][level:comp &expect.BSnd{S: "curl --silent http://kubevirt.io > /dev/null\n"}, &expect.BExp{R: "\\$ "}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\$ ")}, + &expect.BExp{R: tests.RetValue("0")}, }, 15) Expect(err).ToNot(HaveOccurred()) }, @@ -651,13 +651,13 @@ var _ = Describe("[rfe_id:694][crit:medium][vendor:cnv-qe@redhat.com][level:comp &expect.BSnd{S: "dhclient -1 -sf /usr/bin/env --request-options subnet-mask,broadcast-address,time-offset,routers,domain-search,domain-name,domain-name-servers,host-name,nis-domain,nis-servers,ntp-servers,interface-mtu,tftp-server-name,bootfile-name eth0 | tee /dhcp-env\n"}, &expect.BExp{R: "\\#"}, &expect.BSnd{S: "grep -q 'new_tftp_server_name=tftp.kubevirt.io' /dhcp-env; echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\# ")}, + &expect.BExp{R: tests.RetValue("0")}, &expect.BSnd{S: "grep -q 'new_bootfile_name=config' /dhcp-env; echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\# ")}, + &expect.BExp{R: tests.RetValue("0")}, &expect.BSnd{S: "grep -q 'new_ntp_servers=127.0.0.1 127.0.0.2' /dhcp-env; echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\# ")}, + &expect.BExp{R: tests.RetValue("0")}, &expect.BSnd{S: "grep -q 'new_unknown_240=private.options.kubevirt.io' /dhcp-env; echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\# ")}, + &expect.BExp{R: tests.RetValue("0")}, }, 15) Expect(err).ToNot(HaveOccurred()) @@ -901,11 +901,11 @@ func createExpectTraceroute6(address string) []expect.Batcher { &expect.BSnd{S: "traceroute -6 " + address + " -w1 > tr\n"}, &expect.BExp{R: "\\$ "}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\$ ")}, + &expect.BExp{R: tests.RetValue("0")}, &expect.BSnd{S: "cat tr | grep -q \"*\\|!\"\n"}, &expect.BExp{R: "\\$ "}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("1", "\\$ ")}, + &expect.BExp{R: tests.RetValue("1")}, } } @@ -916,7 +916,7 @@ func createExpectStartTcpServer(port string) []expect.Batcher { &expect.BSnd{S: "screen -d -m sudo nc -klp " + port + " -e echo -e 'Hello World!'\n"}, &expect.BExp{R: "\\$ "}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue("0", "\\$ ")}, + &expect.BExp{R: tests.RetValue("0")}, } } @@ -931,7 +931,7 @@ func createExpectConnectToServer(serverIP, tcpPort string, expectSuccess bool) [ &expect.BSnd{S: fmt.Sprintf("echo test | nc %s %s -i 1 -w 1 1> /dev/null\n", serverIP, tcpPort)}, &expect.BExp{R: "\\$ "}, &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: tests.RetValue(expectResult, "\\$ ")}, + &expect.BExp{R: tests.RetValue(expectResult)}, } } From ad05e9268436c08c71b4f80d6937959f40a12eeb Mon Sep 17 00:00:00 2001 From: Alona Kaplan Date: Thu, 13 Aug 2020 08:54:42 +0300 Subject: [PATCH 2/2] tests: change ping to use RetValue and PromptExpression Signed-off-by: Alona Kaplan --- tests/ping.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/tests/ping.go b/tests/ping.go index f86c2a594439..4d0f4e537b2e 100644 --- a/tests/ping.go +++ b/tests/ping.go @@ -34,12 +34,9 @@ import ( "kubevirt.io/client-go/log" ) -const promptExpression = `(\$ |\# )` -const termNLExpression = "\r\n" - var ( - shellSuccess = regexp.MustCompile("\n0" + termNLExpression + ".*" + promptExpression) - shellFail = regexp.MustCompile("\n[1-9].*" + termNLExpression + ".*" + promptExpression) + shellSuccess = regexp.MustCompile(RetValue("0")) + shellFail = regexp.MustCompile(RetValue("[1-9].*")) ) // PingFromVMConsole performs a ping through the provided VMI console. @@ -62,9 +59,9 @@ func PingFromVMConsole(vmi *v1.VirtualMachineInstance, ipAddr string, args ...st err := vmiConsoleExpectBatch(vmi, []expect.Batcher{ &expect.BSnd{S: "\n"}, - &expect.BExp{R: promptExpression}, + &expect.BExp{R: PromptExpression}, &expect.BSnd{S: cmdCheck}, - &expect.BExp{R: promptExpression}, + &expect.BExp{R: PromptExpression}, &expect.BSnd{S: "echo $?\n"}, &expect.BCas{C: []expect.Caser{ &expect.Case{