github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/common/template/template_builder_test.go (about)

     1  package template
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-04-01/compute"
     7  	"github.com/approvals/go-approval-tests"
     8  )
     9  
    10  // Ensure that a Linux template is configured as expected.
    11  //  * Include SSH configuration: authorized key, and key path.
    12  func TestBuildLinux00(t *testing.T) {
    13  	testSubject, err := NewTemplateBuilder(BasicTemplate)
    14  	if err != nil {
    15  		t.Fatal(err)
    16  	}
    17  
    18  	err = testSubject.BuildLinux("--test-ssh-authorized-key--")
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  
    23  	err = testSubject.SetMarketPlaceImage("Canonical", "UbuntuServer", "16.04", "latest")
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  
    28  	doc, err := testSubject.ToJSON()
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  
    33  	err = approvaltests.VerifyJSONBytes(t, []byte(*doc))
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  }
    38  
    39  // Ensure that a user can specify a custom VHD when building a Linux template.
    40  func TestBuildLinux01(t *testing.T) {
    41  	testSubject, err := NewTemplateBuilder(BasicTemplate)
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  
    46  	err = testSubject.BuildLinux("--test-ssh-authorized-key--")
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	err = testSubject.SetImageUrl("http://azure/custom.vhd", compute.Linux)
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  
    56  	doc, err := testSubject.ToJSON()
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  
    61  	err = approvaltests.VerifyJSONBytes(t, []byte(*doc))
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  }
    66  
    67  // Ensure that a user can specify an existing Virtual Network
    68  func TestBuildLinux02(t *testing.T) {
    69  	testSubject, err := NewTemplateBuilder(BasicTemplate)
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  
    74  	testSubject.BuildLinux("--test-ssh-authorized-key--")
    75  	testSubject.SetImageUrl("http://azure/custom.vhd", compute.Linux)
    76  	testSubject.SetOSDiskSizeGB(100)
    77  
    78  	err = testSubject.SetVirtualNetwork("--virtual-network-resource-group--", "--virtual-network--", "--subnet-name--")
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  
    83  	doc, err := testSubject.ToJSON()
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  
    88  	err = approvaltests.VerifyJSONBytes(t, []byte(*doc))
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  }
    93  
    94  // Ensure that a Windows template is configured as expected.
    95  //  * Include WinRM configuration.
    96  //  * Include KeyVault configuration, which is needed for WinRM.
    97  func TestBuildWindows00(t *testing.T) {
    98  	testSubject, err := NewTemplateBuilder(BasicTemplate)
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  
   103  	err = testSubject.BuildWindows("--test-key-vault-name", "--test-winrm-certificate-url--")
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  
   108  	err = testSubject.SetMarketPlaceImage("MicrosoftWindowsServer", "WindowsServer", "2012-R2-Datacenter", "latest")
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  
   113  	doc, err := testSubject.ToJSON()
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  
   118  	err = approvaltests.VerifyJSONBytes(t, []byte(*doc))
   119  	if err != nil {
   120  		t.Fatal(err)
   121  	}
   122  }
   123  
   124  // Windows build with additional disk for an managed build
   125  func TestBuildWindows01(t *testing.T) {
   126  	testSubject, err := NewTemplateBuilder(BasicTemplate)
   127  	if err != nil {
   128  		t.Fatal(err)
   129  	}
   130  
   131  	err = testSubject.BuildWindows("--test-key-vault-name", "--test-winrm-certificate-url--")
   132  	if err != nil {
   133  		t.Fatal(err)
   134  	}
   135  
   136  	err = testSubject.SetManagedMarketplaceImage("MicrosoftWindowsServer", "WindowsServer", "2012-R2-Datacenter", "latest", "2015-1", "1", "Premium_LRS")
   137  	if err != nil {
   138  		t.Fatal(err)
   139  	}
   140  
   141  	err = testSubject.SetAdditionalDisks([]int32{32, 64}, true)
   142  	if err != nil {
   143  		t.Fatal(err)
   144  	}
   145  
   146  	doc, err := testSubject.ToJSON()
   147  	if err != nil {
   148  		t.Fatal(err)
   149  	}
   150  
   151  	err = approvaltests.VerifyJSONBytes(t, []byte(*doc))
   152  	if err != nil {
   153  		t.Fatal(err)
   154  	}
   155  }
   156  
   157  // Windows build with additional disk for an unmanaged build
   158  func TestBuildWindows02(t *testing.T) {
   159  	testSubject, err := NewTemplateBuilder(BasicTemplate)
   160  	if err != nil {
   161  		t.Fatal(err)
   162  	}
   163  
   164  	err = testSubject.BuildWindows("--test-key-vault-name", "--test-winrm-certificate-url--")
   165  	if err != nil {
   166  		t.Fatal(err)
   167  	}
   168  
   169  	err = testSubject.SetAdditionalDisks([]int32{32, 64}, false)
   170  	if err != nil {
   171  		t.Fatal(err)
   172  	}
   173  
   174  	doc, err := testSubject.ToJSON()
   175  	if err != nil {
   176  		t.Fatal(err)
   177  	}
   178  
   179  	err = approvaltests.VerifyJSONBytes(t, []byte(*doc))
   180  	if err != nil {
   181  		t.Fatal(err)
   182  	}
   183  }
   184  
   185  // Shared Image Gallery Build
   186  func TestSharedImageGallery00(t *testing.T) {
   187  	testSubject, err := NewTemplateBuilder(BasicTemplate)
   188  	if err != nil {
   189  		t.Fatal(err)
   190  	}
   191  
   192  	err = testSubject.BuildLinux("--test-ssh-authorized-key--")
   193  	if err != nil {
   194  		t.Fatal(err)
   195  	}
   196  
   197  	imageID := "/subscriptions/ignore/resourceGroups/ignore/providers/Microsoft.Compute/galleries/ignore/images/ignore"
   198  	err = testSubject.SetSharedGalleryImage("westcentralus", imageID)
   199  	if err != nil {
   200  		t.Fatal(err)
   201  	}
   202  
   203  	doc, err := testSubject.ToJSON()
   204  	if err != nil {
   205  		t.Fatal(err)
   206  	}
   207  
   208  	err = approvaltests.VerifyJSONBytes(t, []byte(*doc))
   209  	if err != nil {
   210  		t.Fatal(err)
   211  	}
   212  }