|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package receivers |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/go-logr/logr" |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + rbacv1 "k8s.io/api/rbac/v1" |
| 23 | +) |
| 24 | + |
| 25 | +func Test_generatek8sclusterRbacRules(t *testing.T) { |
| 26 | + tests := []struct { |
| 27 | + name string |
| 28 | + cfg k8sclusterConfig |
| 29 | + want []rbacv1.PolicyRule |
| 30 | + wantErr bool |
| 31 | + }{ |
| 32 | + { |
| 33 | + name: "default configuration", |
| 34 | + cfg: k8sclusterConfig{}, |
| 35 | + want: []rbacv1.PolicyRule{ |
| 36 | + { |
| 37 | + APIGroups: []string{""}, |
| 38 | + Resources: []string{ |
| 39 | + "events", |
| 40 | + "namespaces", |
| 41 | + "namespaces/status", |
| 42 | + "nodes", |
| 43 | + "nodes/spec", |
| 44 | + "pods", |
| 45 | + "pods/status", |
| 46 | + "replicationcontrollers", |
| 47 | + "replicationcontrollers/status", |
| 48 | + "resourcequotas", |
| 49 | + "services", |
| 50 | + }, |
| 51 | + Verbs: []string{"get", "list", "watch"}, |
| 52 | + }, |
| 53 | + { |
| 54 | + APIGroups: []string{"apps"}, |
| 55 | + Resources: []string{ |
| 56 | + "daemonsets", |
| 57 | + "deployments", |
| 58 | + "replicasets", |
| 59 | + "statefulsets", |
| 60 | + }, |
| 61 | + Verbs: []string{"get", "list", "watch"}, |
| 62 | + }, |
| 63 | + { |
| 64 | + APIGroups: []string{"extensions"}, |
| 65 | + Resources: []string{ |
| 66 | + "daemonsets", |
| 67 | + "deployments", |
| 68 | + "replicasets", |
| 69 | + }, |
| 70 | + Verbs: []string{"get", "list", "watch"}, |
| 71 | + }, |
| 72 | + { |
| 73 | + APIGroups: []string{"batch"}, |
| 74 | + Resources: []string{ |
| 75 | + "jobs", |
| 76 | + "cronjobs", |
| 77 | + }, |
| 78 | + Verbs: []string{"get", "list", "watch"}, |
| 79 | + }, |
| 80 | + { |
| 81 | + APIGroups: []string{"autoscaling"}, |
| 82 | + Resources: []string{"horizontalpodautoscalers"}, |
| 83 | + Verbs: []string{"get", "list", "watch"}, |
| 84 | + }, |
| 85 | + }, |
| 86 | + wantErr: false, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "openshift configuration", |
| 90 | + cfg: k8sclusterConfig{ |
| 91 | + Distribution: "openshift", |
| 92 | + }, |
| 93 | + want: []rbacv1.PolicyRule{ |
| 94 | + { |
| 95 | + APIGroups: []string{""}, |
| 96 | + Resources: []string{ |
| 97 | + "events", |
| 98 | + "namespaces", |
| 99 | + "namespaces/status", |
| 100 | + "nodes", |
| 101 | + "nodes/spec", |
| 102 | + "pods", |
| 103 | + "pods/status", |
| 104 | + "replicationcontrollers", |
| 105 | + "replicationcontrollers/status", |
| 106 | + "resourcequotas", |
| 107 | + "services", |
| 108 | + }, |
| 109 | + Verbs: []string{"get", "list", "watch"}, |
| 110 | + }, |
| 111 | + { |
| 112 | + APIGroups: []string{"apps"}, |
| 113 | + Resources: []string{ |
| 114 | + "daemonsets", |
| 115 | + "deployments", |
| 116 | + "replicasets", |
| 117 | + "statefulsets", |
| 118 | + }, |
| 119 | + Verbs: []string{"get", "list", "watch"}, |
| 120 | + }, |
| 121 | + { |
| 122 | + APIGroups: []string{"extensions"}, |
| 123 | + Resources: []string{ |
| 124 | + "daemonsets", |
| 125 | + "deployments", |
| 126 | + "replicasets", |
| 127 | + }, |
| 128 | + Verbs: []string{"get", "list", "watch"}, |
| 129 | + }, |
| 130 | + { |
| 131 | + APIGroups: []string{"batch"}, |
| 132 | + Resources: []string{ |
| 133 | + "jobs", |
| 134 | + "cronjobs", |
| 135 | + }, |
| 136 | + Verbs: []string{"get", "list", "watch"}, |
| 137 | + }, |
| 138 | + { |
| 139 | + APIGroups: []string{"autoscaling"}, |
| 140 | + Resources: []string{"horizontalpodautoscalers"}, |
| 141 | + Verbs: []string{"get", "list", "watch"}, |
| 142 | + }, |
| 143 | + { |
| 144 | + APIGroups: []string{"quota.openshift.io"}, |
| 145 | + Resources: []string{"clusterresourcequotas"}, |
| 146 | + Verbs: []string{"get", "list", "watch"}, |
| 147 | + }, |
| 148 | + }, |
| 149 | + wantErr: false, |
| 150 | + }, |
| 151 | + } |
| 152 | + |
| 153 | + for _, tt := range tests { |
| 154 | + t.Run(tt.name, func(t *testing.T) { |
| 155 | + got, err := generatek8sclusterRbacRules(logr.Discard(), tt.cfg) |
| 156 | + if tt.wantErr { |
| 157 | + assert.Error(t, err) |
| 158 | + return |
| 159 | + } |
| 160 | + assert.NoError(t, err) |
| 161 | + assert.Equal(t, tt.want, got) |
| 162 | + }) |
| 163 | + } |
| 164 | +} |
0 commit comments