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