terraform/builtin/providers/consul/resource_consul_service_tes...

93 lines
2.6 KiB
Go
Raw Normal View History

package consul
import (
"fmt"
"testing"
consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccConsulService_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {},
Providers: testAccProviders,
CheckDestroy: testAccCheckConsulServiceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccConsulServiceConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckConsulServiceExists(),
testAccCheckConsulServiceValue("consul_service.app", "address", "www.google.com"),
testAccCheckConsulServiceValue("consul_service.app", "id", "google"),
Fixes for consul_service resource (#9366) Added `service_id` in place of `id` for resource. modified created, read, update to use `service_id` modified tests to include `service_id`. modified documentation for consul_service to include new value. Tests results CONSUL_HTTP_ADDR=localhost:8500 make testacc TEST=./builtin/providers/consul/ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/10/14 14:43:05 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/consul/ -v -timeout 120m === RUN TestAccDataConsulKeys_basic --- PASS: TestAccDataConsulKeys_basic (0.05s) === RUN TestAccConsulAgentService_basic --- PASS: TestAccConsulAgentService_basic (0.05s) === RUN TestAccConsulCatalogEntry_basic --- PASS: TestAccConsulCatalogEntry_basic (0.06s) === RUN TestAccConsulKeyPrefix_basic --- PASS: TestAccConsulKeyPrefix_basic (0.19s) === RUN TestConsulKeysMigrateState --- PASS: TestConsulKeysMigrateState (0.00s) === RUN TestConsulKeysMigrateState_empty --- PASS: TestConsulKeysMigrateState_empty (0.00s) === RUN TestAccConsulKeys_basic --- PASS: TestAccConsulKeys_basic (0.13s) === RUN TestAccConsulNode_basic --- PASS: TestAccConsulNode_basic (0.05s) === RUN TestAccConsulPreparedQuery_basic --- PASS: TestAccConsulPreparedQuery_basic (0.12s) === RUN TestAccConsulService_basic --- PASS: TestAccConsulService_basic (0.05s) === RUN TestResourceProvider --- PASS: TestResourceProvider (0.00s) === RUN TestResourceProvider_impl --- PASS: TestResourceProvider_impl (0.00s) === RUN TestResourceProvider_Configure --- PASS: TestResourceProvider_Configure (0.00s) === RUN TestResourceProvider_ConfigureTLS --- PASS: TestResourceProvider_ConfigureTLS (0.00s) PASS ok github.com/hashicorp/terraform/builtin/providers/consul 0.708s Refs: #9352
2016-10-26 14:07:00 +02:00
testAccCheckConsulServiceValue("consul_service.app", "service_id", "google"),
testAccCheckConsulServiceValue("consul_service.app", "name", "google"),
testAccCheckConsulServiceValue("consul_service.app", "port", "80"),
testAccCheckConsulServiceValue("consul_service.app", "tags.#", "2"),
testAccCheckConsulServiceValue("consul_service.app", "tags.0", "tag0"),
testAccCheckConsulServiceValue("consul_service.app", "tags.1", "tag1"),
),
},
},
})
}
func testAccCheckConsulServiceDestroy(s *terraform.State) error {
agent := testAccProvider.Meta().(*consulapi.Client).Agent()
services, err := agent.Services()
if err != nil {
return fmt.Errorf("Could not retrieve services: %#v", err)
}
_, ok := services["google"]
if ok {
return fmt.Errorf("Service still exists: %#v", "google")
}
return nil
}
func testAccCheckConsulServiceExists() resource.TestCheckFunc {
return func(s *terraform.State) error {
agent := testAccProvider.Meta().(*consulapi.Client).Agent()
services, err := agent.Services()
if err != nil {
return err
}
_, ok := services["google"]
if !ok {
return fmt.Errorf("Service does not exist: %#v", "google")
}
return nil
}
}
func testAccCheckConsulServiceValue(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[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
}
}
const testAccConsulServiceConfig = `
resource "consul_service" "app" {
address = "www.google.com"
Fixes for consul_service resource (#9366) Added `service_id` in place of `id` for resource. modified created, read, update to use `service_id` modified tests to include `service_id`. modified documentation for consul_service to include new value. Tests results CONSUL_HTTP_ADDR=localhost:8500 make testacc TEST=./builtin/providers/consul/ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/10/14 14:43:05 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/consul/ -v -timeout 120m === RUN TestAccDataConsulKeys_basic --- PASS: TestAccDataConsulKeys_basic (0.05s) === RUN TestAccConsulAgentService_basic --- PASS: TestAccConsulAgentService_basic (0.05s) === RUN TestAccConsulCatalogEntry_basic --- PASS: TestAccConsulCatalogEntry_basic (0.06s) === RUN TestAccConsulKeyPrefix_basic --- PASS: TestAccConsulKeyPrefix_basic (0.19s) === RUN TestConsulKeysMigrateState --- PASS: TestConsulKeysMigrateState (0.00s) === RUN TestConsulKeysMigrateState_empty --- PASS: TestConsulKeysMigrateState_empty (0.00s) === RUN TestAccConsulKeys_basic --- PASS: TestAccConsulKeys_basic (0.13s) === RUN TestAccConsulNode_basic --- PASS: TestAccConsulNode_basic (0.05s) === RUN TestAccConsulPreparedQuery_basic --- PASS: TestAccConsulPreparedQuery_basic (0.12s) === RUN TestAccConsulService_basic --- PASS: TestAccConsulService_basic (0.05s) === RUN TestResourceProvider --- PASS: TestResourceProvider (0.00s) === RUN TestResourceProvider_impl --- PASS: TestResourceProvider_impl (0.00s) === RUN TestResourceProvider_Configure --- PASS: TestResourceProvider_Configure (0.00s) === RUN TestResourceProvider_ConfigureTLS --- PASS: TestResourceProvider_ConfigureTLS (0.00s) PASS ok github.com/hashicorp/terraform/builtin/providers/consul 0.708s Refs: #9352
2016-10-26 14:07:00 +02:00
service_id = "google"
name = "google"
port = 80
tags = ["tag0", "tag1"]
}
`