|
| 1 | +package convert_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/convert" |
| 10 | + "github.com/sebdah/goldie/v2" |
| 11 | + "github.com/spf13/afero" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func TestAdvancedClusterToNew(t *testing.T) { |
| 17 | + const ( |
| 18 | + root = "testdata/adv2new" |
| 19 | + inSuffix = ".in.tf" |
| 20 | + outSuffix = ".out.tf" |
| 21 | + errFilename = "errors.json" |
| 22 | + ) |
| 23 | + fs := afero.NewOsFs() |
| 24 | + errMap := make(map[string]string) |
| 25 | + errContent, err := afero.ReadFile(fs, filepath.Join(root, errFilename)) |
| 26 | + require.NoError(t, err) |
| 27 | + err = json.Unmarshal(errContent, &errMap) |
| 28 | + require.NoError(t, err) |
| 29 | + g := goldie.New(t, |
| 30 | + goldie.WithFixtureDir(root), |
| 31 | + goldie.WithNameSuffix(outSuffix)) |
| 32 | + pattern := filepath.Join(root, "*"+inSuffix) |
| 33 | + inputFiles, err := afero.Glob(fs, pattern) |
| 34 | + require.NoError(t, err) |
| 35 | + assert.NotEmpty(t, inputFiles) |
| 36 | + for _, inputFile := range inputFiles { |
| 37 | + testName := strings.TrimSuffix(filepath.Base(inputFile), inSuffix) |
| 38 | + t.Run(testName, func(t *testing.T) { |
| 39 | + inConfig, err := afero.ReadFile(fs, inputFile) |
| 40 | + require.NoError(t, err) |
| 41 | + outConfig, err := convert.AdvancedClusterToNew(inConfig) |
| 42 | + if err == nil { |
| 43 | + g.Assert(t, testName, outConfig) |
| 44 | + } else { |
| 45 | + errMsg, found := errMap[testName] |
| 46 | + assert.True(t, found, "error not found in file %s for test %s, errMsg: %v", errFilename, testName, err) |
| 47 | + assert.Contains(t, err.Error(), errMsg) |
| 48 | + } |
| 49 | + }) |
| 50 | + } |
| 51 | +} |
0 commit comments