github.com/peterbale/terraform@v0.9.0-beta2.0.20170315142748-5723acd55547/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  		Providers:    testAccProviders,
    15  		CheckDestroy: testAccCheckConsulKeysDestroy,
    16  		Steps: []resource.TestStep{
    17  			resource.TestStep{
    18  				Config: testAccConsulKeysConfig,
    19  				Check: resource.ComposeTestCheckFunc(
    20  					testAccCheckConsulKeysExists(),
    21  					testAccCheckConsulKeysValue("consul_keys.app", "enabled", "true"),
    22  					testAccCheckConsulKeysValue("consul_keys.app", "set", "acceptance"),
    23  					testAccCheckConsulKeysValue("consul_keys.app", "remove_one", "hello"),
    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  					testAccCheckConsulKeysRemoved("consul_keys.app", "remove_one"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func testAccCheckConsulKeysDestroy(s *terraform.State) error {
    40  	kv := testAccProvider.Meta().(*consulapi.Client).KV()
    41  	opts := &consulapi.QueryOptions{Datacenter: "dc1"}
    42  	pair, _, err := kv.Get("test/set", opts)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	if pair != nil {
    47  		return fmt.Errorf("Key still exists: %#v", pair)
    48  	}
    49  	return nil
    50  }
    51  
    52  func testAccCheckConsulKeysExists() resource.TestCheckFunc {
    53  	return func(s *terraform.State) error {
    54  		kv := testAccProvider.Meta().(*consulapi.Client).KV()
    55  		opts := &consulapi.QueryOptions{Datacenter: "dc1"}
    56  		pair, _, err := kv.Get("test/set", opts)
    57  		if err != nil {
    58  			return err
    59  		}
    60  		if pair == nil {
    61  			return fmt.Errorf("Key 'test/set' does not exist")
    62  		}
    63  		return nil
    64  	}
    65  }
    66  
    67  func testAccCheckConsulKeysValue(n, attr, val string) resource.TestCheckFunc {
    68  	return func(s *terraform.State) error {
    69  		rn, ok := s.RootModule().Resources[n]
    70  		if !ok {
    71  			return fmt.Errorf("Resource not found")
    72  		}
    73  		out, ok := rn.Primary.Attributes["var."+attr]
    74  		if !ok {
    75  			return fmt.Errorf("Attribute '%s' not found: %#v", attr, rn.Primary.Attributes)
    76  		}
    77  		if val != "<any>" && out != val {
    78  			return fmt.Errorf("Attribute '%s' value '%s' != '%s'", attr, out, val)
    79  		}
    80  		if val == "<any>" && out == "" {
    81  			return fmt.Errorf("Attribute '%s' value '%s'", attr, out)
    82  		}
    83  		return nil
    84  	}
    85  }
    86  
    87  func testAccCheckConsulKeysRemoved(n, attr string) resource.TestCheckFunc {
    88  	return func(s *terraform.State) error {
    89  		rn, ok := s.RootModule().Resources[n]
    90  		if !ok {
    91  			return fmt.Errorf("Resource not found")
    92  		}
    93  		_, ok = rn.Primary.Attributes["var."+attr]
    94  		if ok {
    95  			return fmt.Errorf("Attribute '%s' still present: %#v", attr, rn.Primary.Attributes)
    96  		}
    97  		return nil
    98  	}
    99  }
   100  
   101  const testAccConsulKeysConfig = `
   102  resource "consul_keys" "app" {
   103  	datacenter = "dc1"
   104  	key {
   105  		name = "enabled"
   106  		path = "test/enabled"
   107  		default = "true"
   108  	}
   109  	key {
   110  		name = "set"
   111  		path = "test/set"
   112  		value = "acceptance"
   113  		delete = true
   114  	}
   115  	key {
   116  		name = "remove_one"
   117  		path = "test/remove_one"
   118  		value = "hello"
   119  		delete = true
   120  	}
   121  }
   122  `
   123  
   124  const testAccConsulKeysConfig_Update = `
   125  resource "consul_keys" "app" {
   126  	datacenter = "dc1"
   127  	key {
   128  		name = "enabled"
   129  		path = "test/enabled"
   130  		default = "true"
   131  	}
   132  	key {
   133  		name = "set"
   134  		path = "test/set"
   135  		value = "acceptanceUpdated"
   136  		delete = true
   137  	}
   138  }
   139  `