2016-04-21 19:50:03 -04:00
package arm
import (
"fmt"
"strings"
"testing"
)
func getFakeSasUrl ( name string ) string {
return fmt . Sprintf ( "SAS-%s" , name )
}
2018-10-10 13:00:55 -04:00
func TestArtifactIdVHD ( t * testing . T ) {
2017-06-07 03:12:03 -04:00
template := CaptureTemplate {
Resources : [ ] CaptureResources {
{
Properties : CaptureProperties {
StorageProfile : CaptureStorageProfile {
OSDisk : CaptureDisk {
Image : CaptureUri {
Uri : "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" ,
} ,
} ,
} ,
} ,
Location : "southcentralus" ,
} ,
} ,
}
2018-08-08 11:04:28 -04:00
artifact , err := NewArtifact ( & template , getFakeSasUrl , "Linux" )
2017-06-07 03:12:03 -04:00
if err != nil {
t . Fatalf ( "err=%s" , err )
}
expected := "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd"
result := artifact . Id ( )
if result != expected {
t . Fatalf ( "bad: %s" , result )
}
}
2018-10-10 13:00:55 -04:00
func TestArtifactIDManagedImage ( t * testing . T ) {
2018-11-15 17:01:16 -05:00
artifact , err := NewManagedImageArtifact ( "Linux" , "fakeResourceGroup" , "fakeName" , "fakeLocation" , "fakeID" , "fakeOsDiskSnapshotName" , "fakeDataDiskSnapshotPrefix" )
2018-10-10 13:00:55 -04:00
if err != nil {
t . Fatalf ( "err=%s" , err )
}
2018-11-15 17:01:16 -05:00
expected := ` Azure . ResourceManagement . VMImage :
2018-10-10 13:00:55 -04:00
2018-11-15 17:01:16 -05:00
OSType : Linux
ManagedImageResourceGroupName : fakeResourceGroup
ManagedImageName : fakeName
ManagedImageId : fakeID
ManagedImageLocation : fakeLocation
ManagedImageOSDiskSnapshotName : fakeOsDiskSnapshotName
ManagedImageDataDiskSnapshotPrefix : fakeDataDiskSnapshotPrefix
`
result := artifact . String ( )
if result != expected {
t . Fatalf ( "bad: %s" , result )
}
}
func TestArtifactIDManagedImageWithoutOSDiskSnapshotName ( t * testing . T ) {
artifact , err := NewManagedImageArtifact ( "Linux" , "fakeResourceGroup" , "fakeName" , "fakeLocation" , "fakeID" , "" , "fakeDataDiskSnapshotPrefix" )
if err != nil {
t . Fatalf ( "err=%s" , err )
}
expected := ` Azure . ResourceManagement . VMImage :
OSType : Linux
ManagedImageResourceGroupName : fakeResourceGroup
ManagedImageName : fakeName
ManagedImageId : fakeID
ManagedImageLocation : fakeLocation
ManagedImageDataDiskSnapshotPrefix : fakeDataDiskSnapshotPrefix
`
result := artifact . String ( )
if result != expected {
t . Fatalf ( "bad: %s" , result )
}
}
func TestArtifactIDManagedImageWithoutDataDiskSnapshotPrefix ( t * testing . T ) {
artifact , err := NewManagedImageArtifact ( "Linux" , "fakeResourceGroup" , "fakeName" , "fakeLocation" , "fakeID" , "fakeOsDiskSnapshotName" , "" )
if err != nil {
t . Fatalf ( "err=%s" , err )
}
expected := ` Azure . ResourceManagement . VMImage :
OSType : Linux
ManagedImageResourceGroupName : fakeResourceGroup
ManagedImageName : fakeName
ManagedImageId : fakeID
ManagedImageLocation : fakeLocation
ManagedImageOSDiskSnapshotName : fakeOsDiskSnapshotName
`
result := artifact . String ( )
2018-10-10 13:00:55 -04:00
if result != expected {
t . Fatalf ( "bad: %s" , result )
}
}
2016-04-21 19:50:03 -04:00
func TestArtifactString ( t * testing . T ) {
template := CaptureTemplate {
Resources : [ ] CaptureResources {
2016-07-16 01:23:53 -04:00
{
2016-04-21 19:50:03 -04:00
Properties : CaptureProperties {
StorageProfile : CaptureStorageProfile {
OSDisk : CaptureDisk {
Image : CaptureUri {
Uri : "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" ,
} ,
} ,
} ,
} ,
Location : "southcentralus" ,
} ,
} ,
}
2018-08-08 11:04:28 -04:00
artifact , err := NewArtifact ( & template , getFakeSasUrl , "Linux" )
2016-04-21 19:50:03 -04:00
if err != nil {
t . Fatalf ( "err=%s" , err )
}
testSubject := artifact . String ( )
if ! strings . Contains ( testSubject , "OSDiskUri: https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" ) {
t . Errorf ( "Expected String() output to contain OSDiskUri" )
}
if ! strings . Contains ( testSubject , "OSDiskUriReadOnlySas: SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" ) {
t . Errorf ( "Expected String() output to contain OSDiskUriReadOnlySas" )
}
if ! strings . Contains ( testSubject , "TemplateUri: https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" ) {
t . Errorf ( "Expected String() output to contain TemplateUri" )
}
if ! strings . Contains ( testSubject , "TemplateUriReadOnlySas: SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" ) {
t . Errorf ( "Expected String() output to contain TemplateUriReadOnlySas" )
}
if ! strings . Contains ( testSubject , "StorageAccountLocation: southcentralus" ) {
t . Errorf ( "Expected String() output to contain StorageAccountLocation" )
}
2018-08-08 11:04:28 -04:00
if ! strings . Contains ( testSubject , "OSType: Linux" ) {
t . Errorf ( "Expected String() output to contain OSType" )
}
2016-04-21 19:50:03 -04:00
}
2018-02-23 18:34:13 -05:00
func TestAdditionalDiskArtifactString ( t * testing . T ) {
template := CaptureTemplate {
Resources : [ ] CaptureResources {
{
Properties : CaptureProperties {
StorageProfile : CaptureStorageProfile {
OSDisk : CaptureDisk {
Image : CaptureUri {
Uri : "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" ,
} ,
} ,
DataDisks : [ ] CaptureDisk {
2018-02-23 21:43:55 -05:00
{
2018-02-23 18:34:13 -05:00
Image : CaptureUri {
Uri : "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-datadisk-1.4085bb15-3644-4641-b9cd-f575918640b4.vhd" ,
} ,
} ,
} ,
} ,
} ,
Location : "southcentralus" ,
} ,
} ,
}
2018-08-08 11:04:28 -04:00
artifact , err := NewArtifact ( & template , getFakeSasUrl , "Linux" )
2018-02-23 18:34:13 -05:00
if err != nil {
t . Fatalf ( "err=%s" , err )
}
testSubject := artifact . String ( )
if ! strings . Contains ( testSubject , "OSDiskUri: https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" ) {
t . Errorf ( "Expected String() output to contain OSDiskUri" )
}
if ! strings . Contains ( testSubject , "OSDiskUriReadOnlySas: SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" ) {
t . Errorf ( "Expected String() output to contain OSDiskUriReadOnlySas" )
}
if ! strings . Contains ( testSubject , "TemplateUri: https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" ) {
t . Errorf ( "Expected String() output to contain TemplateUri" )
}
if ! strings . Contains ( testSubject , "TemplateUriReadOnlySas: SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" ) {
t . Errorf ( "Expected String() output to contain TemplateUriReadOnlySas" )
}
if ! strings . Contains ( testSubject , "StorageAccountLocation: southcentralus" ) {
t . Errorf ( "Expected String() output to contain StorageAccountLocation" )
}
2018-08-08 11:04:28 -04:00
if ! strings . Contains ( testSubject , "OSType: Linux" ) {
t . Errorf ( "Expected String() output to contain OSType" )
}
2018-02-23 18:34:13 -05:00
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" ) {
t . Errorf ( "Expected String() output to contain AdditionalDiskUri" )
}
if ! strings . Contains ( testSubject , "AdditionalDiskUriReadOnlySas (datadisk-1): SAS-Images/images/packer-datadisk-1.4085bb15-3644-4641-b9cd-f575918640b4.vhd" ) {
t . Errorf ( "Expected String() output to contain AdditionalDiskUriReadOnlySas" )
}
}
2016-04-21 19:50:03 -04:00
func TestArtifactProperties ( t * testing . T ) {
template := CaptureTemplate {
Resources : [ ] CaptureResources {
2016-07-16 01:23:53 -04:00
{
2016-04-21 19:50:03 -04:00
Properties : CaptureProperties {
StorageProfile : CaptureStorageProfile {
OSDisk : CaptureDisk {
Image : CaptureUri {
Uri : "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" ,
} ,
} ,
} ,
} ,
Location : "southcentralus" ,
} ,
} ,
}
2018-08-08 11:04:28 -04:00
testSubject , err := NewArtifact ( & template , getFakeSasUrl , "Linux" )
2016-04-21 19:50:03 -04:00
if err != nil {
t . Fatalf ( "err=%s" , err )
}
if testSubject . OSDiskUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" {
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 )
}
if testSubject . OSDiskUriReadOnlySas != "SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" {
t . Errorf ( "Expected template to be 'SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd', but got %s" , testSubject . OSDiskUriReadOnlySas )
}
if testSubject . TemplateUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" {
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 )
}
if testSubject . TemplateUriReadOnlySas != "SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" {
t . Errorf ( "Expected template to be 'SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json', but got %s" , testSubject . TemplateUriReadOnlySas )
}
if testSubject . StorageAccountLocation != "southcentralus" {
t . Errorf ( "Expected StorageAccountLocation to be 'southcentral', but got %s" , testSubject . StorageAccountLocation )
}
2018-08-08 11:04:28 -04:00
if testSubject . OSType != "Linux" {
t . Errorf ( "Expected OSType to be 'Linux', but got %s" , testSubject . OSType )
}
2016-04-21 19:50:03 -04:00
}
2018-02-23 18:34:13 -05:00
func TestAdditionalDiskArtifactProperties ( t * testing . T ) {
template := CaptureTemplate {
Resources : [ ] CaptureResources {
{
Properties : CaptureProperties {
StorageProfile : CaptureStorageProfile {
OSDisk : CaptureDisk {
Image : CaptureUri {
Uri : "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" ,
} ,
} ,
DataDisks : [ ] CaptureDisk {
2018-02-23 21:43:55 -05:00
{
2018-02-23 18:34:13 -05:00
Image : CaptureUri {
Uri : "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-datadisk-1.4085bb15-3644-4641-b9cd-f575918640b4.vhd" ,
} ,
} ,
} ,
} ,
} ,
Location : "southcentralus" ,
} ,
} ,
}
2018-08-08 11:04:28 -04:00
testSubject , err := NewArtifact ( & template , getFakeSasUrl , "Linux" )
2018-02-23 18:34:13 -05:00
if err != nil {
t . Fatalf ( "err=%s" , err )
}
if testSubject . OSDiskUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" {
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 )
}
if testSubject . OSDiskUriReadOnlySas != "SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" {
t . Errorf ( "Expected template to be 'SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd', but got %s" , testSubject . OSDiskUriReadOnlySas )
}
if testSubject . TemplateUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" {
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 )
}
if testSubject . TemplateUriReadOnlySas != "SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" {
t . Errorf ( "Expected template to be 'SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json', but got %s" , testSubject . TemplateUriReadOnlySas )
}
if testSubject . StorageAccountLocation != "southcentralus" {
t . Errorf ( "Expected StorageAccountLocation to be 'southcentral', but got %s" , testSubject . StorageAccountLocation )
}
2018-08-08 11:04:28 -04:00
if testSubject . OSType != "Linux" {
t . Errorf ( "Expected OSType to be 'Linux', but got %s" , testSubject . OSType )
}
2018-02-23 18:34:13 -05:00
if testSubject . AdditionalDisks == nil {
t . Errorf ( "Expected AdditionalDisks to be not nil" )
}
if len ( * testSubject . AdditionalDisks ) != 1 {
t . Errorf ( "Expected AdditionalDisks to have one additional disk, but got %d" , len ( * testSubject . AdditionalDisks ) )
}
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" {
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 )
}
if ( * testSubject . AdditionalDisks ) [ 0 ] . AdditionalDiskUriReadOnlySas != "SAS-Images/images/packer-datadisk-1.4085bb15-3644-4641-b9cd-f575918640b4.vhd" {
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 )
}
}
2018-03-13 03:59:00 -04:00
func TestArtifactOverHyphenatedCaptureUri ( t * testing . T ) {
2016-04-21 19:50:03 -04:00
template := CaptureTemplate {
Resources : [ ] CaptureResources {
2016-07-16 01:23:53 -04:00
{
2016-04-21 19:50:03 -04:00
Properties : CaptureProperties {
StorageProfile : CaptureStorageProfile {
OSDisk : CaptureDisk {
Image : CaptureUri {
Uri : "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/pac-ker-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" ,
} ,
} ,
} ,
} ,
Location : "southcentralus" ,
} ,
} ,
}
2018-08-08 11:04:28 -04:00
testSubject , err := NewArtifact ( & template , getFakeSasUrl , "Linux" )
2016-04-21 19:50:03 -04:00
if err != nil {
t . Fatalf ( "err=%s" , err )
}
if testSubject . TemplateUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/pac-ker-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" {
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 )
}
}
func TestArtifactRejectMalformedTemplates ( t * testing . T ) {
template := CaptureTemplate { }
2018-08-08 11:04:28 -04:00
_ , err := NewArtifact ( & template , getFakeSasUrl , "Linux" )
2016-04-21 19:50:03 -04:00
if err == nil {
t . Fatalf ( "Expected artifact creation to fail, but it succeeded." )
}
}
func TestArtifactRejectMalformedStorageUri ( t * testing . T ) {
template := CaptureTemplate {
Resources : [ ] CaptureResources {
2016-07-16 01:23:53 -04:00
{
2016-04-21 19:50:03 -04:00
Properties : CaptureProperties {
StorageProfile : CaptureStorageProfile {
OSDisk : CaptureDisk {
Image : CaptureUri {
Uri : "bark" ,
} ,
} ,
} ,
} ,
} ,
} ,
}
2018-08-08 11:04:28 -04:00
_ , err := NewArtifact ( & template , getFakeSasUrl , "Linux" )
2016-04-21 19:50:03 -04:00
if err == nil {
t . Fatalf ( "Expected artifact creation to fail, but it succeeded." )
}
}