github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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  )
    11  
    12  func TestAccContainerCluster_basic(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:     func() { testAccPreCheck(t) },
    15  		Providers:    testAccProviders,
    16  		CheckDestroy: testAccCheckContainerClusterDestroy,
    17  		Steps: []resource.TestStep{
    18  			resource.TestStep{
    19  				Config: testAccContainerCluster_basic,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccCheckContainerClusterExists(
    22  						"google_container_cluster.primary"),
    23  				),
    24  			},
    25  		},
    26  	})
    27  }
    28  
    29  func TestAccContainerCluster_withVersion(t *testing.T) {
    30  	resource.Test(t, resource.TestCase{
    31  		PreCheck:     func() { testAccPreCheck(t) },
    32  		Providers:    testAccProviders,
    33  		CheckDestroy: testAccCheckContainerClusterDestroy,
    34  		Steps: []resource.TestStep{
    35  			resource.TestStep{
    36  				Config: testAccContainerCluster_withVersion,
    37  				Check: resource.ComposeTestCheckFunc(
    38  					testAccCheckContainerClusterExists(
    39  						"google_container_cluster.with_version"),
    40  				),
    41  			},
    42  		},
    43  	})
    44  }
    45  
    46  func TestAccContainerCluster_withNodeConfig(t *testing.T) {
    47  	resource.Test(t, resource.TestCase{
    48  		PreCheck:     func() { testAccPreCheck(t) },
    49  		Providers:    testAccProviders,
    50  		CheckDestroy: testAccCheckContainerClusterDestroy,
    51  		Steps: []resource.TestStep{
    52  			resource.TestStep{
    53  				Config: testAccContainerCluster_withNodeConfig,
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testAccCheckContainerClusterExists(
    56  						"google_container_cluster.with_node_config"),
    57  				),
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func TestAccContainerCluster_withNodeConfigScopeAlias(t *testing.T) {
    64  	resource.Test(t, resource.TestCase{
    65  		PreCheck:     func() { testAccPreCheck(t) },
    66  		Providers:    testAccProviders,
    67  		CheckDestroy: testAccCheckContainerClusterDestroy,
    68  		Steps: []resource.TestStep{
    69  			resource.TestStep{
    70  				Config: testAccContainerCluster_withNodeConfigScopeAlias,
    71  				Check: resource.ComposeTestCheckFunc(
    72  					testAccCheckContainerClusterExists(
    73  						"google_container_cluster.with_node_config_scope_alias"),
    74  				),
    75  			},
    76  		},
    77  	})
    78  }
    79  
    80  func TestAccContainerCluster_network(t *testing.T) {
    81  	resource.Test(t, resource.TestCase{
    82  		PreCheck:     func() { testAccPreCheck(t) },
    83  		Providers:    testAccProviders,
    84  		CheckDestroy: testAccCheckContainerClusterDestroy,
    85  		Steps: []resource.TestStep{
    86  			resource.TestStep{
    87  				Config: testAccContainerCluster_networkRef,
    88  				Check: resource.ComposeTestCheckFunc(
    89  					testAccCheckContainerClusterExists(
    90  						"google_container_cluster.with_net_ref_by_url"),
    91  					testAccCheckContainerClusterExists(
    92  						"google_container_cluster.with_net_ref_by_name"),
    93  				),
    94  			},
    95  		},
    96  	})
    97  }
    98  
    99  func testAccCheckContainerClusterDestroy(s *terraform.State) error {
   100  	config := testAccProvider.Meta().(*Config)
   101  
   102  	for _, rs := range s.RootModule().Resources {
   103  		if rs.Type != "google_container_cluster" {
   104  			continue
   105  		}
   106  
   107  		attributes := rs.Primary.Attributes
   108  		_, err := config.clientContainer.Projects.Zones.Clusters.Get(
   109  			config.Project, attributes["zone"], attributes["name"]).Do()
   110  		if err == nil {
   111  			return fmt.Errorf("Cluster still exists")
   112  		}
   113  	}
   114  
   115  	return nil
   116  }
   117  
   118  func testAccCheckContainerClusterExists(n string) resource.TestCheckFunc {
   119  	return func(s *terraform.State) error {
   120  		rs, ok := s.RootModule().Resources[n]
   121  		if !ok {
   122  			return fmt.Errorf("Not found: %s", n)
   123  		}
   124  
   125  		if rs.Primary.ID == "" {
   126  			return fmt.Errorf("No ID is set")
   127  		}
   128  
   129  		config := testAccProvider.Meta().(*Config)
   130  
   131  		attributes := rs.Primary.Attributes
   132  		found, err := config.clientContainer.Projects.Zones.Clusters.Get(
   133  			config.Project, attributes["zone"], attributes["name"]).Do()
   134  		if err != nil {
   135  			return err
   136  		}
   137  
   138  		if found.Name != attributes["name"] {
   139  			return fmt.Errorf("Cluster not found")
   140  		}
   141  
   142  		return nil
   143  	}
   144  }
   145  
   146  var testAccContainerCluster_basic = fmt.Sprintf(`
   147  resource "google_container_cluster" "primary" {
   148  	name = "cluster-test-%s"
   149  	zone = "us-central1-a"
   150  	initial_node_count = 3
   151  
   152  	master_auth {
   153  		username = "mr.yoda"
   154  		password = "adoy.rm"
   155  	}
   156  }`, acctest.RandString(10))
   157  
   158  var testAccContainerCluster_withVersion = fmt.Sprintf(`
   159  resource "google_container_cluster" "with_version" {
   160  	name = "cluster-test-%s"
   161  	zone = "us-central1-a"
   162  	node_version = "1.4.7"
   163  	initial_node_count = 1
   164  
   165  	master_auth {
   166  		username = "mr.yoda"
   167  		password = "adoy.rm"
   168  	}
   169  }`, acctest.RandString(10))
   170  
   171  var testAccContainerCluster_withNodeConfig = fmt.Sprintf(`
   172  resource "google_container_cluster" "with_node_config" {
   173  	name = "cluster-test-%s"
   174  	zone = "us-central1-f"
   175  	initial_node_count = 1
   176  
   177  	master_auth {
   178  		username = "mr.yoda"
   179  		password = "adoy.rm"
   180  	}
   181  
   182  	node_config {
   183  		machine_type = "g1-small"
   184  		disk_size_gb = 15
   185  		oauth_scopes = [
   186  			"https://www.googleapis.com/auth/compute",
   187  			"https://www.googleapis.com/auth/devstorage.read_only",
   188  			"https://www.googleapis.com/auth/logging.write",
   189  			"https://www.googleapis.com/auth/monitoring"
   190  		]
   191  	}
   192  }`, acctest.RandString(10))
   193  
   194  var testAccContainerCluster_withNodeConfigScopeAlias = fmt.Sprintf(`
   195  resource "google_container_cluster" "with_node_config_scope_alias" {
   196  	name = "cluster-test-%s"
   197  	zone = "us-central1-f"
   198  	initial_node_count = 1
   199  
   200  	master_auth {
   201  		username = "mr.yoda"
   202  		password = "adoy.rm"
   203  	}
   204  
   205  	node_config {
   206  		machine_type = "g1-small"
   207  		disk_size_gb = 15
   208  		oauth_scopes = [ "compute-rw", "storage-ro", "logging-write", "monitoring" ]
   209  	}
   210  }`, acctest.RandString(10))
   211  
   212  var testAccContainerCluster_networkRef = fmt.Sprintf(`
   213  resource "google_compute_network" "container_network" {
   214  	name = "container-net-%s"
   215  	auto_create_subnetworks = true
   216  }
   217  
   218  resource "google_container_cluster" "with_net_ref_by_url" {
   219  	name = "cluster-test-%s"
   220  	zone = "us-central1-a"
   221  	initial_node_count = 1
   222  
   223  	master_auth {
   224  		username = "mr.yoda"
   225  		password = "adoy.rm"
   226  	}
   227  
   228  	network = "${google_compute_network.container_network.self_link}"
   229  }
   230  
   231  resource "google_container_cluster" "with_net_ref_by_name" {
   232  	name = "cluster-test-%s"
   233  	zone = "us-central1-a"
   234  	initial_node_count = 1
   235  
   236  	master_auth {
   237  		username = "mr.yoda"
   238  		password = "adoy.rm"
   239  	}
   240  
   241  	network = "${google_compute_network.container_network.name}"
   242  }`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))