15
15
package crosstests
16
16
17
17
import (
18
- "os"
19
- "path/filepath"
18
+ "context"
20
19
21
20
"github.com/hashicorp/terraform-plugin-go/tftypes"
22
21
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
22
+ "github.com/pulumi/providertest/providers"
23
+ "github.com/pulumi/providertest/pulumitest"
24
+ "github.com/pulumi/providertest/pulumitest/optrun"
25
+ "github.com/pulumi/providertest/pulumitest/opttest"
23
26
"github.com/pulumi/pulumi/sdk/v3/go/auto/optpreview"
24
27
"github.com/stretchr/testify/require"
25
28
@@ -44,49 +47,76 @@ type diffTestCase struct {
44
47
ObjectType * tftypes.Object
45
48
DeleteBeforeReplace bool
46
49
DisableAccurateBridgePreviews bool
50
+
51
+ // Optional second schema to use as an upgrade test with a different schema.
52
+ Resource2 * schema.Resource
47
53
}
48
54
49
55
func runDiffCheck (t T , tc diffTestCase ) crosstestsimpl.DiffResult {
50
56
t .Helper ()
51
57
tfwd := t .TempDir ()
52
58
53
59
lifecycleArgs := lifecycleArgs {CreateBeforeDestroy : ! tc .DeleteBeforeReplace }
60
+ resource1 := tc .Resource
61
+ resource2 := tc .Resource2
62
+ if resource2 == nil {
63
+ resource2 = resource1
64
+ }
65
+
66
+ tfConfig1 := coalesceInputs (t , resource1 .Schema , tc .Config1 )
67
+ tfd := newTFResDriver (t , tfwd , defProviderShortName , defRtype , resource1 )
68
+ _ = tfd .writePlanApply (t , resource1 .Schema , defRtype , "example" , tfConfig1 , lifecycleArgs )
54
69
55
- tfConfig1 := coalesceInputs (t , tc .Resource .Schema , tc .Config1 )
56
- tfConfig2 := coalesceInputs (t , tc .Resource .Schema , tc .Config2 )
57
- tfd := newTFResDriver (t , tfwd , defProviderShortName , defRtype , tc .Resource )
58
- _ = tfd .writePlanApply (t , tc .Resource .Schema , defRtype , "example" , tfConfig1 , lifecycleArgs )
59
- tfDiffPlan := tfd .writePlanApply (t , tc .Resource .Schema , defRtype , "example" , tfConfig2 , lifecycleArgs )
70
+ tfConfig2 := coalesceInputs (t , resource2 .Schema , tc .Config2 )
71
+ tfd2 := newTFResDriver (t , tfwd , defProviderShortName , defRtype , resource2 )
72
+ tfDiffPlan := tfd2 .writePlanApply (t , resource2 .Schema , defRtype , "example" , tfConfig2 , lifecycleArgs )
60
73
61
- resMap := map [string ]* schema.Resource {defRtype : tc . Resource }
62
- tfp := & schema.Provider {ResourcesMap : resMap }
74
+ tfp1 := & schema. Provider { ResourcesMap : map [string ]* schema.Resource {defRtype : resource1 } }
75
+ tfp2 := & schema.Provider {ResourcesMap : map [ string ] * schema. Resource { defRtype : resource2 } }
63
76
64
77
opts := []pulcheck.BridgedProviderOpt {}
65
78
if ! tc .DisableAccurateBridgePreviews {
66
79
opts = append (opts , pulcheck .EnableAccurateBridgePreviews ())
67
80
}
68
81
69
- bridgedProvider := pulcheck .BridgedProvider (t , defProviderShortName , tfp , opts ... )
82
+ bridgedProvider1 := pulcheck .BridgedProvider (t , defProviderShortName , tfp1 , opts ... )
83
+ bridgedProvider2 := pulcheck .BridgedProvider (t , defProviderShortName , tfp2 , opts ... )
70
84
if tc .DeleteBeforeReplace {
71
- bridgedProvider .Resources [defRtype ].DeleteBeforeReplace = true
85
+ bridgedProvider1 .Resources [defRtype ].DeleteBeforeReplace = true
86
+ bridgedProvider2 .Resources [defRtype ].DeleteBeforeReplace = true
72
87
}
73
88
74
89
pd := & pulumiDriver {
75
90
name : defProviderShortName ,
76
91
pulumiResourceToken : defRtoken ,
77
92
tfResourceName : defRtype ,
78
93
}
79
-
80
- yamlProgram := pd .generateYAML (t , crosstestsimpl .InferPulumiValue (t ,
81
- bridgedProvider .P .ResourcesMap ().Get (defRtype ).Schema (), nil , tfConfig1 ))
82
- pt := pulcheck .PulCheck (t , bridgedProvider , string (yamlProgram ))
83
- pt .Up (t )
84
-
85
- yamlProgram = pd .generateYAML (t , crosstestsimpl .InferPulumiValue (t ,
86
- bridgedProvider .P .ResourcesMap ().Get (defRtype ).Schema (), nil , tfConfig2 ))
87
- err := os .WriteFile (filepath .Join (pt .CurrentStack ().Workspace ().WorkDir (), "Pulumi.yaml" ), yamlProgram , 0o600 )
88
- require .NoErrorf (t , err , "writing Pulumi.yaml" )
89
-
94
+ yamlProgram1 := pd .generateYAML (t , crosstestsimpl .InferPulumiValue (t ,
95
+ bridgedProvider1 .P .ResourcesMap ().Get (defRtype ).Schema (), nil , tfConfig1 ))
96
+
97
+ yamlProgram2 := pd .generateYAML (t , crosstestsimpl .InferPulumiValue (t ,
98
+ bridgedProvider2 .P .ResourcesMap ().Get (defRtype ).Schema (), nil , tfConfig2 ))
99
+
100
+ // We initialize the second provider as it will be used in the preview.
101
+ // It is temporarily overwritten by the first provider in the Run function.
102
+ pt := pulcheck .PulCheck (t , bridgedProvider2 , string (yamlProgram1 ))
103
+ pt .Run (
104
+ t ,
105
+ func (test * pulumitest.PulumiTest ) {
106
+ test .Up (t )
107
+ },
108
+ optrun .WithOpts (
109
+ opttest .AttachProvider (
110
+ defProviderShortName ,
111
+ func (ctx context.Context , pt providers.PulumiTest ) (providers.Port , error ) {
112
+ handle , err := pulcheck .StartPulumiProvider (ctx , bridgedProvider1 )
113
+ require .NoError (t , err )
114
+ return providers .Port (handle .Port ), nil
115
+ },
116
+ ),
117
+ ),
118
+ )
119
+ pt .WritePulumiYaml (t , string (yamlProgram2 ))
90
120
previewRes := pt .Preview (t , optpreview .Diff ())
91
121
require .Empty (t , previewRes .StdErr , "preview should not have errors" )
92
122
0 commit comments