github.com/nicgrayson/terraform@v0.4.3-0.20150415203910-c4de50829380/builtin/providers/google/resource_compute_instance_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"google.golang.org/api/compute/v1"
    11  )
    12  
    13  func TestAccComputeInstance_basic_deprecated_network(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_deprecated_network,
    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_basic(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_basic,
    45  				Check: resource.ComposeTestCheckFunc(
    46  					testAccCheckComputeInstanceExists(
    47  						"google_compute_instance.foobar", &instance),
    48  					testAccCheckComputeInstanceTag(&instance, "foo"),
    49  					testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
    50  					testAccCheckComputeInstanceMetadata(&instance, "baz", "qux"),
    51  					testAccCheckComputeInstanceDisk(&instance, "terraform-test", true, true),
    52  				),
    53  			},
    54  		},
    55  	})
    56  }
    57  
    58  func TestAccComputeInstance_basic2(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_basic2,
    68  				Check: resource.ComposeTestCheckFunc(
    69  					testAccCheckComputeInstanceExists(
    70  						"google_compute_instance.foobar", &instance),
    71  					testAccCheckComputeInstanceTag(&instance, "foo"),
    72  					testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
    73  					testAccCheckComputeInstanceDisk(&instance, "terraform-test", true, true),
    74  				),
    75  			},
    76  		},
    77  	})
    78  }
    79  
    80  func TestAccComputeInstance_basic3(t *testing.T) {
    81  	var instance compute.Instance
    82  
    83  	resource.Test(t, resource.TestCase{
    84  		PreCheck:     func() { testAccPreCheck(t) },
    85  		Providers:    testAccProviders,
    86  		CheckDestroy: testAccCheckComputeInstanceDestroy,
    87  		Steps: []resource.TestStep{
    88  			resource.TestStep{
    89  				Config: testAccComputeInstance_basic3,
    90  				Check: resource.ComposeTestCheckFunc(
    91  					testAccCheckComputeInstanceExists(
    92  						"google_compute_instance.foobar", &instance),
    93  					testAccCheckComputeInstanceTag(&instance, "foo"),
    94  					testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
    95  					testAccCheckComputeInstanceDisk(&instance, "terraform-test", true, true),
    96  				),
    97  			},
    98  		},
    99  	})
   100  }
   101  
   102  func TestAccComputeInstance_IP(t *testing.T) {
   103  	var instance compute.Instance
   104  
   105  	resource.Test(t, resource.TestCase{
   106  		PreCheck:     func() { testAccPreCheck(t) },
   107  		Providers:    testAccProviders,
   108  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   109  		Steps: []resource.TestStep{
   110  			resource.TestStep{
   111  				Config: testAccComputeInstance_ip,
   112  				Check: resource.ComposeTestCheckFunc(
   113  					testAccCheckComputeInstanceExists(
   114  						"google_compute_instance.foobar", &instance),
   115  					testAccCheckComputeInstanceAccessConfigHasIP(&instance),
   116  				),
   117  			},
   118  		},
   119  	})
   120  }
   121  
   122  func TestAccComputeInstance_disks(t *testing.T) {
   123  	var instance compute.Instance
   124  
   125  	resource.Test(t, resource.TestCase{
   126  		PreCheck:     func() { testAccPreCheck(t) },
   127  		Providers:    testAccProviders,
   128  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   129  		Steps: []resource.TestStep{
   130  			resource.TestStep{
   131  				Config: testAccComputeInstance_disks,
   132  				Check: resource.ComposeTestCheckFunc(
   133  					testAccCheckComputeInstanceExists(
   134  						"google_compute_instance.foobar", &instance),
   135  					testAccCheckComputeInstanceDisk(&instance, "terraform-test", true, true),
   136  					testAccCheckComputeInstanceDisk(&instance, "terraform-test-disk", false, false),
   137  				),
   138  			},
   139  		},
   140  	})
   141  }
   142  
   143  func TestAccComputeInstance_update_deprecated_network(t *testing.T) {
   144  	var instance compute.Instance
   145  
   146  	resource.Test(t, resource.TestCase{
   147  		PreCheck:     func() { testAccPreCheck(t) },
   148  		Providers:    testAccProviders,
   149  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   150  		Steps: []resource.TestStep{
   151  			resource.TestStep{
   152  				Config: testAccComputeInstance_basic_deprecated_network,
   153  				Check: resource.ComposeTestCheckFunc(
   154  					testAccCheckComputeInstanceExists(
   155  						"google_compute_instance.foobar", &instance),
   156  				),
   157  			},
   158  			resource.TestStep{
   159  				Config: testAccComputeInstance_update_deprecated_network,
   160  				Check: resource.ComposeTestCheckFunc(
   161  					testAccCheckComputeInstanceExists(
   162  						"google_compute_instance.foobar", &instance),
   163  					testAccCheckComputeInstanceMetadata(
   164  						&instance, "bar", "baz"),
   165  					testAccCheckComputeInstanceTag(&instance, "baz"),
   166  				),
   167  			},
   168  		},
   169  	})
   170  }
   171  
   172  func TestAccComputeInstance_forceNewAndChangeMetadata(t *testing.T) {
   173  	var instance compute.Instance
   174  
   175  	resource.Test(t, resource.TestCase{
   176  		PreCheck:     func() { testAccPreCheck(t) },
   177  		Providers:    testAccProviders,
   178  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   179  		Steps: []resource.TestStep{
   180  			resource.TestStep{
   181  				Config: testAccComputeInstance_basic,
   182  				Check: resource.ComposeTestCheckFunc(
   183  					testAccCheckComputeInstanceExists(
   184  						"google_compute_instance.foobar", &instance),
   185  				),
   186  			},
   187  			resource.TestStep{
   188  				Config: testAccComputeInstance_forceNewAndChangeMetadata,
   189  				Check: resource.ComposeTestCheckFunc(
   190  					testAccCheckComputeInstanceExists(
   191  						"google_compute_instance.foobar", &instance),
   192  					testAccCheckComputeInstanceMetadata(
   193  						&instance, "qux", "true"),
   194  				),
   195  			},
   196  		},
   197  	})
   198  }
   199  
   200  func TestAccComputeInstance_update(t *testing.T) {
   201  	var instance compute.Instance
   202  
   203  	resource.Test(t, resource.TestCase{
   204  		PreCheck:     func() { testAccPreCheck(t) },
   205  		Providers:    testAccProviders,
   206  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   207  		Steps: []resource.TestStep{
   208  			resource.TestStep{
   209  				Config: testAccComputeInstance_basic,
   210  				Check: resource.ComposeTestCheckFunc(
   211  					testAccCheckComputeInstanceExists(
   212  						"google_compute_instance.foobar", &instance),
   213  				),
   214  			},
   215  			resource.TestStep{
   216  				Config: testAccComputeInstance_update,
   217  				Check: resource.ComposeTestCheckFunc(
   218  					testAccCheckComputeInstanceExists(
   219  						"google_compute_instance.foobar", &instance),
   220  					testAccCheckComputeInstanceMetadata(
   221  						&instance, "bar", "baz"),
   222  					testAccCheckComputeInstanceTag(&instance, "baz"),
   223  					testAccCheckComputeInstanceAccessConfig(&instance),
   224  				),
   225  			},
   226  		},
   227  	})
   228  }
   229  
   230  func testAccCheckComputeInstanceDestroy(s *terraform.State) error {
   231  	config := testAccProvider.Meta().(*Config)
   232  
   233  	for _, rs := range s.RootModule().Resources {
   234  		if rs.Type != "google_compute_instance" {
   235  			continue
   236  		}
   237  
   238  		_, err := config.clientCompute.Instances.Get(
   239  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   240  		if err == nil {
   241  			return fmt.Errorf("Instance still exists")
   242  		}
   243  	}
   244  
   245  	return nil
   246  }
   247  
   248  func testAccCheckComputeInstanceExists(n string, instance *compute.Instance) resource.TestCheckFunc {
   249  	return func(s *terraform.State) error {
   250  		rs, ok := s.RootModule().Resources[n]
   251  		if !ok {
   252  			return fmt.Errorf("Not found: %s", n)
   253  		}
   254  
   255  		if rs.Primary.ID == "" {
   256  			return fmt.Errorf("No ID is set")
   257  		}
   258  
   259  		config := testAccProvider.Meta().(*Config)
   260  
   261  		found, err := config.clientCompute.Instances.Get(
   262  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   263  		if err != nil {
   264  			return err
   265  		}
   266  
   267  		if found.Name != rs.Primary.ID {
   268  			return fmt.Errorf("Instance not found")
   269  		}
   270  
   271  		*instance = *found
   272  
   273  		return nil
   274  	}
   275  }
   276  
   277  func testAccCheckComputeInstanceMetadata(
   278  	instance *compute.Instance,
   279  	k string, v string) resource.TestCheckFunc {
   280  	return func(s *terraform.State) error {
   281  		if instance.Metadata == nil {
   282  			return fmt.Errorf("no metadata")
   283  		}
   284  
   285  		for _, item := range instance.Metadata.Items {
   286  			if k != item.Key {
   287  				continue
   288  			}
   289  
   290  			if v == item.Value {
   291  				return nil
   292  			}
   293  
   294  			return fmt.Errorf("bad value for %s: %s", k, item.Value)
   295  		}
   296  
   297  		return fmt.Errorf("metadata not found: %s", k)
   298  	}
   299  }
   300  
   301  func testAccCheckComputeInstanceAccessConfig(instance *compute.Instance) resource.TestCheckFunc {
   302  	return func(s *terraform.State) error {
   303  		for _, i := range instance.NetworkInterfaces {
   304  			if len(i.AccessConfigs) == 0 {
   305  				return fmt.Errorf("no access_config")
   306  			}
   307  		}
   308  
   309  		return nil
   310  	}
   311  }
   312  
   313  func testAccCheckComputeInstanceAccessConfigHasIP(instance *compute.Instance) resource.TestCheckFunc {
   314  	return func(s *terraform.State) error {
   315  		for _, i := range instance.NetworkInterfaces {
   316  			for _, c := range i.AccessConfigs {
   317  				if c.NatIP == "" {
   318  					return fmt.Errorf("no NAT IP")
   319  				}
   320  			}
   321  		}
   322  
   323  		return nil
   324  	}
   325  }
   326  
   327  func testAccCheckComputeInstanceDisk(instance *compute.Instance, source string, delete bool, boot bool) resource.TestCheckFunc {
   328  	return func(s *terraform.State) error {
   329  		if instance.Disks == nil {
   330  			return fmt.Errorf("no disks")
   331  		}
   332  
   333  		for _, disk := range instance.Disks {
   334  			if strings.LastIndex(disk.Source, "/"+source) == (len(disk.Source)-len(source)-1) && disk.AutoDelete == delete && disk.Boot == boot {
   335  				return nil
   336  			}
   337  		}
   338  
   339  		return fmt.Errorf("Disk not found: %s", source)
   340  	}
   341  }
   342  
   343  func testAccCheckComputeInstanceTag(instance *compute.Instance, n string) resource.TestCheckFunc {
   344  	return func(s *terraform.State) error {
   345  		if instance.Tags == nil {
   346  			return fmt.Errorf("no tags")
   347  		}
   348  
   349  		for _, k := range instance.Tags.Items {
   350  			if k == n {
   351  				return nil
   352  			}
   353  		}
   354  
   355  		return fmt.Errorf("tag not found: %s", n)
   356  	}
   357  }
   358  
   359  const testAccComputeInstance_basic_deprecated_network = `
   360  resource "google_compute_instance" "foobar" {
   361  	name = "terraform-test"
   362  	machine_type = "n1-standard-1"
   363  	zone = "us-central1-a"
   364  	can_ip_forward = false
   365  	tags = ["foo", "bar"]
   366  
   367  	disk {
   368  		image = "debian-7-wheezy-v20140814"
   369  	}
   370  
   371  	network {
   372  		source = "default"
   373  	}
   374  
   375  	metadata {
   376  		foo = "bar"
   377  	}
   378  }`
   379  
   380  const testAccComputeInstance_update_deprecated_network = `
   381  resource "google_compute_instance" "foobar" {
   382  	name = "terraform-test"
   383  	machine_type = "n1-standard-1"
   384  	zone = "us-central1-a"
   385  	tags = ["baz"]
   386  
   387  	disk {
   388  		image = "debian-7-wheezy-v20140814"
   389  	}
   390  
   391  	network {
   392  		source = "default"
   393  	}
   394  
   395  	metadata {
   396  		bar = "baz"
   397  	}
   398  }`
   399  
   400  const testAccComputeInstance_basic = `
   401  resource "google_compute_instance" "foobar" {
   402  	name = "terraform-test"
   403  	machine_type = "n1-standard-1"
   404  	zone = "us-central1-a"
   405  	can_ip_forward = false
   406  	tags = ["foo", "bar"]
   407  
   408  	disk {
   409  		image = "debian-7-wheezy-v20140814"
   410  	}
   411  
   412  	network_interface {
   413  		network = "default"
   414  	}
   415  
   416  	metadata {
   417  		foo = "bar"
   418  	}
   419  	metadata {
   420  		baz = "qux"
   421  	}
   422  }`
   423  
   424  const testAccComputeInstance_basic2 = `
   425  resource "google_compute_instance" "foobar" {
   426  	name = "terraform-test"
   427  	machine_type = "n1-standard-1"
   428  	zone = "us-central1-a"
   429  	can_ip_forward = false
   430  	tags = ["foo", "bar"]
   431  
   432  	disk {
   433  		image = "debian-cloud/debian-7-wheezy-v20140814"
   434  	}
   435  
   436  	network_interface {
   437  		network = "default"
   438  	}
   439  
   440  
   441  	metadata {
   442  		foo = "bar"
   443  	}
   444  }`
   445  
   446  const testAccComputeInstance_basic3 = `
   447  resource "google_compute_instance" "foobar" {
   448  	name = "terraform-test"
   449  	machine_type = "n1-standard-1"
   450  	zone = "us-central1-a"
   451  	can_ip_forward = false
   452  	tags = ["foo", "bar"]
   453  
   454  	disk {
   455  		image = "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140814"
   456  	}
   457  
   458  	network_interface {
   459  		network = "default"
   460  	}
   461  
   462  	metadata {
   463  		foo = "bar"
   464  	}
   465  }`
   466  
   467  // Update zone to ForceNew, and change metadata k/v entirely
   468  // Generates diff mismatch
   469  const testAccComputeInstance_forceNewAndChangeMetadata = `
   470  resource "google_compute_instance" "foobar" {
   471  	name = "terraform-test"
   472  	machine_type = "n1-standard-1"
   473  	zone = "us-central1-a"
   474  	zone = "us-central1-b"
   475  	tags = ["baz"]
   476  
   477  	disk {
   478  		image = "debian-7-wheezy-v20140814"
   479  	}
   480  
   481  	network_interface {
   482  		network = "default"
   483  		access_config { }
   484  	}
   485  
   486  	metadata {
   487  		qux = "true"
   488  	}
   489  }`
   490  
   491  // Update metadata, tags, and network_interface
   492  const testAccComputeInstance_update = `
   493  resource "google_compute_instance" "foobar" {
   494  	name = "terraform-test"
   495  	machine_type = "n1-standard-1"
   496  	zone = "us-central1-a"
   497  	tags = ["baz"]
   498  
   499  	disk {
   500  		image = "debian-7-wheezy-v20140814"
   501  	}
   502  
   503  	network_interface {
   504  		network = "default"
   505  		access_config { }
   506  	}
   507  
   508  	metadata {
   509  		bar = "baz"
   510  	}
   511  }`
   512  
   513  const testAccComputeInstance_ip = `
   514  resource "google_compute_address" "foo" {
   515  	name = "foo"
   516  }
   517  
   518  resource "google_compute_instance" "foobar" {
   519  	name = "terraform-test"
   520  	machine_type = "n1-standard-1"
   521  	zone = "us-central1-a"
   522  	tags = ["foo", "bar"]
   523  
   524  	disk {
   525  		image = "debian-7-wheezy-v20140814"
   526  	}
   527  
   528  	network_interface {
   529  		network = "default"
   530  		access_config {
   531  			nat_ip = "${google_compute_address.foo.address}"
   532  		}
   533  	}
   534  
   535  	metadata {
   536  		foo = "bar"
   537  	}
   538  }`
   539  
   540  const testAccComputeInstance_disks = `
   541  resource "google_compute_disk" "foobar" {
   542  	name = "terraform-test-disk"
   543  	size = 10
   544  	type = "pd-ssd"
   545  	zone = "us-central1-a"
   546  }
   547  
   548  resource "google_compute_instance" "foobar" {
   549  	name = "terraform-test"
   550  	machine_type = "n1-standard-1"
   551  	zone = "us-central1-a"
   552  
   553  	disk {
   554  		image = "debian-7-wheezy-v20140814"
   555  	}
   556  
   557  	disk {
   558  		disk = "${google_compute_disk.foobar.name}"
   559  		auto_delete = false
   560  	}
   561  
   562  	network_interface {
   563  		network = "default"
   564  	}
   565  
   566  	metadata {
   567  		foo = "bar"
   568  	}
   569  }`