Skip to content

Commit

Permalink
SriovOperatorConfig.LogLevel e2e tests
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Panattoni <[email protected]>
  • Loading branch information
zeeke authored and SchSeba committed May 6, 2024
1 parent 975decc commit 36e6b3c
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
1 change: 1 addition & 0 deletions controllers/sriovnetworknodepolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (r *SriovNetworkNodePolicyReconciler) Reconcile(ctx context.Context, req ct
reqLogger.Error(err, "Failed to create default Policy", "Namespace", namespace, "Name", constants.DefaultPolicyName)
return reconcile.Result{}, err
}
reqLogger.Info("Default policy created")
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
Expand Down
136 changes: 136 additions & 0 deletions test/conformance/tests/test_sriov_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,82 @@ var _ = Describe("[sriov] operator", func() {
})
})

Context("LogLevel affects operator's logs", func() {
It("when set to 0 no lifecycle logs are present", func() {
if discovery.Enabled() {
Skip("Test unsuitable to be run in discovery mode")
}

initialLogLevelValue := getOperatorConfigLogLevel()
DeferCleanup(func() {
By("Restore LogLevel to its initial value")
setOperatorConfigLogLevel(initialLogLevelValue)
})

initialDisableDrain, err := cluster.GetNodeDrainState(clients, operatorNamespace)
Expect(err).ToNot(HaveOccurred())

DeferCleanup(func() {
By("Restore DisableDrain to its initial value")
Eventually(func() error {
return cluster.SetDisableNodeDrainState(clients, operatorNamespace, initialDisableDrain)
}, 1*time.Minute, 5*time.Second).ShouldNot(HaveOccurred())
})

By("Set operator LogLevel to 2")
setOperatorConfigLogLevel(2)

By("Flip DisableDrain to trigger operator activity")
since := time.Now()
Eventually(func() error {
return cluster.SetDisableNodeDrainState(clients, operatorNamespace, !initialDisableDrain)
}, 1*time.Minute, 5*time.Second).ShouldNot(HaveOccurred())

By("Assert logs contains verbose output")
Eventually(func(g Gomega) {
logs := getOperatorLogs(since)
g.Expect(logs).To(
ContainElement(And(
ContainSubstring("Reconciling SriovOperatorConfig"),
)),
)

// Should contain verbose logging
g.Expect(logs).To(
ContainElement(
ContainSubstring("Start to sync webhook objects"),
),
)
}, 1*time.Minute, 5*time.Second).Should(Succeed())

By("Reduce operator LogLevel to 0")
setOperatorConfigLogLevel(0)

By("Flip DisableDrain again to trigger operator activity")
since = time.Now()
Eventually(func() error {
return cluster.SetDisableNodeDrainState(clients, operatorNamespace, initialDisableDrain)
}, 1*time.Minute, 5*time.Second).ShouldNot(HaveOccurred())

By("Assert logs contains less operator activity")
Eventually(func(g Gomega) {
logs := getOperatorLogs(since)
g.Expect(logs).To(
ContainElement(And(
ContainSubstring("Reconciling SriovOperatorConfig"),
)),
)

// Should not contain verbose logging
g.Expect(logs).ToNot(
ContainElement(
ContainSubstring("Start to sync webhook objects"),
),
)
}, 1*time.Minute, 5*time.Second).Should(Succeed())

})
})
})

Describe("Generic SriovNetworkNodePolicy", func() {
Expand Down Expand Up @@ -2501,6 +2577,66 @@ func setSriovOperatorSpecFlag(flagName string, flagValue bool) {
}
}

func setOperatorConfigLogLevel(level int) {
instantBeforeSettingLogLevel := time.Now()

Eventually(func(g Gomega) {
cfg := sriovv1.SriovOperatorConfig{}
err := clients.Get(context.TODO(), runtimeclient.ObjectKey{
Name: "default",
Namespace: operatorNamespace,
}, &cfg)
g.Expect(err).ToNot(HaveOccurred())

if cfg.Spec.LogLevel == level {
return
}

cfg.Spec.LogLevel = level

err = clients.Update(context.TODO(), &cfg)
g.Expect(err).ToNot(HaveOccurred())

logs := getOperatorLogs(instantBeforeSettingLogLevel)
g.Expect(logs).To(
ContainElement(
ContainSubstring(fmt.Sprintf(`"new-level": %d`, level)),
),
)
}, 1*time.Minute, 5*time.Second).Should(Succeed())
}

func getOperatorConfigLogLevel() int {
cfg := sriovv1.SriovOperatorConfig{}
err := clients.Get(context.TODO(), runtimeclient.ObjectKey{
Name: "default",
Namespace: operatorNamespace,
}, &cfg)
Expect(err).ToNot(HaveOccurred())

return cfg.Spec.LogLevel
}

func getOperatorLogs(since time.Time) []string {
podList, err := clients.Pods(operatorNamespace).List(context.Background(), metav1.ListOptions{
LabelSelector: "name=sriov-network-operator",
})
ExpectWithOffset(1, err).ToNot(HaveOccurred())
ExpectWithOffset(1, podList.Items).To(HaveLen(1), "One operator pod expected")

pod := podList.Items[0]
logStart := metav1.NewTime(since)
rawLogs, err := clients.Pods(pod.Namespace).
GetLogs(pod.Name, &corev1.PodLogOptions{
Container: pod.Spec.Containers[0].Name,
SinceTime: &logStart,
}).
DoRaw(context.Background())
ExpectWithOffset(1, err).ToNot(HaveOccurred())

return strings.Split(string(rawLogs), "\n")
}

func assertObjectIsNotFound(name string, obj runtimeclient.Object) {
Eventually(func() bool {
err := clients.Get(context.Background(), runtimeclient.ObjectKey{Name: name, Namespace: operatorNamespace}, obj)
Expand Down

0 comments on commit 36e6b3c

Please sign in to comment.