@@ -358,6 +358,107 @@ func Test_TwoInstances_TypeScript(t *testing.T) {
358
358
})
359
359
}
360
360
361
+ // Test that changing the source code of a local module causes the module to be updated
362
+ // without any changes to the program code.
363
+ // In this specific example, the change in the source code is from changing outputs
364
+ func TestChangingLocalModuleSourceCodeOutputsCausesUpdate (t * testing.T ) {
365
+ localProviderBinPath := ensureCompiledProvider (t )
366
+ modulePath := filepath .Join ("testdata" , "modules" , "changing_module_source_outputs" )
367
+ v1 , err := filepath .Abs (filepath .Join (modulePath , "mod_v1" ))
368
+ assert .NoError (t , err , "failed to get absolute path for v1 module" )
369
+ v2 , err := filepath .Abs (filepath .Join (modulePath , "mod_v2" ))
370
+ assert .NoError (t , err , "failed to get absolute path for v2 module" )
371
+
372
+ // YAML program that uses the module.
373
+ // We don't change anything here
374
+ program := filepath .Join ("testdata" , "programs" , "yaml" , "changing_module_source_outputs" )
375
+
376
+ integrationTest := pulumitest .NewPulumiTest (t , program ,
377
+ opttest .SkipInstall (),
378
+ opttest .LocalProviderPath (provider , filepath .Dir (localProviderBinPath )))
379
+
380
+ pulumiPackageAdd (t , integrationTest , localProviderBinPath , v1 , "greeting" )
381
+
382
+ resultV1 := integrationTest .Up (t )
383
+ assert .Len (t , resultV1 .Outputs , 1 , "expected one output" )
384
+ greeting , ok := resultV1 .Outputs ["greeting" ]
385
+ require .True (t , ok , "expected output greeting" )
386
+ require .Equal (t , "Hello, John!" , greeting .Value )
387
+
388
+ // Now change the source code of the module from v1 to v2
389
+ v1SourceCode , err := os .ReadFile (filepath .Join (v1 , "main.tf" ))
390
+ require .NoError (t , err , "failed to read v1 source code" )
391
+ v2SourceCode , err := os .ReadFile (filepath .Join (v2 , "main.tf" ))
392
+ require .NoError (t , err , "failed to read v2 source code" )
393
+
394
+ err = os .WriteFile (filepath .Join (v1 , "main.tf" ), v2SourceCode , 0600 )
395
+ require .NoError (t , err , "failed to write v2 source code to v1 module" )
396
+
397
+ t .Cleanup (func () {
398
+ // Restore the original source code after the test
399
+ err := os .WriteFile (filepath .Join (v1 , "main.tf" ), v1SourceCode , 0600 )
400
+ require .NoError (t , err , "failed to restore v1 source code" )
401
+ })
402
+
403
+ t .Logf ("Changed module source code to v2" )
404
+ resultV2 := integrationTest .Up (t )
405
+ assert .Len (t , resultV2 .Outputs , 1 , "expected one output" )
406
+ greeting , ok = resultV2 .Outputs ["greeting" ]
407
+ require .True (t , ok , "expected output greeting" )
408
+ require .Equal (t , "Goodbye, John!" , greeting .Value )
409
+ }
410
+
411
+ // Test that changing the source code of a local module causes the module to be updated
412
+ // In this specific example, the change in the source code is from changing resources
413
+ func TestChangingLocalModuleSourceCodeResourcesCausesUpdate (t * testing.T ) {
414
+ localProviderBinPath := ensureCompiledProvider (t )
415
+ modulePath := filepath .Join ("testdata" , "modules" , "changing_module_source_resources" )
416
+ v1 , err := filepath .Abs (filepath .Join (modulePath , "mod_v1" ))
417
+ assert .NoError (t , err , "failed to get absolute path for v1 module" )
418
+ v2 , err := filepath .Abs (filepath .Join (modulePath , "mod_v2" ))
419
+ assert .NoError (t , err , "failed to get absolute path for v2 module" )
420
+
421
+ // YAML program that uses the module.
422
+ // We don't change anything here
423
+ program := filepath .Join ("testdata" , "programs" , "yaml" , "changing_module_source_resources" )
424
+
425
+ integrationTest := pulumitest .NewPulumiTest (t , program ,
426
+ opttest .SkipInstall (),
427
+ opttest .LocalProviderPath (provider , filepath .Dir (localProviderBinPath )))
428
+
429
+ pulumiPackageAdd (t , integrationTest , localProviderBinPath , v1 , "rand" )
430
+
431
+ integrationTest .Up (t )
432
+ // assert that the module was created with the expected resource
433
+ mustFindDeploymentResourceByType (t , integrationTest , "rand:tf:random_integer" )
434
+
435
+ // Now change the source code of the module from v1 to v2
436
+ v1SourceCode , err := os .ReadFile (filepath .Join (v1 , "main.tf" ))
437
+ require .NoError (t , err , "failed to read v1 source code" )
438
+ v2SourceCode , err := os .ReadFile (filepath .Join (v2 , "main.tf" ))
439
+ require .NoError (t , err , "failed to read v2 source code" )
440
+
441
+ err = os .WriteFile (filepath .Join (v1 , "main.tf" ), v2SourceCode , 0600 )
442
+ require .NoError (t , err , "failed to write v2 source code to v1 module" )
443
+
444
+ t .Cleanup (func () {
445
+ // Restore the original source code after the test
446
+ err := os .WriteFile (filepath .Join (v1 , "main.tf" ), v1SourceCode , 0600 )
447
+ require .NoError (t , err , "failed to restore v1 source code" )
448
+ })
449
+
450
+ preview := integrationTest .Preview (t )
451
+ assert .Equal (t , 1 , preview .ChangeSummary [apitype .OpType ("delete" )],
452
+ "expected one resource to be deleted" )
453
+ assert .Equal (t , 1 , preview .ChangeSummary [apitype .OpType ("create" )],
454
+ "expected one resource to be created" )
455
+
456
+ integrationTest .Up (t )
457
+ // assert that the resource within the module has changed
458
+ // from random_integer into random_pet
459
+ mustFindDeploymentResourceByType (t , integrationTest , "rand:tf:random_pet" )
460
+ }
461
+
361
462
func TestGenerateTerraformAwsModulesSDKs (t * testing.T ) {
362
463
t .Parallel ()
363
464
@@ -651,6 +752,8 @@ func TestE2eTs(t *testing.T) {
651
752
upExpect map [string ]int
652
753
deleteExpect map [string ]int
653
754
diffNoChangesExpect map [apitype.OpType ]int
755
+ previewRefresh bool // Whether to run preview with refresh enabled
756
+ skipNoChangesExpect bool // Whether to skip the no changes expectation
654
757
}
655
758
656
759
testcases := []testCase {
@@ -677,6 +780,8 @@ func TestE2eTs(t *testing.T) {
677
780
moduleName : "terraform-aws-modules/lambda/aws" ,
678
781
moduleVersion : "7.20.1" ,
679
782
moduleNamespace : "lambda" ,
783
+ // aws lambda module creates drift even in terraform
784
+ skipNoChangesExpect : true ,
680
785
previewExpect : map [apitype.OpType ]int {
681
786
apitype .OpType ("create" ): 8 ,
682
787
},
@@ -702,6 +807,9 @@ func TestE2eTs(t *testing.T) {
702
807
moduleName : "terraform-aws-modules/rds/aws" ,
703
808
moduleVersion : "6.10.0" ,
704
809
moduleNamespace : "rds" ,
810
+ // RDS module has immediate drift after creation,
811
+ // so we need to refresh to get the correct preview
812
+ previewRefresh : true ,
705
813
previewExpect : map [apitype.OpType ]int {
706
814
apitype .OpType ("create" ): 10 ,
707
815
},
@@ -736,7 +844,11 @@ func TestE2eTs(t *testing.T) {
736
844
pulumiPackageAdd (t , integrationTest , localProviderBinPath , tc .moduleName , tc .moduleVersion , tc .moduleNamespace )
737
845
738
846
// Preview
739
- previewResult := integrationTest .Preview (t , optpreview .Diff ())
847
+ previewOptions := []optpreview.Option {optpreview .Diff ()}
848
+ if tc .previewRefresh {
849
+ previewOptions = append (previewOptions , optpreview .Refresh ())
850
+ }
851
+ previewResult := integrationTest .Preview (t , previewOptions ... )
740
852
t .Logf ("pulumi preview:\n %s" , previewResult .StdOut + previewResult .StdErr )
741
853
autogold .Expect (tc .previewExpect ).Equal (t , previewResult .ChangeSummary )
742
854
@@ -746,9 +858,11 @@ func TestE2eTs(t *testing.T) {
746
858
autogold .Expect (& tc .upExpect ).Equal (t , upResult .Summary .ResourceChanges )
747
859
748
860
// Preview expect no changes
749
- previewResult = integrationTest .Preview (t , optpreview . Diff () )
861
+ previewResult = integrationTest .Preview (t , previewOptions ... )
750
862
t .Logf ("pulumi preview\n %s" , previewResult .StdOut + previewResult .StdErr )
751
- autogold .Expect (tc .diffNoChangesExpect ).Equal (t , previewResult .ChangeSummary )
863
+ if ! tc .skipNoChangesExpect {
864
+ autogold .Expect (tc .diffNoChangesExpect ).Equal (t , previewResult .ChangeSummary )
865
+ }
752
866
753
867
// Delete
754
868
destroyResult := integrationTest .Destroy (t )
0 commit comments