github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/context_fixtures_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/terraform-plugin-sdk/internal/configs"
     7  	"github.com/hashicorp/terraform-plugin-sdk/internal/configs/configschema"
     8  	"github.com/hashicorp/terraform-plugin-sdk/internal/providers"
     9  	"github.com/zclconf/go-cty/cty"
    10  )
    11  
    12  // contextTestFixture is a container for a set of objects that work together
    13  // to create a base testing scenario. This is used to represent some common
    14  // situations used as the basis for multiple tests.
    15  type contextTestFixture struct {
    16  	Config           *configs.Config
    17  	ProviderResolver providers.Resolver
    18  	Provisioners     map[string]ProvisionerFactory
    19  }
    20  
    21  // ContextOpts returns a ContextOps pre-populated with the elements of this
    22  // fixture. Each call returns a distinct object, so callers can apply further
    23  // _shallow_ modifications to the options as needed.
    24  func (f *contextTestFixture) ContextOpts() *ContextOpts {
    25  	return &ContextOpts{
    26  		Config:           f.Config,
    27  		ProviderResolver: f.ProviderResolver,
    28  		Provisioners:     f.Provisioners,
    29  	}
    30  }
    31  
    32  // contextFixtureApplyVars builds and returns a test fixture for testing
    33  // input variables, primarily during the apply phase. The configuration is
    34  // loaded from testdata/apply-vars, and the provider resolver is
    35  // configured with a resource type schema for aws_instance that matches
    36  // what's used in that configuration.
    37  func contextFixtureApplyVars(t *testing.T) *contextTestFixture {
    38  	c := testModule(t, "apply-vars")
    39  	p := mockProviderWithResourceTypeSchema("aws_instance", &configschema.Block{
    40  		Attributes: map[string]*configschema.Attribute{
    41  			"id":   {Type: cty.String, Computed: true},
    42  			"foo":  {Type: cty.String, Optional: true},
    43  			"bar":  {Type: cty.String, Optional: true},
    44  			"baz":  {Type: cty.String, Optional: true},
    45  			"num":  {Type: cty.Number, Optional: true},
    46  			"list": {Type: cty.List(cty.String), Optional: true},
    47  			"map":  {Type: cty.Map(cty.String), Optional: true},
    48  		},
    49  	})
    50  	p.ApplyFn = testApplyFn
    51  	p.DiffFn = testDiffFn
    52  	return &contextTestFixture{
    53  		Config: c,
    54  		ProviderResolver: providers.ResolverFixed(
    55  			map[string]providers.Factory{
    56  				"aws": testProviderFuncFixed(p),
    57  			},
    58  		),
    59  	}
    60  }
    61  
    62  // contextFixtureApplyVarsEnv builds and returns a test fixture for testing
    63  // input variables set from the environment. The configuration is
    64  // loaded from testdata/apply-vars-env, and the provider resolver is
    65  // configured with a resource type schema for aws_instance that matches
    66  // what's used in that configuration.
    67  func contextFixtureApplyVarsEnv(t *testing.T) *contextTestFixture {
    68  	c := testModule(t, "apply-vars-env")
    69  	p := mockProviderWithResourceTypeSchema("aws_instance", &configschema.Block{
    70  		Attributes: map[string]*configschema.Attribute{
    71  			"string": {Type: cty.String, Optional: true},
    72  			"list":   {Type: cty.List(cty.String), Optional: true},
    73  			"map":    {Type: cty.Map(cty.String), Optional: true},
    74  			"id":     {Type: cty.String, Computed: true},
    75  			"type":   {Type: cty.String, Computed: true},
    76  		},
    77  	})
    78  	p.ApplyFn = testApplyFn
    79  	p.DiffFn = testDiffFn
    80  	return &contextTestFixture{
    81  		Config: c,
    82  		ProviderResolver: providers.ResolverFixed(
    83  			map[string]providers.Factory{
    84  				"aws": testProviderFuncFixed(p),
    85  			},
    86  		),
    87  	}
    88  }