Repository navigation
Expand file tree
/
Copy pathresource_consul_keys_test.go
More file actions
140 lines (131 loc) · 3.4 KB
/
Copy pathresource_consul_keys_test.go
File metadata and controls
140 lines (131 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package consul
import (
"fmt"
"testing"
consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccConsulKeys_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckConsulKeysDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccConsulKeysConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckConsulKeysExists(),
testAccCheckConsulKeysValue("consul_keys.app", "enabled", "true"),
testAccCheckConsulKeysValue("consul_keys.app", "set", "acceptance"),
testAccCheckConsulKeysValue("consul_keys.app", "remove_one", "hello"),
),
},
resource.TestStep{
Config: testAccConsulKeysConfig_Update,
Check: resource.ComposeTestCheckFunc(
testAccCheckConsulKeysExists(),
testAccCheckConsulKeysValue("consul_keys.app", "enabled", "true"),
testAccCheckConsulKeysValue("consul_keys.app", "set", "acceptanceUpdated"),
testAccCheckConsulKeysRemoved("consul_keys.app", "remove_one"),
),
},
},
})
}
func testAccCheckConsulKeysDestroy(s *terraform.State) error {
kv := testAccProvider.Meta().(*consulapi.Client).KV()
opts := &consulapi.QueryOptions{Datacenter: "dc1"}
pair, _, err := kv.Get("test/set", opts)
if err != nil {
return err
}
if pair != nil {
return fmt.Errorf("Key still exists: %#v", pair)
}
return nil
}
func testAccCheckConsulKeysExists() resource.TestCheckFunc {
return func(s *terraform.State) error {
kv := testAccProvider.Meta().(*consulapi.Client).KV()
opts := &consulapi.QueryOptions{Datacenter: "dc1"}
pair, _, err := kv.Get("test/set", opts)
if err != nil {
return err
}
if pair == nil {
return fmt.Errorf("Key 'test/set' does not exist")
}
return nil
}
}
func testAccCheckConsulKeysValue(n, attr, val string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rn, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Resource not found")
}
out, ok := rn.Primary.Attributes["var."+attr]
if !ok {
return fmt.Errorf("Attribute '%s' not found: %#v", attr, rn.Primary.Attributes)
}
if val != "<any>" && out != val {
return fmt.Errorf("Attribute '%s' value '%s' != '%s'", attr, out, val)
}
if val == "<any>" && out == "" {
return fmt.Errorf("Attribute '%s' value '%s'", attr, out)
}
return nil
}
}
func testAccCheckConsulKeysRemoved(n, attr string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rn, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Resource not found")
}
_, ok = rn.Primary.Attributes["var."+attr]
if ok {
return fmt.Errorf("Attribute '%s' still present: %#v", attr, rn.Primary.Attributes)
}
return nil
}
}
const testAccConsulKeysConfig = `
resource "consul_keys" "app" {
datacenter = "dc1"
key {
name = "enabled"
path = "test/enabled"
default = "true"
}
key {
name = "set"
path = "test/set"
value = "acceptance"
delete = true
}
key {
name = "remove_one"
path = "test/remove_one"
value = "hello"
delete = true
}
}
`
const testAccConsulKeysConfig_Update = `
resource "consul_keys" "app" {
datacenter = "dc1"
key {
name = "enabled"
path = "test/enabled"
default = "true"
}
key {
name = "set"
path = "test/set"
value = "acceptanceUpdated"
delete = true
}
}
`