github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/libvirttools/extdata_test.go (about) 1 /* 2 Copyright 2018 Mirantis 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 libvirttools 18 19 import ( 20 "reflect" 21 "testing" 22 23 v1 "k8s.io/api/core/v1" 24 meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 fakekube "k8s.io/client-go/kubernetes/fake" 26 27 "github.com/Mirantis/virtlet/pkg/metadata/types" 28 "github.com/Mirantis/virtlet/pkg/utils" 29 ) 30 31 func withExternalDataLoader(loader types.ExternalDataLoader, toCall func()) { 32 oldLoader := types.GetExternalDataLoader() 33 defer func() { 34 types.SetExternalDataLoader(oldLoader) 35 }() 36 types.SetExternalDataLoader(loader) 37 toCall() 38 } 39 40 func TestLoadExternalUserData(t *testing.T) { 41 fc := fakekube.NewSimpleClientset( 42 &v1.ConfigMap{ 43 ObjectMeta: meta_v1.ObjectMeta{ 44 Name: "samplecfg", 45 Namespace: "testns", 46 }, 47 Data: map[string]string{ 48 "foo": "bar", 49 "baz": "foobar", 50 }, 51 }, 52 &v1.Secret{ 53 ObjectMeta: meta_v1.ObjectMeta{ 54 Name: "samplesecret", 55 Namespace: "testns", 56 }, 57 Data: map[string][]byte{ 58 "foo": []byte("topSecretBar"), 59 "baz": []byte("topSecretFoobar"), 60 }, 61 }, 62 &v1.ConfigMap{ 63 ObjectMeta: meta_v1.ObjectMeta{ 64 Name: "samplecfg-keys", 65 Namespace: "testns", 66 }, 67 Data: map[string]string{ 68 "authorized_keys": "key1\nkey2\n", 69 }, 70 }, 71 &v1.Secret{ 72 ObjectMeta: meta_v1.ObjectMeta{ 73 Name: "samplesecret-keys", 74 Namespace: "testns", 75 }, 76 Data: map[string][]byte{ 77 "authorized_keys": []byte("secretKey1\nsecretKey2\n"), 78 }, 79 }, 80 &v1.ConfigMap{ 81 ObjectMeta: meta_v1.ObjectMeta{ 82 Name: "samplecfg-keys-custom", 83 Namespace: "testns", 84 }, 85 Data: map[string]string{ 86 "keys": "key1\nkey2\n", 87 }, 88 }, 89 &v1.Secret{ 90 ObjectMeta: meta_v1.ObjectMeta{ 91 Name: "samplesecret-keys-custom", 92 Namespace: "testns", 93 }, 94 Data: map[string][]byte{ 95 "keys": []byte("secretKey1\nsecretKey2\n"), 96 }, 97 }, 98 &v1.ConfigMap{ 99 ObjectMeta: meta_v1.ObjectMeta{ 100 Name: "filemapcfg", 101 Namespace: "testns", 102 }, 103 Data: map[string]string{ 104 "file1": "ZmlsZTEgY29udGVudA==", // "file1 content" 105 "file1_path": "/etc/foo.conf", 106 "file2": "file2 content", 107 "file2_path": "/etc/bar/bar.conf", 108 "file2_encoding": "plain", 109 "file3": "ZmlsZTMgY29udGVudA==", // "file3 content" 110 "file3_path": "/etc/baz/baz.conf", 111 "file3_encoding": "base64", 112 }, 113 }, 114 &v1.Secret{ 115 ObjectMeta: meta_v1.ObjectMeta{ 116 Name: "filemapsecret", 117 Namespace: "testns", 118 }, 119 Data: map[string][]byte{ 120 "file1": []byte("ZmlsZTEgc2VjcmV0IGNvbnRlbnQ="), // "file1 secret content" 121 "file1_path": []byte("/etc/foo.conf"), 122 "file2": []byte("file2 secret content"), 123 "file2_path": []byte("/etc/bar/bar.conf"), 124 "file2_encoding": []byte("plain"), 125 "file3": []byte("ZmlsZTMgc2VjcmV0IGNvbnRlbnQ="), // "file3 secret content" 126 "file3_path": []byte("/etc/baz/baz.conf"), 127 "file3_encoding": []byte("base64"), 128 }, 129 }) 130 for _, tc := range []struct { 131 name string 132 podAnnotations map[string]string 133 expectedUserData map[string]interface{} 134 expectedSSHKeys []string 135 expectedFiles map[string][]byte 136 }{ 137 { 138 name: "user data from configmap", 139 podAnnotations: map[string]string{ 140 "VirtletCloudInitUserDataSource": "configmap/samplecfg", 141 }, 142 expectedUserData: map[string]interface{}{ 143 "foo": "bar", 144 "baz": "foobar", 145 }, 146 }, 147 { 148 name: "user data from secret", 149 podAnnotations: map[string]string{ 150 "VirtletCloudInitUserDataSource": "secret/samplesecret", 151 }, 152 expectedUserData: map[string]interface{}{ 153 "foo": "topSecretBar", 154 "baz": "topSecretFoobar", 155 }, 156 }, 157 { 158 name: "ssh keys from configmap (default key)", 159 podAnnotations: map[string]string{ 160 "VirtletSSHKeySource": "configmap/samplecfg-keys", 161 }, 162 expectedSSHKeys: []string{"key1", "key2"}, 163 }, 164 { 165 name: "user data from secret (default key)", 166 podAnnotations: map[string]string{ 167 "VirtletSSHKeySource": "secret/samplesecret-keys", 168 }, 169 expectedSSHKeys: []string{"secretKey1", "secretKey2"}, 170 }, 171 { 172 name: "ssh keys from configmap (custom key)", 173 podAnnotations: map[string]string{ 174 "VirtletSSHKeySource": "configmap/samplecfg-keys-custom/keys", 175 }, 176 expectedSSHKeys: []string{"key1", "key2"}, 177 }, 178 { 179 name: "user data from secret (custom key)", 180 podAnnotations: map[string]string{ 181 "VirtletSSHKeySource": "secret/samplesecret-keys-custom/keys", 182 }, 183 expectedSSHKeys: []string{"secretKey1", "secretKey2"}, 184 }, 185 { 186 name: "files from configmap", 187 podAnnotations: map[string]string{ 188 "VirtletFilesFromDataSource": "configmap/filemapcfg", 189 }, 190 expectedFiles: map[string][]byte{ 191 "/etc/foo.conf": []byte("file1 content"), 192 "/etc/bar/bar.conf": []byte("file2 content"), 193 "/etc/baz/baz.conf": []byte("file3 content"), 194 }, 195 }, 196 { 197 name: "files from secret", 198 podAnnotations: map[string]string{ 199 "VirtletFilesFromDataSource": "secret/filemapsecret", 200 }, 201 expectedFiles: map[string][]byte{ 202 "/etc/foo.conf": []byte("file1 secret content"), 203 "/etc/bar/bar.conf": []byte("file2 secret content"), 204 "/etc/baz/baz.conf": []byte("file3 secret content"), 205 }, 206 }, 207 } { 208 t.Run(tc.name, func(t *testing.T) { 209 withExternalDataLoader(&defaultExternalDataLoader{kubeClient: fc}, func() { 210 vmc := &types.VMConfig{ 211 PodNamespace: "testns", 212 PodAnnotations: tc.podAnnotations, 213 } 214 if err := vmc.LoadAnnotations(); err != nil { 215 t.Fatalf("LoadAnnotations(): %v", err) 216 } 217 218 if tc.expectedUserData != nil && !reflect.DeepEqual(tc.expectedUserData, vmc.ParsedAnnotations.UserData) { 219 t.Errorf("bad user data. Expected:\n%s\nGot:\n%s", utils.ToJSON(tc.expectedUserData), utils.ToJSON(vmc.ParsedAnnotations.UserData)) 220 } 221 222 if tc.expectedSSHKeys != nil && !reflect.DeepEqual(tc.expectedSSHKeys, vmc.ParsedAnnotations.SSHKeys) { 223 t.Errorf("bad ssh keys. Expected:\n%s\nGot:\n%s", utils.ToJSON(tc.expectedSSHKeys), utils.ToJSON(vmc.ParsedAnnotations.SSHKeys)) 224 } 225 226 if tc.expectedFiles != nil && !reflect.DeepEqual(tc.expectedFiles, vmc.ParsedAnnotations.InjectedFiles) { 227 t.Errorf("bad ssh keys. Expected:\n%s\nGot:\n%s", utils.ToJSON(tc.expectedFiles), utils.ToJSON(vmc.ParsedAnnotations.InjectedFiles)) 228 } 229 }) 230 }) 231 } 232 }