From dffc2bfa941d251c4e2d912e6fe6bc528eb06ee4 Mon Sep 17 00:00:00 2001 From: Mario Manno Date: Tue, 21 Jul 2020 12:51:49 +0200 Subject: [PATCH] Shared start manager and aftereach teardown integration helpers [#173218516](https://www.pivotaltracker.com/story/show/173218516) --- testing/integration/environment.go | 42 +++++++++++++++++++++++++++--- testing/integration/namespace.go | 12 +++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/testing/integration/environment.go b/testing/integration/environment.go index ac9fa33..09be117 100644 --- a/testing/integration/environment.go +++ b/testing/integration/environment.go @@ -1,36 +1,70 @@ package environment import ( + "fmt" "log" "os" "path/filepath" + "github.com/onsi/ginkgo" + "github.com/onsi/gomega" + "github.com/onsi/gomega/gexec" "go.uber.org/zap" "go.uber.org/zap/zaptest/observer" + "sigs.k8s.io/controller-runtime/pkg/manager" _ "k8s.io/client-go/plugin/pkg/client/auth/oidc" //from https://github.com/kubernetes/client-go/issues/345 "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" "code.cloudfoundry.org/quarks-utils/pkg/config" + "code.cloudfoundry.org/quarks-utils/testing/machine" ) -// GinkgoTeardownFunc is used to clean up the test environment -type GinkgoTeardownFunc func(wasFailure bool) - // Environment starts our operator and handles interaction with the k8s // cluster used in the tests type Environment struct { ID int - Teardown GinkgoTeardownFunc KubeConfig *rest.Config Log *zap.SugaredLogger + TeardownFunc machine.TearDownFunc Config *config.Config ObservedLogs *observer.ObservedLogs Namespace string Stop chan struct{} } +// StartManager is used to clean up the test environment +func (e *Environment) StartManager(mgr manager.Manager) { + e.Stop = make(chan struct{}) + go func() { + defer ginkgo.GinkgoRecover() + gomega.Expect(mgr.Start(e.Stop)).NotTo(gomega.HaveOccurred()) + }() +} + +// Teardown is used to clean up the test environment +func (e *Environment) Teardown(wasFailure bool) { + if wasFailure { + DumpENV(e.Namespace) + } + + if e.Stop != nil { + close(e.Stop) + } + + gexec.Kill() + + if e.TeardownFunc == nil { + return + } + err := e.TeardownFunc() + if err != nil && !NamespaceDeletionInProgress(err) { + fmt.Printf("WARNING: failed to delete namespace %s: %v\n", e.Namespace, err) + gomega.Expect(err).NotTo(gomega.HaveOccurred()) + } +} + // KubeConfig returns a kube config for this environment func KubeConfig() (*rest.Config, error) { location := os.Getenv("KUBECONFIG") diff --git a/testing/integration/namespace.go b/testing/integration/namespace.go index 189f3d9..daea478 100644 --- a/testing/integration/namespace.go +++ b/testing/integration/namespace.go @@ -10,6 +10,8 @@ import ( "strings" cmdHelper "code.cloudfoundry.org/quarks-utils/testing" + "code.cloudfoundry.org/quarks-utils/testing/machine" + "github.com/pkg/errors" ) // GetNamespaceName returns a numbered namespace @@ -21,6 +23,16 @@ func GetNamespaceName(namespaceCounter int) string { return ns + "-" + strconv.Itoa(int(namespaceCounter)) } +// SetupNamespace creates a new labeled namespace and sets the teardown func in the environment +func SetupNamespace(e *Environment, m machine.Machine, labels map[string]string) error { + nsTeardown, err := m.CreateLabeledNamespace(e.Namespace, labels) + if err != nil { + return errors.Wrapf(err, "Integration setup failed. Creating namespace %s failed", e.Namespace) + } + e.TeardownFunc = nsTeardown + return nil +} + // DumpENV executes testing/dump_env.sh to write k8s resources to files func DumpENV(namespace string) { fmt.Println("Collecting debug information...")