Skip to content

Commit

Permalink
Improve unit and integration tests
Browse files Browse the repository at this point in the history
Don't set verbose in 'make test' as envTest is really noisey in the
integration tests.

Also fixes an issue with the manager not being shut down cleanly at the
end of integration tests which would result in error log output:
kubernetes-sigs/controller-runtime#904
  • Loading branch information
dyson committed Jul 17, 2020
1 parent a99f1fc commit 192b4ba
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ bin/%: generate fmt vet

# go get -u github.com/onsi/ginkgo/ginkgo
test:
ginkgo -v -r
ginkgo -r

fmt:
go fmt ./...
Expand Down
17 changes: 12 additions & 5 deletions controllers/rbac/integration/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
var (
mgr ctrl.Manager
testEnv *envtest.Environment

finished = make(chan struct{})
)

func TestSuite(t *testing.T) {
Expand Down Expand Up @@ -82,18 +84,23 @@ var _ = BeforeSuite(func(done Done) {
}).SetupWithManager(mgr)
Expect(err).ToNot(HaveOccurred())

go func() {
<-ctrl.SetupSignalHandler()
close(finished)
}()

go func() {
defer GinkgoRecover()
err = mgr.Start(ctrl.SetupSignalHandler())
err = mgr.Start(finished)
Expect(err).ToNot(HaveOccurred(), "failed to run manager")
gexec.KillAndWait(4 * time.Second)
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
}()

close(done)
}, 60)

var _ = AfterSuite(func() {
By("tearing down the test environment")
gexec.KillAndWait(5 * time.Second)
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
close(finished)
})
12 changes: 10 additions & 2 deletions controllers/workloads/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,12 @@ var _ = Describe("Console", func() {
}).ShouldNot(BeNil(),
"the console status should be defined")

Expect(updatedCsl.Status.ExpiryTime).NotTo(BeNil(), "the console expiry time should be set")
Eventually(func() *metav1.Time {
mgr.GetClient().Get(context.TODO(), identifier, updatedCsl)
return updatedCsl.Status.ExpiryTime
}).ShouldNot(BeNil(),
"the console expiry time should be defined")

Expect(
updatedCsl.Status.ExpiryTime.Time.After(time.Now())).To(BeTrue(),
"the console expiry time should be after now()",
Expand Down Expand Up @@ -462,7 +467,10 @@ var _ = Describe("Console", func() {
}).ShouldNot(BeNil(),
"the console status should be defined")

Expect(updatedCsl.Stopped()).To(BeTrue())
Eventually(func() bool {
mgr.GetClient().Get(context.TODO(), identifier, updatedCsl)
return updatedCsl.Stopped()
}).Should(BeTrue())
Expect(
updatedCsl.Status.CompletionTime.Time.Before(time.Now())).To(BeTrue(),
"the console completion time should be before now()",
Expand Down
17 changes: 12 additions & 5 deletions controllers/workloads/integration/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
var (
mgr ctrl.Manager
testEnv *envtest.Environment

finished = make(chan struct{})
)

func TestSuite(t *testing.T) {
Expand Down Expand Up @@ -69,18 +71,23 @@ var _ = BeforeSuite(func(done Done) {
}).SetupWithManager(context.TODO(), mgr)
Expect(err).ToNot(HaveOccurred())

go func() {
<-ctrl.SetupSignalHandler()
close(finished)
}()

go func() {
defer GinkgoRecover()
err = mgr.Start(ctrl.SetupSignalHandler())
err = mgr.Start(finished)
Expect(err).ToNot(HaveOccurred(), "failed to run manager")
gexec.KillAndWait(4 * time.Second)
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
}()

close(done)
}, 60)

var _ = AfterSuite(func() {
By("tearing down the test environment")
gexec.KillAndWait(5 * time.Second)
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
close(finished)
})

0 comments on commit 192b4ba

Please sign in to comment.