@@ -17,89 +17,77 @@ limitations under the License.
17
17
package controllers_test
18
18
19
19
import (
20
- "context"
20
+ "fmt"
21
+ "os"
21
22
"path/filepath"
22
23
"testing"
23
24
24
25
. "github.com/onsi/ginkgo/v2"
25
26
. "github.com/onsi/gomega"
26
- rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
27
- "k8s.io/apimachinery/pkg/api/meta"
27
+
28
28
"k8s.io/apimachinery/pkg/runtime"
29
- "k8s.io/client-go/rest "
29
+ utilruntime "k8s.io/apimachinery/pkg/util/runtime "
30
30
"sigs.k8s.io/controller-runtime/pkg/client"
31
31
"sigs.k8s.io/controller-runtime/pkg/envtest"
32
32
logf "sigs.k8s.io/controller-runtime/pkg/log"
33
33
"sigs.k8s.io/controller-runtime/pkg/log/zap"
34
34
35
+ rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
36
+
35
37
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
36
38
)
37
39
38
- // These tests use Ginkgo (BDD-style Go testing framework). Refer to
39
- // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
40
-
41
40
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
46
43
)
47
44
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
48
49
func TestAPIs (t * testing.T ) {
49
50
RegisterFailHandler (Fail )
50
51
RunSpecs (t , "Controller Suite" )
51
52
}
52
53
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 ) {
54
57
logf .SetLogger (zap .New (zap .WriteTo (GinkgoWriter ), zap .UseDevMode (true )))
55
58
56
- By ( " bootstrapping test environment" )
57
- testEnv = & envtest.Environment {
59
+ // bootstrapping test environment
60
+ testEnv : = & envtest.Environment {
58
61
CRDDirectoryPaths : []string {
59
62
filepath .Join (".." , ".." , "config" , "crd" , "bases" ),
60
63
filepath .Join (".." , ".." , "testdata" , "crds" )},
61
64
ErrorIfCRDPathMissing : true ,
62
65
}
63
66
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
+ }
69
72
70
73
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 ))
75
76
76
77
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
+ }
95
82
96
- func namesFromList (list client.ObjectList ) []string {
97
- items , err := meta .ExtractList (list )
98
- Expect (err ).NotTo (HaveOccurred ())
83
+ code := m .Run ()
99
84
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 )
103
90
}
104
- return names
91
+
92
+ os .Exit (code )
105
93
}
0 commit comments