From b42f2095a16562f2fa2bc15e863aa0cf50c0f078 Mon Sep 17 00:00:00 2001 From: jackofallops Date: Mon, 10 Feb 2020 16:07:12 +0000 Subject: [PATCH 1/4] Updated Windows image sysprep for image acctests --- .../compute/tests/resource_arm_image_test.go | 46 +++++++++++++++++++ ...ws_virtual_machine_resource_images_test.go | 6 +-- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/azurerm/internal/services/compute/tests/resource_arm_image_test.go b/azurerm/internal/services/compute/tests/resource_arm_image_test.go index a12f61a17b8b..1f1176f6cb26 100644 --- a/azurerm/internal/services/compute/tests/resource_arm_image_test.go +++ b/azurerm/internal/services/compute/tests/resource_arm_image_test.go @@ -8,12 +8,14 @@ import ( "strings" "testing" + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/terraform" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" "golang.org/x/crypto/ssh" ) @@ -258,6 +260,50 @@ func TestAccAzureRMImageVMSS_customImageVMSSFromVHD(t *testing.T) { }) } +func testGeneralizeWindowsVMImage(resourceGroup string, name string, user string, password string) resource.TestCheckFunc { + return func(s *terraform.State) error { + client := acceptance.AzureProvider.Meta().(*clients.Client).Compute.VMClient + ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext + + //command := []string{"%windir%\\system32\\sysprep\\Sysprep.exe /oobe /generalize /shutdown /mode:vm /quiet"} + command := []string{ + "$cmd = \"$Env:SystemRoot\\system32\\sysprep\\sysprep.exe\"", + "$args = \"/generalize /oobe /mode:vm /quiet\"", + //"Start-Process -Filepath $cmd -ArgumentList $args -Wait", + "Start-Process powershell -Argument \"$cmd $args\" -Wait", + } + runCommand := compute.RunCommandInput{ + CommandID: utils.String("RunPowerShellScript"), + Script: &command, + } + + future, err := client.RunCommand(ctx, resourceGroup, name, runCommand) + if err != nil { + return fmt.Errorf("Bad: Error in running sysprep: %+v", err) + } + + if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { + return fmt.Errorf("Bad: Error waiting for Windows VM to sysprep: %+v", err) + } + + daFuture, err := client.Deallocate(ctx, resourceGroup, name) + if err != nil { + return fmt.Errorf("Bad: Deallocation error: %+v", err) + } + + if err := daFuture.WaitForCompletionRef(ctx, client.Client); err != nil { + return fmt.Errorf("Bad: Deallocation error: %+v", err) + } + + _, err = client.Generalize(ctx, resourceGroup, name) + if err != nil { + return fmt.Errorf("Bad: Generalizing error: %+v", err) + } + + return nil + } +} + func testGeneralizeVMImage(resourceGroup string, vmName string, userName string, password string, hostName string, port string, location string) resource.TestCheckFunc { return func(s *terraform.State) error { armClient := acceptance.AzureProvider.Meta().(*clients.Client) diff --git a/azurerm/internal/services/compute/tests/windows_virtual_machine_resource_images_test.go b/azurerm/internal/services/compute/tests/windows_virtual_machine_resource_images_test.go index 17662e62346d..43549e39de62 100644 --- a/azurerm/internal/services/compute/tests/windows_virtual_machine_resource_images_test.go +++ b/azurerm/internal/services/compute/tests/windows_virtual_machine_resource_images_test.go @@ -394,11 +394,9 @@ func generalizeWindowsVirtualMachine(resourceName string) func(s *terraform.Stat // TODO: make this for Windows.. resourceGroup := rs.Primary.Attributes["resource_group_name"] name := rs.Primary.Attributes["name"] - username := rs.Primary.Attributes["admin_username"] + user := rs.Primary.Attributes["admin_username"] password := rs.Primary.Attributes["admin_password"] - port := "22" - location := rs.Primary.Attributes["location"] - return testGeneralizeVMImage(resourceGroup, name, username, password, name, port, location)(s) + return testGeneralizeWindowsVMImage(resourceGroup, name, user, password)(s) } } From 7f68dc6066963f2e6f8c42d8e4e67baa924fb01b Mon Sep 17 00:00:00 2001 From: jackofallops Date: Tue, 11 Feb 2020 16:23:44 +0000 Subject: [PATCH 2/4] updated sysprep step for reliability. Tweaked test config. removed comments --- .../compute/tests/resource_arm_image_test.go | 6 ++---- ...ndows_virtual_machine_resource_images_test.go | 16 +++------------- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/azurerm/internal/services/compute/tests/resource_arm_image_test.go b/azurerm/internal/services/compute/tests/resource_arm_image_test.go index 1f1176f6cb26..15fa23a9391b 100644 --- a/azurerm/internal/services/compute/tests/resource_arm_image_test.go +++ b/azurerm/internal/services/compute/tests/resource_arm_image_test.go @@ -260,16 +260,14 @@ func TestAccAzureRMImageVMSS_customImageVMSSFromVHD(t *testing.T) { }) } -func testGeneralizeWindowsVMImage(resourceGroup string, name string, user string, password string) resource.TestCheckFunc { +func testGeneralizeWindowsVMImage(resourceGroup string, name string) resource.TestCheckFunc { return func(s *terraform.State) error { client := acceptance.AzureProvider.Meta().(*clients.Client).Compute.VMClient ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext - //command := []string{"%windir%\\system32\\sysprep\\Sysprep.exe /oobe /generalize /shutdown /mode:vm /quiet"} command := []string{ "$cmd = \"$Env:SystemRoot\\system32\\sysprep\\sysprep.exe\"", - "$args = \"/generalize /oobe /mode:vm /quiet\"", - //"Start-Process -Filepath $cmd -ArgumentList $args -Wait", + "$args = \"/generalize /oobe /mode:vm /quit\"", "Start-Process powershell -Argument \"$cmd $args\" -Wait", } runCommand := compute.RunCommandInput{ diff --git a/azurerm/internal/services/compute/tests/windows_virtual_machine_resource_images_test.go b/azurerm/internal/services/compute/tests/windows_virtual_machine_resource_images_test.go index 43549e39de62..8c61b6414757 100644 --- a/azurerm/internal/services/compute/tests/windows_virtual_machine_resource_images_test.go +++ b/azurerm/internal/services/compute/tests/windows_virtual_machine_resource_images_test.go @@ -72,7 +72,6 @@ func TestAccWindowsVirtualMachine_imageFromSharedImageGallery(t *testing.T) { // create the original VM Config: testWindowsVirtualMachine_imageFromExistingMachinePrep(data), Check: resource.ComposeTestCheckFunc( - checkWindowsVirtualMachineExists("azurerm_windows_virtual_machine.source"), generalizeWindowsVirtualMachine("azurerm_windows_virtual_machine.source"), ), @@ -298,12 +297,6 @@ resource "azurerm_image" "test" { source_virtual_machine_id = azurerm_windows_virtual_machine.source.id } -resource "azurerm_shared_image_gallery" "test" { - name = "acctest-gallery-%d" - resource_group_name = azurerm_resource_group.test.name - location = azurerm_resource_group.test.location -} - resource "azurerm_shared_image" "test" { name = "acctest-gallery-image" gallery_name = azurerm_shared_image_gallery.test.name @@ -334,7 +327,7 @@ resource "azurerm_shared_image_version" "test" { } resource "azurerm_windows_virtual_machine" "test" { - name = local.vm_name + name = "${local.vm_name}2" resource_group_name = azurerm_resource_group.test.name location = azurerm_resource_group.test.location size = "Standard_F2" @@ -350,7 +343,7 @@ resource "azurerm_windows_virtual_machine" "test" { storage_account_type = "Standard_LRS" } } -`, template, data.RandomInteger) +`, template) } func testWindowsVirtualMachine_imageFromSourceImageReference(data acceptance.TestData) string { @@ -391,12 +384,9 @@ func generalizeWindowsVirtualMachine(resourceName string) func(s *terraform.Stat return fmt.Errorf("Not found: %s", resourceName) } - // TODO: make this for Windows.. resourceGroup := rs.Primary.Attributes["resource_group_name"] name := rs.Primary.Attributes["name"] - user := rs.Primary.Attributes["admin_username"] - password := rs.Primary.Attributes["admin_password"] - return testGeneralizeWindowsVMImage(resourceGroup, name, user, password)(s) + return testGeneralizeWindowsVMImage(resourceGroup, name)(s) } } From ee2ae8add227b84c86c7e16ac01bed49015887ca Mon Sep 17 00:00:00 2001 From: jackofallops Date: Tue, 11 Feb 2020 16:47:09 +0000 Subject: [PATCH 3/4] updated Windows VM source_image_id validation --- .../services/compute/windows_virtual_machine_resource.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/azurerm/internal/services/compute/windows_virtual_machine_resource.go b/azurerm/internal/services/compute/windows_virtual_machine_resource.go index d80da30e0fcb..e7633c331f32 100644 --- a/azurerm/internal/services/compute/windows_virtual_machine_resource.go +++ b/azurerm/internal/services/compute/windows_virtual_machine_resource.go @@ -220,7 +220,11 @@ func resourceWindowsVirtualMachine() *schema.Resource { Type: schema.TypeString, Optional: true, ForceNew: true, - ValidateFunc: computeValidate.ImageID, + ValidateFunc: validation.Any( + computeValidate.ImageID, + computeValidate.SharedImageID, + computeValidate.SharedImageVersionID, + ), }, "source_image_reference": sourceImageReferenceSchema(true), From aa6781d713797b647abe17d54b5c082cc5f6bec5 Mon Sep 17 00:00:00 2001 From: jackofallops Date: Wed, 12 Feb 2020 10:19:54 +0000 Subject: [PATCH 4/4] fmt fix --- .../services/compute/windows_virtual_machine_resource.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/azurerm/internal/services/compute/windows_virtual_machine_resource.go b/azurerm/internal/services/compute/windows_virtual_machine_resource.go index e7633c331f32..28324b4b26f1 100644 --- a/azurerm/internal/services/compute/windows_virtual_machine_resource.go +++ b/azurerm/internal/services/compute/windows_virtual_machine_resource.go @@ -217,9 +217,9 @@ func resourceWindowsVirtualMachine() *schema.Resource { "secret": windowsSecretSchema(), "source_image_id": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, + Type: schema.TypeString, + Optional: true, + ForceNew: true, ValidateFunc: validation.Any( computeValidate.ImageID, computeValidate.SharedImageID,