github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/metadata/types/annotations_test.go (about) 1 /* 2 Copyright 2017 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 types 18 19 import ( 20 "reflect" 21 "testing" 22 23 uuid "github.com/nu7hatch/gouuid" 24 ) 25 26 func TestVirtletAnnotations(t *testing.T) { 27 28 for _, testCase := range []struct { 29 name string 30 annotations map[string]string 31 // va being nil means invalid annotations 32 va *VirtletAnnotations 33 }{ 34 { 35 name: "nil annotations", 36 annotations: nil, 37 va: &VirtletAnnotations{ 38 VCPUCount: 1, 39 DiskDriver: "scsi", 40 CDImageType: "nocloud", 41 }, 42 }, 43 { 44 name: "empty annotations", 45 annotations: map[string]string{}, 46 va: &VirtletAnnotations{ 47 VCPUCount: 1, 48 DiskDriver: "scsi", 49 CDImageType: "nocloud", 50 }, 51 }, 52 { 53 name: "non empty cloud init type annotation", 54 annotations: map[string]string{"VirtletCloudInitImageType": "configdrive"}, 55 va: &VirtletAnnotations{ 56 VCPUCount: 1, 57 DiskDriver: "scsi", 58 CDImageType: "configdrive", 59 }, 60 }, 61 { 62 name: "negative vcpu count (default)", 63 annotations: map[string]string{"VirtletVCPUCount": "-1"}, 64 va: &VirtletAnnotations{ 65 VCPUCount: 1, 66 DiskDriver: "scsi", 67 CDImageType: "nocloud", 68 }, 69 }, 70 { 71 name: "zero vcpu count (default)", 72 annotations: map[string]string{"VirtletVCPUCount": "0"}, 73 va: &VirtletAnnotations{ 74 VCPUCount: 1, 75 DiskDriver: "scsi", 76 CDImageType: "nocloud", 77 }, 78 }, 79 { 80 name: "vcpu count specified", 81 annotations: map[string]string{"VirtletVCPUCount": "4"}, 82 va: &VirtletAnnotations{ 83 VCPUCount: 4, 84 DiskDriver: "scsi", 85 CDImageType: "nocloud", 86 }, 87 }, 88 { 89 name: "root volume size", 90 annotations: map[string]string{"VirtletRootVolumeSize": "1Gi"}, 91 va: &VirtletAnnotations{ 92 VCPUCount: 1, 93 DiskDriver: "scsi", 94 CDImageType: "nocloud", 95 RootVolumeSize: 1073741824, 96 }, 97 }, 98 { 99 name: "cloud-init yaml and ssh keys", 100 annotations: map[string]string{ 101 "VirtletCloudInitMetaData": ` 102 instance-id: foobar`, 103 "VirtletCloudInitUserData": ` 104 users: 105 - name: cloudy`, 106 // empty lines are ignored 107 "VirtletSSHKeys": "key1\n\nkey2\n", 108 }, 109 va: &VirtletAnnotations{ 110 VCPUCount: 1, 111 MetaData: map[string]interface{}{ 112 "instance-id": "foobar", 113 }, 114 UserData: map[string]interface{}{ 115 "users": []interface{}{ 116 map[string]interface{}{ 117 "name": "cloudy", 118 }, 119 }, 120 }, 121 SSHKeys: []string{"key1", "key2"}, 122 DiskDriver: "scsi", 123 CDImageType: "nocloud", 124 }, 125 }, 126 { 127 name: "cloud-init user data overwrite set", 128 annotations: map[string]string{ 129 "VirtletCloudInitUserDataOverwrite": "true", 130 }, 131 va: &VirtletAnnotations{ 132 VCPUCount: 1, 133 UserDataOverwrite: true, 134 DiskDriver: "scsi", 135 CDImageType: "nocloud", 136 }, 137 }, 138 { 139 name: "cloud-init user data script", 140 annotations: map[string]string{ 141 "VirtletCloudInitUserDataScript": "#!/bin/sh\necho hi\n", 142 }, 143 va: &VirtletAnnotations{ 144 VCPUCount: 1, 145 UserDataScript: "#!/bin/sh\necho hi\n", 146 DiskDriver: "scsi", 147 CDImageType: "nocloud", 148 }, 149 }, 150 { 151 name: "system UUID", 152 annotations: map[string]string{ 153 "VirtletSystemUUID": "53008994-44c0-4017-ad44-9c49758083da", 154 }, 155 va: &VirtletAnnotations{ 156 VCPUCount: 1, 157 SystemUUID: &uuid.UUID{ 158 0x53, 0, 0x89, 0x94, 159 0x44, 0xc0, 0x40, 0x17, 0xad, 0x44, 160 0x9c, 0x49, 0x75, 0x80, 0x83, 0xda, 161 }, 162 DiskDriver: "scsi", 163 CDImageType: "nocloud", 164 }, 165 }, 166 { 167 name: "force DHCP network config", 168 annotations: map[string]string{ 169 "VirtletForceDHCPNetworkConfig": "true", 170 }, 171 va: &VirtletAnnotations{ 172 VCPUCount: 1, 173 DiskDriver: "scsi", 174 CDImageType: "nocloud", 175 ForceDHCPNetworkConfig: true, 176 }, 177 }, 178 // bad metadata items follow 179 { 180 name: "bad vcpu count", 181 annotations: map[string]string{"VirtletVCPUCount": "256"}, 182 }, 183 { 184 name: "bad disk driver", 185 annotations: map[string]string{"VirtletDiskDriver": "ducttape"}, 186 }, 187 { 188 name: "bad cloud-init meta-data", 189 annotations: map[string]string{ 190 "VirtletCloudInitMetaData": "{", 191 }, 192 }, 193 { 194 name: "bad cloud-init image type", 195 annotations: map[string]string{ 196 "VirtletCloudInitImageType": "ducttape", 197 }, 198 }, 199 { 200 name: "bad cloud-init user-data", 201 annotations: map[string]string{ 202 "VirtletCloudInitUserData": "{", 203 }, 204 }, 205 } { 206 t.Run(testCase.name, func(t *testing.T) { 207 va, err := loadAnnotations("", testCase.annotations) 208 switch { 209 case testCase.va == nil && err == nil: 210 t.Errorf("invalid annotations considered valid:\n%#v", testCase.annotations) 211 case testCase.va != nil && err != nil: 212 t.Errorf("unexpected error %q loading annotations:\n%#v", err, testCase.annotations) 213 case testCase.va != nil: 214 if !reflect.DeepEqual(testCase.va, va) { 215 t.Errorf("virtlet annotations mismatch: got\n%#v\ninstead of\n%#v", va, testCase.va) 216 } 217 } 218 }) 219 } 220 }