github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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", 2),
    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, num int) 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  		additionalZonesSize, err := strconv.Atoi(rs.Primary.Attributes["additional_zones.#"])
   174  		if err != nil {
   175  			return err
   176  		}
   177  		if additionalZonesSize != num {
   178  			return fmt.Errorf("number of additional zones did not match %d, was %d", num, additionalZonesSize)
   179  		}
   180  
   181  		return nil
   182  	}
   183  }
   184  
   185  var testAccContainerCluster_basic = fmt.Sprintf(`
   186  resource "google_container_cluster" "primary" {
   187  	name = "cluster-test-%s"
   188  	zone = "us-central1-a"
   189  	initial_node_count = 3
   190  
   191  	master_auth {
   192  		username = "mr.yoda"
   193  		password = "adoy.rm"
   194  	}
   195  }`, acctest.RandString(10))
   196  
   197  var testAccContainerCluster_withAdditionalZones = fmt.Sprintf(`
   198  resource "google_container_cluster" "with_additional_zones" {
   199  	name = "cluster-test-%s"
   200  	zone = "us-central1-a"
   201  	initial_node_count = 1
   202  
   203  	additional_zones = [
   204  		"us-central1-b",
   205  		"us-central1-c"
   206  	]
   207  
   208  	master_auth {
   209  		username = "mr.yoda"
   210  		password = "adoy.rm"
   211  	}
   212  }`, acctest.RandString(10))
   213  
   214  var testAccContainerCluster_withVersion = fmt.Sprintf(`
   215  resource "google_container_cluster" "with_version" {
   216  	name = "cluster-test-%s"
   217  	zone = "us-central1-a"
   218  	node_version = "1.5.2"
   219  	initial_node_count = 1
   220  
   221  	master_auth {
   222  		username = "mr.yoda"
   223  		password = "adoy.rm"
   224  	}
   225  }`, acctest.RandString(10))
   226  
   227  var testAccContainerCluster_withNodeConfig = fmt.Sprintf(`
   228  resource "google_container_cluster" "with_node_config" {
   229  	name = "cluster-test-%s"
   230  	zone = "us-central1-f"
   231  	initial_node_count = 1
   232  
   233  	master_auth {
   234  		username = "mr.yoda"
   235  		password = "adoy.rm"
   236  	}
   237  
   238  	node_config {
   239  		machine_type = "g1-small"
   240  		disk_size_gb = 15
   241  		oauth_scopes = [
   242  			"https://www.googleapis.com/auth/compute",
   243  			"https://www.googleapis.com/auth/devstorage.read_only",
   244  			"https://www.googleapis.com/auth/logging.write",
   245  			"https://www.googleapis.com/auth/monitoring"
   246  		]
   247  	}
   248  }`, acctest.RandString(10))
   249  
   250  var testAccContainerCluster_withNodeConfigScopeAlias = fmt.Sprintf(`
   251  resource "google_container_cluster" "with_node_config_scope_alias" {
   252  	name = "cluster-test-%s"
   253  	zone = "us-central1-f"
   254  	initial_node_count = 1
   255  
   256  	master_auth {
   257  		username = "mr.yoda"
   258  		password = "adoy.rm"
   259  	}
   260  
   261  	node_config {
   262  		machine_type = "g1-small"
   263  		disk_size_gb = 15
   264  		oauth_scopes = [ "compute-rw", "storage-ro", "logging-write", "monitoring" ]
   265  	}
   266  }`, acctest.RandString(10))
   267  
   268  var testAccContainerCluster_networkRef = fmt.Sprintf(`
   269  resource "google_compute_network" "container_network" {
   270  	name = "container-net-%s"
   271  	auto_create_subnetworks = true
   272  }
   273  
   274  resource "google_container_cluster" "with_net_ref_by_url" {
   275  	name = "cluster-test-%s"
   276  	zone = "us-central1-a"
   277  	initial_node_count = 1
   278  
   279  	master_auth {
   280  		username = "mr.yoda"
   281  		password = "adoy.rm"
   282  	}
   283  
   284  	network = "${google_compute_network.container_network.self_link}"
   285  }
   286  
   287  resource "google_container_cluster" "with_net_ref_by_name" {
   288  	name = "cluster-test-%s"
   289  	zone = "us-central1-a"
   290  	initial_node_count = 1
   291  
   292  	master_auth {
   293  		username = "mr.yoda"
   294  		password = "adoy.rm"
   295  	}
   296  
   297  	network = "${google_compute_network.container_network.name}"
   298  }`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))