github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/consul/resource_consul_node_test.go (about)

     1  package consul
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	consulapi "github.com/hashicorp/consul/api"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccConsulNode_basic(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:     func() {},
    15  		Providers:    testAccProviders,
    16  		CheckDestroy: testAccCheckConsulNodeDestroy,
    17  		Steps: []resource.TestStep{
    18  			resource.TestStep{
    19  				Config: testAccConsulNodeConfig,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccCheckConsulNodeExists(),
    22  					testAccCheckConsulNodeValue("consul_catalog_entry.foo", "address", "127.0.0.1"),
    23  					testAccCheckConsulNodeValue("consul_catalog_entry.foo", "node", "foo"),
    24  				),
    25  			},
    26  		},
    27  	})
    28  }
    29  
    30  func testAccCheckConsulNodeDestroy(s *terraform.State) error {
    31  	catalog := testAccProvider.Meta().(*consulapi.Client).Catalog()
    32  	qOpts := consulapi.QueryOptions{}
    33  	nodes, _, err := catalog.Nodes(&qOpts)
    34  	if err != nil {
    35  		return fmt.Errorf("Could not retrieve services: %#v", err)
    36  	}
    37  	for i := range nodes {
    38  		if nodes[i].Node == "foo" {
    39  			return fmt.Errorf("Node still exists: %#v", "foo")
    40  		}
    41  	}
    42  	return nil
    43  }
    44  
    45  func testAccCheckConsulNodeExists() resource.TestCheckFunc {
    46  	return func(s *terraform.State) error {
    47  		catalog := testAccProvider.Meta().(*consulapi.Client).Catalog()
    48  		qOpts := consulapi.QueryOptions{}
    49  		nodes, _, err := catalog.Nodes(&qOpts)
    50  		if err != nil {
    51  			return err
    52  		}
    53  		for i := range nodes {
    54  			if nodes[i].Node == "foo" {
    55  				return nil
    56  			}
    57  		}
    58  		return fmt.Errorf("Service does not exist: %#v", "google")
    59  	}
    60  }
    61  
    62  func testAccCheckConsulNodeValue(n, attr, val string) resource.TestCheckFunc {
    63  	return func(s *terraform.State) error {
    64  		rn, ok := s.RootModule().Resources[n]
    65  		if !ok {
    66  			return fmt.Errorf("Resource not found")
    67  		}
    68  		out, ok := rn.Primary.Attributes[attr]
    69  		if !ok {
    70  			return fmt.Errorf("Attribute '%s' not found: %#v", attr, rn.Primary.Attributes)
    71  		}
    72  		if val != "<any>" && out != val {
    73  			return fmt.Errorf("Attribute '%s' value '%s' != '%s'", attr, out, val)
    74  		}
    75  		if val == "<any>" && out == "" {
    76  			return fmt.Errorf("Attribute '%s' value '%s'", attr, out)
    77  		}
    78  		return nil
    79  	}
    80  }
    81  
    82  const testAccConsulNodeConfig = `
    83  resource "consul_catalog_entry" "foo" {
    84  	address = "127.0.0.1"
    85  	node = "foo"
    86  }
    87  `