Skip to content

Commit 09b09d1

Browse files
committed
test: add auto cases for feature - config to not create ClusterRoleBinding
case "ClusterRoleBinding NOT created with NoClusterRoleBindings policy" failed now because product bug #COO-1493
1 parent 1b6a208 commit 09b09d1

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

test/e2e/framework/assertions.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
monv1 "github.com/rhobs/obo-prometheus-operator/pkg/apis/monitoring/v1"
2121
appsv1 "k8s.io/api/apps/v1"
2222
v1 "k8s.io/api/core/v1"
23+
rbacv1 "k8s.io/api/rbac/v1"
2324
apierrors "k8s.io/apimachinery/pkg/api/errors"
2425
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2526
"k8s.io/apimachinery/pkg/types"
@@ -727,3 +728,13 @@ func (f *Framework) AssertPrometheusReplicaStatus(name, namespace string, expect
727728
}
728729
}
729730
}
731+
732+
// AssertClusterRoleBindingExists asserts that a ClusterRoleBinding exists within the timeout
733+
func (f *Framework) AssertClusterRoleBindingExists(name string, fns ...OptionFn) func(t *testing.T) {
734+
return f.AssertResourceEventuallyExists(name, "", &rbacv1.ClusterRoleBinding{}, fns...)
735+
}
736+
737+
// AssertClusterRoleBindingAbsent asserts that a ClusterRoleBinding is not present or is deleted within the timeout
738+
func (f *Framework) AssertClusterRoleBindingAbsent(name string, fns ...OptionFn) func(t *testing.T) {
739+
return f.AssertResourceAbsent(name, "", &rbacv1.ClusterRoleBinding{}, fns...)
740+
}

test/e2e/monitoring_stack_controller_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"gotest.tools/v3/assert"
2020
corev1 "k8s.io/api/core/v1"
2121
policyv1 "k8s.io/api/policy/v1"
22+
rbacv1 "k8s.io/api/rbac/v1"
2223
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2324
"k8s.io/apimachinery/pkg/api/errors"
2425
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -136,6 +137,12 @@ func TestMonitoringStackController(t *testing.T) {
136137
}, {
137138
name: "Assert OTLP receiver flag is set when enabled in CR",
138139
scenario: assertDefaultOTLPFlagIsSet,
140+
}, {
141+
name: "ClusterRoleBinding NOT created with NoClusterRoleBindings policy",
142+
scenario: assertNoClusterRoleBindingWithPolicy,
143+
}, {
144+
name: "ClusterRoleBinding cleanup on policy change",
145+
scenario: assertClusterRoleBindingCleanupOnPolicyChange,
139146
}}
140147
for _, tc := range ts {
141148
t.Run(tc.name, tc.scenario)
@@ -1433,3 +1440,89 @@ func newPrometheusExampleAppPod(t *testing.T, name, ns string) *corev1.Pod {
14331440
f.CleanUp(t, func() { f.K8sClient.Delete(context.Background(), pod) })
14341441
return pod
14351442
}
1443+
1444+
func assertNoClusterRoleBindingWithPolicy(t *testing.T) {
1445+
stackName := "crb-no-policy"
1446+
nsLabels := map[string]string{"monitoring.rhobs/stack": stackName}
1447+
1448+
ms := newMonitoringStack(t, stackName, msNamespaceSelector(nsLabels))
1449+
// Explicitly set CreateClusterRoleBindings to NoClusterRoleBindings
1450+
ms.Spec.CreateClusterRoleBindings = stack.NoClusterRoleBindings
1451+
1452+
err := f.K8sClient.Create(context.Background(), ms)
1453+
assert.NilError(t, err, "failed to create a monitoring stack")
1454+
1455+
// Wait for MonitoringStack to become available
1456+
_ = f.GetStackWhenAvailable(t, ms.Name, ms.Namespace)
1457+
1458+
// Assert ClusterRoleBinding is NOT created for Prometheus
1459+
promCRBName := stackName + "-prometheus"
1460+
f.AssertResourceNeverExists(promCRBName, "", &rbacv1.ClusterRoleBinding{})(t)
1461+
1462+
// Assert ClusterRoleBinding is NOT created for Alertmanager
1463+
amCRBName := stackName + "-alertmanager"
1464+
f.AssertResourceNeverExists(amCRBName, "", &rbacv1.ClusterRoleBinding{})(t)
1465+
1466+
// Verify Prometheus is still created and becomes ready
1467+
f.AssertStatefulsetReady("prometheus-"+stackName, e2eTestNamespace, framework.WithTimeout(3*time.Minute))(t)
1468+
1469+
// Verify Alertmanager is still created and becomes ready
1470+
f.AssertStatefulsetReady("alertmanager-"+stackName, e2eTestNamespace, framework.WithTimeout(2*time.Minute))(t)
1471+
}
1472+
1473+
func assertClusterRoleBindingCleanupOnPolicyChange(t *testing.T) {
1474+
stackName := "crb-cleanup"
1475+
nsLabels := map[string]string{"monitoring.rhobs/stack": stackName}
1476+
1477+
// Step 1: Create MonitoringStack with NamespaceSelector (ClusterRoleBindings should be created)
1478+
ms := newMonitoringStack(t, stackName, msNamespaceSelector(nsLabels))
1479+
err := f.K8sClient.Create(context.Background(), ms)
1480+
assert.NilError(t, err, "failed to create a monitoring stack")
1481+
1482+
// Wait for MonitoringStack to become available
1483+
_ = f.GetStackWhenAvailable(t, ms.Name, ms.Namespace)
1484+
1485+
// Assert ClusterRoleBindings exist
1486+
promCRBName := stackName + "-prometheus"
1487+
amCRBName := stackName + "-alertmanager"
1488+
f.AssertClusterRoleBindingExists(promCRBName)(t)
1489+
f.AssertClusterRoleBindingExists(amCRBName)(t)
1490+
1491+
// Step 2: Update MonitoringStack to set CreateClusterRoleBindings to NoClusterRoleBindings
1492+
updatedMS := &stack.MonitoringStack{}
1493+
f.GetResourceWithRetry(t, ms.Name, ms.Namespace, updatedMS)
1494+
1495+
updatedMS.Spec.CreateClusterRoleBindings = stack.NoClusterRoleBindings
1496+
err = f.K8sClient.Update(context.Background(), updatedMS)
1497+
assert.NilError(t, err, "failed to update monitoring stack")
1498+
1499+
// Assert ClusterRoleBindings are removed
1500+
f.AssertClusterRoleBindingAbsent(promCRBName)(t)
1501+
f.AssertClusterRoleBindingAbsent(amCRBName)(t)
1502+
1503+
// Step 3: Update MonitoringStack back to CreateClusterRoleBindings
1504+
updatedMS2 := &stack.MonitoringStack{}
1505+
f.GetResourceWithRetry(t, ms.Name, ms.Namespace, updatedMS2)
1506+
1507+
updatedMS2.Spec.CreateClusterRoleBindings = stack.CreateClusterRoleBindings
1508+
err = f.K8sClient.Update(context.Background(), updatedMS2)
1509+
assert.NilError(t, err, "failed to update monitoring stack")
1510+
1511+
// Assert ClusterRoleBindings are recreated
1512+
f.AssertClusterRoleBindingExists(promCRBName)(t)
1513+
f.AssertClusterRoleBindingExists(amCRBName)(t)
1514+
1515+
// Verify the recreated ClusterRoleBindings have correct references
1516+
var promCRB rbacv1.ClusterRoleBinding
1517+
key := types.NamespacedName{Name: promCRBName}
1518+
err = f.K8sClient.Get(context.Background(), key, &promCRB)
1519+
assert.NilError(t, err, "failed to get recreated prometheus ClusterRoleBinding")
1520+
assert.Equal(t, promCRB.Subjects[0].Name, stackName+"-prometheus", "unexpected ServiceAccount name after recreation")
1521+
1522+
var amCRB rbacv1.ClusterRoleBinding
1523+
key = types.NamespacedName{Name: amCRBName}
1524+
err = f.K8sClient.Get(context.Background(), key, &amCRB)
1525+
assert.NilError(t, err, "failed to get recreated alertmanager ClusterRoleBinding")
1526+
assert.Equal(t, amCRB.Subjects[0].Name, stackName+"-alertmanager", "unexpected ServiceAccount name after recreation")
1527+
1528+
}

0 commit comments

Comments
 (0)