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 47297da
Show file tree
Hide file tree
Showing 9 changed files with 448 additions and 185 deletions.
73 changes: 14 additions & 59 deletions controllers/flowcollector_controller_console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"k8s.io/utils/ptr"

flowslatest "github.com/netobserv/network-observability-operator/apis/flowcollector/v1beta2"
"github.com/netobserv/network-observability-operator/controllers/constants"
"github.com/netobserv/network-observability-operator/pkg/test"
)

Expand All @@ -24,15 +23,9 @@ const cpNamespace = "namespace-console-specs"

// nolint:cyclop
func flowCollectorConsolePluginSpecs() {
cpDepl := test.Deployment(constants.PluginName)
cpCM := test.ConfigMap("console-plugin-config")
cpSvc := test.Service(constants.PluginName)
cpSA := test.ServiceAccount(constants.PluginName)
cpCRB := test.ClusterRoleBinding(constants.PluginName)
cpSM := test.ServiceMonitor(constants.PluginName)
crKey := types.NamespacedName{Name: "cluster"}
consoleCRKey := types.NamespacedName{Name: "cluster"}
configKey := cpCM.GetKey(cpNamespace)
configKey := test.PluginCM.GetKey(cpNamespace)

BeforeEach(func() {
// Add any setup steps that needs to be executed before each test
Expand Down Expand Up @@ -112,25 +105,18 @@ func flowCollectorConsolePluginSpecs() {
// test Kubernetes API server, which isn't the goal here.
Context("Deploying the console plugin", func() {
It("Should create successfully", func() {

objs := expectCreation(cpNamespace,
cpDepl,
cpSvc,
cpCM,
cpSA,
cpCRB,
cpSM,
)
all := append(test.PluginResources, test.PluginCRB)
objs := expectPresence(cpNamespace, all...)
Expect(objs).To(HaveLen(6))
Expect(*objs[0].(*appsv1.Deployment).Spec.Replicas).To(Equal(int32(1)))
Expect(objs[1].(*v1.Service).Spec.Ports[0].Port).To(Equal(int32(9001)))
Expect(objs[2].(*v1.Service).Spec.Ports[0].Port).To(Equal(int32(9001)))

By("Creating the console plugin configmap")
Eventually(getConfigMapData(configKey),
timeout, interval).Should(ContainSubstring("url: http://loki:3100/"))

By("Expecting to create console plugin role binding")
rb := objs[4].(*rbacv1.ClusterRoleBinding)
rb := objs[5].(*rbacv1.ClusterRoleBinding)
Expect(rb.Subjects).Should(HaveLen(1))
Expect(rb.Subjects[0].Name).Should(Equal("netobserv-plugin"))
Expect(rb.RoleRef.Name).Should(Equal("netobserv-plugin"))
Expand All @@ -151,7 +137,7 @@ func flowCollectorConsolePluginSpecs() {
By("Expecting the console plugin Deployment to be scaled up")
Eventually(func() interface{} {
dp := appsv1.Deployment{}
if err := k8sClient.Get(ctx, cpDepl.GetKey(cpNamespace), &dp); err != nil {
if err := k8sClient.Get(ctx, test.PluginDepl.GetKey(cpNamespace), &dp); err != nil {
return err
}
return *dp.Spec.Replicas
Expand All @@ -160,7 +146,7 @@ func flowCollectorConsolePluginSpecs() {
By("Expecting the console plugin Service to be updated")
Eventually(func() interface{} {
svc := v1.Service{}
if err := k8sClient.Get(ctx, cpSvc.GetKey(cpNamespace), &svc); err != nil {
if err := k8sClient.Get(ctx, test.PluginSvc.GetKey(cpNamespace), &svc); err != nil {
return err
}
return svc.Spec.Ports[0].Port
Expand All @@ -170,15 +156,15 @@ func flowCollectorConsolePluginSpecs() {
It("Should create desired objects when they're not found (e.g. case of an operator upgrade)", func() {
// Manually delete ServiceMonitor
By("Deleting ServiceMonitor")
Eventually(func() error { return k8sClient.Delete(ctx, cpSM.Resource) }, timeout, interval).Should(Succeed())
Eventually(func() error { return k8sClient.Delete(ctx, test.PluginSM.Resource) }, timeout, interval).Should(Succeed())

// Do a dummy change that will trigger reconcile, and make sure SM is created again
updateCR(crKey, func(fc *flowslatest.FlowCollector) {
fc.Spec.Processor.LogLevel = "trace"
})

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

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

expectDeletion(cpNamespace,
cpDepl,
cpSvc,
cpSA,
cpCM,
cpSM,
)
expectAbsence(cpNamespace, test.PluginResources...)
})

It("Should recreate console plugin if enabled back", func() {
updateCR(crKey, func(fc *flowslatest.FlowCollector) {
fc.Spec.ConsolePlugin.Enable = ptr.To(true)
})

expectCreation(cpNamespace,
cpDepl,
cpSvc,
cpSA,
cpCM,
cpSM,
)
expectPresence(cpNamespace, test.PluginResources...)
})
})

Context("Checking CR ownership", func() {
It("Should be garbage collected", func() {
expectOwnership(cpNamespace,
cpDepl,
cpSvc,
cpSA,
cpCM,
cpSM,
)
expectOwnership(cpNamespace, test.PluginResources...)
})
})

Expand All @@ -294,21 +262,8 @@ func flowCollectorConsolePluginSpecs() {
})

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

expectCreation(otherNamespace,
cpDepl,
cpSvc,
cpSA,
cpCM,
cpSM,
)
expectAbsence(cpNamespace, test.PluginResources...)
expectPresence(otherNamespace, test.PluginResources...)
})
})

Expand Down
49 changes: 12 additions & 37 deletions controllers/flowcollector_controller_ebpf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,6 @@ func flowCollectorEBPFSpecs() {
operatorNamespace2 := "namespace-ebpf-specs2"
operatorPrivilegedNamespace2 := operatorNamespace2 + "-privileged"

dsRef := test.DaemonSet(constants.EBPFAgentName)
saRef := test.ServiceAccount(constants.EBPFServiceAccount)
svcMetricsRef := test.Service(constants.EBPFAgentMetricsSvcName)
svcFLPMetricsRef := test.Service("netobserv-ebpf-agent-prom")
smRef := test.ServiceMonitor(constants.EBPFAgentMetricsSvcMonitoringName)
smFLPRef := test.ServiceMonitor(constants.EBPFAgentName + "-monitor")
ruleFLPRef := test.PrometheusRule(constants.EBPFAgentName + "-alert")
nsRef := test.Namespace(operatorPrivilegedNamespace)

crKey := types.NamespacedName{Name: "cluster"}

Context("Netobserv eBPF Agent Reconciler", func() {
Expand Down Expand Up @@ -78,16 +69,8 @@ func flowCollectorEBPFSpecs() {
return k8sClient.Create(ctx, desired)
}).WithTimeout(timeout).WithPolling(interval).Should(Succeed())

objs := expectCreation(operatorPrivilegedNamespace,
dsRef,
saRef,
svcMetricsRef,
svcFLPMetricsRef,
smRef,
smFLPRef,
ruleFLPRef,
nsRef,
)
agentResourcesWithNS := append(test.AgentResources, test.AgentNS)
objs := expectPresence(operatorPrivilegedNamespace, agentResourcesWithNS...)
Expect(objs).To(HaveLen(8))

spec := objs[0].(*appsv1.DaemonSet).Spec.Template.Spec
Expand Down Expand Up @@ -171,36 +154,28 @@ func flowCollectorEBPFSpecs() {
Expect(container.SecurityContext.Privileged).To(Not(BeNil()))
Expect(*container.SecurityContext.Privileged).To(BeTrue())
Expect(container.SecurityContext.Capabilities).To(BeNil())

expectDeletion(operatorNamespace+"-privileged",
svcMetricsRef,
smRef,
svcFLPMetricsRef,
smFLPRef,
ruleFLPRef,
)
})

It("Should redeploy all when changing namespace", func() {
updateCR(crKey, func(fc *flowslatest.FlowCollector) {
fc.Spec.Namespace = operatorNamespace2
})

expectDeletion(operatorPrivilegedNamespace,
dsRef,
saRef,
expectAbsence(operatorPrivilegedNamespace,
test.AgentDS,
test.AgentSA,
)
expectCreation(operatorPrivilegedNamespace2,
dsRef,
saRef,
expectPresence(operatorPrivilegedNamespace2,
test.AgentDS,
test.AgentSA,
)
})

It("Should be garbage collected", func() {
expectOwnership(operatorPrivilegedNamespace2,
dsRef,
test.AgentDS,
test.AgentSA,
test.Namespace(operatorPrivilegedNamespace2),
saRef,
)
})

Expand Down Expand Up @@ -253,7 +228,7 @@ func flowCollectorEBPFKafkaSpecs() {
}
Expect(k8sClient.Create(ctx, descriptor)).Should(Succeed())

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

It("Should properly deploy flowlogs-pipeline", func() {
objs := expectCreation(operatorNamespace,
objs := expectPresence(operatorNamespace,
flpRef,
flpSvcRef,
flpSMRef,
Expand Down
Loading

0 comments on commit 47297da

Please sign in to comment.