|
| 1 | +package bundle |
| 2 | + |
| 3 | +import ( |
| 4 | + . "github.com/onsi/ginkgo/v2" |
| 5 | + . "github.com/onsi/gomega" |
| 6 | + |
| 7 | + "github.com/rancher/fleet/integrationtests/utils" |
| 8 | + "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1" |
| 9 | + |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 13 | +) |
| 14 | + |
| 15 | +var _ = Describe("Bundle UserID logging", func() { |
| 16 | + var ( |
| 17 | + bundle *v1alpha1.Bundle |
| 18 | + cluster *v1alpha1.Cluster |
| 19 | + namespace string |
| 20 | + ) |
| 21 | + |
| 22 | + createBundle := func(name, clusterName string, labels map[string]string) { |
| 23 | + bundle = &v1alpha1.Bundle{ |
| 24 | + ObjectMeta: metav1.ObjectMeta{ |
| 25 | + Name: name, |
| 26 | + Namespace: namespace, |
| 27 | + Labels: labels, |
| 28 | + }, |
| 29 | + Spec: v1alpha1.BundleSpec{ |
| 30 | + BundleDeploymentOptions: v1alpha1.BundleDeploymentOptions{ |
| 31 | + DefaultNamespace: "default", |
| 32 | + }, |
| 33 | + Targets: []v1alpha1.BundleTarget{ |
| 34 | + { |
| 35 | + Name: clusterName, |
| 36 | + ClusterName: clusterName, |
| 37 | + }, |
| 38 | + }, |
| 39 | + Resources: []v1alpha1.BundleResource{ |
| 40 | + { |
| 41 | + Name: "test-configmap.yaml", |
| 42 | + Content: "apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: test-cm\ndata:\n key: value\n", |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + } |
| 47 | + Expect(k8sClient.Create(ctx, bundle)).ToNot(HaveOccurred()) |
| 48 | + } |
| 49 | + |
| 50 | + BeforeEach(func() { |
| 51 | + var err error |
| 52 | + namespace, err = utils.NewNamespaceName() |
| 53 | + Expect(err).ToNot(HaveOccurred()) |
| 54 | + ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}} |
| 55 | + Expect(k8sClient.Create(ctx, ns)).ToNot(HaveOccurred()) |
| 56 | + |
| 57 | + logsBuffer.Reset() |
| 58 | + |
| 59 | + DeferCleanup(func() { |
| 60 | + Expect(k8sClient.Delete(ctx, bundle)).ToNot(HaveOccurred()) |
| 61 | + Expect(k8sClient.Delete(ctx, cluster)).ToNot(HaveOccurred()) |
| 62 | + Expect(k8sClient.Delete(ctx, ns)).ToNot(HaveOccurred()) |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + waitForReconciliation := func() { |
| 67 | + Eventually(func() int64 { |
| 68 | + err := k8sClient.Get(ctx, client.ObjectKey{Namespace: bundle.Namespace, Name: bundle.Name}, bundle) |
| 69 | + if err != nil { |
| 70 | + return 0 |
| 71 | + } |
| 72 | + return bundle.Status.ObservedGeneration |
| 73 | + }).Should(BeNumerically(">", 0)) |
| 74 | + |
| 75 | + Eventually(func() string { |
| 76 | + return logsBuffer.String() |
| 77 | + }).Should(ContainSubstring(bundle.Name)) |
| 78 | + } |
| 79 | + |
| 80 | + When("Bundle has user ID label", func() { |
| 81 | + const userID = "user-12345" |
| 82 | + |
| 83 | + BeforeEach(func() { |
| 84 | + var err error |
| 85 | + cluster, err = utils.CreateCluster(ctx, k8sClient, "test-cluster", namespace, nil, namespace) |
| 86 | + Expect(err).NotTo(HaveOccurred()) |
| 87 | + |
| 88 | + createBundle("test-bundle-with-userid", "test-cluster", map[string]string{ |
| 89 | + v1alpha1.CreatedByUserIDLabel: userID, |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + It("includes userID in log output", func() { |
| 94 | + waitForReconciliation() |
| 95 | + |
| 96 | + logs := logsBuffer.String() |
| 97 | + Expect(logs).To(Or( |
| 98 | + ContainSubstring(`"userID":"`+userID+`"`), |
| 99 | + ContainSubstring(`"userID": "`+userID+`"`), |
| 100 | + )) |
| 101 | + |
| 102 | + Expect(logs).To(ContainSubstring("bundle")) |
| 103 | + Expect(logs).To(ContainSubstring(bundle.Name)) |
| 104 | + }) |
| 105 | + }) |
| 106 | + |
| 107 | + When("Bundle does not have user ID label", func() { |
| 108 | + BeforeEach(func() { |
| 109 | + var err error |
| 110 | + cluster, err = utils.CreateCluster(ctx, k8sClient, "test-cluster-2", namespace, nil, namespace) |
| 111 | + Expect(err).NotTo(HaveOccurred()) |
| 112 | + |
| 113 | + createBundle("test-bundle-without-userid", "test-cluster-2", nil) |
| 114 | + }) |
| 115 | + |
| 116 | + It("does not include userID in log output", func() { |
| 117 | + waitForReconciliation() |
| 118 | + |
| 119 | + logs := logsBuffer.String() |
| 120 | + bundleLogs := utils.ExtractResourceLogs(logs, bundle.Name) |
| 121 | + Expect(bundleLogs).NotTo(ContainSubstring(`"userID"`)) |
| 122 | + }) |
| 123 | + }) |
| 124 | +}) |
0 commit comments