github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/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() { testAccPreCheck(t) },
    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", "enabled", "true"),
    23  					testAccCheckConsulKeysValue("consul_keys.app", "set", "acceptance"),
    24  				),
    25  			},
    26  			resource.TestStep{
    27  				Config: testAccConsulKeysConfig_Update,
    28  				Check: resource.ComposeTestCheckFunc(
    29  					testAccCheckConsulKeysExists(),
    30  					testAccCheckConsulKeysValue("consul_keys.app", "enabled", "true"),
    31  					testAccCheckConsulKeysValue("consul_keys.app", "set", "acceptanceUpdated"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func testAccCheckConsulKeysDestroy(s *terraform.State) error {
    39  	kv := testAccProvider.Meta().(*consulapi.Client).KV()
    40  	opts := &consulapi.QueryOptions{Datacenter: "dc1"}
    41  	pair, _, err := kv.Get("test/set", opts)
    42  	if err != nil {
    43  		return err
    44  	}
    45  	if pair != nil {
    46  		return fmt.Errorf("Key still exists: %#v", pair)
    47  	}
    48  	return nil
    49  }
    50  
    51  func testAccCheckConsulKeysExists() resource.TestCheckFunc {
    52  	return func(s *terraform.State) error {
    53  		kv := testAccProvider.Meta().(*consulapi.Client).KV()
    54  		opts := &consulapi.QueryOptions{Datacenter: "dc1"}
    55  		pair, _, err := kv.Get("test/set", opts)
    56  		if err != nil {
    57  			return err
    58  		}
    59  		if pair == nil {
    60  			return fmt.Errorf("Key 'test/set' does not exist")
    61  		}
    62  		return nil
    63  	}
    64  }
    65  
    66  func testAccCheckConsulKeysValue(n, attr, val string) resource.TestCheckFunc {
    67  	return func(s *terraform.State) error {
    68  		rn, ok := s.RootModule().Resources[n]
    69  		if !ok {
    70  			return fmt.Errorf("Resource not found")
    71  		}
    72  		out, ok := rn.Primary.Attributes["var."+attr]
    73  		if !ok {
    74  			return fmt.Errorf("Attribute '%s' not found: %#v", attr, rn.Primary.Attributes)
    75  		}
    76  		if val != "<any>" && out != val {
    77  			return fmt.Errorf("Attribute '%s' value '%s' != '%s'", attr, out, val)
    78  		}
    79  		if val == "<any>" && out == "" {
    80  			return fmt.Errorf("Attribute '%s' value '%s'", attr, out)
    81  		}
    82  		return nil
    83  	}
    84  }
    85  
    86  const testAccConsulKeysConfig = `
    87  resource "consul_keys" "app" {
    88  	datacenter = "dc1"
    89  	key {
    90  		name = "enabled"
    91  		path = "test/enabled"
    92  		default = "true"
    93  	}
    94  	key {
    95  		name = "set"
    96  		path = "test/set"
    97  		value = "acceptance"
    98  		delete = true
    99  	}
   100  }
   101  `
   102  
   103  const testAccConsulKeysConfig_Update = `
   104  resource "consul_keys" "app" {
   105  	datacenter = "dc1"
   106  	key {
   107  		name = "enabled"
   108  		path = "test/enabled"
   109  		default = "true"
   110  	}
   111  	key {
   112  		name = "set"
   113  		path = "test/set"
   114  		value = "acceptanceUpdated"
   115  		delete = true
   116  	}
   117  }
   118  `