|
| 1 | +/* |
| 2 | +Copyright 2022 The Kubernetes Authors All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package store |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "k8s.io/kube-state-metrics/v2/pkg/options" |
| 24 | +) |
| 25 | + |
| 26 | +type LabelsAllowList options.LabelsAllowList |
| 27 | + |
| 28 | +type expectedError struct { |
| 29 | + expectedResourceError bool |
| 30 | + expectedLabelError bool |
| 31 | + expectedNotEqual bool |
| 32 | +} |
| 33 | + |
| 34 | +func TestWithAllowLabels(t *testing.T) { |
| 35 | + tests := []struct { |
| 36 | + Desc string |
| 37 | + LabelsAllowlist map[string][]string |
| 38 | + EnabledResources []string |
| 39 | + Wanted LabelsAllowList |
| 40 | + err expectedError |
| 41 | + }{ |
| 42 | + { |
| 43 | + Desc: "wildcard key-value as the only element", |
| 44 | + LabelsAllowlist: map[string][]string{"*": {"*"}}, |
| 45 | + EnabledResources: []string{"cronjobs", "pods", "deployments"}, |
| 46 | + Wanted: LabelsAllowList(map[string][]string{ |
| 47 | + "deployments": {"*"}, |
| 48 | + "pods": {"*"}, |
| 49 | + "cronjobs": {"*"}, |
| 50 | + }), |
| 51 | + }, |
| 52 | + { |
| 53 | + Desc: "wildcard key-value as not the only element", |
| 54 | + LabelsAllowlist: map[string][]string{"*": {"*"}, "pods": {"*"}, "cronjobs": {"*"}}, |
| 55 | + EnabledResources: []string{"cronjobs", "pods", "deployments"}, |
| 56 | + Wanted: LabelsAllowList(map[string][]string{ |
| 57 | + "deployments": {"*"}, |
| 58 | + "pods": {"*"}, |
| 59 | + "cronjobs": {"*"}, |
| 60 | + }), |
| 61 | + }, |
| 62 | + { |
| 63 | + Desc: "wildcard key-value as not the only element, with resource mismatch", |
| 64 | + LabelsAllowlist: map[string][]string{"*": {"*"}, "pods": {"*"}, "cronjobs": {"*"}, "configmaps": {"*"}}, |
| 65 | + EnabledResources: []string{"cronjobs", "pods", "deployments"}, |
| 66 | + Wanted: LabelsAllowList{}, |
| 67 | + err: expectedError{ |
| 68 | + expectedNotEqual: true, |
| 69 | + }, |
| 70 | + }, |
| 71 | + { |
| 72 | + Desc: "wildcard key-value as not the only element, with other mutually-exclusive keys", |
| 73 | + LabelsAllowlist: map[string][]string{"*": {"*"}, "foo": {"*"}, "bar": {"*"}, "cronjobs": {"*"}}, |
| 74 | + EnabledResources: []string{"cronjobs", "pods", "deployments"}, |
| 75 | + Wanted: LabelsAllowList(nil), |
| 76 | + err: expectedError{ |
| 77 | + expectedLabelError: true, |
| 78 | + }, |
| 79 | + }, |
| 80 | + { |
| 81 | + Desc: "wildcard key-value as not the only element, with other resources that do not exist", |
| 82 | + LabelsAllowlist: map[string][]string{"*": {"*"}, "cronjobs": {"*"}}, |
| 83 | + EnabledResources: []string{"cronjobs", "pods", "deployments", "foo", "bar"}, |
| 84 | + Wanted: LabelsAllowList{}, |
| 85 | + err: expectedError{ |
| 86 | + expectedResourceError: true, |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + for _, test := range tests { |
| 92 | + b := NewBuilder() |
| 93 | + |
| 94 | + // Set the enabled resources. |
| 95 | + err := b.WithEnabledResources(test.EnabledResources) |
| 96 | + if err != nil && !test.err.expectedResourceError { |
| 97 | + t.Log("Did not expect error while setting resources (--resources).") |
| 98 | + t.Errorf("Test error for Desc: %s. Got Error: %v", test.Desc, err) |
| 99 | + } |
| 100 | + |
| 101 | + // Resolve the allow list. |
| 102 | + err = b.WithAllowLabels(test.LabelsAllowlist) |
| 103 | + if err != nil && !test.err.expectedLabelError { |
| 104 | + t.Log("Did not expect error while parsing allow list labels (--metric-labels-allowlist).") |
| 105 | + t.Errorf("Test error for Desc: %s. Got Error: %v", test.Desc, err) |
| 106 | + } |
| 107 | + resolvedAllowLabels := LabelsAllowList(b.allowLabelsList) |
| 108 | + |
| 109 | + // Evaluate. |
| 110 | + if !reflect.DeepEqual(resolvedAllowLabels, test.Wanted) && !test.err.expectedNotEqual { |
| 111 | + t.Log("Expected maps to be equal.") |
| 112 | + t.Errorf("Test error for Desc: %s\n Want: \n%+v\n Got: \n%#+v", test.Desc, test.Wanted, resolvedAllowLabels) |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments