MCPcopy Index your code
hub / github.com/coder/coder / TestConvertStateGolden

Function TestConvertStateGolden

provisioner/terraform/convertstate_test.go:27–129  ·  view source on GitHub ↗

TestConvertStateGolden compares the output of ConvertState to a golden file to prevent regressions. If the logic changes, update the golden files accordingly. This was created to aid in refactoring `ConvertState`.

(t *testing.T)

Source from the content-addressed store, hash-verified

25//
26// This was created to aid in refactoring `ConvertState`.
27func TestConvertStateGolden(t *testing.T) {
28 t.Parallel()
29
30 testResourceDirectories := filepath.Join("testdata", "resources")
31 entries, err := os.ReadDir(testResourceDirectories)
32 require.NoError(t, err)
33
34 for _, testDirectory := range entries {
35 if !testDirectory.IsDir() {
36 continue
37 }
38
39 testFiles, err := os.ReadDir(filepath.Join(testResourceDirectories, testDirectory.Name()))
40 require.NoError(t, err)
41
42 // ConvertState works on both a plan file and a state file.
43 // The test should create a golden file for both.
44 for _, step := range []string{"plan", "state"} {
45 srcIdc := slices.IndexFunc(testFiles, func(entry os.DirEntry) bool {
46 return strings.HasSuffix(entry.Name(), fmt.Sprintf(".tf%s.json", step))
47 })
48 dotIdx := slices.IndexFunc(testFiles, func(entry os.DirEntry) bool {
49 return strings.HasSuffix(entry.Name(), fmt.Sprintf(".tf%s.dot", step))
50 })
51
52 // If the directory is missing these files, we cannot run ConvertState
53 // on it. So it's skipped.
54 if srcIdc == -1 || dotIdx == -1 {
55 continue
56 }
57
58 t.Run(step+"_"+testDirectory.Name(), func(t *testing.T) {
59 t.Parallel()
60 testDirectoryPath := filepath.Join(testResourceDirectories, testDirectory.Name())
61 planFile := filepath.Join(testDirectoryPath, testFiles[srcIdc].Name())
62 dotFile := filepath.Join(testDirectoryPath, testFiles[dotIdx].Name())
63
64 ctx := testutil.Context(t, testutil.WaitMedium)
65 logger := slogtest.Make(t, nil)
66
67 // Gather plan
68 tfStepRaw, err := os.ReadFile(planFile)
69 require.NoError(t, err)
70
71 var modules []*tfjson.StateModule
72 switch step {
73 case "plan":
74 var tfPlan tfjson.Plan
75 err = json.Unmarshal(tfStepRaw, &tfPlan)
76 require.NoError(t, err)
77
78 modules = []*tfjson.StateModule{tfPlan.PlannedValues.RootModule}
79 if tfPlan.PriorState != nil {
80 modules = append(modules, tfPlan.PriorState.Values.RootModule)
81 }
82 case "state":
83 var tfState tfjson.State
84 err = json.Unmarshal(tfStepRaw, &tfState)

Callers

nothing calls this directly

Calls 14

ContextFunction · 0.92
ConvertStateFunction · 0.92
sortResourcesFunction · 0.85
deterministicAppIDsFunction · 0.85
ReadDirMethod · 0.80
NameMethod · 0.65
RunMethod · 0.65
ReadFileMethod · 0.65
WriteFileMethod · 0.65
UnmarshalMethod · 0.45
FatalfMethod · 0.45

Tested by

no test coverage detected