Skip to content

Commit 74a20b6

Browse files
committed
chore: Refactor GetScopedMatchingInstances to rely more on suite_test.go vars
1 parent 704adac commit 74a20b6

File tree

2 files changed

+26
-42
lines changed

2 files changed

+26
-42
lines changed

controllers/controller_shared_test.go

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package controllers
1818

1919
import (
20-
"context"
2120
"testing"
2221

2322
"github.com/grafana/grafana-operator/v5/api/v1beta1"
@@ -28,7 +27,7 @@ import (
2827
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2928
"k8s.io/apimachinery/pkg/types"
3029
ctrl "sigs.k8s.io/controller-runtime"
31-
logf "sigs.k8s.io/controller-runtime/pkg/log"
30+
"sigs.k8s.io/controller-runtime/pkg/client"
3231
)
3332

3433
// Reusable objectMetas and CommonSpecs to make test tables less verbose
@@ -354,24 +353,19 @@ func TestMergeReconcileErrors(t *testing.T) {
354353
}
355354

356355
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",
362358
}}
363359
allowFolder := v1beta1.GrafanaFolder{
364360
ObjectMeta: metav1.ObjectMeta{
361+
Namespace: ns.Name,
365362
Name: "allow-cross-namespace",
366-
Namespace: ns1.Name,
367363
},
368364
Spec: v1beta1.GrafanaFolderSpec{
369365
GrafanaCommonSpec: v1beta1.GrafanaCommonSpec{
370366
AllowCrossNamespaceImport: true,
371367
InstanceSelector: &metav1.LabelSelector{
372-
MatchLabels: map[string]string{
373-
"test": "folder",
374-
},
368+
MatchLabels: map[string]string{"matching-instances": "test"},
375369
},
376370
},
377371
},
@@ -382,69 +376,58 @@ var _ = Describe("GetMatchingInstances functions", Ordered, func() {
382376
denyFolder.Spec.AllowCrossNamespaceImport = false
383377

384378
matchAllFolder := allowFolder.DeepCopy()
385-
matchAllFolder.Name = "invalid-match-labels"
379+
matchAllFolder.Name = "match-all-grafanas"
386380
matchAllFolder.Spec.InstanceSelector = &metav1.LabelSelector{} // InstanceSelector is never nil
387381

388-
DefaultGrafana := v1beta1.Grafana{
382+
BaseGrafana := v1beta1.Grafana{
389383
ObjectMeta: metav1.ObjectMeta{
384+
Namespace: ns.Name,
390385
Name: "instance",
391-
Namespace: ns2.Name,
392-
Labels: map[string]string{
393-
"test": "folder",
394-
},
386+
Labels: map[string]string{"matching-instances": "test"},
395387
},
396388
Spec: v1beta1.GrafanaSpec{},
397389
}
398-
matchesNothingGrafana := DefaultGrafana.DeepCopy()
399-
matchesNothingGrafana.Name = "match-nothing-instance"
390+
matchesNothingGrafana := BaseGrafana.DeepCopy()
391+
matchesNothingGrafana.Name = "no-labels-instance"
400392
matchesNothingGrafana.Labels = nil
401393

402-
secondNamespaceGrafana := DefaultGrafana.DeepCopy()
403-
secondNamespaceGrafana.Name = "second-namespace-instance"
404-
secondNamespaceGrafana.Namespace = ns1.Name
405-
406394
// Status update is skipped for this
407-
unreadyGrafana := DefaultGrafana.DeepCopy()
395+
unreadyGrafana := BaseGrafana.DeepCopy()
408396
unreadyGrafana.Name = "unready-instance"
409397

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}
413399

414400
// Pre-create all resources
415401
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+
}
422405

423-
grafanas := []v1beta1.Grafana{DefaultGrafana, *matchesNothingGrafana, *secondNamespaceGrafana}
406+
grafanas := []v1beta1.Grafana{BaseGrafana, *matchesNothingGrafana}
424407
for _, instance := range grafanas {
425-
Expect(k8sClient.Create(ctx, &instance)).NotTo(HaveOccurred())
408+
Expect(k8sClient.Create(testCtx, &instance)).NotTo(HaveOccurred())
426409

427410
// Apply status to pass instance ready check
428411
instance.Status.Stage = v1beta1.OperatorStageComplete
429412
instance.Status.StageStatus = v1beta1.OperatorStageResultSuccess
430-
Expect(k8sClient.Status().Update(ctx, &instance)).ToNot(HaveOccurred())
413+
Expect(k8sClient.Status().Update(testCtx, &instance)).ToNot(HaveOccurred())
431414
}
432415
})
433416

434417
Context("Ensure AllowCrossNamespaceImport is upheld by GetScopedMatchingInstances", func() {
435418
It("Finds all ready instances when instanceSelector is empty", func() {
436-
instances, err := GetScopedMatchingInstances(ctx, k8sClient, matchAllFolder)
419+
instances, err := GetScopedMatchingInstances(testCtx, k8sClient, matchAllFolder)
437420
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
439422
})
440423
It("Finds all ready and Matching instances", func() {
441-
instances, err := GetScopedMatchingInstances(ctx, k8sClient, &allowFolder)
424+
instances, err := GetScopedMatchingInstances(testCtx, k8sClient, &allowFolder)
442425
Expect(err).ToNot(HaveOccurred())
443426
Expect(instances).ToNot(BeEmpty())
444427
Expect(instances).To(HaveLen(2))
445428
})
446429
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)
448431
Expect(err).ToNot(HaveOccurred())
449432
Expect(instances).ToNot(BeEmpty())
450433
Expect(instances).To(HaveLen(1))

controllers/suite_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@ func createSharedTestCRs(port string) {
178178
Namespace: "default",
179179
Name: grafanaName,
180180
Labels: map[string]string{
181-
"synchronized": "test",
182-
"dashboards": "grafana",
181+
"synchronized": "test",
182+
"matching-instances": "test",
183+
"dashboards": "grafana",
183184
},
184185
},
185186
Spec: v1beta1.GrafanaSpec{

0 commit comments

Comments
 (0)