From e67c359b2d1ffc634e247e213b902e38a92e2056 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Tue, 9 May 2017 11:47:22 -0700 Subject: [PATCH] provider/test: allow assigning a label to each instance When testing the behavior of multiple provider instances (either aliases or child module overrides) it's convenient to be able to label the individual instances to determine which one is actually being used for the purpose of making test assertions. --- builtin/providers/test/data_source_label.go | 25 +++++++++++ .../providers/test/data_source_label_test.go | 45 +++++++++++++++++++ builtin/providers/test/provider.go | 14 +++++- 3 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 builtin/providers/test/data_source_label.go create mode 100644 builtin/providers/test/data_source_label_test.go diff --git a/builtin/providers/test/data_source_label.go b/builtin/providers/test/data_source_label.go new file mode 100644 index 0000000000..40f3bad581 --- /dev/null +++ b/builtin/providers/test/data_source_label.go @@ -0,0 +1,25 @@ +package test + +import ( + "github.com/hashicorp/terraform/helper/schema" +) + +func providerLabelDataSource() *schema.Resource { + return &schema.Resource{ + Read: providerLabelDataSourceRead, + + Schema: map[string]*schema.Schema{ + "label": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func providerLabelDataSourceRead(d *schema.ResourceData, meta interface{}) error { + label := meta.(string) + d.SetId(label) + d.Set("label", label) + return nil +} diff --git a/builtin/providers/test/data_source_label_test.go b/builtin/providers/test/data_source_label_test.go new file mode 100644 index 0000000000..d98a27b060 --- /dev/null +++ b/builtin/providers/test/data_source_label_test.go @@ -0,0 +1,45 @@ +package test + +import ( + "errors" + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestProviderLabelDataSource(t *testing.T) { + resource.UnitTest(t, resource.TestCase{ + Providers: testAccProviders, + CheckDestroy: func(s *terraform.State) error { + return nil + }, + Steps: []resource.TestStep{ + { + Config: strings.TrimSpace(` +provider "test" { + label = "foo" +} + +data "test_provider_label" "test" { +} + `), + Check: func(s *terraform.State) error { + res, hasRes := s.RootModule().Resources["data.test_provider_label.test"] + if !hasRes { + return errors.New("No test_provider_label in state") + } + if got, want := res.Primary.ID, "foo"; got != want { + return fmt.Errorf("wrong id %q; want %q", got, want) + } + if got, want := res.Primary.Attributes["label"], "foo"; got != want { + return fmt.Errorf("wrong id %q; want %q", got, want) + } + return nil + }, + }, + }, + }) +} diff --git a/builtin/providers/test/provider.go b/builtin/providers/test/provider.go index a11e3f1cd0..4afec61b0e 100644 --- a/builtin/providers/test/provider.go +++ b/builtin/providers/test/provider.go @@ -7,17 +7,27 @@ import ( func Provider() terraform.ResourceProvider { return &schema.Provider{ + Schema: map[string]*schema.Schema{ + // Optional attribute to label a particular instance for a test + // that has multiple instances of this provider, so that they + // can be distinguished using the test_provider_label data source. + "label": { + Type: schema.TypeString, + Optional: true, + }, + }, ResourcesMap: map[string]*schema.Resource{ "test_resource": testResource(), "test_resource_gh12183": testResourceGH12183(), }, DataSourcesMap: map[string]*schema.Resource{ - "test_data_source": testDataSource(), + "test_data_source": testDataSource(), + "test_provider_label": providerLabelDataSource(), }, ConfigureFunc: providerConfigure, } } func providerConfigure(d *schema.ResourceData) (interface{}, error) { - return nil, nil + return d.Get("label"), nil }