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