package google import ( "fmt" "strings" "testing" "code.google.com/p/google-api-go-client/compute/v1" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) func TestAccComputeInstance_basic(t *testing.T) { var instance compute.Instance resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckComputeInstanceDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: testAccComputeInstance_basic, Check: resource.ComposeTestCheckFunc( testAccCheckComputeInstanceExists( "google_compute_instance.foobar", &instance), testAccCheckComputeInstanceTag(&instance, "foo"), testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"), testAccCheckComputeInstanceDisk(&instance, "terraform-test", true, true), ), }, }, }) } func TestAccComputeInstance_IP(t *testing.T) { var instance compute.Instance resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckComputeInstanceDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: testAccComputeInstance_ip, Check: resource.ComposeTestCheckFunc( testAccCheckComputeInstanceExists( "google_compute_instance.foobar", &instance), testAccCheckComputeInstanceNetwork(&instance), ), }, }, }) } //!NB requires that disk with name terraform-test-disk is present in gce, //if created as dependency then it tries to remove it while it is still attached //to instance and that fails with an error func TestAccComputeInstance_disks(t *testing.T) { var instance compute.Instance resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckComputeInstanceDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: testAccComputeInstance_disks, Check: resource.ComposeTestCheckFunc( testAccCheckComputeInstanceDisk(&instance, "terraform-test", true, true), testAccCheckComputeInstanceDisk(&instance, "terraform-test-disk", false, false), ), }, }, }) } func TestAccComputeInstance_update(t *testing.T) { var instance compute.Instance resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckComputeInstanceDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: testAccComputeInstance_basic, Check: resource.ComposeTestCheckFunc( testAccCheckComputeInstanceExists( "google_compute_instance.foobar", &instance), ), }, resource.TestStep{ Config: testAccComputeInstance_update, Check: resource.ComposeTestCheckFunc( testAccCheckComputeInstanceExists( "google_compute_instance.foobar", &instance), testAccCheckComputeInstanceMetadata( &instance, "bar", "baz"), testAccCheckComputeInstanceTag(&instance, "baz"), ), }, }, }) } func testAccCheckComputeInstanceDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) for _, rs := range s.RootModule().Resources { if rs.Type != "google_compute_instance" { continue } _, err := config.clientCompute.Instances.Get( config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() if err == nil { return fmt.Errorf("Instance still exists") } } return nil } func testAccCheckComputeInstanceExists(n string, instance *compute.Instance) 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) found, err := config.clientCompute.Instances.Get( config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() if err != nil { return err } if found.Name != rs.Primary.ID { return fmt.Errorf("Instance not found") } *instance = *found return nil } } func testAccCheckComputeInstanceMetadata( instance *compute.Instance, k string, v string) resource.TestCheckFunc { return func(s *terraform.State) error { if instance.Metadata == nil { return fmt.Errorf("no metadata") } for _, item := range instance.Metadata.Items { if k != item.Key { continue } if v == item.Value { return nil } return fmt.Errorf("bad value for %s: %s", k, item.Value) } return fmt.Errorf("metadata not found: %s", k) } } func testAccCheckComputeInstanceNetwork(instance *compute.Instance) resource.TestCheckFunc { return func(s *terraform.State) error { for _, i := range instance.NetworkInterfaces { for _, c := range i.AccessConfigs { if c.NatIP == "" { return fmt.Errorf("no NAT IP") } } } return nil } } func testAccCheckComputeInstanceDisk(instance *compute.Instance, source string, delete bool, boot bool) resource.TestCheckFunc { return func(s *terraform.State) error { if instance.Disks == nil { return fmt.Errorf("no disks") } for _, disk := range instance.Disks { if strings.LastIndex(disk.Source, "/"+source) == (len(disk.Source)-len(source)-1) && disk.AutoDelete == delete && disk.Boot == boot { return nil } } return fmt.Errorf("Disk not found: %s", source) } } func testAccCheckComputeInstanceTag(instance *compute.Instance, n string) resource.TestCheckFunc { return func(s *terraform.State) error { if instance.Tags == nil { return fmt.Errorf("no tags") } for _, k := range instance.Tags.Items { if k == n { return nil } } return fmt.Errorf("tag not found: %s", n) } } const testAccComputeInstance_basic = ` resource "google_compute_instance" "foobar" { name = "terraform-test" machine_type = "n1-standard-1" zone = "us-central1-a" can_ip_forward = false tags = ["foo", "bar"] disk { image = "debian-7-wheezy-v20140814" } network { source = "default" } metadata { foo = "bar" } }` const testAccComputeInstance_update = ` resource "google_compute_instance" "foobar" { name = "terraform-test" machine_type = "n1-standard-1" zone = "us-central1-a" tags = ["baz"] disk { image = "debian-7-wheezy-v20140814" } network { source = "default" } metadata { bar = "baz" } }` const testAccComputeInstance_ip = ` resource "google_compute_address" "foo" { name = "foo" } resource "google_compute_instance" "foobar" { name = "terraform-test" machine_type = "n1-standard-1" zone = "us-central1-a" tags = ["foo", "bar"] disk { image = "debian-7-wheezy-v20140814" } network { source = "default" address = "${google_compute_address.foo.address}" } metadata { foo = "bar" } }` const testAccComputeInstance_disks = ` resource "google_compute_instance" "foobar" { name = "terraform-test" machine_type = "n1-standard-1" zone = "us-central1-a" disk { image = "debian-7-wheezy-v20140814" } disk { disk = "terraform-test-disk" auto_delete = false type = "pd-ssd" } network { source = "default" } metadata { foo = "bar" } }`