github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/consul/resource_consul_keys_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 TestAccConsulKeys_basic(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:     func() {},
    15  		Providers:    testAccProviders,
    16  		CheckDestroy: testAccCheckConsulKeysDestroy,
    17  		Steps: []resource.TestStep{
    18  			resource.TestStep{
    19  				Config: testAccConsulKeysConfig,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccCheckConsulKeysExists(),
    22  					testAccCheckConsulKeysValue("consul_keys.app", "time", "<any>"),
    23  					testAccCheckConsulKeysValue("consul_keys.app", "enabled", "true"),
    24  					testAccCheckConsulKeysValue("consul_keys.app", "set", "acceptance"),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccCheckConsulKeysDestroy(s *terraform.State) error {
    32  	kv := testAccProvider.Meta().(*consulapi.Client).KV()
    33  	opts := &consulapi.QueryOptions{Datacenter: "nyc3"}
    34  	pair, _, err := kv.Get("test/set", opts)
    35  	if err != nil {
    36  		return err
    37  	}
    38  	if pair != nil {
    39  		return fmt.Errorf("Key still exists: %#v", pair)
    40  	}
    41  	return nil
    42  }
    43  
    44  func testAccCheckConsulKeysExists() resource.TestCheckFunc {
    45  	return func(s *terraform.State) error {
    46  		kv := testAccProvider.Meta().(*consulapi.Client).KV()
    47  		opts := &consulapi.QueryOptions{Datacenter: "nyc3"}
    48  		pair, _, err := kv.Get("test/set", opts)
    49  		if err != nil {
    50  			return err
    51  		}
    52  		if pair == nil {
    53  			return fmt.Errorf("Key 'test/set' does not exist")
    54  		}
    55  		return nil
    56  	}
    57  }
    58  
    59  func testAccCheckConsulKeysValue(n, attr, val string) resource.TestCheckFunc {
    60  	return func(s *terraform.State) error {
    61  		rn, ok := s.RootModule().Resources[n]
    62  		if !ok {
    63  			return fmt.Errorf("Resource not found")
    64  		}
    65  		out, ok := rn.Primary.Attributes["var."+attr]
    66  		if !ok {
    67  			return fmt.Errorf("Attribute '%s' not found: %#v", attr, rn.Primary.Attributes)
    68  		}
    69  		if val != "<any>" && out != val {
    70  			return fmt.Errorf("Attribute '%s' value '%s' != '%s'", attr, out, val)
    71  		}
    72  		if val == "<any>" && out == "" {
    73  			return fmt.Errorf("Attribute '%s' value '%s'", attr, out)
    74  		}
    75  		return nil
    76  	}
    77  }
    78  
    79  const testAccConsulKeysConfig = `
    80  resource "consul_keys" "app" {
    81  	datacenter = "nyc3"
    82  	key {
    83  		name = "time"
    84  		path = "global/time"
    85  	}
    86  	key {
    87  		name = "enabled"
    88  		path = "test/enabled"
    89  		default = "true"
    90  	}
    91  	key {
    92  		name = "set"
    93  		path = "test/set"
    94  		value = "acceptance"
    95  		delete = true
    96  	}
    97  }
    98  `