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 )
}
2020-01-30 05:27:58 -05:00
func generatedData ( ) map [ string ] interface { } {
return make ( map [ string ] interface { } )
}
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" ,
} ,
} ,
}
2020-01-30 05:27:58 -05:00
artifact , err := NewArtifact ( & template , getFakeSasUrl , "Linux" , generatedData ( ) )
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 ) {
2020-01-30 05:27:58 -05:00
artifact , err := NewManagedImageArtifact ( "Linux" , "fakeResourceGroup" , "fakeName" , "fakeLocation" , "fakeID" , "fakeOsDiskSnapshotName" , "fakeDataDiskSnapshotPrefix" , generatedData ( ) )
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 ) {
2020-01-30 05:27:58 -05:00
artifact , err := NewManagedImageArtifact ( "Linux" , "fakeResourceGroup" , "fakeName" , "fakeLocation" , "fakeID" , "" , "fakeDataDiskSnapshotPrefix" , generatedData ( ) )
2018-11-15 17:01:16 -05:00
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 ) {
2020-01-30 05:27:58 -05:00
artifact , err := NewManagedImageArtifact ( "Linux" , "fakeResourceGroup" , "fakeName" , "fakeLocation" , "fakeID" , "fakeOsDiskSnapshotName" , "" , generatedData ( ) )
2018-11-15 17:01:16 -05:00
if err != nil {
t . Fatalf ( "err=%s" , err )
}
expected := ` Azure . ResourceManagement . VMImage :
OSType : Linux
ManagedImageResourceGroupName : fakeResourceGroup
ManagedImageName : fakeName
ManagedImageId : fakeID
ManagedImageLocation : fakeLocation
ManagedImageOSDiskSnapshotName : fakeOsDiskSnapshotName
2019-06-20 22:47:39 -04:00
`
result := artifact . String ( )
if result != expected {
t . Fatalf ( "bad: %s" , result )
}
}
2019-07-03 20:44:05 -04:00
func TestArtifactIDManagedImageWithSharedImageGalleryId ( t * testing . T ) {
2020-01-30 05:27:58 -05:00
artifact , err := NewManagedImageArtifactWithSIGAsDestination ( "Linux" , "fakeResourceGroup" , "fakeName" , "fakeLocation" , "fakeID" , "fakeOsDiskSnapshotName" , "fakeDataDiskSnapshotPrefix" , "fakeSharedImageGallery" , generatedData ( ) )
2019-06-20 22:47:39 -04:00
if err != nil {
t . Fatalf ( "err=%s" , err )
}
expected := ` Azure . ResourceManagement . VMImage :
OSType : Linux
ManagedImageResourceGroupName : fakeResourceGroup
ManagedImageName : fakeName
ManagedImageId : fakeID
ManagedImageLocation : fakeLocation
ManagedImageOSDiskSnapshotName : fakeOsDiskSnapshotName
ManagedImageDataDiskSnapshotPrefix : fakeDataDiskSnapshotPrefix
2019-07-03 20:44:05 -04:00
ManagedImageSharedImageGalleryId : fakeSharedImageGallery
2018-11-15 17:01:16 -05:00
`
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" ,
} ,
} ,
}
2020-01-30 05:27:58 -05:00
artifact , err := NewArtifact ( & template , getFakeSasUrl , "Linux" , generatedData ( ) )
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" ,
} ,
} ,
}
2020-01-30 05:27:58 -05:00
artifact , err := NewArtifact ( & template , getFakeSasUrl , "Linux" , generatedData ( ) )
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" ,
} ,
} ,
}
2020-01-30 05:27:58 -05:00
testSubject , err := NewArtifact ( & template , getFakeSasUrl , "Linux" , generatedData ( ) )
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" ,
} ,
} ,
}
2020-01-30 05:27:58 -05:00
testSubject , err := NewArtifact ( & template , getFakeSasUrl , "Linux" , generatedData ( ) )
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" ,
} ,
} ,
}
2020-01-30 05:27:58 -05:00
testSubject , err := NewArtifact ( & template , getFakeSasUrl , "Linux" , generatedData ( ) )
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 { }
2020-01-30 05:27:58 -05:00
_ , err := NewArtifact ( & template , getFakeSasUrl , "Linux" , generatedData ( ) )
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" ,
} ,
} ,
} ,
} ,
} ,
} ,
}
2020-01-30 05:27:58 -05:00
_ , err := NewArtifact ( & template , getFakeSasUrl , "Linux" , generatedData ( ) )
2016-04-21 19:50:03 -04:00
if err == nil {
t . Fatalf ( "Expected artifact creation to fail, but it succeeded." )
}
}
2020-01-30 05:27:58 -05:00
func TestArtifactState_StateData ( t * testing . T ) {
expectedData := "this is the data"
artifact := & Artifact {
StateData : map [ string ] interface { } { "state_data" : expectedData } ,
}
// Valid state
result := artifact . State ( "state_data" )
if result != expectedData {
t . Fatalf ( "Bad: State data was %s instead of %s" , result , expectedData )
}
// Invalid state
result = artifact . State ( "invalid_key" )
if result != nil {
t . Fatalf ( "Bad: State should be nil for invalid state data name" )
}
// Nil StateData should not fail and should return nil
artifact = & Artifact { }
result = artifact . State ( "key" )
if result != nil {
t . Fatalf ( "Bad: State should be nil for nil StateData" )
}
}