Skip to content

Commit 638bf08

Browse files
Improve convert (#130)
Use shared temp dir for convert rather than Go test's default location - Expand test suite including a workaround for setting up python venv. - Add method to construct a new test directly from a conversion result instead of having to create a test around the source YAML program, then call convert. Extras: - Use local temp directory for testing in VSCode.
1 parent 0cf28b0 commit 638bf08

File tree

3 files changed

+182
-6
lines changed

3 files changed

+182
-6
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"go.testTimeout": "320s",
3+
"go.testEnvVars": {
4+
"PULUMITEST_TEMP_DIR": "${workspaceFolder}/.temp"
5+
},
36
"cSpell.language": "en-US",
47
"cSpell.words": [
58
"apitype",

pulumitest/convert.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,39 @@ import (
99
"github.com/pulumi/providertest/pulumitest/opttest"
1010
)
1111

12+
// ConvertResult encapsulates the result of a conversion operation.
1213
type ConvertResult struct {
1314
// PulumiTest instance for the converted program.
1415
PulumiTest *PulumiTest
1516
// Combined output of the `pulumi convert` command.
1617
Output string
1718
}
1819

20+
// Create a new test by converting a program into a specific language.
21+
// It returns a new PulumiTest instance for the converted program which will be outputted into a temporary directory.
22+
func Convert(t PT, source, language string, opts ...opttest.Option) ConvertResult {
23+
t.Helper()
24+
25+
pulumiTest := PulumiTest{
26+
ctx: testContext(t),
27+
workingDir: source,
28+
options: opttest.DefaultOptions(),
29+
}
30+
31+
return pulumiTest.Convert(t, language, opts...)
32+
}
33+
1934
// Convert a program to a given language.
2035
// It returns a new PulumiTest instance for the converted program which will be outputted into a temporary directory.
2136
func (a *PulumiTest) Convert(t PT, language string, opts ...opttest.Option) ConvertResult {
2237
t.Helper()
2338

24-
tempDir := t.TempDir()
39+
options := a.options.Copy()
40+
for _, opt := range opts {
41+
opt.Apply(options)
42+
}
43+
44+
tempDir := tempDirWithoutCleanupOnFailedTest(t, "converted", options.TempDir)
2545
base := filepath.Base(a.workingDir)
2646
targetDir := filepath.Join(tempDir, fmt.Sprintf("%s-%s", base, language))
2747
err := os.Mkdir(targetDir, 0755)
@@ -37,11 +57,6 @@ func (a *PulumiTest) Convert(t PT, language string, opts ...opttest.Option) Conv
3757
ptFatalF(t, "failed to convert directory: %s\n%s", err, out)
3858
}
3959

40-
options := a.options.Copy()
41-
for _, opt := range opts {
42-
opt.Apply(options)
43-
}
44-
4560
convertedTest := &PulumiTest{
4661
ctx: a.ctx,
4762
workingDir: targetDir,

pulumitest/convert_test.go

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package pulumitest_test
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"strings"
7+
"testing"
8+
9+
"github.com/pulumi/providertest/pulumitest"
10+
"github.com/pulumi/providertest/pulumitest/assertup"
11+
"github.com/pulumi/providertest/pulumitest/opttest"
12+
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
var fixedPythonRuntime = `
17+
runtime:
18+
name: python
19+
options:
20+
toolchain: pip
21+
virtualenv: venv
22+
`
23+
24+
func TestImmediateConvertTypescript(t *testing.T) {
25+
t.Parallel()
26+
27+
// No need to copy the source, since we're not going to modify it.
28+
convertResult := pulumitest.Convert(t, filepath.Join("testdata", "yaml_program"), "typescript")
29+
t.Log(convertResult.Output)
30+
test := convertResult.PulumiTest
31+
32+
tsPreview := test.Preview(t)
33+
assert.Equal(t,
34+
map[apitype.OpType]int{apitype.OpCreate: 2},
35+
tsPreview.ChangeSummary)
36+
37+
tsUp := test.Up(t)
38+
assert.Equal(t,
39+
map[string]int{"create": 2},
40+
*tsUp.Summary.ResourceChanges)
41+
42+
assertup.HasNoDeletes(t, tsUp)
43+
44+
// Show the deploy output.
45+
t.Log(tsUp.StdOut)
46+
}
47+
48+
func TestConvertPython(t *testing.T) {
49+
t.Parallel()
50+
51+
// No need to copy the source, since we're not going to modify it.
52+
source := pulumitest.NewPulumiTest(t, filepath.Join("testdata", "yaml_program"), opttest.TestInPlace())
53+
54+
// Convert the original source to Python.
55+
converted := source.Convert(t, "python", opttest.SkipInstall()).PulumiTest
56+
assert.NotEqual(t, converted.Source(), source.Source())
57+
58+
// Fix up the python runtime to use venv.
59+
config, err := os.ReadFile(filepath.Join(converted.WorkingDir(), "Pulumi.yaml"))
60+
assert.NoError(t, err)
61+
config = []byte(strings.Replace(string(config), "runtime: python", fixedPythonRuntime, 1))
62+
os.WriteFile(filepath.Join(converted.WorkingDir(), "Pulumi.yaml"), config, 0644)
63+
64+
converted.Install(t)
65+
66+
pythonPreview := converted.Preview(t)
67+
assert.Equal(t,
68+
map[apitype.OpType]int{apitype.OpCreate: 2},
69+
pythonPreview.ChangeSummary)
70+
71+
pythonUp := converted.Up(t)
72+
assert.Equal(t,
73+
map[string]int{"create": 2},
74+
*pythonUp.Summary.ResourceChanges)
75+
76+
assertup.HasNoDeletes(t, pythonUp)
77+
78+
// Show the deploy output.
79+
t.Log(pythonUp.StdOut)
80+
}
81+
82+
func TestConvertGo(t *testing.T) {
83+
t.Parallel()
84+
85+
// No need to copy the source, since we're not going to modify it.
86+
source := pulumitest.NewPulumiTest(t, filepath.Join("testdata", "yaml_program"), opttest.TestInPlace())
87+
88+
// Convert the original source to Go.
89+
converted := source.Convert(t, "go").PulumiTest
90+
assert.NotEqual(t, converted.Source(), source.Source())
91+
92+
goPreview := converted.Preview(t)
93+
assert.Equal(t,
94+
map[apitype.OpType]int{apitype.OpCreate: 2},
95+
goPreview.ChangeSummary)
96+
97+
goUp := converted.Up(t)
98+
assert.Equal(t,
99+
map[string]int{"create": 2},
100+
*goUp.Summary.ResourceChanges)
101+
102+
assertup.HasNoDeletes(t, goUp)
103+
104+
// Show the deploy output.
105+
t.Log(goUp.StdOut)
106+
}
107+
108+
func TestConvertTypescript(t *testing.T) {
109+
t.Parallel()
110+
111+
// No need to copy the source, since we're not going to modify it.
112+
source := pulumitest.NewPulumiTest(t, filepath.Join("testdata", "yaml_program"), opttest.TestInPlace())
113+
114+
// Convert the original source to TypeScript.
115+
converted := source.Convert(t, "typescript").PulumiTest
116+
assert.NotEqual(t, converted.Source(), source.Source())
117+
118+
tsPreview := converted.Preview(t)
119+
assert.Equal(t,
120+
map[apitype.OpType]int{apitype.OpCreate: 2},
121+
tsPreview.ChangeSummary)
122+
123+
tsUp := converted.Up(t)
124+
assert.Equal(t,
125+
map[string]int{"create": 2},
126+
*tsUp.Summary.ResourceChanges)
127+
128+
assertup.HasNoDeletes(t, tsUp)
129+
130+
// Show the deploy output.
131+
t.Log(tsUp.StdOut)
132+
}
133+
134+
func TestConvertCsharp(t *testing.T) {
135+
t.Parallel()
136+
137+
// No need to copy the source, since we're not going to modify it.
138+
source := pulumitest.NewPulumiTest(t, filepath.Join("testdata", "yaml_program"), opttest.TestInPlace())
139+
140+
// Convert the original source to C#.
141+
converted := source.Convert(t, "csharp").PulumiTest
142+
assert.NotEqual(t, converted.Source(), source.Source())
143+
144+
csharpPreview := converted.Preview(t)
145+
assert.Equal(t,
146+
map[apitype.OpType]int{apitype.OpCreate: 2},
147+
csharpPreview.ChangeSummary)
148+
149+
csharpUp := converted.Up(t)
150+
assert.Equal(t,
151+
map[string]int{"create": 2},
152+
*csharpUp.Summary.ResourceChanges)
153+
154+
assertup.HasNoDeletes(t, csharpUp)
155+
156+
// Show the deploy output.
157+
t.Log(csharpUp.StdOut)
158+
}

0 commit comments

Comments
 (0)