Skip to content

Commit

Permalink
Merge pull request #5661 from terraform-providers/f/fix-windows-vm-im…
Browse files Browse the repository at this point in the history
…age-tests

Updated Windows image sysprep for image acctests
  • Loading branch information
tombuildsstuff authored Feb 13, 2020
2 parents 09375aa + aa6781d commit f0ee98e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
44 changes: 44 additions & 0 deletions azurerm/internal/services/compute/tests/resource_arm_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -258,6 +260,48 @@ func TestAccAzureRMImageVMSS_customImageVMSSFromVHD(t *testing.T) {
})
}

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{
"$cmd = \"$Env:SystemRoot\\system32\\sysprep\\sysprep.exe\"",
"$args = \"/generalize /oobe /mode:vm /quit\"",
"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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -391,14 +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"]
username := 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)(s)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,14 @@ func resourceWindowsVirtualMachine() *schema.Resource {
"secret": windowsSecretSchema(),

"source_image_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: computeValidate.ImageID,
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.Any(
computeValidate.ImageID,
computeValidate.SharedImageID,
computeValidate.SharedImageVersionID,
),
},

"source_image_reference": sourceImageReferenceSchema(true),
Expand Down

0 comments on commit f0ee98e

Please sign in to comment.