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