github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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_basic1(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_local_ssd(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_local_ssd,
   153  				Check: resource.ComposeTestCheckFunc(
   154  					testAccCheckComputeInstanceExists(
   155  						"google_compute_instance.local-ssd", &instance),
   156  					testAccCheckComputeInstanceDisk(&instance, "terraform-test", true, true),
   157  				),
   158  			},
   159  		},
   160  	})
   161  }
   162  
   163  func TestAccComputeInstance_update_deprecated_network(t *testing.T) {
   164  	var instance compute.Instance
   165  
   166  	resource.Test(t, resource.TestCase{
   167  		PreCheck:     func() { testAccPreCheck(t) },
   168  		Providers:    testAccProviders,
   169  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   170  		Steps: []resource.TestStep{
   171  			resource.TestStep{
   172  				Config: testAccComputeInstance_basic_deprecated_network,
   173  				Check: resource.ComposeTestCheckFunc(
   174  					testAccCheckComputeInstanceExists(
   175  						"google_compute_instance.foobar", &instance),
   176  				),
   177  			},
   178  			resource.TestStep{
   179  				Config: testAccComputeInstance_update_deprecated_network,
   180  				Check: resource.ComposeTestCheckFunc(
   181  					testAccCheckComputeInstanceExists(
   182  						"google_compute_instance.foobar", &instance),
   183  					testAccCheckComputeInstanceMetadata(
   184  						&instance, "bar", "baz"),
   185  					testAccCheckComputeInstanceTag(&instance, "baz"),
   186  				),
   187  			},
   188  		},
   189  	})
   190  }
   191  
   192  func TestAccComputeInstance_forceNewAndChangeMetadata(t *testing.T) {
   193  	var instance compute.Instance
   194  
   195  	resource.Test(t, resource.TestCase{
   196  		PreCheck:     func() { testAccPreCheck(t) },
   197  		Providers:    testAccProviders,
   198  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   199  		Steps: []resource.TestStep{
   200  			resource.TestStep{
   201  				Config: testAccComputeInstance_basic,
   202  				Check: resource.ComposeTestCheckFunc(
   203  					testAccCheckComputeInstanceExists(
   204  						"google_compute_instance.foobar", &instance),
   205  				),
   206  			},
   207  			resource.TestStep{
   208  				Config: testAccComputeInstance_forceNewAndChangeMetadata,
   209  				Check: resource.ComposeTestCheckFunc(
   210  					testAccCheckComputeInstanceExists(
   211  						"google_compute_instance.foobar", &instance),
   212  					testAccCheckComputeInstanceMetadata(
   213  						&instance, "qux", "true"),
   214  				),
   215  			},
   216  		},
   217  	})
   218  }
   219  
   220  func TestAccComputeInstance_update(t *testing.T) {
   221  	var instance compute.Instance
   222  
   223  	resource.Test(t, resource.TestCase{
   224  		PreCheck:     func() { testAccPreCheck(t) },
   225  		Providers:    testAccProviders,
   226  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   227  		Steps: []resource.TestStep{
   228  			resource.TestStep{
   229  				Config: testAccComputeInstance_basic,
   230  				Check: resource.ComposeTestCheckFunc(
   231  					testAccCheckComputeInstanceExists(
   232  						"google_compute_instance.foobar", &instance),
   233  				),
   234  			},
   235  			resource.TestStep{
   236  				Config: testAccComputeInstance_update,
   237  				Check: resource.ComposeTestCheckFunc(
   238  					testAccCheckComputeInstanceExists(
   239  						"google_compute_instance.foobar", &instance),
   240  					testAccCheckComputeInstanceMetadata(
   241  						&instance, "bar", "baz"),
   242  					testAccCheckComputeInstanceTag(&instance, "baz"),
   243  					testAccCheckComputeInstanceAccessConfig(&instance),
   244  				),
   245  			},
   246  		},
   247  	})
   248  }
   249  
   250  func TestAccComputeInstance_service_account(t *testing.T) {
   251  	var instance compute.Instance
   252  
   253  	resource.Test(t, resource.TestCase{
   254  		PreCheck:     func() { testAccPreCheck(t) },
   255  		Providers:    testAccProviders,
   256  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   257  		Steps: []resource.TestStep{
   258  			resource.TestStep{
   259  				Config: testAccComputeInstance_service_account,
   260  				Check: resource.ComposeTestCheckFunc(
   261  					testAccCheckComputeInstanceExists(
   262  						"google_compute_instance.foobar", &instance),
   263  					testAccCheckComputeInstanceServiceAccount(&instance,
   264  						"https://www.googleapis.com/auth/compute.readonly"),
   265  					testAccCheckComputeInstanceServiceAccount(&instance,
   266  						"https://www.googleapis.com/auth/devstorage.read_only"),
   267  					testAccCheckComputeInstanceServiceAccount(&instance,
   268  						"https://www.googleapis.com/auth/userinfo.email"),
   269  				),
   270  			},
   271  		},
   272  	})
   273  }
   274  
   275  func TestAccComputeInstance_scheduling(t *testing.T) {
   276  	var instance compute.Instance
   277  
   278  	resource.Test(t, resource.TestCase{
   279  		PreCheck:     func() { testAccPreCheck(t) },
   280  		Providers:    testAccProviders,
   281  		CheckDestroy: testAccCheckComputeInstanceDestroy,
   282  		Steps: []resource.TestStep{
   283  			resource.TestStep{
   284  				Config: testAccComputeInstance_scheduling,
   285  				Check: resource.ComposeTestCheckFunc(
   286  					testAccCheckComputeInstanceExists(
   287  						"google_compute_instance.foobar", &instance),
   288  				),
   289  			},
   290  		},
   291  	})
   292  }
   293  
   294  func testAccCheckComputeInstanceDestroy(s *terraform.State) error {
   295  	config := testAccProvider.Meta().(*Config)
   296  
   297  	for _, rs := range s.RootModule().Resources {
   298  		if rs.Type != "google_compute_instance" {
   299  			continue
   300  		}
   301  
   302  		_, err := config.clientCompute.Instances.Get(
   303  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   304  		if err == nil {
   305  			return fmt.Errorf("Instance still exists")
   306  		}
   307  	}
   308  
   309  	return nil
   310  }
   311  
   312  func testAccCheckComputeInstanceExists(n string, instance *compute.Instance) resource.TestCheckFunc {
   313  	return func(s *terraform.State) error {
   314  		rs, ok := s.RootModule().Resources[n]
   315  		if !ok {
   316  			return fmt.Errorf("Not found: %s", n)
   317  		}
   318  
   319  		if rs.Primary.ID == "" {
   320  			return fmt.Errorf("No ID is set")
   321  		}
   322  
   323  		config := testAccProvider.Meta().(*Config)
   324  
   325  		found, err := config.clientCompute.Instances.Get(
   326  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   327  		if err != nil {
   328  			return err
   329  		}
   330  
   331  		if found.Name != rs.Primary.ID {
   332  			return fmt.Errorf("Instance not found")
   333  		}
   334  
   335  		*instance = *found
   336  
   337  		return nil
   338  	}
   339  }
   340  
   341  func testAccCheckComputeInstanceMetadata(
   342  	instance *compute.Instance,
   343  	k string, v string) resource.TestCheckFunc {
   344  	return func(s *terraform.State) error {
   345  		if instance.Metadata == nil {
   346  			return fmt.Errorf("no metadata")
   347  		}
   348  
   349  		for _, item := range instance.Metadata.Items {
   350  			if k != item.Key {
   351  				continue
   352  			}
   353  
   354  			if item.Value != nil && v == *item.Value {
   355  				return nil
   356  			}
   357  
   358  			return fmt.Errorf("bad value for %s: %s", k, *item.Value)
   359  		}
   360  
   361  		return fmt.Errorf("metadata not found: %s", k)
   362  	}
   363  }
   364  
   365  func testAccCheckComputeInstanceAccessConfig(instance *compute.Instance) resource.TestCheckFunc {
   366  	return func(s *terraform.State) error {
   367  		for _, i := range instance.NetworkInterfaces {
   368  			if len(i.AccessConfigs) == 0 {
   369  				return fmt.Errorf("no access_config")
   370  			}
   371  		}
   372  
   373  		return nil
   374  	}
   375  }
   376  
   377  func testAccCheckComputeInstanceAccessConfigHasIP(instance *compute.Instance) resource.TestCheckFunc {
   378  	return func(s *terraform.State) error {
   379  		for _, i := range instance.NetworkInterfaces {
   380  			for _, c := range i.AccessConfigs {
   381  				if c.NatIP == "" {
   382  					return fmt.Errorf("no NAT IP")
   383  				}
   384  			}
   385  		}
   386  
   387  		return nil
   388  	}
   389  }
   390  
   391  func testAccCheckComputeInstanceDisk(instance *compute.Instance, source string, delete bool, boot bool) resource.TestCheckFunc {
   392  	return func(s *terraform.State) error {
   393  		if instance.Disks == nil {
   394  			return fmt.Errorf("no disks")
   395  		}
   396  
   397  		for _, disk := range instance.Disks {
   398  			if strings.LastIndex(disk.Source, "/"+source) == len(disk.Source)-len(source)-1 && disk.AutoDelete == delete && disk.Boot == boot {
   399  				return nil
   400  			}
   401  		}
   402  
   403  		return fmt.Errorf("Disk not found: %s", source)
   404  	}
   405  }
   406  
   407  func testAccCheckComputeInstanceTag(instance *compute.Instance, n string) resource.TestCheckFunc {
   408  	return func(s *terraform.State) error {
   409  		if instance.Tags == nil {
   410  			return fmt.Errorf("no tags")
   411  		}
   412  
   413  		for _, k := range instance.Tags.Items {
   414  			if k == n {
   415  				return nil
   416  			}
   417  		}
   418  
   419  		return fmt.Errorf("tag not found: %s", n)
   420  	}
   421  }
   422  
   423  func testAccCheckComputeInstanceServiceAccount(instance *compute.Instance, scope string) resource.TestCheckFunc {
   424  	return func(s *terraform.State) error {
   425  		if count := len(instance.ServiceAccounts); count != 1 {
   426  			return fmt.Errorf("Wrong number of ServiceAccounts: expected 1, got %d", count)
   427  		}
   428  
   429  		for _, val := range instance.ServiceAccounts[0].Scopes {
   430  			if val == scope {
   431  				return nil
   432  			}
   433  		}
   434  
   435  		return fmt.Errorf("Scope not found: %s", scope)
   436  	}
   437  }
   438  
   439  const testAccComputeInstance_basic_deprecated_network = `
   440  resource "google_compute_instance" "foobar" {
   441  	name = "terraform-test"
   442  	machine_type = "n1-standard-1"
   443  	zone = "us-central1-a"
   444  	can_ip_forward = false
   445  	tags = ["foo", "bar"]
   446  
   447  	disk {
   448  		image = "debian-7-wheezy-v20140814"
   449  	}
   450  
   451  	network {
   452  		source = "default"
   453  	}
   454  
   455  	metadata {
   456  		foo = "bar"
   457  	}
   458  }`
   459  
   460  const testAccComputeInstance_update_deprecated_network = `
   461  resource "google_compute_instance" "foobar" {
   462  	name = "terraform-test"
   463  	machine_type = "n1-standard-1"
   464  	zone = "us-central1-a"
   465  	tags = ["baz"]
   466  
   467  	disk {
   468  		image = "debian-7-wheezy-v20140814"
   469  	}
   470  
   471  	network {
   472  		source = "default"
   473  	}
   474  
   475  	metadata {
   476  		bar = "baz"
   477  	}
   478  }`
   479  
   480  const testAccComputeInstance_basic = `
   481  resource "google_compute_instance" "foobar" {
   482  	name = "terraform-test"
   483  	machine_type = "n1-standard-1"
   484  	zone = "us-central1-a"
   485  	can_ip_forward = false
   486  	tags = ["foo", "bar"]
   487  
   488  	disk {
   489  		image = "debian-7-wheezy-v20140814"
   490  	}
   491  
   492  	network_interface {
   493  		network = "default"
   494  	}
   495  
   496  	metadata {
   497  		foo = "bar"
   498  		baz = "qux"
   499  	}
   500  
   501  	metadata_startup_script = "echo Hello"
   502  }`
   503  
   504  const testAccComputeInstance_basic2 = `
   505  resource "google_compute_instance" "foobar" {
   506  	name = "terraform-test"
   507  	machine_type = "n1-standard-1"
   508  	zone = "us-central1-a"
   509  	can_ip_forward = false
   510  	tags = ["foo", "bar"]
   511  
   512  	disk {
   513  		image = "debian-cloud/debian-7-wheezy-v20140814"
   514  	}
   515  
   516  	network_interface {
   517  		network = "default"
   518  	}
   519  
   520  
   521  	metadata {
   522  		foo = "bar"
   523  	}
   524  }`
   525  
   526  const testAccComputeInstance_basic3 = `
   527  resource "google_compute_instance" "foobar" {
   528  	name = "terraform-test"
   529  	machine_type = "n1-standard-1"
   530  	zone = "us-central1-a"
   531  	can_ip_forward = false
   532  	tags = ["foo", "bar"]
   533  
   534  	disk {
   535  		image = "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140814"
   536  	}
   537  
   538  	network_interface {
   539  		network = "default"
   540  	}
   541  
   542  	metadata {
   543  		foo = "bar"
   544  	}
   545  }`
   546  
   547  // Update zone to ForceNew, and change metadata k/v entirely
   548  // Generates diff mismatch
   549  const testAccComputeInstance_forceNewAndChangeMetadata = `
   550  resource "google_compute_instance" "foobar" {
   551  	name = "terraform-test"
   552  	machine_type = "n1-standard-1"
   553  	zone = "us-central1-a"
   554  	zone = "us-central1-b"
   555  	tags = ["baz"]
   556  
   557  	disk {
   558  		image = "debian-7-wheezy-v20140814"
   559  	}
   560  
   561  	network_interface {
   562  		network = "default"
   563  		access_config { }
   564  	}
   565  
   566  	metadata {
   567  		qux = "true"
   568  	}
   569  }`
   570  
   571  // Update metadata, tags, and network_interface
   572  const testAccComputeInstance_update = `
   573  resource "google_compute_instance" "foobar" {
   574  	name = "terraform-test"
   575  	machine_type = "n1-standard-1"
   576  	zone = "us-central1-a"
   577  	tags = ["baz"]
   578  
   579  	disk {
   580  		image = "debian-7-wheezy-v20140814"
   581  	}
   582  
   583  	network_interface {
   584  		network = "default"
   585  		access_config { }
   586  	}
   587  
   588  	metadata {
   589  		bar = "baz"
   590  	}
   591  }`
   592  
   593  const testAccComputeInstance_ip = `
   594  resource "google_compute_address" "foo" {
   595  	name = "foo"
   596  }
   597  
   598  resource "google_compute_instance" "foobar" {
   599  	name = "terraform-test"
   600  	machine_type = "n1-standard-1"
   601  	zone = "us-central1-a"
   602  	tags = ["foo", "bar"]
   603  
   604  	disk {
   605  		image = "debian-7-wheezy-v20140814"
   606  	}
   607  
   608  	network_interface {
   609  		network = "default"
   610  		access_config {
   611  			nat_ip = "${google_compute_address.foo.address}"
   612  		}
   613  	}
   614  
   615  	metadata {
   616  		foo = "bar"
   617  	}
   618  }`
   619  
   620  const testAccComputeInstance_disks = `
   621  resource "google_compute_disk" "foobar" {
   622  	name = "terraform-test-disk"
   623  	size = 10
   624  	type = "pd-ssd"
   625  	zone = "us-central1-a"
   626  }
   627  
   628  resource "google_compute_instance" "foobar" {
   629  	name = "terraform-test"
   630  	machine_type = "n1-standard-1"
   631  	zone = "us-central1-a"
   632  
   633  	disk {
   634  		image = "debian-7-wheezy-v20140814"
   635  	}
   636  
   637  	disk {
   638  		disk = "${google_compute_disk.foobar.name}"
   639  		auto_delete = false
   640  	}
   641  
   642  	network_interface {
   643  		network = "default"
   644  	}
   645  
   646  	metadata {
   647  		foo = "bar"
   648  	}
   649  }`
   650  
   651  const testAccComputeInstance_local_ssd = `
   652  resource "google_compute_instance" "local-ssd" {
   653  	name = "terraform-test"
   654  	machine_type = "n1-standard-1"
   655  	zone = "us-central1-a"
   656  
   657  	disk {
   658  		image = "debian-7-wheezy-v20140814"
   659  	}
   660  
   661  	disk {
   662          type = "local-ssd"
   663          scratch = true
   664  	}
   665  
   666  	network_interface {
   667  		network = "default"
   668  	}
   669  
   670  }`
   671  
   672  const testAccComputeInstance_service_account = `
   673  resource "google_compute_instance" "foobar" {
   674  	name = "terraform-test"
   675  	machine_type = "n1-standard-1"
   676  	zone = "us-central1-a"
   677  
   678  	disk {
   679  		image = "debian-7-wheezy-v20140814"
   680  	}
   681  
   682  	network_interface {
   683  		network = "default"
   684  	}
   685  
   686  	service_account {
   687  		scopes = [
   688  			"userinfo-email",
   689  			"compute-ro",
   690  			"storage-ro",
   691  		]
   692  	}
   693  }`
   694  
   695  const testAccComputeInstance_scheduling = `
   696  resource "google_compute_instance" "foobar" {
   697  	name = "terraform-test"
   698  	machine_type = "n1-standard-1"
   699  	zone = "us-central1-a"
   700  
   701  	disk {
   702  		image = "debian-7-wheezy-v20140814"
   703  	}
   704  
   705  	network_interface {
   706  		network = "default"
   707  	}
   708  
   709  	scheduling {
   710  	}
   711  }`