k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cmd/kubeadm/app/util/env_test.go (about) 1 /* 2 Copyright 2023 The Kubernetes Authors. 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 util 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 24 v1 "k8s.io/api/core/v1" 25 26 kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" 27 ) 28 29 func TestMergeKubeadmEnvVars(t *testing.T) { 30 baseEnv := []kubeadmapi.EnvVar{} 31 extraEnv := []kubeadmapi.EnvVar{} 32 MergeKubeadmEnvVars(append(baseEnv, extraEnv...)) 33 var tests = []struct { 34 name string 35 proxyEnv []kubeadmapi.EnvVar 36 extraEnv []kubeadmapi.EnvVar 37 mergedEnv []v1.EnvVar 38 }{ 39 { 40 name: "normal case without duplicated env", 41 proxyEnv: []kubeadmapi.EnvVar{ 42 { 43 EnvVar: v1.EnvVar{Name: "Foo1", Value: "Bar1"}, 44 }, 45 { 46 EnvVar: v1.EnvVar{Name: "Foo2", Value: "Bar2"}, 47 }, 48 }, 49 extraEnv: []kubeadmapi.EnvVar{ 50 { 51 EnvVar: v1.EnvVar{Name: "Foo3", Value: "Bar3"}, 52 }, 53 }, 54 mergedEnv: []v1.EnvVar{ 55 {Name: "Foo1", Value: "Bar1"}, 56 {Name: "Foo2", Value: "Bar2"}, 57 {Name: "Foo3", Value: "Bar3"}, 58 }, 59 }, 60 { 61 name: "extraEnv env take precedence over the proxyEnv", 62 proxyEnv: []kubeadmapi.EnvVar{ 63 { 64 EnvVar: v1.EnvVar{Name: "Foo1", Value: "Bar1"}, 65 }, 66 { 67 EnvVar: v1.EnvVar{Name: "Foo2", Value: "Bar2"}, 68 }, 69 }, 70 extraEnv: []kubeadmapi.EnvVar{ 71 { 72 EnvVar: v1.EnvVar{Name: "Foo2", Value: "Bar3"}, 73 }, 74 }, 75 mergedEnv: []v1.EnvVar{ 76 {Name: "Foo1", Value: "Bar1"}, 77 {Name: "Foo2", Value: "Bar3"}, 78 }, 79 }, 80 } 81 82 for _, test := range tests { 83 t.Run(test.name, func(t *testing.T) { 84 envs := MergeKubeadmEnvVars(test.proxyEnv, test.extraEnv) 85 if !assert.ElementsMatch(t, envs, test.mergedEnv) { 86 t.Errorf("expected env: %v, got: %v", test.mergedEnv, envs) 87 } 88 }) 89 } 90 }