terraform/builtin/providers/consul/resource_consul_keys_test.go

119 lines
2.8 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() { testAccPreCheck(t) },
2014-07-26 04:14:48 +02:00
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"),
),
},
resource.TestStep{
Config: testAccConsulKeysConfig_Update,
Check: resource.ComposeTestCheckFunc(
testAccCheckConsulKeysExists(),
testAccCheckConsulKeysValue("consul_keys.app", "enabled", "true"),
testAccCheckConsulKeysValue("consul_keys.app", "set", "acceptanceUpdated"),
),
},
2014-07-26 04:14:48 +02:00
},
})
}
func testAccCheckConsulKeysDestroy(s *terraform.State) error {
kv := testAccProvider.Meta().(*consulapi.Client).KV()
opts := &consulapi.QueryOptions{Datacenter: "dc1"}
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()
opts := &consulapi.QueryOptions{Datacenter: "dc1"}
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" {
datacenter = "dc1"
key {
name = "enabled"
path = "test/enabled"
default = "true"
}
2014-07-26 21:14:41 +02:00
key {
name = "set"
path = "test/set"
value = "acceptance"
delete = true
2014-07-26 04:14:48 +02:00
}
}
`
const testAccConsulKeysConfig_Update = `
resource "consul_keys" "app" {
datacenter = "dc1"
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"
value = "acceptanceUpdated"
2014-07-26 04:14:48 +02:00
delete = true
}
}
`