Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split tests per controller #510

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 94 additions & 66 deletions controllers/flowcollector_controller_certificates_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package controllers

import (
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
Expand All @@ -14,11 +16,16 @@ import (
"github.com/netobserv/network-observability-operator/controllers/constants"
. "github.com/netobserv/network-observability-operator/controllers/controllerstest"
"github.com/netobserv/network-observability-operator/controllers/flp"
"github.com/netobserv/network-observability-operator/pkg/test"
"github.com/netobserv/network-observability-operator/pkg/watchers"
)

var cmw watchers.ConfigWatchable
var sw watchers.SecretWatchable
var (
cmw watchers.ConfigWatchable
sw watchers.SecretWatchable
consistentlyTimeout = 2 * time.Second
consistentlyInterval = 500 * time.Millisecond
)

// nolint:cyclop
func flowCollectorCertificatesSpecs() {
Expand Down Expand Up @@ -48,7 +55,7 @@ func flowCollectorCertificatesSpecs() {
"other": "any",
},
}
expectedLokiHash, _ := cmw.GetDigest(&lokiCert, []string{"service-ca.crt"})
expectedLokiHash, _ := cmw.GetDigest(&lokiCert, []string{"service-ca.crt"}) // C80Sbg==
kafkaCert := v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "kafka-ca",
Expand All @@ -59,7 +66,7 @@ func flowCollectorCertificatesSpecs() {
"other": "any",
},
}
expectedKafkaHash, _ := cmw.GetDigest(&kafkaCert, []string{"cert.crt"})
expectedKafkaHash, _ := cmw.GetDigest(&kafkaCert, []string{"cert.crt"}) // tDuVsw==
kafkaUserCert := v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "kafka-user",
Expand All @@ -71,7 +78,7 @@ func flowCollectorCertificatesSpecs() {
"other": []byte("any"),
},
}
expectedKafkaUserHash, _ := sw.GetDigest(&kafkaUserCert, []string{"user.crt", "user.key"})
expectedKafkaUserHash, _ := sw.GetDigest(&kafkaUserCert, []string{"user.crt", "user.key"}) // QztU6w==
kafka2Cert := v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "kafka-exporter-ca",
Expand All @@ -82,7 +89,7 @@ func flowCollectorCertificatesSpecs() {
"other": "any",
},
}
expectedKafka2Hash, _ := cmw.GetDigest(&kafka2Cert, []string{"cert.crt"})
expectedKafka2Hash, _ := cmw.GetDigest(&kafka2Cert, []string{"cert.crt"}) // RO7D5Q==
kafka2Sasl := v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "kafka-exporter-sasl",
Expand All @@ -93,8 +100,8 @@ func flowCollectorCertificatesSpecs() {
"password": []byte("azerty"),
},
}
expectedKafkaSaslHash1, _ := sw.GetDigest(&kafka2Sasl, []string{"username"})
expectedKafkaSaslHash2, _ := sw.GetDigest(&kafka2Sasl, []string{"password"})
expectedKafkaSaslHash1, _ := sw.GetDigest(&kafka2Sasl, []string{"username"}) // hlEvyw==
expectedKafkaSaslHash2, _ := sw.GetDigest(&kafka2Sasl, []string{"password"}) // FOs6Rg==

BeforeEach(func() {
// Add any setup steps that needs to be executed before each test
Expand All @@ -107,9 +114,6 @@ func flowCollectorCertificatesSpecs() {
agent := appsv1.DaemonSet{}
flp := appsv1.Deployment{}
plugin := appsv1.Deployment{}
var lastAgentAnnots map[string]string
var lastFLPAnnots map[string]string
var lastPluginAnnots map[string]string

Context("Verify expectations are sane", func() {
It("Expected hashes should all be different", func() {
Expand Down Expand Up @@ -252,63 +256,80 @@ func flowCollectorCertificatesSpecs() {
if err := k8sClient.Get(ctx, agentKey, &agent); err != nil {
return err
}
return agent.Spec.Template.Spec.Volumes
}, timeout, interval).Should(HaveLen(2))
Expect(agent.Spec.Template.Annotations).To(HaveLen(2))
Expect(agent.Spec.Template.Annotations["flows.netobserv.io/watched-kafka-ca"]).To(Equal(expectedKafkaHash))
Expect(agent.Spec.Template.Annotations["flows.netobserv.io/watched-kafka-user"]).To(Equal(expectedKafkaUserHash))
Expect(agent.Spec.Template.Spec.Volumes[0].Name).To(Equal("kafka-certs-ca"))
Expect(agent.Spec.Template.Spec.Volumes[1].Name).To(Equal("kafka-certs-user"))
lastAgentAnnots = agent.Spec.Template.Annotations
return test.VolumeNames(agent.Spec.Template.Spec.Volumes)
}, timeout, interval).Should(ContainElements(
"kafka-certs-ca",
"kafka-certs-user",
))
Eventually(func() interface{} {
if err := k8sClient.Get(ctx, agentKey, &agent); err != nil {
return err
}
return test.Annotations(agent.Spec.Template.Annotations)
}, timeout, interval).Should(ContainElements(
"flows.netobserv.io/watched-kafka-ca="+expectedKafkaHash,
"flows.netobserv.io/watched-kafka-user="+expectedKafkaUserHash,
))

By("Expecting Loki certificate for Plugin mounted")
Eventually(func() interface{} {
if err := k8sClient.Get(ctx, pluginKey, &plugin); err != nil {
return err
}
return plugin.Spec.Template.Spec.Volumes
}, timeout, interval).Should(HaveLen(5))
return test.VolumeNames(plugin.Spec.Template.Spec.Volumes)
}, timeout, interval).Should(ContainElements(
"console-serving-cert",
"config-volume",
"loki-certs-ca",
"loki-status-certs-ca",
"loki-status-certs-user",
))
Expect(plugin.Spec.Template.Annotations).To(HaveLen(1))
Expect(plugin.Spec.Template.Spec.Volumes[0].Name).To(Equal("console-serving-cert"))
Expect(plugin.Spec.Template.Spec.Volumes[1].Name).To(Equal("config-volume"))
Expect(plugin.Spec.Template.Spec.Volumes[2].Name).To(Equal("loki-certs-ca"))
Expect(plugin.Spec.Template.Spec.Volumes[3].Name).To(Equal("loki-status-certs-ca"))
Expect(plugin.Spec.Template.Spec.Volumes[4].Name).To(Equal("loki-status-certs-user"))
lastPluginAnnots = plugin.Spec.Template.Annotations

By("Expecting Loki and Kafka certificates for FLP mounted")
Eventually(func() interface{} {
if err := k8sClient.Get(ctx, flpKey, &flp); err != nil {
return err
}
return flp.Spec.Template.Spec.Volumes
}, timeout, interval).Should(HaveLen(8))
Expect(flp.Spec.Template.Annotations).To(HaveLen(8))
Expect(flp.Spec.Template.Annotations["flows.netobserv.io/watched-kafka-ca"]).To(Equal(expectedKafkaHash))
Expect(flp.Spec.Template.Annotations["flows.netobserv.io/watched-kafka-user"]).To(Equal(expectedKafkaUserHash))
Expect(flp.Spec.Template.Annotations["flows.netobserv.io/watched-kafka-export-0-ca"]).To(Equal(expectedKafka2Hash))
Expect(flp.Spec.Template.Annotations["flows.netobserv.io/watched-kafka-export-0-sd1"]).To(Equal(expectedKafkaSaslHash1))
Expect(flp.Spec.Template.Annotations["flows.netobserv.io/watched-kafka-export-0-sd2"]).To(Equal(expectedKafkaSaslHash2))
Expect(flp.Spec.Template.Spec.Volumes[0].Name).To(Equal("config-volume"))
Expect(flp.Spec.Template.Spec.Volumes[1].Name).To(Equal("kafka-cert-ca"))
Expect(flp.Spec.Template.Spec.Volumes[2].Name).To(Equal("kafka-cert-user"))
Expect(flp.Spec.Template.Spec.Volumes[3].Name).To(Equal("flowlogs-pipeline")) // token
Expect(flp.Spec.Template.Spec.Volumes[4].Name).To(Equal("loki-certs-ca"))
Expect(flp.Spec.Template.Spec.Volumes[5].Name).To(Equal("kafka-export-0-ca"))
Expect(flp.Spec.Template.Spec.Volumes[6].Name).To(Equal("kafka-export-0-sasl-id"))
Expect(flp.Spec.Template.Spec.Volumes[7].Name).To(Equal("kafka-export-0-sasl-secret"))
lastFLPAnnots = flp.Spec.Template.Annotations
return test.VolumeNames(flp.Spec.Template.Spec.Volumes)
}, timeout, interval).Should(ContainElements(
"config-volume",
"kafka-cert-ca",
"kafka-cert-user",
"flowlogs-pipeline",
"loki-certs-ca",
"kafka-export-0-ca",
"kafka-export-0-sasl-id",
"kafka-export-0-sasl-secret",
))
Eventually(func() interface{} {
if err := k8sClient.Get(ctx, flpKey, &flp); err != nil {
return err
}
return test.Annotations(flp.Spec.Template.Annotations)
}, timeout, interval).Should(ContainElements(
"flows.netobserv.io/watched-kafka-ca="+expectedKafkaHash,
"flows.netobserv.io/watched-kafka-user="+expectedKafkaUserHash,
"flows.netobserv.io/watched-kafka-export-0-ca="+expectedKafka2Hash,
"flows.netobserv.io/watched-kafka-export-0-sd1="+expectedKafkaSaslHash1,
"flows.netobserv.io/watched-kafka-export-0-sd2="+expectedKafkaSaslHash2,
))
})
})

Context("Updating Kafka certificates", func() {
var modifiedKafkaHash, modifiedKafkaUserHash string
It("Should update Kafka certificate", func() {
By("Updating Kafka CA certificate")
kafkaCert.Data["cert.crt"] = "--- KAFKA CA CERT MODIFIED ---"
Eventually(func() interface{} { return k8sClient.Update(ctx, &kafkaCert) }, timeout, interval).Should(Succeed())
modifiedKafkaHash, _ = cmw.GetDigest(&kafkaCert, []string{"cert.crt"})
Expect(modifiedKafkaHash).ToNot(Equal(expectedKafkaHash))
By("Updating Kafka User certificate")
kafkaUserCert.Data["user.crt"] = []byte("--- KAFKA USER CERT MODIFIED ---")
Eventually(func() interface{} { return k8sClient.Update(ctx, &kafkaUserCert) }, timeout, interval).Should(Succeed())
modifiedKafkaUserHash, _ = sw.GetDigest(&kafkaUserCert, []string{"user.crt", "user.key"})
Expect(modifiedKafkaUserHash).ToNot(Equal(expectedKafkaUserHash))
})

It("Should copy certificates when necessary", func() {
Expand Down Expand Up @@ -337,24 +358,31 @@ func flowCollectorCertificatesSpecs() {
}))
})

It("Should redeploy eBPF Agent", func() {
It("Should change eBPF Agent annotations", func() {
Eventually(func() interface{} {
if err := k8sClient.Get(ctx, agentKey, &agent); err != nil {
return err
}
return agent.Spec.Template.Annotations
}, timeout, interval).Should(Not(Equal(lastAgentAnnots)))
lastAgentAnnots = agent.Spec.Template.Annotations
return test.Annotations(agent.Spec.Template.Annotations)
}, timeout, interval).Should(ContainElements(
"flows.netobserv.io/watched-kafka-ca="+modifiedKafkaHash,
"flows.netobserv.io/watched-kafka-user="+modifiedKafkaUserHash,
))
})

It("Should redeploy FLP", func() {
It("Should change FLP annotations", func() {
Eventually(func() interface{} {
if err := k8sClient.Get(ctx, flpKey, &flp); err != nil {
return err
}
return flp.Spec.Template.Annotations
}, timeout, interval).Should(Not(Equal(lastFLPAnnots)))
lastFLPAnnots = flp.Spec.Template.Annotations
return test.Annotations(flp.Spec.Template.Annotations)
}, timeout, interval).Should(ContainElements(
"flows.netobserv.io/watched-kafka-ca="+modifiedKafkaHash,
"flows.netobserv.io/watched-kafka-user="+modifiedKafkaUserHash,
"flows.netobserv.io/watched-kafka-export-0-ca="+expectedKafka2Hash,
"flows.netobserv.io/watched-kafka-export-0-sd1="+expectedKafkaSaslHash1,
"flows.netobserv.io/watched-kafka-export-0-sd2="+expectedKafkaSaslHash2,
))
})
})

Expand All @@ -380,25 +408,25 @@ func flowCollectorCertificatesSpecs() {
})

// Console plugin is not restarted, as Loki certificate is always read from file
It("Should not redeploy Console plugin", func() {
Eventually(func() interface{} {
It("Should not trigger Console plugin redeploy", func() {
lastPluginAnnots := plugin.Spec.Template.Annotations
Consistently(func() interface{} {
if err := k8sClient.Get(ctx, pluginKey, &plugin); err != nil {
return err
}
return plugin.Spec.Template.Annotations
}, timeout, interval).Should(Equal(lastPluginAnnots))
lastPluginAnnots = plugin.Spec.Template.Annotations
}, consistentlyTimeout, consistentlyInterval).Should(Equal(lastPluginAnnots))
})

// FLP is not restarted, as Loki certificate is always read from file
It("Should not redeploy FLP", func() {
Eventually(func() interface{} {
It("Should not trigger FLP redeploy", func() {
lastFLPAnnots := flp.Spec.Template.Annotations
Consistently(func() interface{} {
if err := k8sClient.Get(ctx, flpKey, &flp); err != nil {
return err
}
return flp.Spec.Template.Annotations
}, timeout, interval).Should(Equal(lastFLPAnnots))
lastFLPAnnots = flp.Spec.Template.Annotations
}, consistentlyTimeout, consistentlyInterval).Should(Equal(lastFLPAnnots))
})
})

Expand All @@ -415,23 +443,23 @@ func flowCollectorCertificatesSpecs() {
})

It("Should not redeploy Agent", func() {
Eventually(func() interface{} {
lastAgentAnnots := agent.Spec.Template.Annotations
Consistently(func() interface{} {
if err := k8sClient.Get(ctx, agentKey, &agent); err != nil {
return err
}
return agent.Spec.Template.Annotations
}, timeout, interval).Should(Equal(lastAgentAnnots))
lastAgentAnnots = agent.Spec.Template.Annotations
}, consistentlyTimeout, consistentlyInterval).Should(Equal(lastAgentAnnots))
})

It("Should not redeploy FLP", func() {
Eventually(func() interface{} {
lastFLPAnnots := flp.Spec.Template.Annotations
Consistently(func() interface{} {
if err := k8sClient.Get(ctx, flpKey, &flp); err != nil {
return err
}
return flp.Spec.Template.Annotations
}, timeout, interval).Should(Equal(lastFLPAnnots))
lastFLPAnnots = flp.Spec.Template.Annotations
}, consistentlyTimeout, consistentlyInterval).Should(Equal(lastFLPAnnots))
})
})

Expand Down
12 changes: 6 additions & 6 deletions controllers/flowcollector_controller_console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func flowCollectorConsolePluginSpecs() {
}, 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) {
updateCR(crKey, func(fc *flowslatest.FlowCollector) {
fc.Spec.Processor.LogLevel = "trace"
})
By("Expecting ServiceMonitor to exist")
Expand All @@ -207,14 +207,14 @@ func flowCollectorConsolePluginSpecs() {
timeout, interval).Should(ContainSubstring("url: http://loki:3100/"))
})
It("Should update the Loki URL in the Console Plugin if it changes in the Spec", func() {
UpdateCR(crKey, func(fc *flowslatest.FlowCollector) {
updateCR(crKey, func(fc *flowslatest.FlowCollector) {
fc.Spec.Loki.Monolithic.URL = "http://loki.namespace:8888"
})
Eventually(getConfigMapData(configKey),
timeout, interval).Should(ContainSubstring("url: http://loki.namespace:8888"))
})
It("Should use the Loki Querier URL instead of the Loki URL, when switching to manual mode", func() {
UpdateCR(crKey, func(fc *flowslatest.FlowCollector) {
updateCR(crKey, func(fc *flowslatest.FlowCollector) {
fc.Spec.Loki.Mode = flowslatest.LokiModeManual
fc.Spec.Loki.Manual.QuerierURL = "http://loki-querier:6789"
})
Expand All @@ -236,7 +236,7 @@ func flowCollectorConsolePluginSpecs() {

It("Should be unregistered", func() {
By("Update CR to unregister")
UpdateCR(crKey, func(fc *flowslatest.FlowCollector) {
updateCR(crKey, func(fc *flowslatest.FlowCollector) {
fc.Spec.ConsolePlugin.Register = ptr.To(false)
})

Expand All @@ -263,7 +263,7 @@ func flowCollectorConsolePluginSpecs() {
})

It("Should cleanup console plugin if disabled", func() {
UpdateCR(crKey, func(fc *flowslatest.FlowCollector) {
updateCR(crKey, func(fc *flowslatest.FlowCollector) {
fc.Spec.ConsolePlugin.Enable = ptr.To(false)
})
Eventually(func() error {
Expand All @@ -284,7 +284,7 @@ func flowCollectorConsolePluginSpecs() {
})

It("Should recreate console plugin if enabled back", func() {
UpdateCR(crKey, func(fc *flowslatest.FlowCollector) {
updateCR(crKey, func(fc *flowslatest.FlowCollector) {
fc.Spec.ConsolePlugin.Enable = ptr.To(true)
})
Eventually(func() error {
Expand Down
4 changes: 2 additions & 2 deletions controllers/flowcollector_controller_ebpf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func flowCollectorEBPFSpecs() {
})

It("Should update fields that have changed", func() {
UpdateCR(crKey, func(fc *flowslatest.FlowCollector) {
updateCR(crKey, func(fc *flowslatest.FlowCollector) {
Expect(*fc.Spec.Agent.EBPF.Sampling).To(Equal(int32(123)))
*fc.Spec.Agent.EBPF.Sampling = 4
fc.Spec.Agent.EBPF.Privileged = true
Expand Down Expand Up @@ -176,7 +176,7 @@ func flowCollectorEBPFSpecs() {
})

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

Expand Down
3 changes: 2 additions & 1 deletion controllers/flowcollector_controller_iso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"k8s.io/utils/ptr"

flowslatest "github.com/netobserv/network-observability-operator/api/v1beta2"
"github.com/netobserv/network-observability-operator/pkg/test"
)

// nolint:cyclop
Expand Down Expand Up @@ -203,7 +204,7 @@ func flowCollectorIsoSpecs() {
})

It("Should not have modified input CR values", func() {
cr := GetCR(crKey)
cr := test.GetCR(ctx, k8sClient, crKey)

// For easier debugging, we check CR parts one by one
Expect(cr.Spec.Processor).Should(Equal(specInput.Processor))
Expand Down
Loading
Loading