/
program_driver.go
802 lines (759 loc) 路 25 KB
/
program_driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
package test
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/blang/semver"
"github.com/hashicorp/hcl/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/pulumi/pulumi/pkg/v3/codegen"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
"github.com/pulumi/pulumi/pkg/v3/codegen/pcl"
"github.com/pulumi/pulumi/pkg/v3/codegen/testing/utils"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
)
const (
transpiledExamplesDir = "transpiled_examples"
)
func transpiled(dir string) string {
return filepath.Join(transpiledExamplesDir, dir)
}
var allProgLanguages = codegen.NewStringSet("dotnet", "python", "go", "nodejs")
type ProgramTest struct {
Directory string
Description string
Skip codegen.StringSet
ExpectNYIDiags codegen.StringSet
SkipCompile codegen.StringSet
BindOptions []pcl.BindOption
MockPluginVersions map[string]string
}
var testdataPath = filepath.Join("..", "testing", "test", "testdata")
// Get batch number k (base-1 indexed) of tests out of n batches total.
func ProgramTestBatch(k, n int) []ProgramTest {
start := ((k - 1) * len(PulumiPulumiProgramTests)) / n
end := ((k) * len(PulumiPulumiProgramTests)) / n
return PulumiPulumiProgramTests[start:end]
}
// Useful when debugging a single test case.
func SingleTestCase(directoryName string) []ProgramTest {
output := make([]ProgramTest, 0)
for _, t := range PulumiPulumiProgramTests {
if t.Directory == directoryName {
output = append(output, t)
}
}
return output
}
var PulumiPulumiProgramTests = []ProgramTest{
{
Directory: "assets-archives",
Description: "Assets and archives",
},
{
Directory: "synthetic-resource-properties",
Description: "Synthetic resource properties",
SkipCompile: codegen.NewStringSet("nodejs", "dotnet", "go"), // not a real package
},
{
Directory: "aws-s3-folder",
Description: "AWS S3 Folder",
ExpectNYIDiags: codegen.NewStringSet("dotnet", "python"),
SkipCompile: codegen.NewStringSet("go", "python"),
// Blocked on python: TODO[pulumi/pulumi#8062]: Re-enable this test.
// Blocked on go:
// TODO[pulumi/pulumi#8064]
// TODO[pulumi/pulumi#8065]
},
{
Directory: "aws-eks",
Description: "AWS EKS",
},
{
Directory: "aws-fargate",
Description: "AWS Fargate",
},
{
Directory: "aws-static-website",
Description: "an example resource from AWS static website multi-language component",
// TODO: blocked on resolving imports (python) / using statements (C#) for types from external packages
SkipCompile: codegen.NewStringSet("dotnet", "python"),
},
{
Directory: "aws-fargate-output-versioned",
Description: "AWS Fargate Using Output-versioned invokes for python and typescript",
Skip: codegen.NewStringSet("go", "dotnet"),
BindOptions: []pcl.BindOption{pcl.PreferOutputVersionedInvokes},
},
{
Directory: "aws-s3-logging",
Description: "AWS S3 with logging",
SkipCompile: codegen.NewStringSet("go"),
// Blocked on nodejs: TODO[pulumi/pulumi#8068]
// Flaky in go: TODO[pulumi/pulumi#8123]
},
{
Directory: "aws-iam-policy",
Description: "AWS IAM Policy",
},
{
Directory: "read-file-func",
Description: "ReadFile function translation works",
},
{
Directory: "python-regress-10914",
Description: "Python regression test for #10914",
Skip: allProgLanguages.Except("python"),
},
{
Directory: "simplified-invokes",
Description: "Simplified invokes",
Skip: codegen.NewStringSet("python", "go"),
SkipCompile: codegen.NewStringSet("dotnet", "nodejs"),
},
{
Directory: "aws-optionals",
Description: "AWS get invoke with nested object constructor that takes an optional string",
// Testing Go behavior exclusively:
Skip: allProgLanguages.Except("go"),
},
{
Directory: "aws-webserver",
Description: "AWS Webserver",
},
{
Directory: "simple-range",
Description: "Simple range as int expression translation",
},
{
Directory: "azure-native",
Description: "Azure Native",
SkipCompile: codegen.NewStringSet("go", "nodejs", "dotnet"),
// Blocked on go:
// TODO[pulumi/pulumi#8073]
// TODO[pulumi/pulumi#8074]
// Blocked on nodejs:
// TODO[pulumi/pulumi#8075]
},
{
Directory: "azure-sa",
Description: "Azure SA",
},
{
Directory: "string-enum-union-list",
Description: "Contains resource which has a property of type List<Union<String, Enum>>",
// skipping compiling on Go because it doesn't know to handle unions in lists
// and instead generates pulumi.StringArray
SkipCompile: codegen.NewStringSet("go"),
},
{
Directory: "using-object-as-input-for-any",
Description: "Tests using object as input for a property of type 'any'",
},
{
Directory: "kubernetes-operator",
Description: "K8s Operator",
},
{
Directory: "kubernetes-pod",
Description: "K8s Pod",
SkipCompile: codegen.NewStringSet("go", "nodejs"),
// Blocked on go:
// TODO[pulumi/pulumi#8073]
// TODO[pulumi/pulumi#8074]
// Blocked on nodejs:
// TODO[pulumi/pulumi#8075]
},
{
Directory: "kubernetes-template",
Description: "K8s Template",
},
{
Directory: "kubernetes-template-quoted",
Description: "K8s Template with quoted string property keys to ensure that resource binding works here",
},
{
Directory: "random-pet",
Description: "Random Pet",
},
{
Directory: "aws-secret",
Description: "Secret",
},
{
Directory: "functions",
Description: "Functions",
},
{
Directory: "output-funcs-aws",
Description: "Output Versioned Functions",
},
{
Directory: "third-party-package",
Description: "Ensuring correct imports for third party packages",
// compiling and type checking involves downloading the real package to
// check against. Because we are checking against the "other" package
// (which doesn't exist), this does not work.
SkipCompile: codegen.NewStringSet("nodejs", "dotnet", "go"),
},
{
Directory: "invalid-go-sprintf",
Description: "Regress invalid Go",
Skip: codegen.NewStringSet("python", "nodejs", "dotnet"),
},
{
Directory: "typed-enum",
Description: "Supply strongly typed enums",
Skip: codegen.NewStringSet(golang),
},
{
Directory: "pulumi-stack-reference",
Description: "StackReference as resource",
},
{
Directory: "python-resource-names",
Description: "Repro for #9357",
Skip: codegen.NewStringSet("go", "nodejs", "dotnet"),
},
{
Directory: "logical-name",
Description: "Logical names",
},
{
Directory: "aws-lambda",
Description: "AWS Lambdas",
// We have special testing for this case because lambda is a python keyword.
Skip: codegen.NewStringSet("go", "nodejs", "dotnet"),
},
{
Directory: "basic-unions",
Description: "Tests program generation of fields of type union",
SkipCompile: allProgLanguages, // because the schema is synthetic
},
{
Directory: "traverse-union-repro",
Description: `Handling the error "cannot traverse value of type union(...)"`,
BindOptions: []pcl.BindOption{
pcl.SkipResourceTypechecking,
pcl.AllowMissingVariables,
pcl.AllowMissingProperties,
},
// The example is known to be invalid. Specifically it hands a
// `[aws_subnet.test1.id]` to a `string` attribute, where `aws_subnet` is not in
// scope.
//
// The example is invalid in two ways:
// 1. `aws_subnet` is a missing variable.
// 2. `[...]` is a tuple, which can never be a string.
//
// Even though the generated code will not type check, it should still be
// directionally correct.
SkipCompile: allProgLanguages,
},
{
Directory: "components",
Description: "Components",
SkipCompile: codegen.NewStringSet("go"),
},
{
Directory: "entries-function",
Description: "Using the entries function",
// go and dotnet do fully not support GenForExpression yet
// Todo: https://github.com/pulumi/pulumi/issues/12606
SkipCompile: allProgLanguages.Except("nodejs").Except("python"),
},
{
Directory: "retain-on-delete",
Description: "Generate RetainOnDelete option",
},
{
Directory: "depends-on-array",
Description: "Using DependsOn resource option with an array of resources",
},
{
Directory: "multiline-string",
Description: "Multiline string literals",
},
{
Directory: "config-variables",
Description: "Basic program with a bunch of config variables",
// TODO[https://github.com/pulumi/pulumi/issues/14957] - object config variables are broken here
SkipCompile: codegen.NewStringSet("go", "dotnet"),
},
{
Directory: "regress-11176",
Description: "Regression test for https://github.com/pulumi/pulumi/issues/11176",
Skip: allProgLanguages.Except("go"),
},
{
Directory: "throw-not-implemented",
Description: "Function notImplemented is compiled to a runtime error at call-site",
},
{
Directory: "python-reserved",
Description: "Test python reserved words aren't used",
Skip: allProgLanguages.Except("python"),
},
{
Directory: "iterating-optional-range-expressions",
Description: "Test that we can iterate over range expression that are option(iterator)",
// TODO: dotnet and go
Skip: allProgLanguages.Except("nodejs").Except("python"),
// We are using a synthetic schema defined in range-1.0.0.json so we can't compile all the way
SkipCompile: allProgLanguages,
},
{
Directory: "output-literals",
Description: "Tests that we can return various literal values via stack outputs",
SkipCompile: codegen.NewStringSet("go"),
},
{
Directory: "dynamic-entries",
Description: "Testing iteration of dynamic entries in TypeScript",
Skip: allProgLanguages.Except("nodejs"),
SkipCompile: allProgLanguages,
},
{
Directory: "single-or-none",
Description: "Tests using the singleOrNone function",
// TODO[pulumi/pulumi#4899]: Skip compiling for Go because it is trying to pass a value of type float64
// as an argument to ctx.Export but float64 does not implement pulumi.Input. The value needs to be
// wrapped as a pulumi.Float64.
SkipCompile: codegen.NewStringSet("go"),
},
{
Directory: "simple-splat",
Description: "An example that shows we can compile splat expressions from array of objects",
// Skip compiling because we are using a test schema without a corresponding real package
SkipCompile: allProgLanguages,
},
{
Directory: "invoke-inside-conditional-range",
Description: "Using the result of an invoke inside a conditional range expression of a resource",
Skip: allProgLanguages.Except("nodejs").Except("dotnet"),
SkipCompile: allProgLanguages,
},
{
Directory: "output-name-conflict",
Description: "Tests whether we are able to generate programs where output variables have same id as config var",
SkipCompile: codegen.NewStringSet("go"),
},
{
Directory: "snowflake-python-12998",
Description: "Tests regression for issue https://github.com/pulumi/pulumi/issues/12998",
Skip: allProgLanguages.Except("python"),
SkipCompile: allProgLanguages,
BindOptions: []pcl.BindOption{pcl.AllowMissingVariables, pcl.AllowMissingProperties},
},
{
Directory: "unknown-resource",
Description: "Tests generating code for unknown resources when skipping resource type-checking",
SkipCompile: allProgLanguages,
BindOptions: []pcl.BindOption{pcl.SkipResourceTypechecking},
},
{
Directory: "using-dashes",
Description: "Test program generation on packages with a dash in the name",
SkipCompile: allProgLanguages, // since we are using a synthetic schema
},
{
Directory: "unknown-invoke",
Description: "Tests generating code for unknown invokes when skipping invoke type checking",
SkipCompile: allProgLanguages,
BindOptions: []pcl.BindOption{pcl.SkipInvokeTypechecking},
},
{
Directory: "optional-complex-config",
Description: "Tests generating code for optional and complex config values",
Skip: allProgLanguages.Except("nodejs").Except("dotnet"),
SkipCompile: allProgLanguages.Except("nodejs").Except("dotnet"),
},
{
Directory: "interpolated-string-keys",
Description: "Tests that interpolated string keys are supported in maps. ",
Skip: allProgLanguages.Except("nodejs").Except("python"),
},
{
Directory: "regress-node-12507",
Description: "Regression test for https://github.com/pulumi/pulumi/issues/12507",
Skip: allProgLanguages.Except("nodejs"),
BindOptions: []pcl.BindOption{pcl.PreferOutputVersionedInvokes},
},
{
Directory: "csharp-plain-lists",
Description: "Tests that plain lists are supported in C#",
Skip: allProgLanguages.Except("dotnet"),
},
{
Directory: "csharp-typed-for-expressions",
Description: "Testing for expressions with typed target expressions in csharp",
Skip: allProgLanguages.Except("dotnet"),
},
{
Directory: "empty-list-property",
Description: "Tests compiling empty list expressions of object properties",
},
{
Directory: "python-regress-14037",
Description: "Regression test for rewriting qoutes in python",
Skip: allProgLanguages.Except("python"),
},
{
Directory: "inline-invokes",
Description: "Tests whether using inline invoke expressions works",
SkipCompile: codegen.NewStringSet("go"),
},
}
var PulumiPulumiYAMLProgramTests = []ProgramTest{
// PCL files from pulumi/yaml transpiled examples
{
Directory: transpiled("aws-eks"),
Description: "AWS EKS",
Skip: codegen.NewStringSet("go", "nodejs", "dotnet"),
},
{
Directory: transpiled("aws-static-website"),
Description: "AWS static website",
Skip: codegen.NewStringSet("go", "nodejs", "dotnet"),
BindOptions: []pcl.BindOption{pcl.SkipResourceTypechecking},
},
{
Directory: transpiled("awsx-fargate"),
Description: "AWSx Fargate",
Skip: codegen.NewStringSet("dotnet", "nodejs", "go"),
},
{
Directory: transpiled("azure-app-service"),
Description: "Azure App Service",
Skip: codegen.NewStringSet("go", "dotnet"),
},
{
Directory: transpiled("azure-container-apps"),
Description: "Azure Container Apps",
Skip: codegen.NewStringSet("go", "nodejs", "dotnet", "python"),
},
{
Directory: transpiled("azure-static-website"),
Description: "Azure static website",
Skip: codegen.NewStringSet("go", "nodejs", "dotnet", "python"),
},
{
Directory: transpiled("cue-eks"),
Description: "Cue EKS",
Skip: codegen.NewStringSet("go", "nodejs", "dotnet"),
},
{
Directory: transpiled("cue-random"),
Description: "Cue random",
},
{
Directory: transpiled("getting-started"),
Description: "Getting started",
},
{
Directory: transpiled("kubernetes"),
Description: "Kubernetes",
Skip: codegen.NewStringSet("go"),
},
{
Directory: transpiled("pulumi-variable"),
Description: "Pulumi variable",
Skip: codegen.NewStringSet("go", "nodejs", "dotnet"),
},
{
Directory: transpiled("random"),
Description: "Random",
Skip: codegen.NewStringSet("nodejs"),
},
{
Directory: transpiled("readme"),
Description: "README",
Skip: codegen.NewStringSet("go", "dotnet"),
},
{
Directory: transpiled("stackreference-consumer"),
Description: "Stack reference consumer",
Skip: codegen.NewStringSet("go", "nodejs", "dotnet"),
},
{
Directory: transpiled("stackreference-producer"),
Description: "Stack reference producer",
Skip: codegen.NewStringSet("go", "dotnet"),
},
{
Directory: transpiled("webserver-json"),
Description: "Webserver JSON",
Skip: codegen.NewStringSet("go", "dotnet", "python"),
},
{
Directory: transpiled("webserver"),
Description: "Webserver",
Skip: codegen.NewStringSet("go", "dotnet", "python"),
},
}
// Checks that a generated program is correct
//
// The arguments are to be read:
// (Testing environment, path to generated code, set of dependencies)
type CheckProgramOutput = func(*testing.T, string, codegen.StringSet)
// Generates a program from a pcl.Program
type GenProgram = func(program *pcl.Program) (map[string][]byte, hcl.Diagnostics, error)
// Generates a project from a pcl.Program
type GenProject = func(
directory string, project workspace.Project,
program *pcl.Program, localDependencies map[string]string,
) error
type ProgramCodegenOptions struct {
Language string
Extension string
OutputFile string
Check CheckProgramOutput
GenProgram GenProgram
TestCases []ProgramTest
// For generating a full project
IsGenProject bool
GenProject GenProject
// Maps a test file (i.e. "aws-resource-options") to a struct containing a package
// (i.e. "github.com/pulumi/pulumi-aws/sdk/v5", "pulumi-aws) and its
// version prefixed by an operator (i.e. " v5.11.0", ==5.11.0")
ExpectedVersion map[string]PkgVersionInfo
DependencyFile string
}
type PkgVersionInfo struct {
Pkg string
OpAndVersion string
}
// TestProgramCodegen runs the complete set of program code generation tests against a particular
// language's code generator.
//
// A program code generation test consists of a PCL file (.pp extension) and a set of expected outputs
// for each language.
//
// The PCL file is the only piece that must be manually authored. Once the schema has been written, the expected outputs
// can be generated by running `PULUMI_ACCEPT=true go test ./..." from the `pkg/codegen` directory.
//
//nolint:revive
func TestProgramCodegen(
t *testing.T,
testcase ProgramCodegenOptions,
) {
if runtime.GOOS == "windows" {
t.Skip("TestProgramCodegen is skipped on Windows")
}
assert.NotNil(t, testcase.TestCases, "Caller must provide test cases")
pulumiAccept := cmdutil.IsTruthy(os.Getenv("PULUMI_ACCEPT"))
skipCompile := cmdutil.IsTruthy(os.Getenv("PULUMI_SKIP_COMPILE_TEST"))
for _, tt := range testcase.TestCases {
tt := tt // avoid capturing loop variable
t.Run(tt.Directory, func(t *testing.T) {
// These tests should not run in parallel.
// They take up a fair bit of memory
// and can OOM in CI with too many running.
var err error
if tt.Skip.Has(testcase.Language) {
t.Skip()
return
}
expectNYIDiags := tt.ExpectNYIDiags.Has(testcase.Language)
testDir := filepath.Join(testdataPath, tt.Directory+"-pp")
pclFile := filepath.Join(testDir, tt.Directory+".pp")
if strings.HasPrefix(tt.Directory, transpiledExamplesDir) {
pclFile = filepath.Join(testDir, filepath.Base(tt.Directory)+".pp")
}
testDir = filepath.Join(testDir, testcase.Language)
err = os.MkdirAll(testDir, 0o700)
if err != nil && !os.IsExist(err) {
t.Fatalf("Failed to create %q: %s", testDir, err)
}
contents, err := os.ReadFile(pclFile)
if err != nil {
t.Fatalf("could not read %v: %v", pclFile, err)
}
expectedFile := filepath.Join(testDir, tt.Directory+"."+testcase.Extension)
if strings.HasPrefix(tt.Directory, transpiledExamplesDir) {
expectedFile = filepath.Join(testDir, filepath.Base(tt.Directory)+"."+testcase.Extension)
}
expected, err := os.ReadFile(expectedFile)
if err != nil && !pulumiAccept {
t.Fatalf("could not read %v: %v", expectedFile, err)
}
parser := syntax.NewParser()
err = parser.ParseFile(bytes.NewReader(contents), tt.Directory+".pp")
if err != nil {
t.Fatalf("could not read %v: %v", pclFile, err)
}
if parser.Diagnostics.HasErrors() {
t.Fatalf("failed to parse files: %v", parser.Diagnostics)
}
hclFiles := map[string]*hcl.File{
tt.Directory + ".pp": {Body: parser.Files[0].Body, Bytes: parser.Files[0].Bytes},
}
opts := append(tt.BindOptions, pcl.PluginHost(utils.NewHost(testdataPath)))
rootProgramPath := filepath.Join(testdataPath, tt.Directory+"-pp")
absoluteProgramPath, err := filepath.Abs(rootProgramPath)
if err != nil {
t.Fatalf("failed to bind program: unable to find the absolute path of %v", rootProgramPath)
}
opts = append(opts, pcl.DirPath(absoluteProgramPath))
opts = append(opts, pcl.ComponentBinder(pcl.ComponentProgramBinderFromFileSystem()))
program, diags, err := pcl.BindProgram(parser.Files, opts...)
if err != nil {
t.Fatalf("could not bind program: %v", err)
}
bindDiags := new(bytes.Buffer)
if len(diags) > 0 {
require.NoError(t, hcl.NewDiagnosticTextWriter(bindDiags, hclFiles, 80, false).WriteDiagnostics(diags))
if diags.HasErrors() {
t.Fatalf("failed to bind program:\n%s", bindDiags)
}
t.Logf("bind diags:\n%s", bindDiags)
}
var files map[string][]byte
// generate a full project and check expected package versions
if testcase.IsGenProject {
project := workspace.Project{
Name: "test",
Runtime: workspace.NewProjectRuntimeInfo(testcase.Language, nil),
}
err = testcase.GenProject(testDir, project, program, nil /*localDependencies*/)
assert.NoError(t, err)
depFilePath := filepath.Join(testDir, testcase.DependencyFile)
outfilePath := filepath.Join(testDir, testcase.OutputFile)
CheckVersion(t, tt.Directory, depFilePath, testcase.ExpectedVersion)
GenProjectCleanUp(t, testDir, depFilePath, outfilePath)
}
files, diags, err = testcase.GenProgram(program)
assert.NoError(t, err)
if expectNYIDiags {
var tmpDiags hcl.Diagnostics
for _, d := range diags {
if !strings.HasPrefix(d.Summary, "not yet implemented") {
tmpDiags = append(tmpDiags, d)
}
}
diags = tmpDiags
}
if diags.HasErrors() {
buf := new(bytes.Buffer)
err := hcl.NewDiagnosticTextWriter(buf, hclFiles, 80, false).WriteDiagnostics(diags)
require.NoError(t, err, "Failed to write diag")
t.Fatalf("failed to generate program:\n%s", buf)
}
if pulumiAccept {
err := os.WriteFile(expectedFile, files[testcase.OutputFile], 0o600)
require.NoError(t, err)
// generate the rest of the files
for fileName, content := range files {
if fileName != testcase.OutputFile {
outputPath := filepath.Join(testDir, fileName)
err := os.WriteFile(outputPath, content, 0o600)
require.NoError(t, err, "Failed to write file %s", outputPath)
}
}
} else {
assert.Equal(t, string(expected), string(files[testcase.OutputFile]))
// assert that the content is correct for the rest of the files
for fileName, content := range files {
if fileName != testcase.OutputFile {
outputPath := filepath.Join(testDir, fileName)
outputContent, err := os.ReadFile(outputPath)
require.NoError(t, err)
assert.Equal(t, string(outputContent), string(content))
}
}
}
if !skipCompile && testcase.Check != nil && !tt.SkipCompile.Has(testcase.Language) {
extraPulumiPackages := codegen.NewStringSet()
collectExtraPulumiPackages(program, extraPulumiPackages)
testcase.Check(t, expectedFile, extraPulumiPackages)
}
})
}
}
func collectExtraPulumiPackages(program *pcl.Program, extraPulumiPackages codegen.StringSet) {
for _, n := range program.Nodes {
if r, isResource := n.(*pcl.Resource); isResource {
pkg, _, _, _ := r.DecomposeToken()
if pkg != "pulumi" {
extraPulumiPackages.Add(pkg)
}
}
if component, isComponent := n.(*pcl.Component); isComponent {
collectExtraPulumiPackages(component.Program, extraPulumiPackages)
}
}
}
// CheckVersion checks for an expected package version
// Todo: support checking multiple package expected versions
func CheckVersion(t *testing.T, dir, depFilePath string, expectedVersionMap map[string]PkgVersionInfo) {
depFile, err := os.Open(depFilePath)
require.NoError(t, err)
defer depFile.Close()
// Splits on newlines by default.
scanner := bufio.NewScanner(depFile)
match := false
expectedPkg, expectedVersion := strings.TrimSpace(expectedVersionMap[dir].Pkg),
strings.TrimSpace(expectedVersionMap[dir].OpAndVersion)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, expectedPkg) {
line = strings.TrimSpace(line)
actualVersion := strings.TrimPrefix(line, expectedPkg)
actualVersion = strings.TrimSpace(actualVersion)
expectedVersion = strings.Trim(expectedVersion, "v:^/> ")
actualVersion = strings.Trim(actualVersion, "v:^/> ")
if expectedVersion == actualVersion {
match = true
break
}
actualSemver, err := semver.Make(actualVersion)
if err == nil {
continue
}
expectedSemver, _ := semver.Make(expectedVersion)
if actualSemver.Compare(expectedSemver) >= 0 {
match = true
break
}
}
}
require.Truef(t, match, "Did not find expected package version pair (%q,%q). Searched in:\n%s",
expectedPkg, expectedVersion, newLazyStringer(func() string {
// Reset the read on the file
_, err := depFile.Seek(0, io.SeekStart)
require.NoError(t, err)
buf := new(strings.Builder)
_, err = io.Copy(buf, depFile)
require.NoError(t, err)
return buf.String()
}).String())
}
func GenProjectCleanUp(t *testing.T, dir, depFilePath, outfilePath string) {
os.Remove(depFilePath)
os.Remove(outfilePath)
os.Remove(dir + "/.gitignore")
os.Remove(dir + "/Pulumi.yaml")
}
type lazyStringer struct {
cache string
f func() string
}
func (l lazyStringer) String() string {
if l.cache == "" {
l.cache = l.f()
}
return l.cache
}
// The `fmt` `%s` calls .String() if the object is not a string itself. We can delay
// computing expensive display logic until and unless we actually will use it.
func newLazyStringer(f func() string) fmt.Stringer {
return lazyStringer{f: f}
}