github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/config/config_test.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 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 config 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 ) 22 23 func Test_getMergeConfig(t *testing.T) { 24 testSrcData := `apiVersion: v1 25 data: 26 key1: myConfigMap1 27 kind: ConfigMap 28 metadata: 29 name: myConfigMap1 30 --- 31 apiVersion: v1 32 data: 33 key2: myConfigMap2 34 kind: ConfigMap 35 metadata: 36 name: myConfigMap2 37 --- 38 apiVersion: v1 39 data: 40 key3: myConfigMap3 41 kind: ConfigMap 42 metadata: 43 name: myConfigMap3 44 ` 45 46 wantedData := `apiVersion: v1 47 data: 48 key1: myConfigMap1 49 test-key: test-key 50 kind: ConfigMap 51 metadata: 52 name: myConfigMap1 53 namespace: test-namespace 54 --- 55 apiVersion: v1 56 data: 57 key2: myConfigMap2 58 test-key: test-key 59 kind: ConfigMap 60 metadata: 61 name: myConfigMap2 62 namespace: test-namespace 63 --- 64 apiVersion: v1 65 data: 66 key3: myConfigMap3 67 test-key: test-key 68 kind: ConfigMap 69 metadata: 70 name: myConfigMap3 71 namespace: test-namespace 72 ` 73 74 configmapData := `data: 75 test-key: test-key 76 metadata: 77 namespace: test-namespace 78 ` 79 80 type args struct { 81 src []byte 82 configData []byte 83 } 84 tests := []struct { 85 name string 86 args args 87 }{ 88 { 89 name: "add namespace to each configmap", 90 args: args{ 91 configData: []byte(configmapData), 92 src: []byte(testSrcData), 93 }, 94 }, 95 } 96 97 for _, tt := range tests { 98 t.Run(tt.name, func(t *testing.T) { 99 got, err := getMergeConfigData(tt.args.src, tt.args.configData) 100 if err != nil { 101 assert.Errorf(t, err, "failed to MergeConfigData") 102 return 103 } 104 assert.Equal(t, wantedData, string(got)) 105 }) 106 } 107 } 108 109 func Test_convertSecretYaml(t *testing.T) { 110 configData := `global: e2FiYzogeHh4fQo= 111 components: e215c3FsOntjcHU6e3JlcXVlc3Q6IDEwMDBtfX19Cg==` 112 113 testFileData := `apiVersion: v1 114 data: 115 kind: Secret 116 metadata: 117 name: gu-demo-configuration 118 namespace: default 119 type: Opaque` 120 121 secretFileExistWanted := `apiVersion: v1 122 data: 123 components: ZTIxNWMzRnNPbnRqY0hVNmUzSmxjWFZsYzNRNklERXdNREJ0ZlgxOUNnPT0= 124 global: ZTJGaVl6b2dlSGg0ZlFvPQ== 125 kind: Secret 126 metadata: 127 creationTimestamp: null 128 name: gu-demo-configuration 129 namespace: default 130 type: Opaque 131 ` 132 133 secretFileNotExistWanted := `data: 134 components: ZTIxNWMzRnNPbnRqY0hVNmUzSmxjWFZsYzNRNklERXdNREJ0ZlgxOUNnPT0= 135 global: ZTJGaVl6b2dlSGg0ZlFvPQ== 136 metadata: 137 creationTimestamp: null 138 ` 139 140 type args struct { 141 src []byte 142 configData []byte 143 wanted []byte 144 } 145 tests := []struct { 146 name string 147 args args 148 }{ 149 { 150 "test secret convert with src", 151 args{ 152 src: []byte(testFileData), configData: []byte(configData), wanted: []byte(secretFileExistWanted)}, 153 }, 154 { 155 "test secret convert without src", 156 args{src: nil, configData: []byte(configData), wanted: []byte(secretFileNotExistWanted)}}, 157 } 158 for _, tt := range tests { 159 t.Run(tt.name, func(t *testing.T) { 160 got, err := convertSecretYaml(tt.args.src, tt.args.configData) 161 if err != nil { 162 t.Errorf("convertSecretYaml() error = %v", err) 163 return 164 } 165 assert.Equal(t, tt.args.wanted, got) 166 }) 167 } 168 }