github.com/peterbale/terraform@v0.9.0-beta2.0.20170315142748-5723acd55547/builtin/providers/consul/resource_consul_key_prefix_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 TestAccConsulKeyPrefix_basic(t *testing.T) { 13 resource.Test(t, resource.TestCase{ 14 Providers: testAccProviders, 15 CheckDestroy: resource.ComposeTestCheckFunc( 16 testAccCheckConsulKeyPrefixKeyAbsent("species"), 17 testAccCheckConsulKeyPrefixKeyAbsent("meat"), 18 testAccCheckConsulKeyPrefixKeyAbsent("cheese"), 19 testAccCheckConsulKeyPrefixKeyAbsent("bread"), 20 ), 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccConsulKeyPrefixConfig, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckConsulKeyPrefixKeyValue("cheese", "chevre"), 26 testAccCheckConsulKeyPrefixKeyValue("bread", "baguette"), 27 testAccCheckConsulKeyPrefixKeyAbsent("species"), 28 testAccCheckConsulKeyPrefixKeyAbsent("meat"), 29 ), 30 }, 31 resource.TestStep{ 32 Config: testAccConsulKeyPrefixConfig, 33 ExpectNonEmptyPlan: true, 34 Check: resource.ComposeTestCheckFunc( 35 // This will add a rogue key that Terraform isn't 36 // expecting, causing a non-empty plan that wants 37 // to remove it. 38 testAccAddConsulKeyPrefixRogue("species", "gorilla"), 39 ), 40 }, 41 resource.TestStep{ 42 Config: testAccConsulKeyPrefixConfig_Update, 43 Check: resource.ComposeTestCheckFunc( 44 testAccCheckConsulKeyPrefixKeyValue("meat", "ham"), 45 testAccCheckConsulKeyPrefixKeyValue("bread", "batard"), 46 testAccCheckConsulKeyPrefixKeyAbsent("cheese"), 47 testAccCheckConsulKeyPrefixKeyAbsent("species"), 48 ), 49 }, 50 resource.TestStep{ 51 Config: testAccConsulKeyPrefixConfig_Update, 52 ExpectNonEmptyPlan: true, 53 Check: resource.ComposeTestCheckFunc( 54 testAccAddConsulKeyPrefixRogue("species", "gorilla"), 55 ), 56 }, 57 }, 58 }) 59 } 60 61 func testAccCheckConsulKeyPrefixDestroy(s *terraform.State) error { 62 kv := testAccProvider.Meta().(*consulapi.Client).KV() 63 opts := &consulapi.QueryOptions{Datacenter: "dc1"} 64 pair, _, err := kv.Get("test/set", opts) 65 if err != nil { 66 return err 67 } 68 if pair != nil { 69 return fmt.Errorf("Key still exists: %#v", pair) 70 } 71 return nil 72 } 73 74 func testAccCheckConsulKeyPrefixKeyAbsent(name string) resource.TestCheckFunc { 75 fullName := "prefix_test/" + name 76 return func(s *terraform.State) error { 77 kv := testAccProvider.Meta().(*consulapi.Client).KV() 78 opts := &consulapi.QueryOptions{Datacenter: "dc1"} 79 pair, _, err := kv.Get(fullName, opts) 80 if err != nil { 81 return err 82 } 83 if pair != nil { 84 return fmt.Errorf("key '%s' exists, but shouldn't", fullName) 85 } 86 return nil 87 } 88 } 89 90 // This one is actually not a check, but rather a mutation step. It writes 91 // a value directly into Consul, bypassing our Terraform resource. 92 func testAccAddConsulKeyPrefixRogue(name, value string) resource.TestCheckFunc { 93 fullName := "prefix_test/" + name 94 return func(s *terraform.State) error { 95 kv := testAccProvider.Meta().(*consulapi.Client).KV() 96 opts := &consulapi.WriteOptions{Datacenter: "dc1"} 97 pair := &consulapi.KVPair{ 98 Key: fullName, 99 Value: []byte(value), 100 } 101 _, err := kv.Put(pair, opts) 102 return err 103 } 104 } 105 106 func testAccCheckConsulKeyPrefixKeyValue(name, value string) resource.TestCheckFunc { 107 fullName := "prefix_test/" + name 108 return func(s *terraform.State) error { 109 kv := testAccProvider.Meta().(*consulapi.Client).KV() 110 opts := &consulapi.QueryOptions{Datacenter: "dc1"} 111 pair, _, err := kv.Get(fullName, opts) 112 if err != nil { 113 return err 114 } 115 if pair == nil { 116 return fmt.Errorf("key %v doesn't exist, but should", fullName) 117 } 118 if string(pair.Value) != value { 119 return fmt.Errorf("key %v has value %v; want %v", fullName, pair.Value, value) 120 } 121 return nil 122 } 123 } 124 125 const testAccConsulKeyPrefixConfig = ` 126 resource "consul_key_prefix" "app" { 127 datacenter = "dc1" 128 129 path_prefix = "prefix_test/" 130 131 subkeys = { 132 cheese = "chevre" 133 bread = "baguette" 134 } 135 } 136 ` 137 138 const testAccConsulKeyPrefixConfig_Update = ` 139 resource "consul_key_prefix" "app" { 140 datacenter = "dc1" 141 142 path_prefix = "prefix_test/" 143 144 subkeys = { 145 bread = "batard" 146 meat = "ham" 147 } 148 } 149 `