diff --git a/builtin/providers/consul/data_source_consul_agent_self_test.go b/builtin/providers/consul/data_source_consul_agent_self_test.go index 341a89c9c3..16d6a9ead6 100644 --- a/builtin/providers/consul/data_source_consul_agent_self_test.go +++ b/builtin/providers/consul/data_source_consul_agent_self_test.go @@ -10,7 +10,6 @@ import ( func TestAccDataConsulAgentSelf_basic(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ resource.TestStep{ @@ -25,7 +24,8 @@ func TestAccDataConsulAgentSelf_basic(t *testing.T) { testAccCheckDataSourceValue("data.consul_agent_self.read", "advertise_addr", ""), testAccCheckDataSourceValue("data.consul_agent_self.read", "bind_addr", ""), testAccCheckDataSourceValue("data.consul_agent_self.read", "bootstrap_expect", ""), - testAccCheckDataSourceValue("data.consul_agent_self.read", "bootstrap_mode", "false"), + // the local test server is bootstrapped + testAccCheckDataSourceValue("data.consul_agent_self.read", "bootstrap_mode", "true"), testAccCheckDataSourceValue("data.consul_agent_self.read", "client_addr", ""), testAccCheckDataSourceValue("data.consul_agent_self.read", "datacenter", ""), testAccCheckDataSourceValue("data.consul_agent_self.read", "dev_mode", ""), diff --git a/builtin/providers/consul/data_source_consul_catalog_nodes_test.go b/builtin/providers/consul/data_source_consul_catalog_nodes_test.go index 94cea160d4..8912950443 100644 --- a/builtin/providers/consul/data_source_consul_catalog_nodes_test.go +++ b/builtin/providers/consul/data_source_consul_catalog_nodes_test.go @@ -8,7 +8,6 @@ import ( func TestAccDataConsulCatalogNodes_basic(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ resource.TestStep{ diff --git a/builtin/providers/consul/data_source_consul_catalog_service_test.go b/builtin/providers/consul/data_source_consul_catalog_service_test.go index 0ef7d1ab0a..b5901c4c5a 100644 --- a/builtin/providers/consul/data_source_consul_catalog_service_test.go +++ b/builtin/providers/consul/data_source_consul_catalog_service_test.go @@ -8,7 +8,6 @@ import ( func TestAccDataConsulCatalogService_basic(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ resource.TestStep{ diff --git a/builtin/providers/consul/data_source_consul_catalog_services_test.go b/builtin/providers/consul/data_source_consul_catalog_services_test.go index 1087073f7d..8617fc205c 100644 --- a/builtin/providers/consul/data_source_consul_catalog_services_test.go +++ b/builtin/providers/consul/data_source_consul_catalog_services_test.go @@ -8,7 +8,6 @@ import ( func TestAccDataConsulCatalogServices_basic(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ resource.TestStep{ diff --git a/builtin/providers/consul/data_source_consul_keys_test.go b/builtin/providers/consul/data_source_consul_keys_test.go index 09f62a927c..ef9baafd6e 100644 --- a/builtin/providers/consul/data_source_consul_keys_test.go +++ b/builtin/providers/consul/data_source_consul_keys_test.go @@ -8,7 +8,6 @@ import ( func TestAccDataConsulKeys_basic(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ resource.TestStep{ diff --git a/builtin/providers/consul/resource_consul_key_prefix_test.go b/builtin/providers/consul/resource_consul_key_prefix_test.go index e33fb13da5..141ddf4466 100644 --- a/builtin/providers/consul/resource_consul_key_prefix_test.go +++ b/builtin/providers/consul/resource_consul_key_prefix_test.go @@ -11,7 +11,6 @@ import ( func TestAccConsulKeyPrefix_basic(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: resource.ComposeTestCheckFunc( testAccCheckConsulKeyPrefixKeyAbsent("species"), diff --git a/builtin/providers/consul/resource_consul_keys_test.go b/builtin/providers/consul/resource_consul_keys_test.go index f6045658eb..f24c294edc 100644 --- a/builtin/providers/consul/resource_consul_keys_test.go +++ b/builtin/providers/consul/resource_consul_keys_test.go @@ -11,7 +11,6 @@ import ( func TestAccConsulKeys_basic(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckConsulKeysDestroy, Steps: []resource.TestStep{ diff --git a/builtin/providers/consul/resource_consul_prepared_query_test.go b/builtin/providers/consul/resource_consul_prepared_query_test.go index 6b08adaa86..12501c235a 100644 --- a/builtin/providers/consul/resource_consul_prepared_query_test.go +++ b/builtin/providers/consul/resource_consul_prepared_query_test.go @@ -11,7 +11,6 @@ import ( func TestAccConsulPreparedQuery_basic(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckConsulPreparedQueryDestroy, Steps: []resource.TestStep{ diff --git a/builtin/providers/consul/resource_provider_test.go b/builtin/providers/consul/resource_provider_test.go index df8fe1b85d..9c964adb89 100644 --- a/builtin/providers/consul/resource_provider_test.go +++ b/builtin/providers/consul/resource_provider_test.go @@ -1,24 +1,64 @@ package consul import ( + "io/ioutil" + "log" "os" "testing" + "github.com/hashicorp/consul/testutil" "github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/terraform" + "github.com/mitchellh/mapstructure" ) var testAccProviders map[string]terraform.ResourceProvider var testAccProvider *schema.Provider +var testConsulHTTPAddr string func init() { testAccProvider = Provider().(*schema.Provider) + testAccProvider.ConfigureFunc = testProviderConfigure + testAccProviders = map[string]terraform.ResourceProvider{ "consul": testAccProvider, } } +// we need to overrride the configured address for the tests +func testProviderConfigure(d *schema.ResourceData) (interface{}, error) { + var config Config + configRaw := d.Get("").(map[string]interface{}) + if err := mapstructure.Decode(configRaw, &config); err != nil { + return nil, err + } + config.Address = testConsulHTTPAddr + + log.Printf("[INFO] Initializing Consul test client") + return config.Client() +} + +func TestMain(m *testing.M) { + t := struct { + testutil.TestingT + }{} + + // start and stop the test consul server once for all tests + srv := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) { + c.LogLevel = "warn" + c.Stdout = ioutil.Discard + c.Stderr = ioutil.Discard + }) + + testConsulHTTPAddr = srv.HTTPAddr + + ret := m.Run() + + srv.Stop() + os.Exit(ret) +} + func TestResourceProvider(t *testing.T) { if err := Provider().(*schema.Provider).InternalValidate(); err != nil { t.Fatalf("err: %s", err) @@ -32,8 +72,9 @@ func TestResourceProvider_impl(t *testing.T) { func TestResourceProvider_Configure(t *testing.T) { rp := Provider() + // these configuration tests don't require an running server raw := map[string]interface{}{ - "address": "demo.consul.io:80", + "address": "example.com:8500", "datacenter": "nyc3", "scheme": "https", } @@ -53,7 +94,7 @@ func TestResourceProvider_ConfigureTLS(t *testing.T) { rp := Provider() raw := map[string]interface{}{ - "address": "demo.consul.io:80", + "address": "example.com:8943", "ca_file": "test-fixtures/cacert.pem", "cert_file": "test-fixtures/usercert.pem", "datacenter": "nyc3", @@ -71,13 +112,3 @@ func TestResourceProvider_ConfigureTLS(t *testing.T) { t.Fatalf("err: %s", err) } } - -func testAccPreCheck(t *testing.T) { - if v := os.Getenv("CONSUL_HTTP_ADDR"); v != "" { - return - } - if v := os.Getenv("CONSUL_ADDRESS"); v != "" { - return - } - t.Fatal("Either CONSUL_ADDRESS or CONSUL_HTTP_ADDR must be set for acceptance tests") -}