|
| 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_generatek8sobjectsRbacRules(t *testing.T) { |
| 26 | + tests := []struct { |
| 27 | + name string |
| 28 | + config k8sobjectsConfig |
| 29 | + want []rbacv1.PolicyRule |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "basic watch mode", |
| 33 | + config: k8sobjectsConfig{ |
| 34 | + Objects: []k8sObject{ |
| 35 | + { |
| 36 | + Name: "pods", |
| 37 | + Mode: "watch", |
| 38 | + Group: "v1", |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + want: []rbacv1.PolicyRule{ |
| 43 | + { |
| 44 | + APIGroups: []string{"v1"}, |
| 45 | + Resources: []string{"pods"}, |
| 46 | + Verbs: []string{"list", "watch"}, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "pull mode with events", |
| 52 | + config: k8sobjectsConfig{ |
| 53 | + Objects: []k8sObject{ |
| 54 | + { |
| 55 | + Name: "events", |
| 56 | + Mode: "pull", |
| 57 | + Group: "v1", |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + want: []rbacv1.PolicyRule{ |
| 62 | + { |
| 63 | + APIGroups: []string{"v1"}, |
| 64 | + Resources: []string{"events"}, |
| 65 | + Verbs: []string{"list"}, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "pull mode with non-events", |
| 71 | + config: k8sobjectsConfig{ |
| 72 | + Objects: []k8sObject{ |
| 73 | + { |
| 74 | + Name: "pods", |
| 75 | + Mode: "pull", |
| 76 | + Group: "v1", |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + want: []rbacv1.PolicyRule{ |
| 81 | + { |
| 82 | + APIGroups: []string{"v1"}, |
| 83 | + Resources: []string{"pods"}, |
| 84 | + Verbs: []string{"list", "get"}, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "multiple objects", |
| 90 | + config: k8sobjectsConfig{ |
| 91 | + Objects: []k8sObject{ |
| 92 | + { |
| 93 | + Name: "pods", |
| 94 | + Mode: "pull", |
| 95 | + Group: "v1", |
| 96 | + }, |
| 97 | + { |
| 98 | + Name: "events", |
| 99 | + Mode: "pull", |
| 100 | + Group: "v1", |
| 101 | + }, |
| 102 | + { |
| 103 | + Name: "deployments", |
| 104 | + Mode: "watch", |
| 105 | + Group: "apps/v1", |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + want: []rbacv1.PolicyRule{ |
| 110 | + { |
| 111 | + APIGroups: []string{"v1"}, |
| 112 | + Resources: []string{"pods"}, |
| 113 | + Verbs: []string{"list", "get"}, |
| 114 | + }, |
| 115 | + { |
| 116 | + APIGroups: []string{"v1"}, |
| 117 | + Resources: []string{"events"}, |
| 118 | + Verbs: []string{"list"}, |
| 119 | + }, |
| 120 | + { |
| 121 | + APIGroups: []string{"apps/v1"}, |
| 122 | + Resources: []string{"deployments"}, |
| 123 | + Verbs: []string{"list", "watch"}, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + } |
| 128 | + |
| 129 | + for _, tt := range tests { |
| 130 | + t.Run(tt.name, func(t *testing.T) { |
| 131 | + got, err := generatek8sobjectsRbacRules(logr.Logger{}, tt.config) |
| 132 | + assert.NoError(t, err) |
| 133 | + assert.Equal(t, tt.want, got) |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
0 commit comments