github.com/adamar/terraform@v0.2.2-0.20141016210445-2e703afdad0e/builtin/providers/google/resource_compute_instance_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"code.google.com/p/google-api-go-client/compute/v1"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccComputeInstance_basic(t *testing.T) {
    14  	var instance compute.Instance
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckComputeInstanceDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccComputeInstance_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckComputeInstanceExists(
    25  						"google_compute_instance.foobar", &instance),
    26  					testAccCheckComputeInstanceTag(&instance, "foo"),
    27  					testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
    28  					testAccCheckComputeInstanceDisk(&instance, "terraform-test", true, true),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func TestAccComputeInstance_IP(t *testing.T) {
    36  	var instance compute.Instance
    37  
    38  	resource.Test(t, resource.TestCase{
    39  		PreCheck:     func() { testAccPreCheck(t) },
    40  		Providers:    testAccProviders,
    41  		CheckDestroy: testAccCheckComputeInstanceDestroy,
    42  		Steps: []resource.TestStep{
    43  			resource.TestStep{
    44  				Config: testAccComputeInstance_ip,
    45  				Check: resource.ComposeTestCheckFunc(
    46  					testAccCheckComputeInstanceExists(
    47  						"google_compute_instance.foobar", &instance),
    48  					testAccCheckComputeInstanceNetwork(&instance),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  //!NB requires that disk with name terraform-test-disk is present in gce,
    56  //if created as dependency then it tries to remove it while it is still attached
    57  //to instance and that fails with an error
    58  func TestAccComputeInstance_disks(t *testing.T) {
    59  	var instance compute.Instance
    60  
    61  	resource.Test(t, resource.TestCase{
    62  		PreCheck:     func() { testAccPreCheck(t) },
    63  		Providers:    testAccProviders,
    64  		CheckDestroy: testAccCheckComputeInstanceDestroy,
    65  		Steps: []resource.TestStep{
    66  			resource.TestStep{
    67  				Config: testAccComputeInstance_disks,
    68  				Check: resource.ComposeTestCheckFunc(
    69  					testAccCheckComputeInstanceDisk(&instance, "terraform-test", true, true),
    70  					testAccCheckComputeInstanceDisk(&instance, "terraform-test-disk", false, false),
    71  				),
    72  			},
    73  		},
    74  	})
    75  }
    76  
    77  func TestAccComputeInstance_update(t *testing.T) {
    78  	var instance compute.Instance
    79  
    80  	resource.Test(t, resource.TestCase{
    81  		PreCheck:     func() { testAccPreCheck(t) },
    82  		Providers:    testAccProviders,
    83  		CheckDestroy: testAccCheckComputeInstanceDestroy,
    84  		Steps: []resource.TestStep{
    85  			resource.TestStep{
    86  				Config: testAccComputeInstance_basic,
    87  				Check: resource.ComposeTestCheckFunc(
    88  					testAccCheckComputeInstanceExists(
    89  						"google_compute_instance.foobar", &instance),
    90  				),
    91  			},
    92  			resource.TestStep{
    93  				Config: testAccComputeInstance_update,
    94  				Check: resource.ComposeTestCheckFunc(
    95  					testAccCheckComputeInstanceExists(
    96  						"google_compute_instance.foobar", &instance),
    97  					testAccCheckComputeInstanceMetadata(
    98  						&instance, "bar", "baz"),
    99  					testAccCheckComputeInstanceTag(&instance, "baz"),
   100  				),
   101  			},
   102  		},
   103  	})
   104  }
   105  
   106  func testAccCheckComputeInstanceDestroy(s *terraform.State) error {
   107  	config := testAccProvider.Meta().(*Config)
   108  
   109  	for _, rs := range s.RootModule().Resources {
   110  		if rs.Type != "google_compute_instance" {
   111  			continue
   112  		}
   113  
   114  		_, err := config.clientCompute.Instances.Get(
   115  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   116  		if err == nil {
   117  			return fmt.Errorf("Instance still exists")
   118  		}
   119  	}
   120  
   121  	return nil
   122  }
   123  
   124  func testAccCheckComputeInstanceExists(n string, instance *compute.Instance) resource.TestCheckFunc {
   125  	return func(s *terraform.State) error {
   126  		rs, ok := s.RootModule().Resources[n]
   127  		if !ok {
   128  			return fmt.Errorf("Not found: %s", n)
   129  		}
   130  
   131  		if rs.Primary.ID == "" {
   132  			return fmt.Errorf("No ID is set")
   133  		}
   134  
   135  		config := testAccProvider.Meta().(*Config)
   136  
   137  		found, err := config.clientCompute.Instances.Get(
   138  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   139  		if err != nil {
   140  			return err
   141  		}
   142  
   143  		if found.Name != rs.Primary.ID {
   144  			return fmt.Errorf("Instance not found")
   145  		}
   146  
   147  		*instance = *found
   148  
   149  		return nil
   150  	}
   151  }
   152  
   153  func testAccCheckComputeInstanceMetadata(
   154  	instance *compute.Instance,
   155  	k string, v string) resource.TestCheckFunc {
   156  	return func(s *terraform.State) error {
   157  		if instance.Metadata == nil {
   158  			return fmt.Errorf("no metadata")
   159  		}
   160  
   161  		for _, item := range instance.Metadata.Items {
   162  			if k != item.Key {
   163  				continue
   164  			}
   165  
   166  			if v == item.Value {
   167  				return nil
   168  			}
   169  
   170  			return fmt.Errorf("bad value for %s: %s", k, item.Value)
   171  		}
   172  
   173  		return fmt.Errorf("metadata not found: %s", k)
   174  	}
   175  }
   176  
   177  func testAccCheckComputeInstanceNetwork(instance *compute.Instance) resource.TestCheckFunc {
   178  	return func(s *terraform.State) error {
   179  		for _, i := range instance.NetworkInterfaces {
   180  			for _, c := range i.AccessConfigs {
   181  				if c.NatIP == "" {
   182  					return fmt.Errorf("no NAT IP")
   183  				}
   184  			}
   185  		}
   186  
   187  		return nil
   188  	}
   189  }
   190  
   191  func testAccCheckComputeInstanceDisk(instance *compute.Instance, source string, delete bool, boot bool) resource.TestCheckFunc {
   192  	return func(s *terraform.State) error {
   193  		if instance.Disks == nil {
   194  			return fmt.Errorf("no disks")
   195  		}
   196  
   197  		for _, disk := range instance.Disks {
   198  			if strings.LastIndex(disk.Source, "/"+source) == (len(disk.Source)-len(source)-1) && disk.AutoDelete == delete && disk.Boot == boot {
   199  				return nil
   200  			}
   201  		}
   202  
   203  		return fmt.Errorf("Disk not found: %s", source)
   204  	}
   205  }
   206  
   207  func testAccCheckComputeInstanceTag(instance *compute.Instance, n string) resource.TestCheckFunc {
   208  	return func(s *terraform.State) error {
   209  		if instance.Tags == nil {
   210  			return fmt.Errorf("no tags")
   211  		}
   212  
   213  		for _, k := range instance.Tags.Items {
   214  			if k == n {
   215  				return nil
   216  			}
   217  		}
   218  
   219  		return fmt.Errorf("tag not found: %s", n)
   220  	}
   221  }
   222  
   223  const testAccComputeInstance_basic = `
   224  resource "google_compute_instance" "foobar" {
   225  	name = "terraform-test"
   226  	machine_type = "n1-standard-1"
   227  	zone = "us-central1-a"
   228  	can_ip_forward = false
   229  	tags = ["foo", "bar"]
   230  
   231  	disk {
   232  		image = "debian-7-wheezy-v20140814"
   233  	}
   234  
   235  	network {
   236  		source = "default"
   237  	}
   238  
   239  	metadata {
   240  		foo = "bar"
   241  	}
   242  }`
   243  
   244  const testAccComputeInstance_update = `
   245  resource "google_compute_instance" "foobar" {
   246  	name = "terraform-test"
   247  	machine_type = "n1-standard-1"
   248  	zone = "us-central1-a"
   249  	tags = ["baz"]
   250  
   251  	disk {
   252  		image = "debian-7-wheezy-v20140814"
   253  	}
   254  
   255  	network {
   256  		source = "default"
   257  	}
   258  
   259  	metadata {
   260  		bar = "baz"
   261  	}
   262  }`
   263  
   264  const testAccComputeInstance_ip = `
   265  resource "google_compute_address" "foo" {
   266  	name = "foo"
   267  }
   268  
   269  resource "google_compute_instance" "foobar" {
   270  	name = "terraform-test"
   271  	machine_type = "n1-standard-1"
   272  	zone = "us-central1-a"
   273  	tags = ["foo", "bar"]
   274  
   275  	disk {
   276  		image = "debian-7-wheezy-v20140814"
   277  	}
   278  
   279  	network {
   280  		source = "default"
   281  		address = "${google_compute_address.foo.address}"
   282  	}
   283  
   284  	metadata {
   285  		foo = "bar"
   286  	}
   287  }`
   288  
   289  const testAccComputeInstance_disks = `
   290  resource "google_compute_instance" "foobar" {
   291  	name = "terraform-test"
   292  	machine_type = "n1-standard-1"
   293  	zone = "us-central1-a"
   294  
   295  	disk {
   296  		image = "debian-7-wheezy-v20140814"
   297  	}
   298  
   299  	disk {
   300  		disk = "terraform-test-disk"
   301  		auto_delete = false
   302  		type = "pd-ssd"
   303  	}
   304  
   305  	network {
   306  		source = "default"
   307  	}
   308  
   309  	metadata {
   310  		foo = "bar"
   311  	}
   312  }`