github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/azure/arm/artifact_test.go (about)

     1  // Copyright (c) Microsoft Corporation. All rights reserved.
     2  // Licensed under the MIT License. See the LICENSE file in builder/azure for license information.
     3  
     4  package arm
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func getFakeSasUrl(name string) string {
    13  	return fmt.Sprintf("SAS-%s", name)
    14  }
    15  
    16  func TestArtifactString(t *testing.T) {
    17  	template := CaptureTemplate{
    18  		Resources: []CaptureResources{
    19  			{
    20  				Properties: CaptureProperties{
    21  					StorageProfile: CaptureStorageProfile{
    22  						OSDisk: CaptureDisk{
    23  							Image: CaptureUri{
    24  								Uri: "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd",
    25  							},
    26  						},
    27  					},
    28  				},
    29  				Location: "southcentralus",
    30  			},
    31  		},
    32  	}
    33  
    34  	artifact, err := NewArtifact(&template, getFakeSasUrl)
    35  	if err != nil {
    36  		t.Fatalf("err=%s", err)
    37  	}
    38  
    39  	testSubject := artifact.String()
    40  	if !strings.Contains(testSubject, "OSDiskUri: https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd") {
    41  		t.Errorf("Expected String() output to contain OSDiskUri")
    42  	}
    43  	if !strings.Contains(testSubject, "OSDiskUriReadOnlySas: SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd") {
    44  		t.Errorf("Expected String() output to contain OSDiskUriReadOnlySas")
    45  	}
    46  	if !strings.Contains(testSubject, "TemplateUri: https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json") {
    47  		t.Errorf("Expected String() output to contain TemplateUri")
    48  	}
    49  	if !strings.Contains(testSubject, "TemplateUriReadOnlySas: SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json") {
    50  		t.Errorf("Expected String() output to contain TemplateUriReadOnlySas")
    51  	}
    52  	if !strings.Contains(testSubject, "StorageAccountLocation: southcentralus") {
    53  		t.Errorf("Expected String() output to contain StorageAccountLocation")
    54  	}
    55  }
    56  
    57  func TestArtifactProperties(t *testing.T) {
    58  	template := CaptureTemplate{
    59  		Resources: []CaptureResources{
    60  			{
    61  				Properties: CaptureProperties{
    62  					StorageProfile: CaptureStorageProfile{
    63  						OSDisk: CaptureDisk{
    64  							Image: CaptureUri{
    65  								Uri: "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd",
    66  							},
    67  						},
    68  					},
    69  				},
    70  				Location: "southcentralus",
    71  			},
    72  		},
    73  	}
    74  
    75  	testSubject, err := NewArtifact(&template, getFakeSasUrl)
    76  	if err != nil {
    77  		t.Fatalf("err=%s", err)
    78  	}
    79  
    80  	if testSubject.OSDiskUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" {
    81  		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)
    82  	}
    83  	if testSubject.OSDiskUriReadOnlySas != "SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" {
    84  		t.Errorf("Expected template to be 'SAS-Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd', but got %s", testSubject.OSDiskUriReadOnlySas)
    85  	}
    86  	if testSubject.TemplateUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" {
    87  		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)
    88  	}
    89  	if testSubject.TemplateUriReadOnlySas != "SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" {
    90  		t.Errorf("Expected template to be 'SAS-Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json', but got %s", testSubject.TemplateUriReadOnlySas)
    91  	}
    92  	if testSubject.StorageAccountLocation != "southcentralus" {
    93  		t.Errorf("Expected StorageAccountLocation to be 'southcentral', but got %s", testSubject.StorageAccountLocation)
    94  	}
    95  }
    96  
    97  func TestArtifactOverHypenatedCaptureUri(t *testing.T) {
    98  	template := CaptureTemplate{
    99  		Resources: []CaptureResources{
   100  			{
   101  				Properties: CaptureProperties{
   102  					StorageProfile: CaptureStorageProfile{
   103  						OSDisk: CaptureDisk{
   104  							Image: CaptureUri{
   105  								Uri: "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/pac-ker-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd",
   106  							},
   107  						},
   108  					},
   109  				},
   110  				Location: "southcentralus",
   111  			},
   112  		},
   113  	}
   114  
   115  	testSubject, err := NewArtifact(&template, getFakeSasUrl)
   116  	if err != nil {
   117  		t.Fatalf("err=%s", err)
   118  	}
   119  
   120  	if testSubject.TemplateUri != "https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/pac-ker-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" {
   121  		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)
   122  	}
   123  }
   124  
   125  func TestArtifactRejectMalformedTemplates(t *testing.T) {
   126  	template := CaptureTemplate{}
   127  
   128  	_, err := NewArtifact(&template, getFakeSasUrl)
   129  	if err == nil {
   130  		t.Fatalf("Expected artifact creation to fail, but it succeeded.")
   131  	}
   132  }
   133  
   134  func TestArtifactRejectMalformedStorageUri(t *testing.T) {
   135  	template := CaptureTemplate{
   136  		Resources: []CaptureResources{
   137  			{
   138  				Properties: CaptureProperties{
   139  					StorageProfile: CaptureStorageProfile{
   140  						OSDisk: CaptureDisk{
   141  							Image: CaptureUri{
   142  								Uri: "bark",
   143  							},
   144  						},
   145  					},
   146  				},
   147  			},
   148  		},
   149  	}
   150  
   151  	_, err := NewArtifact(&template, getFakeSasUrl)
   152  	if err == nil {
   153  		t.Fatalf("Expected artifact creation to fail, but it succeeded.")
   154  	}
   155  }