github.com/jsoriano/terraform@v0.6.7-0.20151026070445-8b70867fdd95/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 testAccCheckComputeInstanceDestroy(s *terraform.State) error {
   276  	config := testAccProvider.Meta().(*Config)
   277  
   278  	for _, rs := range s.RootModule().Resources {
   279  		if rs.Type != "google_compute_instance" {
   280  			continue
   281  		}
   282  
   283  		_, err := config.clientCompute.Instances.Get(
   284  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   285  		if err == nil {
   286  			return fmt.Errorf("Instance still exists")
   287  		}
   288  	}
   289  
   290  	return nil
   291  }
   292  
   293  func testAccCheckComputeInstanceExists(n string, instance *compute.Instance) resource.TestCheckFunc {
   294  	return func(s *terraform.State) error {
   295  		rs, ok := s.RootModule().Resources[n]
   296  		if !ok {
   297  			return fmt.Errorf("Not found: %s", n)
   298  		}
   299  
   300  		if rs.Primary.ID == "" {
   301  			return fmt.Errorf("No ID is set")
   302  		}
   303  
   304  		config := testAccProvider.Meta().(*Config)
   305  
   306  		found, err := config.clientCompute.Instances.Get(
   307  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   308  		if err != nil {
   309  			return err
   310  		}
   311  
   312  		if found.Name != rs.Primary.ID {
   313  			return fmt.Errorf("Instance not found")
   314  		}
   315  
   316  		*instance = *found
   317  
   318  		return nil
   319  	}
   320  }
   321  
   322  func testAccCheckComputeInstanceMetadata(
   323  	instance *compute.Instance,
   324  	k string, v string) resource.TestCheckFunc {
   325  	return func(s *terraform.State) error {
   326  		if instance.Metadata == nil {
   327  			return fmt.Errorf("no metadata")
   328  		}
   329  
   330  		for _, item := range instance.Metadata.Items {
   331  			if k != item.Key {
   332  				continue
   333  			}
   334  
   335  			if item.Value != nil && v == *item.Value {
   336  				return nil
   337  			}
   338  
   339  			return fmt.Errorf("bad value for %s: %s", k, *item.Value)
   340  		}
   341  
   342  		return fmt.Errorf("metadata not found: %s", k)
   343  	}
   344  }
   345  
   346  func testAccCheckComputeInstanceAccessConfig(instance *compute.Instance) resource.TestCheckFunc {
   347  	return func(s *terraform.State) error {
   348  		for _, i := range instance.NetworkInterfaces {
   349  			if len(i.AccessConfigs) == 0 {
   350  				return fmt.Errorf("no access_config")
   351  			}
   352  		}
   353  
   354  		return nil
   355  	}
   356  }
   357  
   358  func testAccCheckComputeInstanceAccessConfigHasIP(instance *compute.Instance) resource.TestCheckFunc {
   359  	return func(s *terraform.State) error {
   360  		for _, i := range instance.NetworkInterfaces {
   361  			for _, c := range i.AccessConfigs {
   362  				if c.NatIP == "" {
   363  					return fmt.Errorf("no NAT IP")
   364  				}
   365  			}
   366  		}
   367  
   368  		return nil
   369  	}
   370  }
   371  
   372  func testAccCheckComputeInstanceDisk(instance *compute.Instance, source string, delete bool, boot bool) resource.TestCheckFunc {
   373  	return func(s *terraform.State) error {
   374  		if instance.Disks == nil {
   375  			return fmt.Errorf("no disks")
   376  		}
   377  
   378  		for _, disk := range instance.Disks {
   379  			if strings.LastIndex(disk.Source, "/"+source) == len(disk.Source)-len(source)-1 && disk.AutoDelete == delete && disk.Boot == boot {
   380  				return nil
   381  			}
   382  		}
   383  
   384  		return fmt.Errorf("Disk not found: %s", source)
   385  	}
   386  }
   387  
   388  func testAccCheckComputeInstanceTag(instance *compute.Instance, n string) resource.TestCheckFunc {
   389  	return func(s *terraform.State) error {
   390  		if instance.Tags == nil {
   391  			return fmt.Errorf("no tags")
   392  		}
   393  
   394  		for _, k := range instance.Tags.Items {
   395  			if k == n {
   396  				return nil
   397  			}
   398  		}
   399  
   400  		return fmt.Errorf("tag not found: %s", n)
   401  	}
   402  }
   403  
   404  func testAccCheckComputeInstanceServiceAccount(instance *compute.Instance, scope string) resource.TestCheckFunc {
   405  	return func(s *terraform.State) error {
   406  		if count := len(instance.ServiceAccounts); count != 1 {
   407  			return fmt.Errorf("Wrong number of ServiceAccounts: expected 1, got %d", count)
   408  		}
   409  
   410  		for _, val := range instance.ServiceAccounts[0].Scopes {
   411  			if val == scope {
   412  				return nil
   413  			}
   414  		}
   415  
   416  		return fmt.Errorf("Scope not found: %s", scope)
   417  	}
   418  }
   419  
   420  const testAccComputeInstance_basic_deprecated_network = `
   421  resource "google_compute_instance" "foobar" {
   422  	name = "terraform-test"
   423  	machine_type = "n1-standard-1"
   424  	zone = "us-central1-a"
   425  	can_ip_forward = false
   426  	tags = ["foo", "bar"]
   427  
   428  	disk {
   429  		image = "debian-7-wheezy-v20140814"
   430  	}
   431  
   432  	network {
   433  		source = "default"
   434  	}
   435  
   436  	metadata {
   437  		foo = "bar"
   438  	}
   439  }`
   440  
   441  const testAccComputeInstance_update_deprecated_network = `
   442  resource "google_compute_instance" "foobar" {
   443  	name = "terraform-test"
   444  	machine_type = "n1-standard-1"
   445  	zone = "us-central1-a"
   446  	tags = ["baz"]
   447  
   448  	disk {
   449  		image = "debian-7-wheezy-v20140814"
   450  	}
   451  
   452  	network {
   453  		source = "default"
   454  	}
   455  
   456  	metadata {
   457  		bar = "baz"
   458  	}
   459  }`
   460  
   461  const testAccComputeInstance_basic = `
   462  resource "google_compute_instance" "foobar" {
   463  	name = "terraform-test"
   464  	machine_type = "n1-standard-1"
   465  	zone = "us-central1-a"
   466  	can_ip_forward = false
   467  	tags = ["foo", "bar"]
   468  
   469  	disk {
   470  		image = "debian-7-wheezy-v20140814"
   471  	}
   472  
   473  	network_interface {
   474  		network = "default"
   475  	}
   476  
   477  	metadata {
   478  		foo = "bar"
   479  		baz = "qux"
   480  	}
   481  
   482  	metadata_startup_script = "echo Hello"
   483  }`
   484  
   485  const testAccComputeInstance_basic2 = `
   486  resource "google_compute_instance" "foobar" {
   487  	name = "terraform-test"
   488  	machine_type = "n1-standard-1"
   489  	zone = "us-central1-a"
   490  	can_ip_forward = false
   491  	tags = ["foo", "bar"]
   492  
   493  	disk {
   494  		image = "debian-cloud/debian-7-wheezy-v20140814"
   495  	}
   496  
   497  	network_interface {
   498  		network = "default"
   499  	}
   500  
   501  
   502  	metadata {
   503  		foo = "bar"
   504  	}
   505  }`
   506  
   507  const testAccComputeInstance_basic3 = `
   508  resource "google_compute_instance" "foobar" {
   509  	name = "terraform-test"
   510  	machine_type = "n1-standard-1"
   511  	zone = "us-central1-a"
   512  	can_ip_forward = false
   513  	tags = ["foo", "bar"]
   514  
   515  	disk {
   516  		image = "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140814"
   517  	}
   518  
   519  	network_interface {
   520  		network = "default"
   521  	}
   522  
   523  	metadata {
   524  		foo = "bar"
   525  	}
   526  }`
   527  
   528  // Update zone to ForceNew, and change metadata k/v entirely
   529  // Generates diff mismatch
   530  const testAccComputeInstance_forceNewAndChangeMetadata = `
   531  resource "google_compute_instance" "foobar" {
   532  	name = "terraform-test"
   533  	machine_type = "n1-standard-1"
   534  	zone = "us-central1-a"
   535  	zone = "us-central1-b"
   536  	tags = ["baz"]
   537  
   538  	disk {
   539  		image = "debian-7-wheezy-v20140814"
   540  	}
   541  
   542  	network_interface {
   543  		network = "default"
   544  		access_config { }
   545  	}
   546  
   547  	metadata {
   548  		qux = "true"
   549  	}
   550  }`
   551  
   552  // Update metadata, tags, and network_interface
   553  const testAccComputeInstance_update = `
   554  resource "google_compute_instance" "foobar" {
   555  	name = "terraform-test"
   556  	machine_type = "n1-standard-1"
   557  	zone = "us-central1-a"
   558  	tags = ["baz"]
   559  
   560  	disk {
   561  		image = "debian-7-wheezy-v20140814"
   562  	}
   563  
   564  	network_interface {
   565  		network = "default"
   566  		access_config { }
   567  	}
   568  
   569  	metadata {
   570  		bar = "baz"
   571  	}
   572  }`
   573  
   574  const testAccComputeInstance_ip = `
   575  resource "google_compute_address" "foo" {
   576  	name = "foo"
   577  }
   578  
   579  resource "google_compute_instance" "foobar" {
   580  	name = "terraform-test"
   581  	machine_type = "n1-standard-1"
   582  	zone = "us-central1-a"
   583  	tags = ["foo", "bar"]
   584  
   585  	disk {
   586  		image = "debian-7-wheezy-v20140814"
   587  	}
   588  
   589  	network_interface {
   590  		network = "default"
   591  		access_config {
   592  			nat_ip = "${google_compute_address.foo.address}"
   593  		}
   594  	}
   595  
   596  	metadata {
   597  		foo = "bar"
   598  	}
   599  }`
   600  
   601  const testAccComputeInstance_disks = `
   602  resource "google_compute_disk" "foobar" {
   603  	name = "terraform-test-disk"
   604  	size = 10
   605  	type = "pd-ssd"
   606  	zone = "us-central1-a"
   607  }
   608  
   609  resource "google_compute_instance" "foobar" {
   610  	name = "terraform-test"
   611  	machine_type = "n1-standard-1"
   612  	zone = "us-central1-a"
   613  
   614  	disk {
   615  		image = "debian-7-wheezy-v20140814"
   616  	}
   617  
   618  	disk {
   619  		disk = "${google_compute_disk.foobar.name}"
   620  		auto_delete = false
   621  	}
   622  
   623  	network_interface {
   624  		network = "default"
   625  	}
   626  
   627  	metadata {
   628  		foo = "bar"
   629  	}
   630  }`
   631  
   632  const testAccComputeInstance_local_ssd = `
   633  resource "google_compute_instance" "local-ssd" {
   634  	name = "terraform-test"
   635  	machine_type = "n1-standard-1"
   636  	zone = "us-central1-a"
   637  
   638  	disk {
   639  		image = "debian-7-wheezy-v20140814"
   640  	}
   641  
   642  	disk {
   643          type = "local-ssd"
   644          scratch = true
   645  	}
   646  
   647  	network_interface {
   648  		network = "default"
   649  	}
   650  
   651  }`
   652  
   653  const testAccComputeInstance_service_account = `
   654  resource "google_compute_instance" "foobar" {
   655  	name = "terraform-test"
   656  	machine_type = "n1-standard-1"
   657  	zone = "us-central1-a"
   658  
   659  	disk {
   660  		image = "debian-7-wheezy-v20140814"
   661  	}
   662  
   663  	network_interface {
   664  		network = "default"
   665  	}
   666  
   667  	service_account {
   668  		scopes = [
   669  			"userinfo-email",
   670  			"compute-ro",
   671  			"storage-ro",
   672  		]
   673  	}
   674  }`