From 3ba044ee79b4f88981f90f8c182fd19ca2f06bb9 Mon Sep 17 00:00:00 2001 From: David Vossel Date: Mon, 2 Oct 2017 14:17:41 -0400 Subject: [PATCH] Basic virt-launcher sanity check unit tests Signed-off-by: David Vossel --- .gitignore | 1 + cmd/fake-qemu-process/fake-qemu.go | 31 +++++++ hack/config-default.sh | 2 +- pkg/virt-launcher/monitor_suite_test.go | 32 +++++++ pkg/virt-launcher/monitor_test.go | 115 ++++++++++++++++++++++++ 5 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 cmd/fake-qemu-process/fake-qemu.go create mode 100644 pkg/virt-launcher/monitor_suite_test.go create mode 100644 pkg/virt-launcher/monitor_test.go diff --git a/.gitignore b/.gitignore index b2d6abdb52b7..c914677c1886 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ vendor/* !vendor/vendor.json .idea *.iml +cmd/fake-qemu-process/fake-qemu* cmd/virt-manifest/virt-manifest* cmd/virt-controller/virt-controller* cmd/virt-launcher/virt-launcher* diff --git a/cmd/fake-qemu-process/fake-qemu.go b/cmd/fake-qemu-process/fake-qemu.go new file mode 100644 index 000000000000..71db058608d6 --- /dev/null +++ b/cmd/fake-qemu-process/fake-qemu.go @@ -0,0 +1,31 @@ +/* + * This file is part of the KubeVirt project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright 2017 Red Hat, Inc. + * + */ + +package main + +import ( + "fmt" + "time" +) + +func main() { + fmt.Printf("Started fake qemu process\n") + time.Sleep(60 * time.Second) + fmt.Printf("Exit fake qemu process\n") +} diff --git a/hack/config-default.sh b/hack/config-default.sh index e2146178f16b..0409d41961d1 100644 --- a/hack/config-default.sh +++ b/hack/config-default.sh @@ -1,4 +1,4 @@ -binaries="cmd/virt-controller cmd/virt-launcher cmd/virt-handler cmd/virt-api cmd/virtctl cmd/virt-manifest" +binaries="cmd/virt-controller cmd/virt-launcher cmd/virt-handler cmd/virt-api cmd/virtctl cmd/virt-manifest cmd/fake-qemu-process" docker_images="cmd/virt-controller cmd/virt-launcher cmd/virt-handler cmd/virt-api cmd/virt-manifest images/haproxy images/iscsi-demo-target-tgtd images/vm-killer images/libvirt-kubevirt images/spice-proxy cmd/virt-migrator cmd/registry-disk-v1alpha images/cirros-registry-disk-demo" optional_docker_images="cmd/registry-disk-v1alpha images/fedora-atomic-registry-disk-demo" docker_prefix=kubevirt diff --git a/pkg/virt-launcher/monitor_suite_test.go b/pkg/virt-launcher/monitor_suite_test.go new file mode 100644 index 000000000000..107361a5cb3a --- /dev/null +++ b/pkg/virt-launcher/monitor_suite_test.go @@ -0,0 +1,32 @@ +/* + * This file is part of the KubeVirt project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright 2017 Red Hat, Inc. + * + */ + +package virtlauncher + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "testing" +) + +func TestVirtLauncher(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "VirtLauncher Test Suite") +} diff --git a/pkg/virt-launcher/monitor_test.go b/pkg/virt-launcher/monitor_test.go new file mode 100644 index 000000000000..15a4fa9b2dd9 --- /dev/null +++ b/pkg/virt-launcher/monitor_test.go @@ -0,0 +1,115 @@ +/* + * This file is part of the KubeVirt project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright 2017 Red Hat, Inc. + * + */ + +package virtlauncher + +import ( + "os" + "os/exec" + "strings" + "time" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("VirtLauncher", func() { + var mon *monitor + var cmd *exec.Cmd + + dir := os.Getenv("PWD") + dir = strings.TrimSuffix(dir, "pkg/virt-launcher") + + processName := "fake-qemu-process" + processPath := dir + "/cmd/fake-qemu-process/" + processName + + StartProcess := func() { + cmd = exec.Command(processPath) + err := cmd.Start() + Expect(err).ToNot(HaveOccurred()) + } + + StopProcess := func() { + cmd.Process.Kill() + cmd.Wait() + } + + VerifyProcessStarted := func() { + Eventually(func() bool { + + mon.refresh() + if mon.pid != 0 { + return true + } + return false + + }).Should(BeTrue()) + + } + + VerifyProcessStopped := func() { + Eventually(func() bool { + + mon.refresh() + if mon.pid == 0 && mon.isDone == true { + return true + } + return false + + }).Should(BeTrue()) + + } + + BeforeEach(func() { + mon = &monitor{ + exename: processName, + debugMode: true, + } + }) + + Describe("VirtLauncher", func() { + Context("process monitor", func() { + It("verify pid detection works", func() { + StartProcess() + VerifyProcessStarted() + StopProcess() + VerifyProcessStopped() + }) + + It("verify start timeout works", func() { + done := make(chan string) + + go func() { + mon.RunForever(time.Second) + done <- "exit" + }() + noExitCheck := time.After(3 * time.Second) + + exited := false + select { + case <-noExitCheck: + case <-done: + exited = true + } + + Expect(exited).To(Equal(true)) + }) + }) + }) +})