Skip to content

Commit

Permalink
Add test to PR 9320 (#11645)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfrahry authored and stack72 committed Feb 3, 2017
1 parent d2aaa45 commit d421829
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions builtin/providers/google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,10 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error

d.Set("can_ip_forward", instance.CanIpForward)

machineTypeResource := strings.Split(instance.MachineType, "/")
machineType := machineTypeResource[len(machineTypeResource)-1]
d.Set("machine_type", machineType)

// Set the service accounts
serviceAccounts := make([]map[string]interface{}, 0, 1)
for _, serviceAccount := range instance.ServiceAccounts {
Expand Down
60 changes: 60 additions & 0 deletions builtin/providers/google/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,66 @@ func TestAccComputeInstance_invalid_disk(t *testing.T) {
})
}

func TestAccComputeInstance_forceChangeMachineTypeManually(t *testing.T) {
var instance compute.Instance
var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeInstance_basic(instanceName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists("google_compute_instance.foobar", &instance),
testAccCheckComputeInstanceUpdateMachineType("google_compute_instance.foobar"),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckComputeInstanceUpdateMachineType(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}

config := testAccProvider.Meta().(*Config)

op, err := config.clientCompute.Instances.Stop(config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
if err != nil {
return fmt.Errorf("Could not stop instance: %s", err)
}
err = computeOperationWaitZone(config, op, config.Project, rs.Primary.Attributes["zone"], "Waiting on stop")
if err != nil {
return fmt.Errorf("Could not stop instance: %s", err)
}

machineType := compute.InstancesSetMachineTypeRequest{
MachineType: "zones/us-central1-a/machineTypes/f1-micro",
}

op, err = config.clientCompute.Instances.SetMachineType(
config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID, &machineType).Do()
if err != nil {
return fmt.Errorf("Could not change machine type: %s", err)
}
err = computeOperationWaitZone(config, op, config.Project, rs.Primary.Attributes["zone"], "Waiting machine type change")
if err != nil {
return fmt.Errorf("Could not change machine type: %s", err)
}
return nil
}
}

func testAccCheckComputeInstanceDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)

Expand Down

0 comments on commit d421829

Please sign in to comment.