github.com/mitchellh/packer@v1.3.2/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 TestArtifactIdVHD(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, "Linux")
    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 TestArtifactIDManagedImage(t *testing.T) {
    45  	artifact, err := NewManagedImageArtifact("Linux", "fakeResourceGroup", "fakeName", "fakeLocation", "fakeID")
    46  	if err != nil {
    47  		t.Fatalf("err=%s", err)
    48  	}
    49  
    50  	expected := "fakeID"
    51  
    52  	result := artifact.Id()
    53  	if result != expected {
    54  		t.Fatalf("bad: %s", result)
    55  	}
    56  }
    57  
    58  func TestArtifactString(t *testing.T) {
    59  	template := CaptureTemplate{
    60  		Resources: []CaptureResources{
    61  			{
    62  				Properties: CaptureProperties{
    63  					StorageProfile: CaptureStorageProfile{
    64  						OSDisk: CaptureDisk{
    65  							Image: CaptureUri{
    66  								Uri: "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd",
    67  							},
    68  						},
    69  					},
    70  				},
    71  				Location: "southcentralus",
    72  			},
    73  		},
    74  	}
    75  
    76  	artifact, err := NewArtifact(&template, getFakeSasUrl, "Linux")
    77  	if err != nil {
    78  		t.Fatalf("err=%s", err)
    79  	}
    80  
    81  	testSubject := artifact.String()
    82  	if !strings.Contains(testSubject, "OSDiskUri: https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd") {
    83  		t.Errorf("Expected String() output to contain OSDiskUri")
    84  	}
    85  	if !strings.Contains(testSubject, "OSDiskUriReadOnlySas: SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd") {
    86  		t.Errorf("Expected String() output to contain OSDiskUriReadOnlySas")
    87  	}
    88  	if !strings.Contains(testSubject, "TemplateUri: https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json") {
    89  		t.Errorf("Expected String() output to contain TemplateUri")
    90  	}
    91  	if !strings.Contains(testSubject, "TemplateUriReadOnlySas: SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json") {
    92  		t.Errorf("Expected String() output to contain TemplateUriReadOnlySas")
    93  	}
    94  	if !strings.Contains(testSubject, "StorageAccountLocation: southcentralus") {
    95  		t.Errorf("Expected String() output to contain StorageAccountLocation")
    96  	}
    97  	if !strings.Contains(testSubject, "OSType: Linux") {
    98  		t.Errorf("Expected String() output to contain OSType")
    99  	}
   100  }
   101  
   102  func TestAdditionalDiskArtifactString(t *testing.T) {
   103  	template := CaptureTemplate{
   104  		Resources: []CaptureResources{
   105  			{
   106  				Properties: CaptureProperties{
   107  					StorageProfile: CaptureStorageProfile{
   108  						OSDisk: CaptureDisk{
   109  							Image: CaptureUri{
   110  								Uri: "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd",
   111  							},
   112  						},
   113  						DataDisks: []CaptureDisk{
   114  							{
   115  								Image: CaptureUri{
   116  									Uri: "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-datadisk-1.4085bb15-3644-4641-b9cd-f575918640b4.vhd",
   117  								},
   118  							},
   119  						},
   120  					},
   121  				},
   122  				Location: "southcentralus",
   123  			},
   124  		},
   125  	}
   126  
   127  	artifact, err := NewArtifact(&template, getFakeSasUrl, "Linux")
   128  	if err != nil {
   129  		t.Fatalf("err=%s", err)
   130  	}
   131  
   132  	testSubject := artifact.String()
   133  	if !strings.Contains(testSubject, "OSDiskUri: https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd") {
   134  		t.Errorf("Expected String() output to contain OSDiskUri")
   135  	}
   136  	if !strings.Contains(testSubject, "OSDiskUriReadOnlySas: SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd") {
   137  		t.Errorf("Expected String() output to contain OSDiskUriReadOnlySas")
   138  	}
   139  	if !strings.Contains(testSubject, "TemplateUri: https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json") {
   140  		t.Errorf("Expected String() output to contain TemplateUri")
   141  	}
   142  	if !strings.Contains(testSubject, "TemplateUriReadOnlySas: SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json") {
   143  		t.Errorf("Expected String() output to contain TemplateUriReadOnlySas")
   144  	}
   145  	if !strings.Contains(testSubject, "StorageAccountLocation: southcentralus") {
   146  		t.Errorf("Expected String() output to contain StorageAccountLocation")
   147  	}
   148  	if !strings.Contains(testSubject, "OSType: Linux") {
   149  		t.Errorf("Expected String() output to contain OSType")
   150  	}
   151  	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") {
   152  		t.Errorf("Expected String() output to contain AdditionalDiskUri")
   153  	}
   154  	if !strings.Contains(testSubject, "AdditionalDiskUriReadOnlySas (datadisk-1): SAS-Images/images/packer-datadisk-1.4085bb15-3644-4641-b9cd-f575918640b4.vhd") {
   155  		t.Errorf("Expected String() output to contain AdditionalDiskUriReadOnlySas")
   156  	}
   157  }
   158  
   159  func TestArtifactProperties(t *testing.T) {
   160  	template := CaptureTemplate{
   161  		Resources: []CaptureResources{
   162  			{
   163  				Properties: CaptureProperties{
   164  					StorageProfile: CaptureStorageProfile{
   165  						OSDisk: CaptureDisk{
   166  							Image: CaptureUri{
   167  								Uri: "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd",
   168  							},
   169  						},
   170  					},
   171  				},
   172  				Location: "southcentralus",
   173  			},
   174  		},
   175  	}
   176  
   177  	testSubject, err := NewArtifact(&template, getFakeSasUrl, "Linux")
   178  	if err != nil {
   179  		t.Fatalf("err=%s", err)
   180  	}
   181  
   182  	if testSubject.OSDiskUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" {
   183  		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)
   184  	}
   185  	if testSubject.OSDiskUriReadOnlySas != "SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" {
   186  		t.Errorf("Expected template to be 'SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd', but got %s", testSubject.OSDiskUriReadOnlySas)
   187  	}
   188  	if testSubject.TemplateUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" {
   189  		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)
   190  	}
   191  	if testSubject.TemplateUriReadOnlySas != "SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" {
   192  		t.Errorf("Expected template to be 'SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json', but got %s", testSubject.TemplateUriReadOnlySas)
   193  	}
   194  	if testSubject.StorageAccountLocation != "southcentralus" {
   195  		t.Errorf("Expected StorageAccountLocation to be 'southcentral', but got %s", testSubject.StorageAccountLocation)
   196  	}
   197  	if testSubject.OSType != "Linux" {
   198  		t.Errorf("Expected OSType to be 'Linux', but got %s", testSubject.OSType)
   199  	}
   200  }
   201  
   202  func TestAdditionalDiskArtifactProperties(t *testing.T) {
   203  	template := CaptureTemplate{
   204  		Resources: []CaptureResources{
   205  			{
   206  				Properties: CaptureProperties{
   207  					StorageProfile: CaptureStorageProfile{
   208  						OSDisk: CaptureDisk{
   209  							Image: CaptureUri{
   210  								Uri: "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd",
   211  							},
   212  						},
   213  						DataDisks: []CaptureDisk{
   214  							{
   215  								Image: CaptureUri{
   216  									Uri: "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-datadisk-1.4085bb15-3644-4641-b9cd-f575918640b4.vhd",
   217  								},
   218  							},
   219  						},
   220  					},
   221  				},
   222  				Location: "southcentralus",
   223  			},
   224  		},
   225  	}
   226  
   227  	testSubject, err := NewArtifact(&template, getFakeSasUrl, "Linux")
   228  	if err != nil {
   229  		t.Fatalf("err=%s", err)
   230  	}
   231  
   232  	if testSubject.OSDiskUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" {
   233  		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)
   234  	}
   235  	if testSubject.OSDiskUriReadOnlySas != "SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" {
   236  		t.Errorf("Expected template to be 'SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd', but got %s", testSubject.OSDiskUriReadOnlySas)
   237  	}
   238  	if testSubject.TemplateUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" {
   239  		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)
   240  	}
   241  	if testSubject.TemplateUriReadOnlySas != "SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" {
   242  		t.Errorf("Expected template to be 'SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json', but got %s", testSubject.TemplateUriReadOnlySas)
   243  	}
   244  	if testSubject.StorageAccountLocation != "southcentralus" {
   245  		t.Errorf("Expected StorageAccountLocation to be 'southcentral', but got %s", testSubject.StorageAccountLocation)
   246  	}
   247  	if testSubject.OSType != "Linux" {
   248  		t.Errorf("Expected OSType to be 'Linux', but got %s", testSubject.OSType)
   249  	}
   250  	if testSubject.AdditionalDisks == nil {
   251  		t.Errorf("Expected AdditionalDisks to be not nil")
   252  	}
   253  	if len(*testSubject.AdditionalDisks) != 1 {
   254  		t.Errorf("Expected AdditionalDisks to have one additional disk, but got %d", len(*testSubject.AdditionalDisks))
   255  	}
   256  	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" {
   257  		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)
   258  	}
   259  	if (*testSubject.AdditionalDisks)[0].AdditionalDiskUriReadOnlySas != "SAS-Images/images/packer-datadisk-1.4085bb15-3644-4641-b9cd-f575918640b4.vhd" {
   260  		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)
   261  	}
   262  }
   263  
   264  func TestArtifactOverHyphenatedCaptureUri(t *testing.T) {
   265  	template := CaptureTemplate{
   266  		Resources: []CaptureResources{
   267  			{
   268  				Properties: CaptureProperties{
   269  					StorageProfile: CaptureStorageProfile{
   270  						OSDisk: CaptureDisk{
   271  							Image: CaptureUri{
   272  								Uri: "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/pac-ker-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd",
   273  							},
   274  						},
   275  					},
   276  				},
   277  				Location: "southcentralus",
   278  			},
   279  		},
   280  	}
   281  
   282  	testSubject, err := NewArtifact(&template, getFakeSasUrl, "Linux")
   283  	if err != nil {
   284  		t.Fatalf("err=%s", err)
   285  	}
   286  
   287  	if testSubject.TemplateUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/pac-ker-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" {
   288  		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)
   289  	}
   290  }
   291  
   292  func TestArtifactRejectMalformedTemplates(t *testing.T) {
   293  	template := CaptureTemplate{}
   294  
   295  	_, err := NewArtifact(&template, getFakeSasUrl, "Linux")
   296  	if err == nil {
   297  		t.Fatalf("Expected artifact creation to fail, but it succeeded.")
   298  	}
   299  }
   300  
   301  func TestArtifactRejectMalformedStorageUri(t *testing.T) {
   302  	template := CaptureTemplate{
   303  		Resources: []CaptureResources{
   304  			{
   305  				Properties: CaptureProperties{
   306  					StorageProfile: CaptureStorageProfile{
   307  						OSDisk: CaptureDisk{
   308  							Image: CaptureUri{
   309  								Uri: "bark",
   310  							},
   311  						},
   312  					},
   313  				},
   314  			},
   315  		},
   316  	}
   317  
   318  	_, err := NewArtifact(&template, getFakeSasUrl, "Linux")
   319  	if err == nil {
   320  		t.Fatalf("Expected artifact creation to fail, but it succeeded.")
   321  	}
   322  }