Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
Shared start manager and aftereach teardown integration helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Manno authored and Mario Manno committed Jul 21, 2020
1 parent 5826866 commit 007feae
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
42 changes: 38 additions & 4 deletions testing/integration/environment.go
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
12 changes: 12 additions & 0 deletions testing/integration/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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...")
Expand Down

0 comments on commit 007feae

Please sign in to comment.