github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/google/resource_container_node_pool_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccContainerNodePool_basic(t *testing.T) {
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckContainerNodePoolDestroy,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccContainerNodePool_basic,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckContainerNodePoolMatches("google_container_node_pool.np"),
    23  				),
    24  			},
    25  		},
    26  	})
    27  }
    28  
    29  func testAccCheckContainerNodePoolDestroy(s *terraform.State) error {
    30  	config := testAccProvider.Meta().(*Config)
    31  
    32  	for _, rs := range s.RootModule().Resources {
    33  		if rs.Type != "google_container_node_pool" {
    34  			continue
    35  		}
    36  
    37  		attributes := rs.Primary.Attributes
    38  		_, err := config.clientContainer.Projects.Zones.Clusters.NodePools.Get(
    39  			config.Project, attributes["zone"], attributes["cluster"], attributes["name"]).Do()
    40  		if err == nil {
    41  			return fmt.Errorf("NodePool still exists")
    42  		}
    43  	}
    44  
    45  	return nil
    46  }
    47  
    48  func testAccCheckContainerNodePoolMatches(n string) resource.TestCheckFunc {
    49  	return func(s *terraform.State) error {
    50  		config := testAccProvider.Meta().(*Config)
    51  
    52  		rs, ok := s.RootModule().Resources[n]
    53  		if !ok {
    54  			return fmt.Errorf("Not found: %s", n)
    55  		}
    56  
    57  		if rs.Primary.ID == "" {
    58  			return fmt.Errorf("No ID is set")
    59  		}
    60  
    61  		attributes := rs.Primary.Attributes
    62  		found, err := config.clientContainer.Projects.Zones.Clusters.NodePools.Get(
    63  			config.Project, attributes["zone"], attributes["cluster"], attributes["name"]).Do()
    64  		if err != nil {
    65  			return err
    66  		}
    67  
    68  		if found.Name != attributes["name"] {
    69  			return fmt.Errorf("NodePool not found")
    70  		}
    71  
    72  		inc, err := strconv.Atoi(attributes["initial_node_count"])
    73  		if err != nil {
    74  			return err
    75  		}
    76  		if found.InitialNodeCount != int64(inc) {
    77  			return fmt.Errorf("Mismatched initialNodeCount. TF State: %s. GCP State: %d",
    78  				attributes["initial_node_count"], found.InitialNodeCount)
    79  		}
    80  		return nil
    81  	}
    82  }
    83  
    84  var testAccContainerNodePool_basic = fmt.Sprintf(`
    85  resource "google_container_cluster" "cluster" {
    86  	name = "tf-cluster-nodepool-test-%s"
    87  	zone = "us-central1-a"
    88  	initial_node_count = 3
    89  
    90  	master_auth {
    91  		username = "mr.yoda"
    92  		password = "adoy.rm"
    93  	}
    94  }
    95  
    96  resource "google_container_node_pool" "np" {
    97  	name = "tf-nodepool-test-%s"
    98  	zone = "us-central1-a"
    99  	cluster = "${google_container_cluster.cluster.name}"
   100  	initial_node_count = 2
   101  }`, acctest.RandString(10), acctest.RandString(10))