Skip to content

Commit

Permalink
Basic virt-launcher sanity check unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: David Vossel <[email protected]>
  • Loading branch information
davidvossel committed Oct 2, 2017
1 parent 9815d81 commit 3ba044e
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
31 changes: 31 additions & 0 deletions cmd/fake-qemu-process/fake-qemu.go
Original file line number Diff line number Diff line change
@@ -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")
}
2 changes: 1 addition & 1 deletion hack/config-default.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
32 changes: 32 additions & 0 deletions pkg/virt-launcher/monitor_suite_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
115 changes: 115 additions & 0 deletions pkg/virt-launcher/monitor_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
})
})
})

0 comments on commit 3ba044e

Please sign in to comment.