2016-05-21 02:01:16 -04:00
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-05-01 15:53:20 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-04-01/compute"
|
2019-03-22 09:53:28 -04:00
|
|
|
approvaltests "github.com/approvals/go-approval-tests"
|
2016-05-21 02:01:16 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Ensure that a Linux template is configured as expected.
|
|
|
|
// * Include SSH configuration: authorized key, and key path.
|
|
|
|
func TestBuildLinux00(t *testing.T) {
|
2016-07-29 17:17:33 -04:00
|
|
|
testSubject, err := NewTemplateBuilder(BasicTemplate)
|
2016-05-21 02:01:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:44:01 -04:00
|
|
|
err = testSubject.BuildLinux("--test-ssh-authorized-key--", true)
|
2016-05-21 02:01:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-11-30 15:32:41 -05:00
|
|
|
err = testSubject.SetMarketPlaceImage("Canonical", "UbuntuServer", "16.04", "latest", compute.CachingTypesReadWrite)
|
2016-05-21 02:01:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
doc, err := testSubject.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-07-11 17:36:49 -04:00
|
|
|
err = approvaltests.VerifyJSONBytes(t, []byte(*doc))
|
2016-05-21 02:01:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that a user can specify a custom VHD when building a Linux template.
|
|
|
|
func TestBuildLinux01(t *testing.T) {
|
2016-07-29 17:17:33 -04:00
|
|
|
testSubject, err := NewTemplateBuilder(BasicTemplate)
|
2016-05-21 02:01:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:44:01 -04:00
|
|
|
err = testSubject.BuildLinux("--test-ssh-authorized-key--", false)
|
2016-05-21 02:01:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-11-30 15:32:41 -05:00
|
|
|
err = testSubject.SetImageUrl("http://azure/custom.vhd", compute.Linux, compute.CachingTypesReadWrite)
|
2016-05-21 02:01:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
doc, err := testSubject.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-07-11 17:36:49 -04:00
|
|
|
err = approvaltests.VerifyJSONBytes(t, []byte(*doc))
|
2016-05-21 02:01:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-30 19:51:52 -04:00
|
|
|
// Ensure that a user can specify an existing Virtual Network
|
|
|
|
func TestBuildLinux02(t *testing.T) {
|
2016-07-29 17:17:33 -04:00
|
|
|
testSubject, err := NewTemplateBuilder(BasicTemplate)
|
2016-06-30 19:51:52 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:44:01 -04:00
|
|
|
testSubject.BuildLinux("--test-ssh-authorized-key--", true)
|
2018-11-30 15:32:41 -05:00
|
|
|
testSubject.SetImageUrl("http://azure/custom.vhd", compute.Linux, compute.CachingTypesReadWrite)
|
2016-10-12 19:24:04 -04:00
|
|
|
testSubject.SetOSDiskSizeGB(100)
|
2016-06-30 19:51:52 -04:00
|
|
|
|
|
|
|
err = testSubject.SetVirtualNetwork("--virtual-network-resource-group--", "--virtual-network--", "--subnet-name--")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
doc, err := testSubject.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = approvaltests.VerifyJSONBytes(t, []byte(*doc))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-21 02:01:16 -04:00
|
|
|
// Ensure that a Windows template is configured as expected.
|
|
|
|
// * Include WinRM configuration.
|
|
|
|
// * Include KeyVault configuration, which is needed for WinRM.
|
|
|
|
func TestBuildWindows00(t *testing.T) {
|
2016-07-29 17:17:33 -04:00
|
|
|
testSubject, err := NewTemplateBuilder(BasicTemplate)
|
2016-05-21 02:01:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = testSubject.BuildWindows("--test-key-vault-name", "--test-winrm-certificate-url--")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-11-30 15:32:41 -05:00
|
|
|
err = testSubject.SetMarketPlaceImage("MicrosoftWindowsServer", "WindowsServer", "2012-R2-Datacenter", "latest", compute.CachingTypesReadWrite)
|
2016-05-21 02:01:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
doc, err := testSubject.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-07-11 17:36:49 -04:00
|
|
|
err = approvaltests.VerifyJSONBytes(t, []byte(*doc))
|
2016-05-21 02:01:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2018-02-23 18:34:13 -05:00
|
|
|
|
|
|
|
// Windows build with additional disk for an managed build
|
|
|
|
func TestBuildWindows01(t *testing.T) {
|
|
|
|
testSubject, err := NewTemplateBuilder(BasicTemplate)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = testSubject.BuildWindows("--test-key-vault-name", "--test-winrm-certificate-url--")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-11-30 15:32:41 -05:00
|
|
|
err = testSubject.SetManagedMarketplaceImage("MicrosoftWindowsServer", "WindowsServer", "2012-R2-Datacenter", "latest", "2015-1", "1", "Premium_LRS", compute.CachingTypesReadWrite)
|
2018-02-23 18:34:13 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:58:38 -04:00
|
|
|
err = testSubject.SetAdditionalDisks([]int32{32, 64}, "datadisk", true, compute.CachingTypesReadWrite)
|
2018-02-23 18:34:13 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
doc, err := testSubject.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = approvaltests.VerifyJSONBytes(t, []byte(*doc))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Windows build with additional disk for an unmanaged build
|
|
|
|
func TestBuildWindows02(t *testing.T) {
|
|
|
|
testSubject, err := NewTemplateBuilder(BasicTemplate)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = testSubject.BuildWindows("--test-key-vault-name", "--test-winrm-certificate-url--")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:58:38 -04:00
|
|
|
err = testSubject.SetAdditionalDisks([]int32{32, 64}, "datadisk", false, compute.CachingTypesReadWrite)
|
2018-02-23 18:34:13 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
doc, err := testSubject.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = approvaltests.VerifyJSONBytes(t, []byte(*doc))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2018-10-09 16:56:49 -04:00
|
|
|
|
|
|
|
// Shared Image Gallery Build
|
2018-10-09 19:38:50 -04:00
|
|
|
func TestSharedImageGallery00(t *testing.T) {
|
2018-10-09 16:56:49 -04:00
|
|
|
testSubject, err := NewTemplateBuilder(BasicTemplate)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:44:01 -04:00
|
|
|
err = testSubject.BuildLinux("--test-ssh-authorized-key--", false)
|
2018-10-09 16:56:49 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-10-09 19:38:50 -04:00
|
|
|
imageID := "/subscriptions/ignore/resourceGroups/ignore/providers/Microsoft.Compute/galleries/ignore/images/ignore"
|
2018-11-30 15:32:41 -05:00
|
|
|
err = testSubject.SetSharedGalleryImage("westcentralus", imageID, compute.CachingTypesReadOnly)
|
2018-10-09 16:56:49 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-10-10 12:20:34 -04:00
|
|
|
doc, err := testSubject.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2018-10-09 16:56:49 -04:00
|
|
|
}
|
|
|
|
|
2018-10-10 12:20:34 -04:00
|
|
|
err = approvaltests.VerifyJSONBytes(t, []byte(*doc))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2018-10-09 16:56:49 -04:00
|
|
|
}
|
|
|
|
}
|
2019-10-08 17:56:43 -04:00
|
|
|
|
|
|
|
// Linux build with Network Security Group
|
|
|
|
func TestNetworkSecurityGroup00(t *testing.T) {
|
|
|
|
testSubject, err := NewTemplateBuilder(BasicTemplate)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:44:01 -04:00
|
|
|
err = testSubject.BuildLinux("--test-ssh-authorized-key--", false)
|
2019-10-08 17:56:43 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = testSubject.SetMarketPlaceImage("Canonical", "UbuntuServer", "16.04", "latest", compute.CachingTypesReadWrite)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = testSubject.SetNetworkSecurityGroup([]string{"127.0.0.1", "192.168.100.0/24"}, 123)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
doc, err := testSubject.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = approvaltests.VerifyJSONBytes(t, []byte(*doc))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2020-05-21 15:20:24 -04:00
|
|
|
|
|
|
|
// Linux with user assigned managed identity configured
|
|
|
|
func TestSetIdentity00(t *testing.T) {
|
|
|
|
testSubject, err := NewTemplateBuilder(BasicTemplate)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:44:01 -04:00
|
|
|
if err = testSubject.BuildLinux("--test-ssh-authorized-key--", true); err != nil {
|
2020-05-21 15:20:24 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = testSubject.SetMarketPlaceImage("Canonical", "UbuntuServer", "16.04", "latest", compute.CachingTypesReadWrite); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = testSubject.SetIdentity([]string{"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg1/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id"}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
doc, err := testSubject.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = approvaltests.VerifyJSONBytes(t, []byte(*doc)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|