Skip to content

Commit fa76324

Browse files
Improve test isolation for TestTranslateCodeBlocks (#2981)
This pull request fixes up `TestTranslateCodeBlocks`: - Provides a fully assigned Generator with a mock test host to the test case. This: - prevents the underlying code from attempting to re-create a Generator for each Convert call, resulting in very quick rate limiting - additionally prevents the automatic downloading of the terraform converter plugin - Ensures the test provider names in the test files match the test provider name in the test (`simple`). - Removes the `required_providers` section as unnecessary As a result of pulumi/pulumi-converter-terraform#319, which is now in use by the bridge, we also no longer download the terraform-provider for a nonexisting provider. Fixes #2974
1 parent 7ad8ca6 commit fa76324

File tree

5 files changed

+50
-84
lines changed

5 files changed

+50
-84
lines changed

pkg/tfgen/installation_docs_test.go

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ func TestTranslateCodeBlocks(t *testing.T) {
425425
contentStr string
426426
g *Generator
427427
}
428-
p := tfbridge.ProviderInfo{
428+
providerInfo := tfbridge.ProviderInfo{
429429
Name: "simple",
430430
P: sdkv2.NewProvider(&schema.Provider{
431431
ResourcesMap: map[string]*schema.Resource{
@@ -464,60 +464,37 @@ func TestTranslateCodeBlocks(t *testing.T) {
464464
},
465465
},
466466
}
467-
pclsMap := make(map[string]translatedExample)
467+
generator, err := NewGenerator(GeneratorOptions{
468+
Language: RegistryDocs,
469+
PluginHost: &testPluginHost{},
470+
ProviderInfo: providerInfo,
471+
})
472+
assert.NoError(t, err)
468473

469474
testCases := []testCase{
470475
{
471476
name: "configuration",
472477
desc: "Translates HCL from examples ",
473478
contentStr: readfile(t, "test_data/installation-docs/configuration.md"),
474-
g: &Generator{
475-
sink: mockSink{},
476-
cliConverterState: &cliConverter{
477-
info: p,
478-
pcls: pclsMap,
479-
},
480-
language: RegistryDocs,
481-
},
479+
g: generator,
482480
},
483481
{
484482
name: "invalid-example",
485483
desc: "Does not translate an invalid example and leaves example block blank",
486484
contentStr: readfile(t, "test_data/installation-docs/invalid-example.md"),
487-
g: &Generator{
488-
sink: mockSink{},
489-
cliConverterState: &cliConverter{
490-
info: p,
491-
pcls: pclsMap,
492-
},
493-
language: RegistryDocs,
494-
},
485+
g: generator,
495486
},
496487
{
497488
name: "provider-config-only",
498489
desc: "Translates standalone provider config into Pulumi config YAML",
499490
contentStr: readfile(t, "test_data/installation-docs/provider-config-only.md"),
500-
g: &Generator{
501-
sink: mockSink{},
502-
cliConverterState: &cliConverter{
503-
info: p,
504-
pcls: pclsMap,
505-
},
506-
language: RegistryDocs,
507-
},
491+
g: generator,
508492
},
509493
{
510494
name: "example-only",
511495
desc: "Translates standalone example into languages",
512496
contentStr: readfile(t, "test_data/installation-docs/example-only.md"),
513-
g: &Generator{
514-
sink: mockSink{},
515-
cliConverterState: &cliConverter{
516-
info: p,
517-
pcls: pclsMap,
518-
},
519-
language: RegistryDocs,
520-
},
497+
g: generator,
521498
},
522499
}
523500

pkg/tfgen/test_data/installation-docs/configuration.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,8 @@ Use the navigation to the left to read about the available resources.
33
## Example Usage
44

55
```hcl
6-
# Define required providers
7-
terraform {
8-
required_version = ">= 0.14.0"
9-
required_providers {
10-
simple = {
11-
source = "terraform-provider-simple/simple"
12-
version = "~> 1.53.0"
13-
}
14-
}
15-
}
16-
17-
# Configure the OpenStack Provider
18-
provider "simple-provider" {
6+
# Configure the Simple Provider
7+
provider "simple" {
198
user_name = "admin"
209
tenant_name = "admin"
2110
password = "pwd"

pkg/tfgen/test_data/installation-docs/provider-config-only.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ This example should translate at least the Pulumi config
33
## Example Usage
44

55
```hcl
6-
# Configure the OpenStack Provider
7-
provider "simple-provider" {
6+
# Configure the Simple Provider
7+
provider "simple" {
88
user_name = "admin"
99
tenant_name = "admin"
1010
password = "pwd"

pkg/tfgen/testdata/TestTranslateCodeBlocks/configuration.golden

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ Use the navigation to the left to read about the available resources.
99
name: configuration-example
1010
runtime: nodejs
1111
config:
12-
simple-provider:authUrl:
12+
simple:authUrl:
1313
value: http://myauthurl:5000/v3
14-
simple-provider:password:
14+
simple:password:
1515
value: pwd
16-
simple-provider:region:
16+
simple:region:
1717
value: RegionOne
18-
simple-provider:tenantName:
18+
simple:tenantName:
1919
value: admin
20-
simple-provider:userName:
20+
simple:userName:
2121
value: admin
2222

2323
```
@@ -39,15 +39,15 @@ export const someOutput = aResource.result;
3939
name: configuration-example
4040
runtime: python
4141
config:
42-
simple-provider:authUrl:
42+
simple:authUrl:
4343
value: http://myauthurl:5000/v3
44-
simple-provider:password:
44+
simple:password:
4545
value: pwd
46-
simple-provider:region:
46+
simple:region:
4747
value: RegionOne
48-
simple-provider:tenantName:
48+
simple:tenantName:
4949
value: admin
50-
simple-provider:userName:
50+
simple:userName:
5151
value: admin
5252

5353
```
@@ -68,15 +68,15 @@ pulumi.export("someOutput", a_resource["result"])
6868
name: configuration-example
6969
runtime: dotnet
7070
config:
71-
simple-provider:authUrl:
71+
simple:authUrl:
7272
value: http://myauthurl:5000/v3
73-
simple-provider:password:
73+
simple:password:
7474
value: pwd
75-
simple-provider:region:
75+
simple:region:
7676
value: RegionOne
77-
simple-provider:tenantName:
77+
simple:tenantName:
7878
value: admin
79-
simple-provider:userName:
79+
simple:userName:
8080
value: admin
8181

8282
```
@@ -109,15 +109,15 @@ return await Deployment.RunAsync(() =>
109109
name: configuration-example
110110
runtime: go
111111
config:
112-
simple-provider:authUrl:
112+
simple:authUrl:
113113
value: http://myauthurl:5000/v3
114-
simple-provider:password:
114+
simple:password:
115115
value: pwd
116-
simple-provider:region:
116+
simple:region:
117117
value: RegionOne
118-
simple-provider:tenantName:
118+
simple:tenantName:
119119
value: admin
120-
simple-provider:userName:
120+
simple:userName:
121121
value: admin
122122

123123
```
@@ -151,15 +151,15 @@ func main() {
151151
name: configuration-example
152152
runtime: yaml
153153
config:
154-
simple-provider:authUrl:
154+
simple:authUrl:
155155
value: http://myauthurl:5000/v3
156-
simple-provider:password:
156+
simple:password:
157157
value: pwd
158-
simple-provider:region:
158+
simple:region:
159159
value: RegionOne
160-
simple-provider:tenantName:
160+
simple:tenantName:
161161
value: admin
162-
simple-provider:userName:
162+
simple:userName:
163163
value: admin
164164

165165
```
@@ -182,15 +182,15 @@ outputs:
182182
name: configuration-example
183183
runtime: java
184184
config:
185-
simple-provider:authUrl:
185+
simple:authUrl:
186186
value: http://myauthurl:5000/v3
187-
simple-provider:password:
187+
simple:password:
188188
value: pwd
189-
simple-provider:region:
189+
simple:region:
190190
value: RegionOne
191-
simple-provider:tenantName:
191+
simple:tenantName:
192192
value: admin
193-
simple-provider:userName:
193+
simple:userName:
194194
value: admin
195195

196196
```

pkg/tfgen/testdata/TestTranslateCodeBlocks/provider-config-only.golden

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ This example should translate at least the Pulumi config
77
name: configuration-example
88
runtime:
99
config:
10-
simple-provider:authUrl:
10+
simple:authUrl:
1111
value: http://myauthurl:5000/v3
12-
simple-provider:password:
12+
simple:password:
1313
value: pwd
14-
simple-provider:region:
14+
simple:region:
1515
value: RegionOne
16-
simple-provider:tenantName:
16+
simple:tenantName:
1717
value: admin
18-
simple-provider:userName:
18+
simple:userName:
1919
value: admin
2020

2121
```

0 commit comments

Comments
 (0)