Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add status argument (RUNNING/TERMINATED) to google_compute_instance #4797

Merged
62 changes: 62 additions & 0 deletions google/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,42 @@ func TestAccComputeInstance_statusTerminatedOnCreation(t *testing.T) {
})
}

func TestAccComputeInstance_statusTerminatedManuallyAndRestart(t *testing.T) {
t.Parallel()

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{
{
Config: testAccComputeInstance_basic(instanceName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
"google_compute_instance.foobar", &instance),
testAccCheckComputeInstanceHasStatusRunning(&instance),
testAccCheckComputeInstanceStop("google_compute_instance.foobar"),
testAccCheckComputeInstanceExists(
"google_compute_instance.foobar", &instance),
testAccCheckComputeInstanceHasStatusTerminated(&instance),
),
ExpectNonEmptyPlan: true,
},
{
Config: testAccComputeInstance_basic(instanceName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
"google_compute_instance.foobar", &instance),
testAccCheckComputeInstanceHasStatusRunning(&instance),
),
},
},
})
}

func testAccCheckComputeInstanceUpdateMachineType(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -1948,6 +1984,32 @@ func testAccCheckComputeInstanceHasStatusTerminated(instance *compute.Instance)
}
}

func testAccCheckComputeInstanceStop(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 = computeOperationWait(config.clientCompute, op, config.Project, "Waiting on stop")
if err != nil {
return fmt.Errorf("Could not stop instance: %s", err)
}

norbjd marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
}

func testAccComputeInstance_basic(instance string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
Expand Down