github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestAccContainerCluster_basic(t *testing.T) {
    12  	resource.Test(t, resource.TestCase{
    13  		PreCheck:     func() { testAccPreCheck(t) },
    14  		Providers:    testAccProviders,
    15  		CheckDestroy: testAccCheckContainerClusterDestroy,
    16  		Steps: []resource.TestStep{
    17  			resource.TestStep{
    18  				Config: testAccContainerCluster_basic,
    19  				Check: resource.ComposeTestCheckFunc(
    20  					testAccCheckContainerClusterExists(
    21  						"google_container_cluster.primary"),
    22  				),
    23  			},
    24  		},
    25  	})
    26  }
    27  
    28  func TestAccContainerCluster_withNodeConfig(t *testing.T) {
    29  	resource.Test(t, resource.TestCase{
    30  		PreCheck:     func() { testAccPreCheck(t) },
    31  		Providers:    testAccProviders,
    32  		CheckDestroy: testAccCheckContainerClusterDestroy,
    33  		Steps: []resource.TestStep{
    34  			resource.TestStep{
    35  				Config: testAccContainerCluster_withNodeConfig,
    36  				Check: resource.ComposeTestCheckFunc(
    37  					testAccCheckContainerClusterExists(
    38  						"google_container_cluster.with_node_config"),
    39  				),
    40  			},
    41  		},
    42  	})
    43  }
    44  
    45  func testAccCheckContainerClusterDestroy(s *terraform.State) error {
    46  	config := testAccProvider.Meta().(*Config)
    47  
    48  	for _, rs := range s.RootModule().Resources {
    49  		if rs.Type != "google_container_cluster" {
    50  			continue
    51  		}
    52  
    53  		attributes := rs.Primary.Attributes
    54  		_, err := config.clientContainer.Projects.Zones.Clusters.Get(
    55  			config.Project, attributes["zone"], attributes["name"]).Do()
    56  		if err == nil {
    57  			return fmt.Errorf("Cluster still exists")
    58  		}
    59  	}
    60  
    61  	return nil
    62  }
    63  
    64  func testAccCheckContainerClusterExists(n string) resource.TestCheckFunc {
    65  	return func(s *terraform.State) error {
    66  		rs, ok := s.RootModule().Resources[n]
    67  		if !ok {
    68  			return fmt.Errorf("Not found: %s", n)
    69  		}
    70  
    71  		if rs.Primary.ID == "" {
    72  			return fmt.Errorf("No ID is set")
    73  		}
    74  
    75  		config := testAccProvider.Meta().(*Config)
    76  
    77  		attributes := rs.Primary.Attributes
    78  		found, err := config.clientContainer.Projects.Zones.Clusters.Get(
    79  			config.Project, attributes["zone"], attributes["name"]).Do()
    80  		if err != nil {
    81  			return err
    82  		}
    83  
    84  		if found.Name != attributes["name"] {
    85  			return fmt.Errorf("Cluster not found")
    86  		}
    87  
    88  		return nil
    89  	}
    90  }
    91  
    92  const testAccContainerCluster_basic = `
    93  resource "google_container_cluster" "primary" {
    94  	name = "terraform-foo-bar-test"
    95  	zone = "us-central1-a"
    96  	initial_node_count = 3
    97  
    98  	master_auth {
    99  		username = "mr.yoda"
   100  		password = "adoy.rm"
   101  	}
   102  }`
   103  
   104  const testAccContainerCluster_withNodeConfig = `
   105  resource "google_container_cluster" "with_node_config" {
   106  	name = "terraform-foo-bar-with-nodeconfig"
   107  	zone = "us-central1-f"
   108  	initial_node_count = 1
   109  
   110  	master_auth {
   111  		username = "mr.yoda"
   112  		password = "adoy.rm"
   113  	}
   114  
   115  	node_config {
   116  		machine_type = "g1-small"
   117  		disk_size_gb = 15
   118  		oauth_scopes = [
   119  			"https://www.googleapis.com/auth/compute",
   120  			"https://www.googleapis.com/auth/devstorage.read_only",
   121  			"https://www.googleapis.com/auth/logging.write",
   122  			"https://www.googleapis.com/auth/monitoring"
   123  		]
   124  	}
   125  }`