Skip to content

Commit

Permalink
New test focused on installed resources checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jotak committed Mar 11, 2024
1 parent 5881bc4 commit ea1f111
Show file tree
Hide file tree
Showing 8 changed files with 374 additions and 47 deletions.
12 changes: 6 additions & 6 deletions controllers/flowcollector_controller_console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func flowCollectorConsolePluginSpecs() {
Context("Deploying the console plugin", func() {
It("Should create successfully", func() {

objs := expectCreation(cpNamespace,
objs := expectPresence(cpNamespace,
cpDepl,
cpSvc,
cpCM,
Expand Down Expand Up @@ -178,7 +178,7 @@ func flowCollectorConsolePluginSpecs() {
})

By("Expecting ServiceMonitor to exist")
expectCreation(cpNamespace, cpSM)
expectPresence(cpNamespace, cpSM)
})
})

Expand Down Expand Up @@ -248,7 +248,7 @@ func flowCollectorConsolePluginSpecs() {
fc.Spec.ConsolePlugin.Enable = ptr.To(false)
})

expectDeletion(cpNamespace,
expectAbsence(cpNamespace,
cpDepl,
cpSvc,
cpSA,
Expand All @@ -262,7 +262,7 @@ func flowCollectorConsolePluginSpecs() {
fc.Spec.ConsolePlugin.Enable = ptr.To(true)
})

expectCreation(cpNamespace,
expectPresence(cpNamespace,
cpDepl,
cpSvc,
cpSA,
Expand Down Expand Up @@ -294,15 +294,15 @@ func flowCollectorConsolePluginSpecs() {
})

It("Should redeploy console plugin in new namespace", func() {
expectDeletion(cpNamespace,
expectAbsence(cpNamespace,
cpDepl,
cpSvc,
cpSA,
cpCM,
cpSM,
)

expectCreation(otherNamespace,
expectPresence(otherNamespace,
cpDepl,
cpSvc,
cpSA,
Expand Down
12 changes: 6 additions & 6 deletions controllers/flowcollector_controller_ebpf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func flowCollectorEBPFSpecs() {
return k8sClient.Create(ctx, desired)
}).WithTimeout(timeout).WithPolling(interval).Should(Succeed())

objs := expectCreation(operatorPrivilegedNamespace,
objs := expectPresence(operatorPrivilegedNamespace,
dsRef,
saRef,
svcMetricsRef,
Expand Down Expand Up @@ -172,7 +172,7 @@ func flowCollectorEBPFSpecs() {
Expect(*container.SecurityContext.Privileged).To(BeTrue())
Expect(container.SecurityContext.Capabilities).To(BeNil())

expectDeletion(operatorNamespace+"-privileged",
expectAbsence(operatorNamespace+"-privileged",
svcMetricsRef,
smRef,
svcFLPMetricsRef,
Expand All @@ -186,11 +186,11 @@ func flowCollectorEBPFSpecs() {
fc.Spec.Namespace = operatorNamespace2
})

expectDeletion(operatorPrivilegedNamespace,
expectAbsence(operatorPrivilegedNamespace,
dsRef,
saRef,
)
expectCreation(operatorPrivilegedNamespace2,
expectPresence(operatorPrivilegedNamespace2,
dsRef,
saRef,
)
Expand Down Expand Up @@ -253,7 +253,7 @@ func flowCollectorEBPFKafkaSpecs() {
}
Expect(k8sClient.Create(ctx, descriptor)).Should(Succeed())

objs := expectCreation(operatorPrivilegedNamespace,
objs := expectPresence(operatorPrivilegedNamespace,
dsRef,
saRef,
)
Expand All @@ -270,7 +270,7 @@ func flowCollectorEBPFKafkaSpecs() {
})

It("Should properly deploy flowlogs-pipeline", func() {
objs := expectCreation(operatorNamespace,
objs := expectPresence(operatorNamespace,
flpRef,
flpSvcRef,
flpSMRef,
Expand Down
235 changes: 227 additions & 8 deletions controllers/flowcollector_controller_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package controllers

import (
. "github.com/onsi/ginkgo/v2"
operatorsv1 "github.com/openshift/api/operator/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"

flowslatest "github.com/netobserv/network-observability-operator/apis/flowcollector/v1beta2"
"github.com/netobserv/network-observability-operator/pkg/test"
"github.com/onsi/ginkgo/v2"
)

const (
Expand All @@ -21,16 +25,231 @@ var (
cleanupCR = func(key types.NamespacedName) {
test.CleanupCR(ctx, k8sClient, key)
}
expectCreation = func(namespace string, objs ...test.ResourceRef) []client.Object {
ginkgo.GinkgoHelper()
return test.ExpectCreation(ctx, k8sClient, namespace, objs...)
installResources = func(objs ...client.Object) []client.Object {
GinkgoHelper()
return test.InstallResources(ctx, k8sClient, objs)
}
expectDeletion = func(namespace string, objs ...test.ResourceRef) {
ginkgo.GinkgoHelper()
test.ExpectDeletion(ctx, k8sClient, namespace, objs...)
cleanupResources = func(objs []client.Object) {
GinkgoHelper()
test.CleanupResources(ctx, k8sClient, objs)
}
expectPresence = func(namespace string, objs ...test.ResourceRef) []client.Object {
GinkgoHelper()
return test.ExpectPresence(ctx, k8sClient, namespace, objs...)
}
expectAbsence = func(namespace string, objs ...test.ResourceRef) {
GinkgoHelper()
test.ExpectAbsence(ctx, k8sClient, namespace, objs...)
}
expectOwnership = func(namespace string, objs ...test.ResourceRef) {
ginkgo.GinkgoHelper()
GinkgoHelper()
test.ExpectOwnership(ctx, k8sClient, namespace, objs...)
}
)

type testCase struct {
name string
using *flowslatest.FlowCollectorSpec
expect []test.ResourceRef
}

// nolint:cyclop
func checkInstalledResources() {
cases := []testCase{
{
name: "Minimal CR",
using: &flowslatest.FlowCollectorSpec{},
expect: []test.ResourceRef{
test.AgentDS,
test.AgentSA,
test.AgentFLPMetricsSvc,
test.AgentFLPSM,
test.AgentFLPRule,
test.AgentFLPCRB,
test.AgentNS,
test.PluginDepl,
test.PluginCM,
test.PluginSvc,
test.PluginSA,
test.PluginCRB,
test.PluginSM,
},
},
{
name: "With agent metrics",
using: &flowslatest.FlowCollectorSpec{
Agent: flowslatest.FlowCollectorAgent{
EBPF: flowslatest.FlowCollectorEBPF{
Metrics: flowslatest.EBPFMetrics{
Enable: ptr.To(true),
},
},
},
},
expect: []test.ResourceRef{
test.AgentDS,
test.AgentSA,
test.AgentFLPMetricsSvc,
test.AgentFLPSM,
test.AgentFLPRule,
test.AgentFLPCRB,
test.AgentMetricsSvc,
test.AgentSM,
test.AgentNS,
test.PluginDepl,
test.PluginCM,
test.PluginSvc,
test.PluginSA,
test.PluginCRB,
test.PluginSM,
},
},
{
name: "With Kafka",
using: &flowslatest.FlowCollectorSpec{
DeploymentModel: flowslatest.DeploymentModelKafka,
},
expect: []test.ResourceRef{
test.AgentDS,
test.AgentSA,
test.AgentNS,
test.FLPDepl,
test.FLPCM,
test.FLPSA,
test.FLPMetricsSvc,
test.FLPSM,
test.FLPRule,
test.FLPCRB,
test.PluginDepl,
test.PluginCM,
test.PluginSvc,
test.PluginSA,
test.PluginCRB,
test.PluginSM,
},
},
{
name: "Without Console plugin",
using: &flowslatest.FlowCollectorSpec{
DeploymentModel: flowslatest.DeploymentModelKafka,
ConsolePlugin: flowslatest.FlowCollectorConsolePlugin{
Enable: ptr.To(false),
},
},
expect: []test.ResourceRef{
test.AgentDS,
test.AgentSA,
test.AgentNS,
test.FLPDepl,
test.FLPCM,
test.FLPSA,
test.FLPMetricsSvc,
test.FLPSM,
test.FLPRule,
test.FLPCRB,
},
},
{
name: "With LokiStack",
using: &flowslatest.FlowCollectorSpec{
Loki: flowslatest.FlowCollectorLoki{
Mode: flowslatest.LokiModeLokiStack,
LokiStack: flowslatest.LokiStackRef{
Name: "loki",
Namespace: "default",
},
},
},
expect: []test.ResourceRef{
test.AgentDS,
test.AgentSA,
test.AgentFLPMetricsSvc,
test.AgentFLPSM,
test.AgentFLPRule,
test.AgentFLPCRB,
test.AgentNS,
test.PluginDepl,
test.PluginCM,
test.PluginSvc,
test.PluginSA,
test.PluginCRB,
test.PluginSM,
test.LokiReaderCR,
test.LokiWriterCR,
test.LokiWriterCRB,
},
},
}

var installed []client.Object
It("Should install initial resources", func() {
installed = installResources(
&operatorsv1.Console{
ObjectMeta: metav1.ObjectMeta{Name: "cluster"},
Spec: operatorsv1.ConsoleSpec{
OperatorSpec: operatorsv1.OperatorSpec{
ManagementState: operatorsv1.Unmanaged,
},
},
},
&flowslatest.FlowCollector{
ObjectMeta: metav1.ObjectMeta{Name: "cluster"},
Spec: flowslatest.FlowCollectorSpec{Namespace: test.TestNamespace},
},
&v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "loki-gateway-ca-bundle",
Namespace: "default",
},
Data: map[string]string{"service-ca.crt": "certificate data"},
},
)
})

Context("Iterating over test cases", func() {
for _, c := range cases {
checkCase(c)
}
})

Context("Iterating in reverse order", func() {
for i := len(cases) - 1; i >= 0; i-- {
checkCase(cases[i])
}
})

Context("Cleanup", func() {
It("Should delete initial resources", func() {
cleanupResources(installed)
})

It("Should cleanup other data", func() {
for _, obj := range test.AllClusterResource {
_ = k8sClient.Delete(ctx, obj.Resource)
}
})
})
}

func checkCase(c testCase) {
It("Running case: "+c.name, func() {
updateCR(types.NamespacedName{Name: "cluster"}, func(fc *flowslatest.FlowCollector) {
fc.Spec = *c.using
fc.Spec.Namespace = test.TestNamespace
})
clusterResources := test.GetResources(c.expect, test.AllClusterResource)
resourcesMainNamespace := test.GetResources(c.expect, test.AllResourcesMainNamespace)
resourcesPrivilegedNamespace := test.GetResources(c.expect, test.AllResourcesPrivilegedNamespace)
// Ensure presence
expectPresence("", clusterResources...)
expectPresence(test.TestNamespace, resourcesMainNamespace...)
expectPresence(test.TestNamespace+"-privileged", resourcesPrivilegedNamespace...)
expectOwnership(test.TestNamespace, resourcesMainNamespace...)
expectOwnership(test.TestNamespace+"-privileged", resourcesPrivilegedNamespace...)
// Ensure absence
unusedResourcesMainNamespace := test.GetComplement(resourcesMainNamespace, test.AllResourcesMainNamespace)
unusedResourcesPrivilegedNamespace := test.GetComplement(resourcesPrivilegedNamespace, test.AllResourcesPrivilegedNamespace)
expectAbsence(test.TestNamespace, unusedResourcesMainNamespace...)
expectAbsence(test.TestNamespace+"-privileged", unusedResourcesPrivilegedNamespace...)
})
}
Loading

0 comments on commit ea1f111

Please sign in to comment.