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

Shared start manager and aftereach teardown integration helpers #68

Merged
merged 1 commit into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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