From 178a3a5eea6c6c16eaeb692d5ef52a4df96fddce Mon Sep 17 00:00:00 2001 From: James Munnelly Date: Wed, 4 Apr 2018 23:40:44 +0100 Subject: [PATCH] Fix up bugs in unit testing framework --- pkg/issuer/acme/util_test.go | 12 +++++++++--- test/unit/fixture.go | 11 ++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pkg/issuer/acme/util_test.go b/pkg/issuer/acme/util_test.go index 6e8051188..b65aefccb 100644 --- a/pkg/issuer/acme/util_test.go +++ b/pkg/issuer/acme/util_test.go @@ -1,6 +1,7 @@ package acme import ( + "context" "testing" "k8s.io/apimachinery/pkg/runtime" @@ -25,9 +26,11 @@ type acmeFixture struct { Client *client.FakeACME PreFn func(*acmeFixture) - CheckFn func(*acmeFixture) + CheckFn func(*acmeFixture, ...interface{}) Err bool + Ctx context.Context + // f is the integration test fixture being used for this test f *unit.Fixture } @@ -36,6 +39,9 @@ func (s *acmeFixture) Setup(t *testing.T) { if s.Client == nil { s.Client = &client.FakeACME{} } + if s.Ctx == nil { + s.Ctx = context.Background() + } s.f = &unit.Fixture{ T: t, KubeObjects: s.KubeObjects, @@ -50,13 +56,13 @@ func (s *acmeFixture) Setup(t *testing.T) { } } -func (s *acmeFixture) Finish(t *testing.T) { +func (s *acmeFixture) Finish(t *testing.T, args ...interface{}) { defer s.f.Stop() // resync listers before running checks s.f.Sync() // run custom checks if s.CheckFn != nil { - s.CheckFn(s) + s.CheckFn(s, args...) } } diff --git a/test/unit/fixture.go b/test/unit/fixture.go index eafd2fd3f..d5d95f304 100644 --- a/test/unit/fixture.go +++ b/test/unit/fixture.go @@ -60,7 +60,10 @@ func (s *Fixture) CertManagerInformerFactory() informers.SharedInformerFactory { func (s *Fixture) Start() { s.kubeClient = kubefake.NewSimpleClientset(s.KubeObjects...) s.cmClient = cmfake.NewSimpleClientset(s.CertManagerObjects...) - s.recorder = record.NewFakeRecorder(0) + // create a fake recorder with a buffer of 5. + // this may need to be increased in future to acomodate tests that + // produce more than 5 events + s.recorder = record.NewFakeRecorder(5) s.kubeClient.PrependReactor("create", "*", func(action coretesting.Action) (handled bool, ret runtime.Object, err error) { obj := action.(coretesting.CreateAction).GetObject().(metav1.Object) @@ -79,8 +82,14 @@ func (s *Fixture) Start() { } // Stop will signal the informers to stop watching changes +// This method is *not* safe to be called concurrently func (s *Fixture) Stop() { + if s.stopCh == nil { + return + } + close(s.stopCh) + s.stopCh = nil } // WaitForResync will wait for the informer factory informer duration by