Skip to content

Commit 557a7fa

Browse files
authored
Refactor controllers test setup & teardown (#473)
This will enable both Ginkgo and standard Go tests to use the same setup & teardown. Signed-off-by: Mikalai Radchuk <[email protected]>
1 parent 7d53835 commit 557a7fa

File tree

1 file changed

+36
-48
lines changed

1 file changed

+36
-48
lines changed

internal/controllers/suite_test.go

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,89 +17,77 @@ limitations under the License.
1717
package controllers_test
1818

1919
import (
20-
"context"
20+
"fmt"
21+
"os"
2122
"path/filepath"
2223
"testing"
2324

2425
. "github.com/onsi/ginkgo/v2"
2526
. "github.com/onsi/gomega"
26-
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
27-
"k8s.io/apimachinery/pkg/api/meta"
27+
2828
"k8s.io/apimachinery/pkg/runtime"
29-
"k8s.io/client-go/rest"
29+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3030
"sigs.k8s.io/controller-runtime/pkg/client"
3131
"sigs.k8s.io/controller-runtime/pkg/envtest"
3232
logf "sigs.k8s.io/controller-runtime/pkg/log"
3333
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3434

35+
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
36+
3537
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
3638
)
3739

38-
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
39-
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
40-
4140
var (
42-
cfg *rest.Config
43-
cl client.Client
44-
sch *runtime.Scheme
45-
testEnv *envtest.Environment
41+
cl client.Client
42+
sch *runtime.Scheme
4643
)
4744

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

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

56-
By("bootstrapping test environment")
57-
testEnv = &envtest.Environment{
59+
// bootstrapping test environment
60+
testEnv := &envtest.Environment{
5861
CRDDirectoryPaths: []string{
5962
filepath.Join("..", "..", "config", "crd", "bases"),
6063
filepath.Join("..", "..", "testdata", "crds")},
6164
ErrorIfCRDPathMissing: true,
6265
}
6366

64-
var err error
65-
// cfg is defined in this file globally.
66-
cfg, err = testEnv.Start()
67-
Expect(err).NotTo(HaveOccurred())
68-
Expect(cfg).NotTo(BeNil())
67+
cfg, err := testEnv.Start()
68+
if err != nil {
69+
fmt.Println(err)
70+
os.Exit(1)
71+
}
6972

7073
sch = runtime.NewScheme()
71-
err = operatorsv1alpha1.AddToScheme(sch)
72-
Expect(err).NotTo(HaveOccurred())
73-
err = rukpakv1alpha1.AddToScheme(sch)
74-
Expect(err).NotTo(HaveOccurred())
74+
utilruntime.Must(operatorsv1alpha1.AddToScheme(sch))
75+
utilruntime.Must(rukpakv1alpha1.AddToScheme(sch))
7576

7677
cl, err = client.New(cfg, client.Options{Scheme: sch})
77-
Expect(err).NotTo(HaveOccurred())
78-
Expect(cl).NotTo(BeNil())
79-
})
80-
81-
var _ = AfterSuite(func() {
82-
var operators operatorsv1alpha1.OperatorList
83-
var bundleDeployments rukpakv1alpha1.BundleDeploymentList
84-
85-
Expect(cl.List(context.Background(), &operators)).To(Succeed())
86-
Expect(cl.List(context.Background(), &bundleDeployments)).To(Succeed())
87-
88-
Expect(namesFromList(&operators)).To(BeEmpty(), "operators left in the cluster")
89-
Expect(namesFromList(&bundleDeployments)).To(BeEmpty(), "bundle deployments left in the cluster")
90-
91-
By("tearing down the test environment")
92-
err := testEnv.Stop()
93-
Expect(err).NotTo(HaveOccurred())
94-
})
78+
if err != nil {
79+
fmt.Println(err)
80+
os.Exit(1)
81+
}
9582

96-
func namesFromList(list client.ObjectList) []string {
97-
items, err := meta.ExtractList(list)
98-
Expect(err).NotTo(HaveOccurred())
83+
code := m.Run()
9984

100-
names := make([]string, 0, len(items))
101-
for _, item := range items {
102-
names = append(names, item.(client.Object).GetName())
85+
// tearing down the test environment
86+
err = testEnv.Stop()
87+
if err != nil {
88+
fmt.Println(err)
89+
os.Exit(1)
10390
}
104-
return names
91+
92+
os.Exit(code)
10593
}

0 commit comments

Comments
 (0)