github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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_network(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_networkRef,
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testAccCheckContainerClusterExists(
    56  						"google_container_cluster.with_net_ref_by_url"),
    57  					testAccCheckContainerClusterExists(
    58  						"google_container_cluster.with_net_ref_by_name"),
    59  				),
    60  			},
    61  		},
    62  	})
    63  }
    64  
    65  func testAccCheckContainerClusterDestroy(s *terraform.State) error {
    66  	config := testAccProvider.Meta().(*Config)
    67  
    68  	for _, rs := range s.RootModule().Resources {
    69  		if rs.Type != "google_container_cluster" {
    70  			continue
    71  		}
    72  
    73  		attributes := rs.Primary.Attributes
    74  		_, err := config.clientContainer.Projects.Zones.Clusters.Get(
    75  			config.Project, attributes["zone"], attributes["name"]).Do()
    76  		if err == nil {
    77  			return fmt.Errorf("Cluster still exists")
    78  		}
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  func testAccCheckContainerClusterExists(n string) resource.TestCheckFunc {
    85  	return func(s *terraform.State) error {
    86  		rs, ok := s.RootModule().Resources[n]
    87  		if !ok {
    88  			return fmt.Errorf("Not found: %s", n)
    89  		}
    90  
    91  		if rs.Primary.ID == "" {
    92  			return fmt.Errorf("No ID is set")
    93  		}
    94  
    95  		config := testAccProvider.Meta().(*Config)
    96  
    97  		attributes := rs.Primary.Attributes
    98  		found, err := config.clientContainer.Projects.Zones.Clusters.Get(
    99  			config.Project, attributes["zone"], attributes["name"]).Do()
   100  		if err != nil {
   101  			return err
   102  		}
   103  
   104  		if found.Name != attributes["name"] {
   105  			return fmt.Errorf("Cluster not found")
   106  		}
   107  
   108  		return nil
   109  	}
   110  }
   111  
   112  var testAccContainerCluster_basic = fmt.Sprintf(`
   113  resource "google_container_cluster" "primary" {
   114  	name = "cluster-test-%s"
   115  	zone = "us-central1-a"
   116  	initial_node_count = 3
   117  
   118  	master_auth {
   119  		username = "mr.yoda"
   120  		password = "adoy.rm"
   121  	}
   122  }`, acctest.RandString(10))
   123  
   124  var testAccContainerCluster_withNodeConfig = fmt.Sprintf(`
   125  resource "google_container_cluster" "with_node_config" {
   126  	name = "cluster-test-%s"
   127  	zone = "us-central1-f"
   128  	initial_node_count = 1
   129  
   130  	master_auth {
   131  		username = "mr.yoda"
   132  		password = "adoy.rm"
   133  	}
   134  
   135  	node_config {
   136  		machine_type = "g1-small"
   137  		disk_size_gb = 15
   138  		oauth_scopes = [
   139  			"https://www.googleapis.com/auth/compute",
   140  			"https://www.googleapis.com/auth/devstorage.read_only",
   141  			"https://www.googleapis.com/auth/logging.write",
   142  			"https://www.googleapis.com/auth/monitoring"
   143  		]
   144  	}
   145  }`, acctest.RandString(10))
   146  
   147  var testAccContainerCluster_networkRef = fmt.Sprintf(`
   148  resource "google_compute_network" "container_network" {
   149  	name = "container-net-%s"
   150  	auto_create_subnetworks = true
   151  }
   152  
   153  resource "google_container_cluster" "with_net_ref_by_url" {
   154  	name = "cluster-test-%s"
   155  	zone = "us-central1-a"
   156  	initial_node_count = 1
   157  
   158  	master_auth {
   159  		username = "mr.yoda"
   160  		password = "adoy.rm"
   161  	}
   162  
   163  	network = "${google_compute_network.container_network.self_link}"
   164  }
   165  
   166  resource "google_container_cluster" "with_net_ref_by_name" {
   167  	name = "cluster-test-%s"
   168  	zone = "us-central1-a"
   169  	initial_node_count = 1
   170  
   171  	master_auth {
   172  		username = "mr.yoda"
   173  		password = "adoy.rm"
   174  	}
   175  
   176  	network = "${google_compute_network.container_network.name}"
   177  }`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))