Skip to content

Commit

Permalink
Fix merger tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Apr 10, 2024
1 parent 418dec0 commit 5aad29f
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 59 deletions.
4 changes: 4 additions & 0 deletions schema/functions_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func (m *FunctionsMerger) FunctionsForModule(meta *tfmod.Meta) (map[string]schem
return m.coreFunctions, nil
}

if m.stateReader == nil {
return m.coreFunctions, nil
}

if m.terraformVersion.LessThan(v1_8) {
return m.coreFunctions, nil
}
Expand Down
4 changes: 2 additions & 2 deletions schema/functions_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestFunctionsMerger_FunctionsForModule_18(t *testing.T) {
}

fm := NewFunctionsMerger(coreFunctions)
fm.SetSchemaReader(&testJsonSchemaReader{
fm.SetStateReader(&testJsonSchemaReader{
ps: &tfjson.ProviderSchemas{
FormatVersion: "1.0",
Schemas: providerSchemaWithFunctions,
Expand Down Expand Up @@ -150,7 +150,7 @@ func TestFunctionsMerger_FunctionsForModule_18(t *testing.T) {

func TestFunctionsMerger_FunctionsForModule_17(t *testing.T) {
fm := NewFunctionsMerger(map[string]schema.FunctionSignature{})
fm.SetSchemaReader(&testJsonSchemaReader{
fm.SetStateReader(&testJsonSchemaReader{
ps: &tfjson.ProviderSchemas{
FormatVersion: "1.0",
Schemas: providerSchemaWithFunctions,
Expand Down
4 changes: 4 additions & 0 deletions schema/schema_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func (m *SchemaMerger) SchemaForModule(meta *tfmod.Meta) (*schema.BodySchema, er
return m.coreSchema, nil
}

if m.stateReader == nil {
return m.coreSchema, nil
}

mergedSchema := m.coreSchema.Copy()

if mergedSchema.Blocks["provider"].DependentBody == nil {
Expand Down
185 changes: 128 additions & 57 deletions schema/schema_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"testing"
Expand Down Expand Up @@ -167,7 +168,7 @@ func TestSchemaMerger_SchemaForModule_providerNameMatch(t *testing.T) {
},
}
sm := NewSchemaMerger(testCoreSchema)
sm.SetSchemaReader(&testJsonSchemaReader{
sm.SetStateReader(&testJsonSchemaReader{
ps: &tfjson.ProviderSchemas{
FormatVersion: "1.0",
Schemas: map[string]*tfjson.ProviderSchema{
Expand Down Expand Up @@ -391,8 +392,8 @@ func TestSchemaMerger_SchemaForModule_twiceMerged(t *testing.T) {
},
}
sm := NewSchemaMerger(testCoreSchema)
sr := testSchemaReader(t, filepath.Join("testdata", "provider-schemas-0.15.json"), false)
sm.SetSchemaReader(sr)
sr := testSchemaReader(t, filepath.Join("testdata", "provider-schemas-0.15.json"), false, false)
sm.SetStateReader(sr)

vc := version.MustConstraints(version.NewConstraint("0.0.0"))

Expand Down Expand Up @@ -433,8 +434,8 @@ func TestSchemaMerger_SchemaForModule_twiceMerged(t *testing.T) {

func TestMergeWithJsonProviderSchemas_v012(t *testing.T) {
sm := NewSchemaMerger(testCoreSchema())
sr := testSchemaReader(t, filepath.Join("testdata", "provider-schemas-0.12.json"), true)
sm.SetSchemaReader(sr)
sr := testSchemaReader(t, filepath.Join("testdata", "provider-schemas-0.12.json"), true, false)
sm.SetStateReader(sr)
sm.SetTerraformVersion(v0_12_0)
meta := testModuleMeta(t, "testdata/test-config-0.12.tf")
mergedSchema, err := sm.SchemaForModule(meta)
Expand All @@ -449,8 +450,8 @@ func TestMergeWithJsonProviderSchemas_v012(t *testing.T) {

func TestMergeWithJsonProviderSchemas_v013(t *testing.T) {
sm := NewSchemaMerger(testCoreSchema())
sr := testSchemaReader(t, filepath.Join("testdata", "provider-schemas-0.13.json"), false)
sm.SetSchemaReader(sr)
sr := testSchemaReader(t, filepath.Join("testdata", "provider-schemas-0.13.json"), false, false)
sm.SetStateReader(sr)
sm.SetTerraformVersion(v0_13_0)
meta := testModuleMeta(t, "testdata/test-config-0.13.tf")
mergedSchema, err := sm.SchemaForModule(meta)
Expand Down Expand Up @@ -492,7 +493,7 @@ func TestMergeWithJsonProviderSchemas_concurrencyBug(t *testing.T) {
go func(t *testing.T) {
defer wg.Done()
sm := NewSchemaMerger(testCoreSchema())
sm.SetSchemaReader(exactSchemaReader{ps: ps})
sm.SetStateReader(&exactSchemaReader{ps: ps})
sm.SetTerraformVersion(v0_15_0)
_, err := sm.SchemaForModule(meta)
if err != nil {
Expand All @@ -502,7 +503,7 @@ func TestMergeWithJsonProviderSchemas_concurrencyBug(t *testing.T) {
go func(t *testing.T) {
defer wg.Done()
sm := NewSchemaMerger(testCoreSchema())
sm.SetSchemaReader(exactSchemaReader{ps: ps})
sm.SetStateReader(&exactSchemaReader{ps: ps})
sm.SetTerraformVersion(v0_15_0)
_, err := sm.SchemaForModule(meta)
if err != nil {
Expand All @@ -516,14 +517,30 @@ type exactSchemaReader struct {
ps *ProviderSchema
}

func (sr exactSchemaReader) ProviderSchema(modPath string, addr tfaddr.Provider, vc version.Constraints) (*ProviderSchema, error) {
func (sr *exactSchemaReader) ProviderSchema(modPath string, addr tfaddr.Provider, vc version.Constraints) (*ProviderSchema, error) {
return sr.ps, nil
}

func (sr *exactSchemaReader) RegistryModuleMeta(addr tfaddr.Module, cons version.Constraints) (*registry.ModuleData, error) {
return nil, nil
}

func (sr *exactSchemaReader) DeclaredModuleCalls(modPath string) (map[string]module.DeclaredModuleCall, error) {
return nil, nil
}

func (sr *exactSchemaReader) InstalledModuleCalls(modPath string) (map[string]module.InstalledModuleCall, error) {
return nil, nil
}

func (sr *exactSchemaReader) LocalModuleMeta(modPath string) (*module.Meta, error) {
return nil, nil
}

func TestMergeWithJsonProviderSchemas_v015(t *testing.T) {
sm := NewSchemaMerger(testCoreSchema())
sr := testSchemaReader(t, filepath.Join("testdata", "provider-schemas-0.15.json"), false)
sm.SetSchemaReader(sr)
sr := testSchemaReader(t, filepath.Join("testdata", "provider-schemas-0.15.json"), false, false)
sm.SetStateReader(sr)
sm.SetTerraformVersion(v0_15_0)
meta := testModuleMeta(t, "testdata/test-config-0.15.tf")
mergedSchema, err := sm.SchemaForModule(meta)
Expand All @@ -538,9 +555,8 @@ func TestMergeWithJsonProviderSchemas_v015(t *testing.T) {

func TestMergeWithJsonProviderSchemasAndModuleVariables_v015(t *testing.T) {
sm := NewSchemaMerger(testCoreSchema())
sr := testSchemaReader(t, filepath.Join("testdata", "provider-schemas-0.15.json"), false)
sm.SetSchemaReader(sr)
sm.SetModuleReader(testModuleReader())
sr := testSchemaReader(t, filepath.Join("testdata", "provider-schemas-0.15.json"), false, true)
sm.SetStateReader(sr)
sm.SetTerraformVersion(v0_15_0)
meta := testModuleMeta(t, "testdata/test-config-0.15.tf")
mergedSchema, err := sm.SchemaForModule(meta)
Expand All @@ -555,7 +571,7 @@ func TestMergeWithJsonProviderSchemasAndModuleVariables_v015(t *testing.T) {

func TestMergeWithJsonProviderSchemasAndModuleVariables_registryModule(t *testing.T) {
sm := NewSchemaMerger(testCoreSchema())
sm.SetModuleReader(testRegistryModuleReader())
sm.SetStateReader(testRegistryStateReader())
sm.SetTerraformVersion(v0_15_0)
meta := testModuleMeta(t, "testdata/test-config-remote-module.tf")
mergedSchema, err := sm.SchemaForModule(meta)
Expand Down Expand Up @@ -590,9 +606,9 @@ func testModuleMeta(t *testing.T, path string) *module.Meta {
return meta
}

func testSchemaReader(t *testing.T, jsonPath string, legacyStyle bool) SchemaReader {
func testSchemaReader(t *testing.T, jsonPath string, legacyStyle bool, withModuleCalls bool) StateReader {
ps := &tfjson.ProviderSchemas{}
b, err := ioutil.ReadFile(jsonPath)
b, err := os.ReadFile(jsonPath)
if err != nil {
t.Fatal(err)
}
Expand All @@ -603,8 +619,9 @@ func testSchemaReader(t *testing.T, jsonPath string, legacyStyle bool) SchemaRea

if legacyStyle {
return &testJsonSchemaReader{
ps: ps,
useTypeOnly: true,
ps: ps,
useTypeOnly: true,
withModuleCalls: withModuleCalls,
migrations: map[tfaddr.Provider]tfaddr.Provider{
addr.NewLegacyProvider("null"): addr.NewDefaultProvider("null"),
addr.NewLegacyProvider("random"): addr.NewDefaultProvider("random"),
Expand All @@ -613,7 +630,8 @@ func testSchemaReader(t *testing.T, jsonPath string, legacyStyle bool) SchemaRea
}
}
return &testJsonSchemaReader{
ps: ps,
ps: ps,
withModuleCalls: withModuleCalls,
migrations: map[tfaddr.Provider]tfaddr.Provider{
// the builtin provider doesn't have entry in required_providers
addr.NewLegacyProvider("terraform"): addr.NewBuiltInProvider("terraform"),
Expand All @@ -622,12 +640,71 @@ func testSchemaReader(t *testing.T, jsonPath string, legacyStyle bool) SchemaRea
}

type testJsonSchemaReader struct {
ps *tfjson.ProviderSchemas
useTypeOnly bool
migrations map[tfaddr.Provider]tfaddr.Provider
ps *tfjson.ProviderSchemas
useTypeOnly bool
migrations map[tfaddr.Provider]tfaddr.Provider
withModuleCalls bool
}

func (r *testJsonSchemaReader) ProviderSchema(_ string, pAddr tfaddr.Provider, _ version.Constraints) (*ProviderSchema, error) {
if newAddr, ok := r.migrations[pAddr]; ok {
pAddr = newAddr
}

addr := pAddr.String()
if r.useTypeOnly {
addr = pAddr.Type
}

jsonSchema, ok := r.ps.Schemas[addr]
if !ok {
return nil, fmt.Errorf("%s: schema not found", pAddr.String())
}

return ProviderSchemaFromJson(jsonSchema, pAddr), nil
}

func (m *testJsonSchemaReader) RegistryModuleMeta(addr tfaddr.Module, cons version.Constraints) (*registry.ModuleData, error) {
return nil, nil
}

func (m *testJsonSchemaReader) DeclaredModuleCalls(modPath string) (map[string]module.DeclaredModuleCall, error) {
if !m.withModuleCalls {
return nil, nil
}

return map[string]module.DeclaredModuleCall{
"example": {
LocalName: "example",
SourceAddr: module.LocalSourceAddr("./source"),
},
}, nil
}

func (m *testJsonSchemaReader) InstalledModuleCalls(modPath string) (map[string]module.InstalledModuleCall, error) {
return nil, nil
}

func (m *testJsonSchemaReader) LocalModuleMeta(modPath string) (*module.Meta, error) {
if !m.withModuleCalls {
return nil, nil
}

if modPath == filepath.Join("testdata", "source") {
return &module.Meta{
Path: "path",
Variables: map[string]module.Variable{
"test": {
Type: cty.String,
Description: "test var",
},
},
}, nil
}
return nil, fmt.Errorf("invalid source")
}

func testModuleReader() ModuleReader {
func testModuleStateReader() StateReader {
return &testModuleReaderStruct{}
}

Expand All @@ -638,17 +715,19 @@ func (m *testModuleReaderStruct) RegistryModuleMeta(addr tfaddr.Module, cons ver
return nil, nil
}

func (m *testModuleReaderStruct) ModuleCalls(modPath string) (module.ModuleCalls, error) {
return module.ModuleCalls{
Declared: map[string]module.DeclaredModuleCall{
"example": {
LocalName: "example",
SourceAddr: module.LocalSourceAddr("./source"),
},
func (m *testModuleReaderStruct) DeclaredModuleCalls(modPath string) (map[string]module.DeclaredModuleCall, error) {
return map[string]module.DeclaredModuleCall{
"example": {
LocalName: "example",
SourceAddr: module.LocalSourceAddr("./source"),
},
}, nil
}

func (m *testModuleReaderStruct) InstalledModuleCalls(modPath string) (map[string]module.InstalledModuleCall, error) {
return nil, nil
}

func (m *testModuleReaderStruct) LocalModuleMeta(modPath string) (*module.Meta, error) {
if modPath == filepath.Join("testdata", "source") {
return &module.Meta{
Expand All @@ -664,7 +743,11 @@ func (m *testModuleReaderStruct) LocalModuleMeta(modPath string) (*module.Meta,
return nil, fmt.Errorf("invalid source")
}

func testRegistryModuleReader() ModuleReader {
func (r *testModuleReaderStruct) ProviderSchema(_ string, pAddr tfaddr.Provider, _ version.Constraints) (*ProviderSchema, error) {
return nil, nil
}

func testRegistryStateReader() StateReader {
return &testRegistryModuleReaderStruct{}
}

Expand All @@ -675,14 +758,16 @@ func (m *testRegistryModuleReaderStruct) RegistryModuleMeta(addr tfaddr.Module,
return nil, nil
}

func (m *testRegistryModuleReaderStruct) ModuleCalls(modPath string) (module.ModuleCalls, error) {
return module.ModuleCalls{
Installed: map[string]module.InstalledModuleCall{
"remote-example": {
LocalName: "remote-example",
SourceAddr: tfaddr.MustParseModuleSource("registry.terraform.io/namespace/foo/bar"),
Path: ".terraform/modules/remote-example",
},
func (m *testRegistryModuleReaderStruct) DeclaredModuleCalls(modPath string) (map[string]module.DeclaredModuleCall, error) {
return nil, nil
}

func (m *testRegistryModuleReaderStruct) InstalledModuleCalls(modPath string) (map[string]module.InstalledModuleCall, error) {
return map[string]module.InstalledModuleCall{
"remote-example": {
LocalName: "remote-example",
SourceAddr: tfaddr.MustParseModuleSource("registry.terraform.io/namespace/foo/bar"),
Path: ".terraform/modules/remote-example",
},
}, nil
}
Expand All @@ -702,22 +787,8 @@ func (m *testRegistryModuleReaderStruct) LocalModuleMeta(modPath string) (*modul
return nil, fmt.Errorf("invalid source")
}

func (r *testJsonSchemaReader) ProviderSchema(_ string, pAddr tfaddr.Provider, _ version.Constraints) (*ProviderSchema, error) {
if newAddr, ok := r.migrations[pAddr]; ok {
pAddr = newAddr
}

addr := pAddr.String()
if r.useTypeOnly {
addr = pAddr.Type
}

jsonSchema, ok := r.ps.Schemas[addr]
if !ok {
return nil, fmt.Errorf("%s: schema not found", pAddr.String())
}

return ProviderSchemaFromJson(jsonSchema, pAddr), nil
func (r *testRegistryModuleReaderStruct) ProviderSchema(_ string, pAddr tfaddr.Provider, _ version.Constraints) (*ProviderSchema, error) {
return nil, nil
}

func testCoreSchema() *schema.BodySchema {
Expand Down

0 comments on commit 5aad29f

Please sign in to comment.