github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/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 TestArtifactProperties(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  					},
    97  				},
    98  				Location: "southcentralus",
    99  			},
   100  		},
   101  	}
   102  
   103  	testSubject, err := NewArtifact(&template, getFakeSasUrl)
   104  	if err != nil {
   105  		t.Fatalf("err=%s", err)
   106  	}
   107  
   108  	if testSubject.OSDiskUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" {
   109  		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)
   110  	}
   111  	if testSubject.OSDiskUriReadOnlySas != "SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" {
   112  		t.Errorf("Expected template to be 'SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd', but got %s", testSubject.OSDiskUriReadOnlySas)
   113  	}
   114  	if testSubject.TemplateUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" {
   115  		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)
   116  	}
   117  	if testSubject.TemplateUriReadOnlySas != "SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" {
   118  		t.Errorf("Expected template to be 'SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json', but got %s", testSubject.TemplateUriReadOnlySas)
   119  	}
   120  	if testSubject.StorageAccountLocation != "southcentralus" {
   121  		t.Errorf("Expected StorageAccountLocation to be 'southcentral', but got %s", testSubject.StorageAccountLocation)
   122  	}
   123  }
   124  
   125  func TestArtifactOverHypenatedCaptureUri(t *testing.T) {
   126  	template := CaptureTemplate{
   127  		Resources: []CaptureResources{
   128  			{
   129  				Properties: CaptureProperties{
   130  					StorageProfile: CaptureStorageProfile{
   131  						OSDisk: CaptureDisk{
   132  							Image: CaptureUri{
   133  								Uri: "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/pac-ker-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd",
   134  							},
   135  						},
   136  					},
   137  				},
   138  				Location: "southcentralus",
   139  			},
   140  		},
   141  	}
   142  
   143  	testSubject, err := NewArtifact(&template, getFakeSasUrl)
   144  	if err != nil {
   145  		t.Fatalf("err=%s", err)
   146  	}
   147  
   148  	if testSubject.TemplateUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/pac-ker-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" {
   149  		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)
   150  	}
   151  }
   152  
   153  func TestArtifactRejectMalformedTemplates(t *testing.T) {
   154  	template := CaptureTemplate{}
   155  
   156  	_, err := NewArtifact(&template, getFakeSasUrl)
   157  	if err == nil {
   158  		t.Fatalf("Expected artifact creation to fail, but it succeeded.")
   159  	}
   160  }
   161  
   162  func TestArtifactRejectMalformedStorageUri(t *testing.T) {
   163  	template := CaptureTemplate{
   164  		Resources: []CaptureResources{
   165  			{
   166  				Properties: CaptureProperties{
   167  					StorageProfile: CaptureStorageProfile{
   168  						OSDisk: CaptureDisk{
   169  							Image: CaptureUri{
   170  								Uri: "bark",
   171  							},
   172  						},
   173  					},
   174  				},
   175  			},
   176  		},
   177  	}
   178  
   179  	_, err := NewArtifact(&template, getFakeSasUrl)
   180  	if err == nil {
   181  		t.Fatalf("Expected artifact creation to fail, but it succeeded.")
   182  	}
   183  }