terraform/builtin/providers/consul/resource_consul_keys_test.go

99 lines
2.3 KiB
Go
Raw Normal View History

2014-07-26 04:14:48 +02:00
package consul
import (
"fmt"
"testing"
consulapi "github.com/hashicorp/consul/api"
2014-07-26 04:14:48 +02:00
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccConsulKeys_basic(t *testing.T) {
2014-07-26 04:14:48 +02:00
resource.Test(t, resource.TestCase{
PreCheck: func() {},
Providers: testAccProviders,
CheckDestroy: testAccCheckConsulKeysDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccConsulKeysConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckConsulKeysExists(),
testAccCheckConsulKeysValue("consul_keys.app", "time", "<any>"),
testAccCheckConsulKeysValue("consul_keys.app", "enabled", "true"),
testAccCheckConsulKeysValue("consul_keys.app", "set", "acceptance"),
),
},
},
})
}
func testAccCheckConsulKeysDestroy(s *terraform.State) error {
kv := testAccProvider.Meta().(*consulapi.Client).KV()
2014-10-20 21:06:16 +02:00
opts := &consulapi.QueryOptions{Datacenter: "nyc3"}
2014-07-26 04:14:48 +02:00
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()
2014-10-20 21:06:16 +02:00
opts := &consulapi.QueryOptions{Datacenter: "nyc3"}
2014-07-26 04:14:48 +02:00
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]
2014-07-26 04:14:48 +02:00
if !ok {
return fmt.Errorf("Resource not found")
}
out, ok := rn.Primary.Attributes["var."+attr]
2014-07-26 04:14:48 +02:00
if !ok {
return fmt.Errorf("Attribute '%s' not found: %#v", attr, rn.Primary.Attributes)
2014-07-26 04:14:48 +02:00
}
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
}
}
const testAccConsulKeysConfig = `
resource "consul_keys" "app" {
2014-10-20 21:02:37 +02:00
datacenter = "nyc3"
2014-07-26 21:14:41 +02:00
key {
name = "time"
path = "global/time"
2014-07-26 04:14:48 +02:00
}
2014-07-26 21:14:41 +02:00
key {
name = "enabled"
path = "test/enabled"
2014-07-26 04:14:48 +02:00
default = "true"
}
2014-07-26 21:14:41 +02:00
key {
name = "set"
path = "test/set"
2014-07-26 04:14:48 +02:00
value = "acceptance"
delete = true
}
}
`