github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/azure/arm/artifact_test.go (about) 1 package arm 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 ) 8 9 func getFakeSasUrl(name string) string { 10 return fmt.Sprintf("SAS-%s", name) 11 } 12 13 func TestArtifactId(t *testing.T) { 14 template := CaptureTemplate{ 15 Resources: []CaptureResources{ 16 { 17 Properties: CaptureProperties{ 18 StorageProfile: CaptureStorageProfile{ 19 OSDisk: CaptureDisk{ 20 Image: CaptureUri{ 21 Uri: "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd", 22 }, 23 }, 24 }, 25 }, 26 Location: "southcentralus", 27 }, 28 }, 29 } 30 31 artifact, err := NewArtifact(&template, getFakeSasUrl) 32 if err != nil { 33 t.Fatalf("err=%s", err) 34 } 35 36 expected := "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" 37 38 result := artifact.Id() 39 if result != expected { 40 t.Fatalf("bad: %s", result) 41 } 42 } 43 44 func TestArtifactString(t *testing.T) { 45 template := CaptureTemplate{ 46 Resources: []CaptureResources{ 47 { 48 Properties: CaptureProperties{ 49 StorageProfile: CaptureStorageProfile{ 50 OSDisk: CaptureDisk{ 51 Image: CaptureUri{ 52 Uri: "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd", 53 }, 54 }, 55 }, 56 }, 57 Location: "southcentralus", 58 }, 59 }, 60 } 61 62 artifact, err := NewArtifact(&template, getFakeSasUrl) 63 if err != nil { 64 t.Fatalf("err=%s", err) 65 } 66 67 testSubject := artifact.String() 68 if !strings.Contains(testSubject, "OSDiskUri: https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd") { 69 t.Errorf("Expected String() output to contain OSDiskUri") 70 } 71 if !strings.Contains(testSubject, "OSDiskUriReadOnlySas: SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd") { 72 t.Errorf("Expected String() output to contain OSDiskUriReadOnlySas") 73 } 74 if !strings.Contains(testSubject, "TemplateUri: https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json") { 75 t.Errorf("Expected String() output to contain TemplateUri") 76 } 77 if !strings.Contains(testSubject, "TemplateUriReadOnlySas: SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json") { 78 t.Errorf("Expected String() output to contain TemplateUriReadOnlySas") 79 } 80 if !strings.Contains(testSubject, "StorageAccountLocation: southcentralus") { 81 t.Errorf("Expected String() output to contain StorageAccountLocation") 82 } 83 } 84 85 func TestAdditionalDiskArtifactString(t *testing.T) { 86 template := CaptureTemplate{ 87 Resources: []CaptureResources{ 88 { 89 Properties: CaptureProperties{ 90 StorageProfile: CaptureStorageProfile{ 91 OSDisk: CaptureDisk{ 92 Image: CaptureUri{ 93 Uri: "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd", 94 }, 95 }, 96 DataDisks: []CaptureDisk{ 97 { 98 Image: CaptureUri{ 99 Uri: "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-datadisk-1.4085bb15-3644-4641-b9cd-f575918640b4.vhd", 100 }, 101 }, 102 }, 103 }, 104 }, 105 Location: "southcentralus", 106 }, 107 }, 108 } 109 110 artifact, err := NewArtifact(&template, getFakeSasUrl) 111 if err != nil { 112 t.Fatalf("err=%s", err) 113 } 114 115 testSubject := artifact.String() 116 if !strings.Contains(testSubject, "OSDiskUri: https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd") { 117 t.Errorf("Expected String() output to contain OSDiskUri") 118 } 119 if !strings.Contains(testSubject, "OSDiskUriReadOnlySas: SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd") { 120 t.Errorf("Expected String() output to contain OSDiskUriReadOnlySas") 121 } 122 if !strings.Contains(testSubject, "TemplateUri: https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json") { 123 t.Errorf("Expected String() output to contain TemplateUri") 124 } 125 if !strings.Contains(testSubject, "TemplateUriReadOnlySas: SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json") { 126 t.Errorf("Expected String() output to contain TemplateUriReadOnlySas") 127 } 128 if !strings.Contains(testSubject, "StorageAccountLocation: southcentralus") { 129 t.Errorf("Expected String() output to contain StorageAccountLocation") 130 } 131 if !strings.Contains(testSubject, "AdditionalDiskUri (datadisk-1): https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-datadisk-1.4085bb15-3644-4641-b9cd-f575918640b4.vhd") { 132 t.Errorf("Expected String() output to contain AdditionalDiskUri") 133 } 134 if !strings.Contains(testSubject, "AdditionalDiskUriReadOnlySas (datadisk-1): SAS-Images/images/packer-datadisk-1.4085bb15-3644-4641-b9cd-f575918640b4.vhd") { 135 t.Errorf("Expected String() output to contain AdditionalDiskUriReadOnlySas") 136 } 137 } 138 139 func TestArtifactProperties(t *testing.T) { 140 template := CaptureTemplate{ 141 Resources: []CaptureResources{ 142 { 143 Properties: CaptureProperties{ 144 StorageProfile: CaptureStorageProfile{ 145 OSDisk: CaptureDisk{ 146 Image: CaptureUri{ 147 Uri: "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd", 148 }, 149 }, 150 }, 151 }, 152 Location: "southcentralus", 153 }, 154 }, 155 } 156 157 testSubject, err := NewArtifact(&template, getFakeSasUrl) 158 if err != nil { 159 t.Fatalf("err=%s", err) 160 } 161 162 if testSubject.OSDiskUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" { 163 t.Errorf("Expected template to be 'https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd', but got %s", testSubject.OSDiskUri) 164 } 165 if testSubject.OSDiskUriReadOnlySas != "SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" { 166 t.Errorf("Expected template to be 'SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd', but got %s", testSubject.OSDiskUriReadOnlySas) 167 } 168 if testSubject.TemplateUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" { 169 t.Errorf("Expected template to be 'https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json', but got %s", testSubject.TemplateUri) 170 } 171 if testSubject.TemplateUriReadOnlySas != "SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" { 172 t.Errorf("Expected template to be 'SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json', but got %s", testSubject.TemplateUriReadOnlySas) 173 } 174 if testSubject.StorageAccountLocation != "southcentralus" { 175 t.Errorf("Expected StorageAccountLocation to be 'southcentral', but got %s", testSubject.StorageAccountLocation) 176 } 177 } 178 179 func TestAdditionalDiskArtifactProperties(t *testing.T) { 180 template := CaptureTemplate{ 181 Resources: []CaptureResources{ 182 { 183 Properties: CaptureProperties{ 184 StorageProfile: CaptureStorageProfile{ 185 OSDisk: CaptureDisk{ 186 Image: CaptureUri{ 187 Uri: "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd", 188 }, 189 }, 190 DataDisks: []CaptureDisk{ 191 { 192 Image: CaptureUri{ 193 Uri: "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-datadisk-1.4085bb15-3644-4641-b9cd-f575918640b4.vhd", 194 }, 195 }, 196 }, 197 }, 198 }, 199 Location: "southcentralus", 200 }, 201 }, 202 } 203 204 testSubject, err := NewArtifact(&template, getFakeSasUrl) 205 if err != nil { 206 t.Fatalf("err=%s", err) 207 } 208 209 if testSubject.OSDiskUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" { 210 t.Errorf("Expected template to be 'https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd', but got %s", testSubject.OSDiskUri) 211 } 212 if testSubject.OSDiskUriReadOnlySas != "SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" { 213 t.Errorf("Expected template to be 'SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd', but got %s", testSubject.OSDiskUriReadOnlySas) 214 } 215 if testSubject.TemplateUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" { 216 t.Errorf("Expected template to be 'https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json', but got %s", testSubject.TemplateUri) 217 } 218 if testSubject.TemplateUriReadOnlySas != "SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" { 219 t.Errorf("Expected template to be 'SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json', but got %s", testSubject.TemplateUriReadOnlySas) 220 } 221 if testSubject.StorageAccountLocation != "southcentralus" { 222 t.Errorf("Expected StorageAccountLocation to be 'southcentral', but got %s", testSubject.StorageAccountLocation) 223 } 224 if testSubject.AdditionalDisks == nil { 225 t.Errorf("Expected AdditionalDisks to be not nil") 226 } 227 if len(*testSubject.AdditionalDisks) != 1 { 228 t.Errorf("Expected AdditionalDisks to have one additional disk, but got %d", len(*testSubject.AdditionalDisks)) 229 } 230 if (*testSubject.AdditionalDisks)[0].AdditionalDiskUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-datadisk-1.4085bb15-3644-4641-b9cd-f575918640b4.vhd" { 231 t.Errorf("Expected additional disk uri to be 'https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-datadisk-1.4085bb15-3644-4641-b9cd-f575918640b4.vhd', but got %s", (*testSubject.AdditionalDisks)[0].AdditionalDiskUri) 232 } 233 if (*testSubject.AdditionalDisks)[0].AdditionalDiskUriReadOnlySas != "SAS-Images/images/packer-datadisk-1.4085bb15-3644-4641-b9cd-f575918640b4.vhd" { 234 t.Errorf("Expected additional disk sas to be 'SAS-Images/images/packer-datadisk-1.4085bb15-3644-4641-b9cd-f575918640b4.vhd', but got %s", (*testSubject.AdditionalDisks)[0].AdditionalDiskUriReadOnlySas) 235 } 236 } 237 238 func TestArtifactOverHyphenatedCaptureUri(t *testing.T) { 239 template := CaptureTemplate{ 240 Resources: []CaptureResources{ 241 { 242 Properties: CaptureProperties{ 243 StorageProfile: CaptureStorageProfile{ 244 OSDisk: CaptureDisk{ 245 Image: CaptureUri{ 246 Uri: "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/pac-ker-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd", 247 }, 248 }, 249 }, 250 }, 251 Location: "southcentralus", 252 }, 253 }, 254 } 255 256 testSubject, err := NewArtifact(&template, getFakeSasUrl) 257 if err != nil { 258 t.Fatalf("err=%s", err) 259 } 260 261 if testSubject.TemplateUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/pac-ker-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" { 262 t.Errorf("Expected template to be 'https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/pac-ker-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json', but got %s", testSubject.TemplateUri) 263 } 264 } 265 266 func TestArtifactRejectMalformedTemplates(t *testing.T) { 267 template := CaptureTemplate{} 268 269 _, err := NewArtifact(&template, getFakeSasUrl) 270 if err == nil { 271 t.Fatalf("Expected artifact creation to fail, but it succeeded.") 272 } 273 } 274 275 func TestArtifactRejectMalformedStorageUri(t *testing.T) { 276 template := CaptureTemplate{ 277 Resources: []CaptureResources{ 278 { 279 Properties: CaptureProperties{ 280 StorageProfile: CaptureStorageProfile{ 281 OSDisk: CaptureDisk{ 282 Image: CaptureUri{ 283 Uri: "bark", 284 }, 285 }, 286 }, 287 }, 288 }, 289 }, 290 } 291 292 _, err := NewArtifact(&template, getFakeSasUrl) 293 if err == nil { 294 t.Fatalf("Expected artifact creation to fail, but it succeeded.") 295 } 296 }