1414package app_test
1515
1616import (
17+ "context"
1718 "fmt"
18- "os"
19- "path/filepath"
2019 "time"
2120
2221 . "github.com/onsi/ginkgo/v2"
2322 . "github.com/onsi/gomega"
23+ apiErrors "k8s.io/apimachinery/pkg/api/errors"
2424 "k8s.io/apimachinery/pkg/types"
2525 "k8s.io/apimachinery/pkg/util/json"
2626 "sigs.k8s.io/controller-runtime/pkg/client"
@@ -34,8 +34,11 @@ import (
3434)
3535
3636const (
37- testNodeName = "node1"
38- testAnnotation = "foo.bar/spam"
37+ testConfigMapName = "test"
38+ testConfigMapNamespace = "default"
39+ testConfigMapKey = "conf"
40+ testNodeName = "node1"
41+ testAnnotation = "foo.bar/spam"
3942)
4043
4144func createNode (name string ) * corev1.Node {
@@ -44,44 +47,67 @@ func createNode(name string) *corev1.Node {
4447 return node
4548}
4649
47- func createConfig (path string , cfg configPgk.Config ) {
50+ func createConfig (cfg configPgk.Config ) {
4851 data , err := json .Marshal (cfg )
4952 ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
50- ExpectWithOffset (1 , os .WriteFile (path , data , 0x744 ))
53+ err = k8sClient .Create (ctx , & corev1.ConfigMap {
54+ ObjectMeta : metav1.ObjectMeta {Name : testConfigMapName , Namespace : testConfigMapNamespace },
55+ Data : map [string ]string {testConfigMapKey : string (data )},
56+ })
57+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
58+ }
59+
60+ func newOpts () * options.Options {
61+ return & options.Options {
62+ ConfigMapName : testConfigMapName ,
63+ ConfigMapNamespace : testConfigMapNamespace ,
64+ ConfigMapKey : testConfigMapKey ,
65+ }
5166}
5267
5368var _ = Describe ("Init container" , func () {
5469 var (
55- configPath string
70+ testCtx context.Context
71+ testCFunc context.CancelFunc
5672 )
73+
5774 BeforeEach (func () {
58- configPath = filepath .Join (GinkgoT ().TempDir (), "config" )
75+ testCtx , testCFunc = context .WithCancel (ctx )
76+ })
77+
78+ AfterEach (func () {
79+ err := k8sClient .Delete (ctx , & corev1.ConfigMap {
80+ ObjectMeta : metav1.ObjectMeta {Name : testConfigMapName , Namespace : testConfigMapNamespace },
81+ })
82+ if ! apiErrors .IsNotFound (err ) {
83+ Expect (err ).NotTo (HaveOccurred ())
84+ }
85+ testCFunc ()
5986 })
6087 It ("Succeed" , func () {
6188 testDone := make (chan interface {})
6289 go func () {
6390 defer close (testDone )
6491 defer GinkgoRecover ()
65- opts := options . New ()
92+ opts := newOpts ()
6693 opts .NodeName = testNodeName
67- opts .ConfigPath = configPath
68- createConfig (configPath , configPgk.Config {SafeDriverLoad : configPgk.SafeDriverLoadConfig {
94+ createConfig (configPgk.Config {SafeDriverLoad : configPgk.SafeDriverLoadConfig {
6995 Enable : true ,
7096 Annotation : testAnnotation ,
7197 }})
7298 var err error
7399 appExit := make (chan interface {})
74100 go func () {
75- err = app .RunNetworkOperatorInitContainer (ctx , cfg , opts )
101+ err = app .RunNetworkOperatorInitContainer (testCtx , cfg , opts )
76102 close (appExit )
77103 }()
78104 node := & corev1.Node {}
79105 Eventually (func (g Gomega ) {
80- g .Expect (k8sClient .Get (ctx , types.NamespacedName {Name : testNodeName }, node )).NotTo (HaveOccurred ())
106+ g .Expect (k8sClient .Get (testCtx , types.NamespacedName {Name : testNodeName }, node )).NotTo (HaveOccurred ())
81107 g .Expect (node .GetAnnotations ()[testAnnotation ]).NotTo (BeEmpty ())
82108 }, 30 , 1 ).Should (Succeed ())
83109 // remove annotation
84- Expect (k8sClient .Patch (ctx , node , client .RawPatch (
110+ Expect (k8sClient .Patch (testCtx , node , client .RawPatch (
85111 types .MergePatchType , []byte (
86112 fmt .Sprintf (`{"metadata":{"annotations":{%q: null}}}` ,
87113 testAnnotation ))))).NotTo (HaveOccurred ())
@@ -95,17 +121,16 @@ var _ = Describe("Init container", func() {
95121 go func () {
96122 defer close (testDone )
97123 defer GinkgoRecover ()
98- opts := options . New ()
124+ opts := newOpts ()
99125 opts .NodeName = "unknown-node"
100- opts .ConfigPath = configPath
101- createConfig (configPath , configPgk.Config {SafeDriverLoad : configPgk.SafeDriverLoadConfig {
126+ createConfig (configPgk.Config {SafeDriverLoad : configPgk.SafeDriverLoadConfig {
102127 Enable : true ,
103128 Annotation : testAnnotation ,
104129 }})
105130 var err error
106131 appExit := make (chan interface {})
107132 go func () {
108- err = app .RunNetworkOperatorInitContainer (ctx , cfg , opts )
133+ err = app .RunNetworkOperatorInitContainer (testCtx , cfg , opts )
109134 close (appExit )
110135 }()
111136 Eventually (appExit , 30 , 1 ).Should (BeClosed ())
@@ -118,20 +143,19 @@ var _ = Describe("Init container", func() {
118143 go func () {
119144 defer close (testDone )
120145 defer GinkgoRecover ()
121- opts := options . New ()
146+ opts := newOpts ()
122147 opts .NodeName = testNodeName
123- opts .ConfigPath = configPath
124- createConfig (configPath , configPgk.Config {SafeDriverLoad : configPgk.SafeDriverLoadConfig {
148+ createConfig (configPgk.Config {SafeDriverLoad : configPgk.SafeDriverLoadConfig {
125149 Enable : true ,
126150 Annotation : testAnnotation ,
127151 }})
128152 var err error
129153 appExit := make (chan interface {})
130154 go func () {
131- err = app .RunNetworkOperatorInitContainer (ctx , cfg , opts )
155+ err = app .RunNetworkOperatorInitContainer (testCtx , cfg , opts )
132156 close (appExit )
133157 }()
134- cFunc ()
158+ testCFunc ()
135159 Eventually (appExit , 30 , 1 ).Should (BeClosed ())
136160 Expect (err ).To (HaveOccurred ())
137161 }()
@@ -142,13 +166,12 @@ var _ = Describe("Init container", func() {
142166 go func () {
143167 defer close (testDone )
144168 defer GinkgoRecover ()
145- opts := options . New ()
169+ opts := newOpts ()
146170 opts .NodeName = "unknown-node"
147- opts .ConfigPath = configPath
148171 var err error
149172 appExit := make (chan interface {})
150173 go func () {
151- err = app .RunNetworkOperatorInitContainer (ctx , cfg , opts )
174+ err = app .RunNetworkOperatorInitContainer (testCtx , cfg , opts )
152175 close (appExit )
153176 }()
154177 Eventually (appExit , 30 , 1 ).Should (BeClosed ())
@@ -161,16 +184,15 @@ var _ = Describe("Init container", func() {
161184 go func () {
162185 defer close (testDone )
163186 defer GinkgoRecover ()
164- opts := options . New ()
187+ opts := newOpts ()
165188 opts .NodeName = testNodeName
166- opts .ConfigPath = configPath
167- createConfig (configPath , configPgk.Config {SafeDriverLoad : configPgk.SafeDriverLoadConfig {
189+ createConfig (configPgk.Config {SafeDriverLoad : configPgk.SafeDriverLoadConfig {
168190 Enable : false ,
169191 }})
170192 var err error
171193 appExit := make (chan interface {})
172194 go func () {
173- err = app .RunNetworkOperatorInitContainer (ctx , cfg , opts )
195+ err = app .RunNetworkOperatorInitContainer (testCtx , cfg , opts )
174196 close (appExit )
175197 }()
176198 Eventually (appExit , 30 , 1 ).Should (BeClosed ())
0 commit comments