Skip to content

Commit

Permalink
Make integration tests work again with workqueue
Browse files Browse the repository at this point in the history
  • Loading branch information
rmohr committed Jan 11, 2017
1 parent ab17775 commit 2f605dc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/virt-handler/libvirt/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package libvirt

import (
"encoding/xml"
"fmt"
"github.com/jeevatkm/go-model"
"github.com/rgbkrk/libvirt-go"
"k8s.io/client-go/pkg/api"
Expand All @@ -16,7 +17,6 @@ import (
"k8s.io/client-go/tools/record"
"kubevirt.io/kubevirt/pkg/api/v1"
"kubevirt.io/kubevirt/pkg/logging"
"fmt"
)

type DomainManager interface {
Expand Down
2 changes: 0 additions & 2 deletions pkg/virt-handler/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,11 @@ func (c *Controller) runWorker() {
func (c *Controller) processNextWorkItem() bool {
key, quit := c.queue.Get()
if quit {
logging.DefaultLogger().V(3).Info().Msg("Exiting")
return false
}
defer c.queue.Done(key)
// Fetch the latest Vm state from cache
obj, exists, err := c.indexer.GetByKey(key.(string))
logging.DefaultLogger().V(3).Info().Msgf("Object %s", obj)

if err != nil {
// TODO do something more smart here
Expand Down
9 changes: 7 additions & 2 deletions tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"k8s.io/client-go/pkg/fields"
"k8s.io/client-go/pkg/labels"
"k8s.io/client-go/pkg/runtime"
"k8s.io/client-go/pkg/util/rand"
"kubevirt.io/kubevirt/pkg/api/v1"
"kubevirt.io/kubevirt/pkg/kubecli"
)
Expand All @@ -28,9 +29,9 @@ func (w *ObjectEventWatcher) Watch() {
panic(err)
}

uid := w.object.(meta.ObjectMetaAccessor).GetObjectMeta().GetUID()
uid := w.object.(meta.ObjectMetaAccessor).GetObjectMeta().GetName()
eventWatcher, err := cli.Core().Events(api.NamespaceDefault).
Watch(kubev1.ListOptions{FieldSelector: fields.ParseSelectorOrDie("involvedObject.uid=" + string(uid)).String()})
Watch(kubev1.ListOptions{FieldSelector: fields.ParseSelectorOrDie("involvedObject.name=" + string(uid)).String()})
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -65,3 +66,7 @@ func PanicOnError(err error) {
panic(err)
}
}

func NewRandomVM() *v1.VM {
return v1.NewMinimalVM("testvm" + rand.String(5))
}
22 changes: 14 additions & 8 deletions tests/vmlifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"kubevirt.io/kubevirt/pkg/api/v1"
"kubevirt.io/kubevirt/pkg/kubecli"
"kubevirt.io/kubevirt/tests"
"strings"
)

var _ = Describe("Vmlifecycle", func() {
Expand All @@ -17,13 +18,14 @@ var _ = Describe("Vmlifecycle", func() {

restClient, err := kubecli.GetRESTClient()
tests.PanicOnError(err)
var vm *v1.VM

BeforeEach(func() {
vm = tests.NewRandomVM()
tests.MustCleanup()
})

Context("New VM given", func() {
vm := v1.NewMinimalVM("testvm")

It("Should be accepted on POST", func() {
err := restClient.Post().Resource("vms").Namespace(api.NamespaceDefault).Body(vm).Do().Error()
Expand All @@ -39,7 +41,7 @@ var _ = Describe("Vmlifecycle", func() {
Expect(event.Type).NotTo(Equal("Warning"), "Received VM warning event")
if event.Type == "Normal" && event.Reason == v1.Started.String() {
result = restClient.Get().Namespace(api.NamespaceDefault).
Resource("vms").Name("testvm").Do()
Resource("vms").Name(vm.GetObjectMeta().GetName()).Do()
obj, err := result.Get()
Expect(err).To(BeNil())
Expect(string(obj.(*v1.VM).Status.Phase)).To(Equal(string(v1.Running)))
Expand All @@ -51,10 +53,9 @@ var _ = Describe("Vmlifecycle", func() {
}, 10)

Context("New VM which can't be started", func() {
vm := v1.NewMinimalVM("testvm")
vm.Spec.Domain.Devices.Interfaces[0].Source.Network = "nonexistent"

It("Should retry starting the VM", func(done Done) {
vm.Spec.Domain.Devices.Interfaces[0].Source.Network = "nonexistent"
result := restClient.Post().Resource("vms").Namespace(api.NamespaceDefault).Body(vm).Do()
Expect(result.Error()).To(BeNil())

Expand All @@ -63,8 +64,8 @@ var _ = Describe("Vmlifecycle", func() {
tests.NewObjectEventWatcher(obj, func(event *kubev1.Event) bool {
if event.Type == "Warning" && event.Reason == v1.SyncFailed.String() {
retryCount++
if retryCount >= 3 {
// Done, three retries is enough
if retryCount >= 2 {
// Done, two retries is enough
return true
}
}
Expand All @@ -73,13 +74,15 @@ var _ = Describe("Vmlifecycle", func() {
close(done)
}, 10)

PIt("Should stop retrying starting the VM on VM delete", func(done Done) {
It("Should stop retrying invalid VM and go on to latest change request", func(done Done) {
vm.Spec.Domain.Devices.Interfaces[0].Source.Network = "nonexistent"
result := restClient.Post().Resource("vms").Namespace(api.NamespaceDefault).Body(vm).Do()
Expect(result.Error()).To(BeNil())

// Wait until we see that starting the VM is failing
obj, _ := result.Get()
tests.NewObjectEventWatcher(obj, func(event *kubev1.Event) bool {
if event.Type == "Warning" && event.Reason == v1.SyncFailed.String() {
if event.Type == "Warning" && event.Reason == v1.SyncFailed.String() && strings.Contains(event.Message, "nonexistent") {
return true
}
return false
Expand All @@ -88,13 +91,16 @@ var _ = Describe("Vmlifecycle", func() {
result = restClient.Delete().Resource("vms").Namespace(api.NamespaceDefault).Name(vm.GetObjectMeta().GetName()).Do()
Expect(result.Error()).To(BeNil())

// Check that the definition is deleted from the host
tests.NewObjectEventWatcher(obj, func(event *kubev1.Event) bool {
if event.Type == "Normal" && event.Reason == v1.Deleted.String() {
return true
}
return false
}).Watch()

close(done)

}, 10)
})
})
Expand Down

0 comments on commit 2f605dc

Please sign in to comment.