|
1 | 1 | package resolver
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "github.com/stretchr/testify/require" |
5 | 4 | "math/rand"
|
6 | 5 | "reflect"
|
7 | 6 | "strings"
|
8 | 7 | "testing"
|
9 | 8 | "testing/quick"
|
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + rbacv1 "k8s.io/api/rbac/v1" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + |
| 15 | + "github.com/operator-framework/api/pkg/operators/v1alpha1" |
10 | 16 | )
|
11 | 17 |
|
12 | 18 | func TestGenerateName(t *testing.T) {
|
@@ -63,3 +69,135 @@ func TestGeneratesWithinRange(t *testing.T) {
|
63 | 69 | }
|
64 | 70 | require.NoError(t, quick.Check(f, nil))
|
65 | 71 | }
|
| 72 | + |
| 73 | +func TestRBACBindings(t *testing.T) { |
| 74 | + serviceAccount1 := "test-service-account" |
| 75 | + serviceAccount2 := "second-account" |
| 76 | + |
| 77 | + rules := []rbacv1.PolicyRule{ |
| 78 | + { |
| 79 | + Verbs: []string{"get"}, |
| 80 | + APIGroups: []string{""}, |
| 81 | + Resources: []string{"pods"}, |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + tests := []struct { |
| 86 | + name string |
| 87 | + csv v1alpha1.ClusterServiceVersion |
| 88 | + want map[string]*OperatorPermissions |
| 89 | + }{ |
| 90 | + { |
| 91 | + name: "RoleBinding", |
| 92 | + csv: v1alpha1.ClusterServiceVersion{ |
| 93 | + ObjectMeta: metav1.ObjectMeta{ |
| 94 | + Name: "test-csv-1.1.0", |
| 95 | + }, |
| 96 | + Spec: v1alpha1.ClusterServiceVersionSpec{ |
| 97 | + InstallStrategy: v1alpha1.NamedInstallStrategy{ |
| 98 | + StrategyName: v1alpha1.InstallStrategyNameDeployment, |
| 99 | + StrategySpec: v1alpha1.StrategyDetailsDeployment{ |
| 100 | + Permissions: []v1alpha1.StrategyDeploymentPermissions{ |
| 101 | + { |
| 102 | + ServiceAccountName: serviceAccount1, |
| 103 | + Rules: rules, |
| 104 | + }, |
| 105 | + { |
| 106 | + ServiceAccountName: serviceAccount1, |
| 107 | + Rules: []rbacv1.PolicyRule{ |
| 108 | + { |
| 109 | + Verbs: []string{"get"}, |
| 110 | + APIGroups: []string{""}, |
| 111 | + Resources: []string{"deployments"}, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + { |
| 116 | + ServiceAccountName: serviceAccount2, |
| 117 | + Rules: rules, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + want: map[string]*OperatorPermissions{ |
| 125 | + serviceAccount1: { |
| 126 | + RoleBindings: []*rbacv1.RoleBinding{{}, {}}, |
| 127 | + }, |
| 128 | + serviceAccount2: { |
| 129 | + RoleBindings: []*rbacv1.RoleBinding{{}}, |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "ClusterRoleBinding", |
| 135 | + csv: v1alpha1.ClusterServiceVersion{ |
| 136 | + ObjectMeta: metav1.ObjectMeta{ |
| 137 | + Name: "second-csv-1.1.0", |
| 138 | + }, |
| 139 | + Spec: v1alpha1.ClusterServiceVersionSpec{ |
| 140 | + InstallStrategy: v1alpha1.NamedInstallStrategy{ |
| 141 | + StrategyName: v1alpha1.InstallStrategyNameDeployment, |
| 142 | + StrategySpec: v1alpha1.StrategyDetailsDeployment{ |
| 143 | + ClusterPermissions: []v1alpha1.StrategyDeploymentPermissions{ |
| 144 | + { |
| 145 | + ServiceAccountName: serviceAccount1, |
| 146 | + Rules: rules, |
| 147 | + }, |
| 148 | + { |
| 149 | + ServiceAccountName: serviceAccount2, |
| 150 | + Rules: rules, |
| 151 | + }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + want: map[string]*OperatorPermissions{ |
| 158 | + serviceAccount1: { |
| 159 | + ClusterRoleBindings: []*rbacv1.ClusterRoleBinding{{}}, |
| 160 | + }, |
| 161 | + serviceAccount2: { |
| 162 | + ClusterRoleBindings: []*rbacv1.ClusterRoleBinding{{}}, |
| 163 | + }, |
| 164 | + }, |
| 165 | + }, |
| 166 | + } |
| 167 | + for _, tt := range tests { |
| 168 | + t.Run(tt.name, func(t *testing.T) { |
| 169 | + result, err := RBACForClusterServiceVersion(&tt.csv) |
| 170 | + require.NoError(t, err) |
| 171 | + |
| 172 | + roleBindingNames := map[string]bool{} |
| 173 | + clusterRoleBindingNames := map[string]bool{} |
| 174 | + for serviceAccount, permissions := range tt.want { |
| 175 | + // Check that correct number of bindings is created |
| 176 | + require.Equal(t, len(permissions.RoleBindings), len(result[serviceAccount].RoleBindings)) |
| 177 | + require.Equal(t, len(permissions.ClusterRoleBindings), len(result[serviceAccount].ClusterRoleBindings)) |
| 178 | + |
| 179 | + // Check that testing ServiceAccount is the Subject of RoleBindings |
| 180 | + for _, roleBinding := range result[serviceAccount].RoleBindings { |
| 181 | + require.Len(t, roleBinding.Subjects, 1) |
| 182 | + require.Equal(t, serviceAccount, roleBinding.Subjects[0].Name) |
| 183 | + |
| 184 | + // Check that RoleBindings are created with unique names |
| 185 | + _, rbWithNameExists := roleBindingNames[roleBinding.Name] |
| 186 | + require.False(t, rbWithNameExists, "RoleBinding with the same name already generated") |
| 187 | + roleBindingNames[roleBinding.Name] = true |
| 188 | + } |
| 189 | + |
| 190 | + // Check that testing ServiceAccount is the Subject of ClusterrRoleBindings |
| 191 | + for _, clusterRoleBinding := range result[serviceAccount].ClusterRoleBindings { |
| 192 | + require.Len(t, clusterRoleBinding.Subjects, 1) |
| 193 | + require.Equal(t, serviceAccount, clusterRoleBinding.Subjects[0].Name) |
| 194 | + |
| 195 | + // Check that ClusterRoleBindings are created with unique names |
| 196 | + _, crbWithNameExists := clusterRoleBindingNames[clusterRoleBinding.Name] |
| 197 | + require.False(t, crbWithNameExists, "ClusterRoleBinding with the same name already generated") |
| 198 | + clusterRoleBindingNames[clusterRoleBinding.Name] = true |
| 199 | + } |
| 200 | + } |
| 201 | + }) |
| 202 | + } |
| 203 | +} |
0 commit comments