Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0c43789

Browse files
authored
test: move TestConvertStateGolden to only linux + mac (#20901)
Windows runners are flaky for golden filesclosescoder/internal#1141
1 parentcefe07d commit0c43789

File tree

2 files changed

+129
-110
lines changed

2 files changed

+129
-110
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
//go:build linux || darwin
2+
3+
package terraform_test
4+
5+
import (
6+
"encoding/json"
7+
"fmt"
8+
"os"
9+
"path/filepath"
10+
"slices"
11+
"strings"
12+
"testing"
13+
14+
tfjson"github.com/hashicorp/terraform-json"
15+
"github.com/stretchr/testify/require"
16+
17+
"cdr.dev/slog/sloggers/slogtest"
18+
"github.com/coder/coder/v2/provisioner/terraform"
19+
"github.com/coder/coder/v2/testutil"
20+
)
21+
22+
// TestConvertStateGolden compares the output of ConvertState to a golden
23+
// file to prevent regressions. If the logic changes, update the golden files
24+
// accordingly.
25+
//
26+
// This was created to aid in refactoring `ConvertState`.
27+
funcTestConvertStateGolden(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:=rangeentries {
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+
returnstrings.HasSuffix(entry.Name(),fmt.Sprintf(".tf%s.json",step))
47+
})
48+
dotIdx:=slices.IndexFunc(testFiles,func(entry os.DirEntry)bool {
49+
returnstrings.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+
ifsrcIdc==-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+
varmodules []*tfjson.StateModule
72+
switchstep {
73+
case"plan":
74+
vartfPlan tfjson.Plan
75+
err=json.Unmarshal(tfStepRaw,&tfPlan)
76+
require.NoError(t,err)
77+
78+
modules= []*tfjson.StateModule{tfPlan.PlannedValues.RootModule}
79+
iftfPlan.PriorState!=nil {
80+
modules=append(modules,tfPlan.PriorState.Values.RootModule)
81+
}
82+
case"state":
83+
vartfState tfjson.State
84+
err=json.Unmarshal(tfStepRaw,&tfState)
85+
require.NoError(t,err)
86+
modules= []*tfjson.StateModule{tfState.Values.RootModule}
87+
default:
88+
t.Fatalf("unknown step: %s",step)
89+
}
90+
91+
// Gather graph
92+
dotFileRaw,err:=os.ReadFile(dotFile)
93+
require.NoError(t,err)
94+
95+
// expectedOutput is `any` to support errors too. If `ConvertState` returns an
96+
// error, that error is the golden file output.
97+
varexpectedOutputany
98+
state,err:=terraform.ConvertState(ctx,modules,string(dotFileRaw),logger)
99+
iferr==nil {
100+
sortResources(state.Resources)
101+
sortExternalAuthProviders(state.ExternalAuthProviders)
102+
deterministicAppIDs(state.Resources)
103+
expectedOutput=state
104+
}else {
105+
// Write the error to the file then. Track errors as much as valid paths.
106+
expectedOutput=err.Error()
107+
}
108+
109+
expPath:=filepath.Join(testDirectoryPath,fmt.Sprintf("converted_state.%s.golden",step))
110+
if*updateGoldenFiles {
111+
gotBytes,err:=json.MarshalIndent(expectedOutput,""," ")
112+
require.NoError(t,err,"marshaling converted state to JSON")
113+
// Newline at end of file for git purposes
114+
err=os.WriteFile(expPath,append(gotBytes,'\n'),0o600)
115+
require.NoError(t,err)
116+
return
117+
}
118+
119+
gotBytes,err:=json.Marshal(expectedOutput)
120+
require.NoError(t,err,"marshaling converted state to JSON")
121+
122+
expBytes,err:=os.ReadFile(expPath)
123+
require.NoError(t,err)
124+
125+
require.JSONEq(t,string(expBytes),string(gotBytes),"converted state")
126+
})
127+
}
128+
}
129+
}

‎provisioner/terraform/resources_test.go‎

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"os"
99
"path/filepath"
1010
"runtime"
11-
"slices"
1211
"sort"
1312
"strings"
1413
"testing"
@@ -33,115 +32,6 @@ func ctxAndLogger(t *testing.T) (context.Context, slog.Logger) {
3332
returncontext.Background(),testutil.Logger(t)
3433
}
3534

36-
// TestConvertStateGolden compares the output of ConvertState to a golden
37-
// file to prevent regressions. If the logic changes, update the golden files
38-
// accordingly.
39-
//
40-
// This was created to aid in refactoring `ConvertState`.
41-
funcTestConvertStateGolden(t*testing.T) {
42-
t.Parallel()
43-
44-
testResourceDirectories:=filepath.Join("testdata","resources")
45-
entries,err:=os.ReadDir(testResourceDirectories)
46-
require.NoError(t,err)
47-
48-
for_,testDirectory:=rangeentries {
49-
if!testDirectory.IsDir() {
50-
continue
51-
}
52-
53-
testFiles,err:=os.ReadDir(filepath.Join(testResourceDirectories,testDirectory.Name()))
54-
require.NoError(t,err)
55-
56-
// ConvertState works on both a plan file and a state file.
57-
// The test should create a golden file for both.
58-
for_,step:=range []string{"plan","state"} {
59-
srcIdc:=slices.IndexFunc(testFiles,func(entry os.DirEntry)bool {
60-
returnstrings.HasSuffix(entry.Name(),fmt.Sprintf(".tf%s.json",step))
61-
})
62-
dotIdx:=slices.IndexFunc(testFiles,func(entry os.DirEntry)bool {
63-
returnstrings.HasSuffix(entry.Name(),fmt.Sprintf(".tf%s.dot",step))
64-
})
65-
66-
// If the directory is missing these files, we cannot run ConvertState
67-
// on it. So it's skipped.
68-
ifsrcIdc==-1||dotIdx==-1 {
69-
continue
70-
}
71-
72-
t.Run(step+"_"+testDirectory.Name(),func(t*testing.T) {
73-
t.Parallel()
74-
testDirectoryPath:=filepath.Join(testResourceDirectories,testDirectory.Name())
75-
planFile:=filepath.Join(testDirectoryPath,testFiles[srcIdc].Name())
76-
dotFile:=filepath.Join(testDirectoryPath,testFiles[dotIdx].Name())
77-
78-
ctx:=testutil.Context(t,testutil.WaitMedium)
79-
logger:=slogtest.Make(t,nil)
80-
81-
// Gather plan
82-
tfStepRaw,err:=os.ReadFile(planFile)
83-
require.NoError(t,err)
84-
85-
varmodules []*tfjson.StateModule
86-
switchstep {
87-
case"plan":
88-
vartfPlan tfjson.Plan
89-
err=json.Unmarshal(tfStepRaw,&tfPlan)
90-
require.NoError(t,err)
91-
92-
modules= []*tfjson.StateModule{tfPlan.PlannedValues.RootModule}
93-
iftfPlan.PriorState!=nil {
94-
modules=append(modules,tfPlan.PriorState.Values.RootModule)
95-
}
96-
case"state":
97-
vartfState tfjson.State
98-
err=json.Unmarshal(tfStepRaw,&tfState)
99-
require.NoError(t,err)
100-
modules= []*tfjson.StateModule{tfState.Values.RootModule}
101-
default:
102-
t.Fatalf("unknown step: %s",step)
103-
}
104-
105-
// Gather graph
106-
dotFileRaw,err:=os.ReadFile(dotFile)
107-
require.NoError(t,err)
108-
109-
// expectedOutput is `any` to support errors too. If `ConvertState` returns an
110-
// error, that error is the golden file output.
111-
varexpectedOutputany
112-
state,err:=terraform.ConvertState(ctx,modules,string(dotFileRaw),logger)
113-
iferr==nil {
114-
sortResources(state.Resources)
115-
sortExternalAuthProviders(state.ExternalAuthProviders)
116-
deterministicAppIDs(state.Resources)
117-
expectedOutput=state
118-
}else {
119-
// Write the error to the file then. Track errors as much as valid paths.
120-
expectedOutput=err.Error()
121-
}
122-
123-
expPath:=filepath.Join(testDirectoryPath,fmt.Sprintf("converted_state.%s.golden",step))
124-
if*updateGoldenFiles {
125-
gotBytes,err:=json.MarshalIndent(expectedOutput,""," ")
126-
require.NoError(t,err,"marshaling converted state to JSON")
127-
// Newline at end of file for git purposes
128-
err=os.WriteFile(expPath,append(gotBytes,'\n'),0o600)
129-
require.NoError(t,err)
130-
return
131-
}
132-
133-
gotBytes,err:=json.Marshal(expectedOutput)
134-
require.NoError(t,err,"marshaling converted state to JSON")
135-
136-
expBytes,err:=os.ReadFile(expPath)
137-
require.NoError(t,err)
138-
139-
require.JSONEq(t,string(expBytes),string(gotBytes),"converted state")
140-
})
141-
}
142-
}
143-
}
144-
14535
funcTestConvertResources(t*testing.T) {
14636
t.Parallel()
14737
// nolint:dogsled

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp