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