|
| 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 | + "github.com/stretchr/testify/require" |
| 23 | + rbacv1 "k8s.io/api/rbac/v1" |
| 24 | +) |
| 25 | + |
| 26 | +func TestGenerateKubeletStatsRbacRules(t *testing.T) { |
| 27 | + baseRule := rbacv1.PolicyRule{ |
| 28 | + APIGroups: []string{""}, |
| 29 | + Resources: []string{"nodes/stats"}, |
| 30 | + Verbs: []string{"get"}, |
| 31 | + } |
| 32 | + |
| 33 | + proxyRule := rbacv1.PolicyRule{ |
| 34 | + APIGroups: []string{""}, |
| 35 | + Resources: []string{"nodes/proxy"}, |
| 36 | + Verbs: []string{"get"}, |
| 37 | + } |
| 38 | + |
| 39 | + tests := []struct { |
| 40 | + name string |
| 41 | + config kubeletStatsConfig |
| 42 | + expectedRules []rbacv1.PolicyRule |
| 43 | + expectedErrMsg string |
| 44 | + }{ |
| 45 | + { |
| 46 | + name: "Default config", |
| 47 | + config: kubeletStatsConfig{}, |
| 48 | + expectedRules: []rbacv1.PolicyRule{baseRule}, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "Extra metadata labels", |
| 52 | + config: kubeletStatsConfig{ |
| 53 | + ExtraMetadataLabels: []string{"label1", "label2"}, |
| 54 | + }, |
| 55 | + expectedRules: []rbacv1.PolicyRule{baseRule, proxyRule}, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "CPU limit utilization enabled", |
| 59 | + config: kubeletStatsConfig{ |
| 60 | + Metrics: metrics{ |
| 61 | + K8sContainerCPULimitUtilization: metricConfig{Enabled: true}, |
| 62 | + }, |
| 63 | + }, |
| 64 | + expectedRules: []rbacv1.PolicyRule{baseRule, proxyRule}, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "Memory request utilization enabled", |
| 68 | + config: kubeletStatsConfig{ |
| 69 | + Metrics: metrics{ |
| 70 | + K8sPodMemoryRequestUtilization: metricConfig{Enabled: true}, |
| 71 | + }, |
| 72 | + }, |
| 73 | + expectedRules: []rbacv1.PolicyRule{baseRule, proxyRule}, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "No extra permissions needed", |
| 77 | + config: kubeletStatsConfig{ |
| 78 | + Metrics: metrics{ |
| 79 | + K8sContainerCPULimitUtilization: metricConfig{Enabled: false}, |
| 80 | + }, |
| 81 | + }, |
| 82 | + expectedRules: []rbacv1.PolicyRule{baseRule}, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + for _, tt := range tests { |
| 87 | + t.Run(tt.name, func(t *testing.T) { |
| 88 | + rules, err := generateKubeletStatsRbacRules(logr.Logger{}, tt.config) |
| 89 | + |
| 90 | + if tt.expectedErrMsg != "" { |
| 91 | + require.Error(t, err) |
| 92 | + assert.Contains(t, err.Error(), tt.expectedErrMsg) |
| 93 | + } else { |
| 94 | + require.NoError(t, err) |
| 95 | + assert.Equal(t, tt.expectedRules, rules) |
| 96 | + } |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
0 commit comments