github.com/pbthorste/terraform@v0.8.6-0.20170127005045-deb56bd93da2/builtin/providers/google/resource_container_cluster_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"strconv"
    11  )
    12  
    13  func TestAccContainerCluster_basic(t *testing.T) {
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckContainerClusterDestroy,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccContainerCluster_basic,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckContainerClusterExists(
    23  						"google_container_cluster.primary"),
    24  				),
    25  			},
    26  		},
    27  	})
    28  }
    29  
    30  func TestAccContainerCluster_withAdditionalZones(t *testing.T) {
    31  	resource.Test(t, resource.TestCase{
    32  		PreCheck:     func() { testAccPreCheck(t) },
    33  		Providers:    testAccProviders,
    34  		CheckDestroy: testAccCheckContainerClusterDestroy,
    35  		Steps: []resource.TestStep{
    36  			resource.TestStep{
    37  				Config: testAccContainerCluster_withAdditionalZones,
    38  				Check: resource.ComposeTestCheckFunc(
    39  					testAccCheckContainerClusterExists(
    40  						"google_container_cluster.with_additional_zones"),
    41  					testAccCheckContainerClusterAdditionalZonesExist(
    42  						"google_container_cluster.with_additional_zones"),
    43  				),
    44  			},
    45  		},
    46  	})
    47  }
    48  
    49  func TestAccContainerCluster_withVersion(t *testing.T) {
    50  	resource.Test(t, resource.TestCase{
    51  		PreCheck:     func() { testAccPreCheck(t) },
    52  		Providers:    testAccProviders,
    53  		CheckDestroy: testAccCheckContainerClusterDestroy,
    54  		Steps: []resource.TestStep{
    55  			resource.TestStep{
    56  				Config: testAccContainerCluster_withVersion,
    57  				Check: resource.ComposeTestCheckFunc(
    58  					testAccCheckContainerClusterExists(
    59  						"google_container_cluster.with_version"),
    60  				),
    61  			},
    62  		},
    63  	})
    64  }
    65  
    66  func TestAccContainerCluster_withNodeConfig(t *testing.T) {
    67  	resource.Test(t, resource.TestCase{
    68  		PreCheck:     func() { testAccPreCheck(t) },
    69  		Providers:    testAccProviders,
    70  		CheckDestroy: testAccCheckContainerClusterDestroy,
    71  		Steps: []resource.TestStep{
    72  			resource.TestStep{
    73  				Config: testAccContainerCluster_withNodeConfig,
    74  				Check: resource.ComposeTestCheckFunc(
    75  					testAccCheckContainerClusterExists(
    76  						"google_container_cluster.with_node_config"),
    77  				),
    78  			},
    79  		},
    80  	})
    81  }
    82  
    83  func TestAccContainerCluster_withNodeConfigScopeAlias(t *testing.T) {
    84  	resource.Test(t, resource.TestCase{
    85  		PreCheck:     func() { testAccPreCheck(t) },
    86  		Providers:    testAccProviders,
    87  		CheckDestroy: testAccCheckContainerClusterDestroy,
    88  		Steps: []resource.TestStep{
    89  			resource.TestStep{
    90  				Config: testAccContainerCluster_withNodeConfigScopeAlias,
    91  				Check: resource.ComposeTestCheckFunc(
    92  					testAccCheckContainerClusterExists(
    93  						"google_container_cluster.with_node_config_scope_alias"),
    94  				),
    95  			},
    96  		},
    97  	})
    98  }
    99  
   100  func TestAccContainerCluster_network(t *testing.T) {
   101  	resource.Test(t, resource.TestCase{
   102  		PreCheck:     func() { testAccPreCheck(t) },
   103  		Providers:    testAccProviders,
   104  		CheckDestroy: testAccCheckContainerClusterDestroy,
   105  		Steps: []resource.TestStep{
   106  			resource.TestStep{
   107  				Config: testAccContainerCluster_networkRef,
   108  				Check: resource.ComposeTestCheckFunc(
   109  					testAccCheckContainerClusterExists(
   110  						"google_container_cluster.with_net_ref_by_url"),
   111  					testAccCheckContainerClusterExists(
   112  						"google_container_cluster.with_net_ref_by_name"),
   113  				),
   114  			},
   115  		},
   116  	})
   117  }
   118  
   119  func testAccCheckContainerClusterDestroy(s *terraform.State) error {
   120  	config := testAccProvider.Meta().(*Config)
   121  
   122  	for _, rs := range s.RootModule().Resources {
   123  		if rs.Type != "google_container_cluster" {
   124  			continue
   125  		}
   126  
   127  		attributes := rs.Primary.Attributes
   128  		_, err := config.clientContainer.Projects.Zones.Clusters.Get(
   129  			config.Project, attributes["zone"], attributes["name"]).Do()
   130  		if err == nil {
   131  			return fmt.Errorf("Cluster still exists")
   132  		}
   133  	}
   134  
   135  	return nil
   136  }
   137  
   138  func testAccCheckContainerClusterExists(n string) resource.TestCheckFunc {
   139  	return func(s *terraform.State) error {
   140  		rs, ok := s.RootModule().Resources[n]
   141  		if !ok {
   142  			return fmt.Errorf("Not found: %s", n)
   143  		}
   144  
   145  		if rs.Primary.ID == "" {
   146  			return fmt.Errorf("No ID is set")
   147  		}
   148  
   149  		config := testAccProvider.Meta().(*Config)
   150  
   151  		attributes := rs.Primary.Attributes
   152  		found, err := config.clientContainer.Projects.Zones.Clusters.Get(
   153  			config.Project, attributes["zone"], attributes["name"]).Do()
   154  		if err != nil {
   155  			return err
   156  		}
   157  
   158  		if found.Name != attributes["name"] {
   159  			return fmt.Errorf("Cluster not found")
   160  		}
   161  
   162  		return nil
   163  	}
   164  }
   165  
   166  func testAccCheckContainerClusterAdditionalZonesExist(n string) resource.TestCheckFunc {
   167  	return func(s *terraform.State) error {
   168  		rs, ok := s.RootModule().Resources[n]
   169  		if !ok {
   170  			return fmt.Errorf("Not found: %s", n)
   171  		}
   172  
   173  		var (
   174  			additionalZonesSize int
   175  			err                 error
   176  		)
   177  
   178  		if additionalZonesSize, err = strconv.Atoi(rs.Primary.Attributes["additional_zones.#"]); err != nil {
   179  			return err
   180  		}
   181  		if additionalZonesSize != 2 {
   182  			return fmt.Errorf("number of additional zones did not match 2")
   183  		}
   184  
   185  		return nil
   186  	}
   187  }
   188  
   189  var testAccContainerCluster_basic = fmt.Sprintf(`
   190  resource "google_container_cluster" "primary" {
   191  	name = "cluster-test-%s"
   192  	zone = "us-central1-a"
   193  	initial_node_count = 3
   194  
   195  	master_auth {
   196  		username = "mr.yoda"
   197  		password = "adoy.rm"
   198  	}
   199  }`, acctest.RandString(10))
   200  
   201  var testAccContainerCluster_withAdditionalZones = fmt.Sprintf(`
   202  resource "google_container_cluster" "with_additional_zones" {
   203  	name = "cluster-test-%s"
   204  	zone = "us-central1-a"
   205  	initial_node_count = 1
   206  
   207  	additional_zones = [
   208  		"us-central1-b",
   209  		"us-central1-c"
   210  	]
   211  
   212  	master_auth {
   213  		username = "mr.yoda"
   214  		password = "adoy.rm"
   215  	}
   216  }`, acctest.RandString(10))
   217  
   218  var testAccContainerCluster_withVersion = fmt.Sprintf(`
   219  resource "google_container_cluster" "with_version" {
   220  	name = "cluster-test-%s"
   221  	zone = "us-central1-a"
   222  	node_version = "1.4.7"
   223  	initial_node_count = 1
   224  
   225  	master_auth {
   226  		username = "mr.yoda"
   227  		password = "adoy.rm"
   228  	}
   229  }`, acctest.RandString(10))
   230  
   231  var testAccContainerCluster_withNodeConfig = fmt.Sprintf(`
   232  resource "google_container_cluster" "with_node_config" {
   233  	name = "cluster-test-%s"
   234  	zone = "us-central1-f"
   235  	initial_node_count = 1
   236  
   237  	master_auth {
   238  		username = "mr.yoda"
   239  		password = "adoy.rm"
   240  	}
   241  
   242  	node_config {
   243  		machine_type = "g1-small"
   244  		disk_size_gb = 15
   245  		oauth_scopes = [
   246  			"https://www.googleapis.com/auth/compute",
   247  			"https://www.googleapis.com/auth/devstorage.read_only",
   248  			"https://www.googleapis.com/auth/logging.write",
   249  			"https://www.googleapis.com/auth/monitoring"
   250  		]
   251  	}
   252  }`, acctest.RandString(10))
   253  
   254  var testAccContainerCluster_withNodeConfigScopeAlias = fmt.Sprintf(`
   255  resource "google_container_cluster" "with_node_config_scope_alias" {
   256  	name = "cluster-test-%s"
   257  	zone = "us-central1-f"
   258  	initial_node_count = 1
   259  
   260  	master_auth {
   261  		username = "mr.yoda"
   262  		password = "adoy.rm"
   263  	}
   264  
   265  	node_config {
   266  		machine_type = "g1-small"
   267  		disk_size_gb = 15
   268  		oauth_scopes = [ "compute-rw", "storage-ro", "logging-write", "monitoring" ]
   269  	}
   270  }`, acctest.RandString(10))
   271  
   272  var testAccContainerCluster_networkRef = fmt.Sprintf(`
   273  resource "google_compute_network" "container_network" {
   274  	name = "container-net-%s"
   275  	auto_create_subnetworks = true
   276  }
   277  
   278  resource "google_container_cluster" "with_net_ref_by_url" {
   279  	name = "cluster-test-%s"
   280  	zone = "us-central1-a"
   281  	initial_node_count = 1
   282  
   283  	master_auth {
   284  		username = "mr.yoda"
   285  		password = "adoy.rm"
   286  	}
   287  
   288  	network = "${google_compute_network.container_network.self_link}"
   289  }
   290  
   291  resource "google_container_cluster" "with_net_ref_by_name" {
   292  	name = "cluster-test-%s"
   293  	zone = "us-central1-a"
   294  	initial_node_count = 1
   295  
   296  	master_auth {
   297  		username = "mr.yoda"
   298  		password = "adoy.rm"
   299  	}
   300  
   301  	network = "${google_compute_network.container_network.name}"
   302  }`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))