mirror of https://github.com/hashicorp/terraform
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.pull/14410/head
parent
4435f4362e
commit
e67c359b2d
@ -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
|
||||
}
|
||||
@ -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
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
Loading…
Reference in new issue