@@ -17,7 +17,6 @@ limitations under the License.
17
17
package controllers
18
18
19
19
import (
20
- "context"
21
20
"testing"
22
21
23
22
"github.com/grafana/grafana-operator/v5/api/v1beta1"
@@ -28,7 +27,7 @@ import (
28
27
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
28
"k8s.io/apimachinery/pkg/types"
30
29
ctrl "sigs.k8s.io/controller-runtime"
31
- logf "sigs.k8s.io/controller-runtime/pkg/log "
30
+ "sigs.k8s.io/controller-runtime/pkg/client "
32
31
)
33
32
34
33
// Reusable objectMetas and CommonSpecs to make test tables less verbose
@@ -354,24 +353,19 @@ func TestMergeReconcileErrors(t *testing.T) {
354
353
}
355
354
356
355
var _ = Describe ("GetMatchingInstances functions" , Ordered , func () {
357
- ns1 := corev1.Namespace {ObjectMeta : metav1.ObjectMeta {
358
- Name : "get-matching-test" ,
359
- }}
360
- ns2 := corev1.Namespace {ObjectMeta : metav1.ObjectMeta {
361
- Name : "additional-grafana-namespace" ,
356
+ ns := corev1.Namespace {ObjectMeta : metav1.ObjectMeta {
357
+ Name : "matching-instances" ,
362
358
}}
363
359
allowFolder := v1beta1.GrafanaFolder {
364
360
ObjectMeta : metav1.ObjectMeta {
361
+ Namespace : ns .Name ,
365
362
Name : "allow-cross-namespace" ,
366
- Namespace : ns1 .Name ,
367
363
},
368
364
Spec : v1beta1.GrafanaFolderSpec {
369
365
GrafanaCommonSpec : v1beta1.GrafanaCommonSpec {
370
366
AllowCrossNamespaceImport : true ,
371
367
InstanceSelector : & metav1.LabelSelector {
372
- MatchLabels : map [string ]string {
373
- "test" : "folder" ,
374
- },
368
+ MatchLabels : map [string ]string {"matching-instances" : "test" },
375
369
},
376
370
},
377
371
},
@@ -382,69 +376,58 @@ var _ = Describe("GetMatchingInstances functions", Ordered, func() {
382
376
denyFolder .Spec .AllowCrossNamespaceImport = false
383
377
384
378
matchAllFolder := allowFolder .DeepCopy ()
385
- matchAllFolder .Name = "invalid- match-labels "
379
+ matchAllFolder .Name = "match-all-grafanas "
386
380
matchAllFolder .Spec .InstanceSelector = & metav1.LabelSelector {} // InstanceSelector is never nil
387
381
388
- DefaultGrafana := v1beta1.Grafana {
382
+ BaseGrafana := v1beta1.Grafana {
389
383
ObjectMeta : metav1.ObjectMeta {
384
+ Namespace : ns .Name ,
390
385
Name : "instance" ,
391
- Namespace : ns2 .Name ,
392
- Labels : map [string ]string {
393
- "test" : "folder" ,
394
- },
386
+ Labels : map [string ]string {"matching-instances" : "test" },
395
387
},
396
388
Spec : v1beta1.GrafanaSpec {},
397
389
}
398
- matchesNothingGrafana := DefaultGrafana .DeepCopy ()
399
- matchesNothingGrafana .Name = "match-nothing -instance"
390
+ matchesNothingGrafana := BaseGrafana .DeepCopy ()
391
+ matchesNothingGrafana .Name = "no-labels -instance"
400
392
matchesNothingGrafana .Labels = nil
401
393
402
- secondNamespaceGrafana := DefaultGrafana .DeepCopy ()
403
- secondNamespaceGrafana .Name = "second-namespace-instance"
404
- secondNamespaceGrafana .Namespace = ns1 .Name
405
-
406
394
// Status update is skipped for this
407
- unreadyGrafana := DefaultGrafana .DeepCopy ()
395
+ unreadyGrafana := BaseGrafana .DeepCopy ()
408
396
unreadyGrafana .Name = "unready-instance"
409
397
410
- ctx := context .Background ()
411
- testLog := logf .FromContext (ctx ).WithSink (logf.NullLogSink {})
412
- ctx = logf .IntoContext (ctx , testLog )
398
+ createCRs := []client.Object {& ns , & allowFolder , denyFolder , matchAllFolder , unreadyGrafana }
413
399
414
400
// Pre-create all resources
415
401
BeforeAll (func () { // Necessary to use assertions
416
- Expect (k8sClient .Create (ctx , & ns1 )).NotTo (HaveOccurred ())
417
- Expect (k8sClient .Create (ctx , & ns2 )).NotTo (HaveOccurred ())
418
- Expect (k8sClient .Create (ctx , & allowFolder )).NotTo (HaveOccurred ())
419
- Expect (k8sClient .Create (ctx , denyFolder )).NotTo (HaveOccurred ())
420
- Expect (k8sClient .Create (ctx , matchAllFolder )).NotTo (HaveOccurred ())
421
- Expect (k8sClient .Create (ctx , unreadyGrafana )).NotTo (HaveOccurred ())
402
+ for _ , cr := range createCRs {
403
+ Expect (k8sClient .Create (testCtx , cr )).Should (Succeed ())
404
+ }
422
405
423
- grafanas := []v1beta1.Grafana {DefaultGrafana , * matchesNothingGrafana , * secondNamespaceGrafana }
406
+ grafanas := []v1beta1.Grafana {BaseGrafana , * matchesNothingGrafana }
424
407
for _ , instance := range grafanas {
425
- Expect (k8sClient .Create (ctx , & instance )).NotTo (HaveOccurred ())
408
+ Expect (k8sClient .Create (testCtx , & instance )).NotTo (HaveOccurred ())
426
409
427
410
// Apply status to pass instance ready check
428
411
instance .Status .Stage = v1beta1 .OperatorStageComplete
429
412
instance .Status .StageStatus = v1beta1 .OperatorStageResultSuccess
430
- Expect (k8sClient .Status ().Update (ctx , & instance )).ToNot (HaveOccurred ())
413
+ Expect (k8sClient .Status ().Update (testCtx , & instance )).ToNot (HaveOccurred ())
431
414
}
432
415
})
433
416
434
417
Context ("Ensure AllowCrossNamespaceImport is upheld by GetScopedMatchingInstances" , func () {
435
418
It ("Finds all ready instances when instanceSelector is empty" , func () {
436
- instances , err := GetScopedMatchingInstances (ctx , k8sClient , matchAllFolder )
419
+ instances , err := GetScopedMatchingInstances (testCtx , k8sClient , matchAllFolder )
437
420
Expect (err ).ToNot (HaveOccurred ())
438
- Expect (instances ).To (HaveLen (3 + 2 )) // +2 To account for instances created in controllers/suite_test.go to provoke conditions
421
+ Expect (instances ).To (HaveLen (2 + 2 )) // +2 To account for instances created in controllers/suite_test.go to provoke conditions
439
422
})
440
423
It ("Finds all ready and Matching instances" , func () {
441
- instances , err := GetScopedMatchingInstances (ctx , k8sClient , & allowFolder )
424
+ instances , err := GetScopedMatchingInstances (testCtx , k8sClient , & allowFolder )
442
425
Expect (err ).ToNot (HaveOccurred ())
443
426
Expect (instances ).ToNot (BeEmpty ())
444
427
Expect (instances ).To (HaveLen (2 ))
445
428
})
446
429
It ("Finds matching and ready and matching instance in namespace" , func () {
447
- instances , err := GetScopedMatchingInstances (ctx , k8sClient , denyFolder )
430
+ instances , err := GetScopedMatchingInstances (testCtx , k8sClient , denyFolder )
448
431
Expect (err ).ToNot (HaveOccurred ())
449
432
Expect (instances ).ToNot (BeEmpty ())
450
433
Expect (instances ).To (HaveLen (1 ))
0 commit comments