Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor controllers test setup & teardown for reusability between Ginkgo and standard Go tests #473

Merged
Merged
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
84 changes: 36 additions & 48 deletions internal/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,89 +17,77 @@ limitations under the License.
package controllers_test

import (
"context"
"fmt"
"os"
"path/filepath"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
"k8s.io/apimachinery/pkg/api/meta"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"

operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var (
cfg *rest.Config
cl client.Client
sch *runtime.Scheme
testEnv *envtest.Environment
cl client.Client
sch *runtime.Scheme
Comment on lines +41 to +42
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed cfg and testEnv from here. They were defined on the package level so that both BeforeSuite and AfterSuite could access them. Now that we do setup and teardown in single TestMain function - they can be function scoped.

)

// Some of the tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
// We plan phase Ginkgo out for unit tests.
// See: https://github.com/operator-framework/operator-controller/issues/189
func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Controller Suite")
}

var _ = BeforeSuite(func() {
// This setup allows for Ginkgo and standard Go tests to co-exist
// and use the same setup and teardown.
func TestMain(m *testing.M) {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

By("bootstrapping test environment")
testEnv = &envtest.Environment{
// bootstrapping test environment
testEnv := &envtest.Environment{
CRDDirectoryPaths: []string{
filepath.Join("..", "..", "config", "crd", "bases"),
filepath.Join("..", "..", "testdata", "crds")},
ErrorIfCRDPathMissing: true,
}

var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())
cfg, err := testEnv.Start()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

sch = runtime.NewScheme()
err = operatorsv1alpha1.AddToScheme(sch)
Expect(err).NotTo(HaveOccurred())
err = rukpakv1alpha1.AddToScheme(sch)
Expect(err).NotTo(HaveOccurred())
utilruntime.Must(operatorsv1alpha1.AddToScheme(sch))
utilruntime.Must(rukpakv1alpha1.AddToScheme(sch))

cl, err = client.New(cfg, client.Options{Scheme: sch})
Expect(err).NotTo(HaveOccurred())
Expect(cl).NotTo(BeNil())
})

var _ = AfterSuite(func() {
var operators operatorsv1alpha1.OperatorList
var bundleDeployments rukpakv1alpha1.BundleDeploymentList

Expect(cl.List(context.Background(), &operators)).To(Succeed())
Expect(cl.List(context.Background(), &bundleDeployments)).To(Succeed())

Expect(namesFromList(&operators)).To(BeEmpty(), "operators left in the cluster")
Expect(namesFromList(&bundleDeployments)).To(BeEmpty(), "bundle deployments left in the cluster")
Comment on lines -82 to -89
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I droped this bit. I do not see much value in failing after tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was to ensure everything is cleaned up before stopping the running envtest process, but if envtest exits just fine without this cleanup I think its OK to remove


By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}

func namesFromList(list client.ObjectList) []string {
items, err := meta.ExtractList(list)
Expect(err).NotTo(HaveOccurred())
code := m.Run()

names := make([]string, 0, len(items))
for _, item := range items {
names = append(names, item.(client.Object).GetName())
// tearing down the test environment
err = testEnv.Stop()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return names

os.Exit(code)
}