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 committed Nov 9, 2023
1 parent a231780 commit ac9098d
Show file tree
Hide file tree
Showing 2 changed files with 103 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 @@ -100,6 +100,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
102 changes: 102 additions & 0 deletions test/conformance/tests/test_sriov_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"

sriovv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
"github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/clean"
"github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/cluster"
"github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/discovery"
Expand Down Expand Up @@ -171,6 +172,63 @@ var _ = Describe("[sriov] operator", func() {
})
})

Context("LogLevel affects operator's logs", func() {
It("when set to 0 no lifecycle logs are present", func() {
initialLogLevelValue := getOperatorConfigLogLevel()
DeferCleanup(func() {
By("Restore LogLevel to its initial value")
setOperatorConfigLogLevel(initialLogLevelValue)
})

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

since := time.Now()

By("Delete default policy to trigget some operator's activity")
err := clients.SriovnetworkV1Interface.SriovNetworkNodePolicies(operatorNamespace).Delete(context.Background(), consts.DefaultPolicyName, metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())

By("Assert logs contains operator activity")
Eventually(func(g Gomega) {
logs := getOperatorLogs(since)
g.Expect(logs).To(
ContainElement(
ContainSubstring("Default policy created"),
),
)

g.Expect(logs).To(
ContainElement(
ContainSubstring("Start to sync device plugin ConfigMap"),
),
)
}, 1*time.Minute, 1*time.Second).Should(Succeed())

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

By("Delete default policy to trigget some operator's activity")
err = clients.SriovnetworkV1Interface.SriovNetworkNodePolicies(operatorNamespace).Delete(context.Background(), consts.DefaultPolicyName, metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())

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

g.Expect(logs).ToNot(
ContainElement(
ContainSubstring("Start to sync device plugin ConfigMap"),
),
)
}, 1*time.Minute, 1*time.Second).Should(Succeed())
})
})
})

Describe("Generic SriovNetworkNodePolicy", func() {
Expand Down Expand Up @@ -2389,6 +2447,50 @@ func setSriovOperatorSpecFlag(flagName string, flagValue bool) {
}
}

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

cfg.Spec.LogLevel = level
err = clients.Update(context.TODO(), &cfg)
Expect(err).ToNot(HaveOccurred())
}

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 ac9098d

Please sign in to comment.