github.com/aspring/terraform@v0.8.2-0.20161216122603-6a8619a5db2e/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_withNodeConfig(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_withNodeConfig,
    37  				Check: resource.ComposeTestCheckFunc(
    38  					testAccCheckContainerClusterExists(
    39  						"google_container_cluster.with_node_config"),
    40  				),
    41  			},
    42  		},
    43  	})
    44  }
    45  
    46  func TestAccContainerCluster_withNodeConfigScopeAlias(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_withNodeConfigScopeAlias,
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testAccCheckContainerClusterExists(
    56  						"google_container_cluster.with_node_config_scope_alias"),
    57  				),
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func TestAccContainerCluster_network(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_networkRef,
    71  				Check: resource.ComposeTestCheckFunc(
    72  					testAccCheckContainerClusterExists(
    73  						"google_container_cluster.with_net_ref_by_url"),
    74  					testAccCheckContainerClusterExists(
    75  						"google_container_cluster.with_net_ref_by_name"),
    76  				),
    77  			},
    78  		},
    79  	})
    80  }
    81  
    82  func testAccCheckContainerClusterDestroy(s *terraform.State) error {
    83  	config := testAccProvider.Meta().(*Config)
    84  
    85  	for _, rs := range s.RootModule().Resources {
    86  		if rs.Type != "google_container_cluster" {
    87  			continue
    88  		}
    89  
    90  		attributes := rs.Primary.Attributes
    91  		_, err := config.clientContainer.Projects.Zones.Clusters.Get(
    92  			config.Project, attributes["zone"], attributes["name"]).Do()
    93  		if err == nil {
    94  			return fmt.Errorf("Cluster still exists")
    95  		}
    96  	}
    97  
    98  	return nil
    99  }
   100  
   101  func testAccCheckContainerClusterExists(n string) resource.TestCheckFunc {
   102  	return func(s *terraform.State) error {
   103  		rs, ok := s.RootModule().Resources[n]
   104  		if !ok {
   105  			return fmt.Errorf("Not found: %s", n)
   106  		}
   107  
   108  		if rs.Primary.ID == "" {
   109  			return fmt.Errorf("No ID is set")
   110  		}
   111  
   112  		config := testAccProvider.Meta().(*Config)
   113  
   114  		attributes := rs.Primary.Attributes
   115  		found, err := config.clientContainer.Projects.Zones.Clusters.Get(
   116  			config.Project, attributes["zone"], attributes["name"]).Do()
   117  		if err != nil {
   118  			return err
   119  		}
   120  
   121  		if found.Name != attributes["name"] {
   122  			return fmt.Errorf("Cluster not found")
   123  		}
   124  
   125  		return nil
   126  	}
   127  }
   128  
   129  var testAccContainerCluster_basic = fmt.Sprintf(`
   130  resource "google_container_cluster" "primary" {
   131  	name = "cluster-test-%s"
   132  	zone = "us-central1-a"
   133  	initial_node_count = 3
   134  
   135  	master_auth {
   136  		username = "mr.yoda"
   137  		password = "adoy.rm"
   138  	}
   139  }`, acctest.RandString(10))
   140  
   141  var testAccContainerCluster_withNodeConfig = fmt.Sprintf(`
   142  resource "google_container_cluster" "with_node_config" {
   143  	name = "cluster-test-%s"
   144  	zone = "us-central1-f"
   145  	initial_node_count = 1
   146  
   147  	master_auth {
   148  		username = "mr.yoda"
   149  		password = "adoy.rm"
   150  	}
   151  
   152  	node_config {
   153  		machine_type = "g1-small"
   154  		disk_size_gb = 15
   155  		oauth_scopes = [
   156  			"https://www.googleapis.com/auth/compute",
   157  			"https://www.googleapis.com/auth/devstorage.read_only",
   158  			"https://www.googleapis.com/auth/logging.write",
   159  			"https://www.googleapis.com/auth/monitoring"
   160  		]
   161  	}
   162  }`, acctest.RandString(10))
   163  
   164  var testAccContainerCluster_withNodeConfigScopeAlias = fmt.Sprintf(`
   165  resource "google_container_cluster" "with_node_config_scope_alias" {
   166  	name = "cluster-test-%s"
   167  	zone = "us-central1-f"
   168  	initial_node_count = 1
   169  
   170  	master_auth {
   171  		username = "mr.yoda"
   172  		password = "adoy.rm"
   173  	}
   174  
   175  	node_config {
   176  		machine_type = "g1-small"
   177  		disk_size_gb = 15
   178  		oauth_scopes = [ "compute-rw", "storage-ro", "logging-write", "monitoring" ]
   179  	}
   180  }`, acctest.RandString(10))
   181  
   182  var testAccContainerCluster_networkRef = fmt.Sprintf(`
   183  resource "google_compute_network" "container_network" {
   184  	name = "container-net-%s"
   185  	auto_create_subnetworks = true
   186  }
   187  
   188  resource "google_container_cluster" "with_net_ref_by_url" {
   189  	name = "cluster-test-%s"
   190  	zone = "us-central1-a"
   191  	initial_node_count = 1
   192  
   193  	master_auth {
   194  		username = "mr.yoda"
   195  		password = "adoy.rm"
   196  	}
   197  
   198  	network = "${google_compute_network.container_network.self_link}"
   199  }
   200  
   201  resource "google_container_cluster" "with_net_ref_by_name" {
   202  	name = "cluster-test-%s"
   203  	zone = "us-central1-a"
   204  	initial_node_count = 1
   205  
   206  	master_auth {
   207  		username = "mr.yoda"
   208  		password = "adoy.rm"
   209  	}
   210  
   211  	network = "${google_compute_network.container_network.name}"
   212  }`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))