|
1 | 1 | package tfbridgetests |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "math/big" |
| 6 | + "os" |
4 | 7 | "testing" |
5 | 8 |
|
| 9 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
6 | 10 | rschema "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
7 | 11 | "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
8 | 12 | "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
9 | 13 | "github.com/hexops/autogold/v2" |
| 14 | + "github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil" |
10 | 15 | "github.com/zclconf/go-cty/cty" |
11 | 16 |
|
12 | 17 | pb "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/internal/providerbuilder" |
@@ -176,3 +181,111 @@ func TestPFDetailedDiffStringAttribute(t *testing.T) { |
176 | 181 | }) |
177 | 182 | } |
178 | 183 | } |
| 184 | + |
| 185 | +func TestPFDetailedDiffDynamicType(t *testing.T) { |
| 186 | + t.Parallel() |
| 187 | + if d, ok := os.LookupEnv("PULUMI_RAW_STATE_DELTA_ENABLED"); ok && cmdutil.IsTruthy(d) { |
| 188 | + // TODO[pulumi/pulumi-terraform-bridge#3078] |
| 189 | + t.Skip("Does not work with PULUMI_RAW_STATE_DELTA_ENABLED=true") |
| 190 | + } |
| 191 | + |
| 192 | + attributeSchema := rschema.Schema{ |
| 193 | + Attributes: map[string]rschema.Attribute{ |
| 194 | + "key": rschema.DynamicAttribute{ |
| 195 | + Optional: true, |
| 196 | + }, |
| 197 | + }, |
| 198 | + } |
| 199 | + res := pb.NewResource(pb.NewResourceArgs{ |
| 200 | + ResourceSchema: attributeSchema, |
| 201 | + }) |
| 202 | + |
| 203 | + t.Run("no change", func(t *testing.T) { |
| 204 | + crosstests.Diff(t, res, |
| 205 | + map[string]cty.Value{"key": cty.StringVal("value")}, |
| 206 | + map[string]cty.Value{"key": cty.StringVal("value")}, |
| 207 | + ) |
| 208 | + }) |
| 209 | + |
| 210 | + t.Run("change", func(t *testing.T) { |
| 211 | + crosstests.Diff(t, res, |
| 212 | + map[string]cty.Value{"key": cty.StringVal("value")}, |
| 213 | + map[string]cty.Value{"key": cty.StringVal("value1")}, |
| 214 | + ) |
| 215 | + }) |
| 216 | + |
| 217 | + t.Run("int no change", func(t *testing.T) { |
| 218 | + crosstests.Diff(t, res, |
| 219 | + map[string]cty.Value{"key": cty.NumberVal(big.NewFloat(1))}, |
| 220 | + map[string]cty.Value{"key": cty.NumberVal(big.NewFloat(1))}, |
| 221 | + ) |
| 222 | + }) |
| 223 | + |
| 224 | + t.Run("type change", func(t *testing.T) { |
| 225 | + // TODO[pulumi/pulumi-terraform-bridge#3078] |
| 226 | + t.Skip(`Error converting tftypes.Number<"1"> (value2) at "AttributeName(\"key\")": can't unmarshal tftypes.Number into *string, expected string`) |
| 227 | + crosstests.Diff(t, res, |
| 228 | + map[string]cty.Value{"key": cty.StringVal("value")}, |
| 229 | + map[string]cty.Value{"key": cty.NumberVal(big.NewFloat(1))}, |
| 230 | + ) |
| 231 | + }) |
| 232 | +} |
| 233 | + |
| 234 | +func TestPFDetailedDiffDynamicTypeWithMigration(t *testing.T) { |
| 235 | + t.Parallel() |
| 236 | + // TODO[pulumi/pulumi-terraform-bridge#3078] |
| 237 | + t.Skip("DynamicPseudoType is not supported") |
| 238 | + |
| 239 | + attributeSchema := rschema.Schema{ |
| 240 | + Attributes: map[string]rschema.Attribute{ |
| 241 | + "key": rschema.DynamicAttribute{ |
| 242 | + Optional: true, |
| 243 | + }, |
| 244 | + }, |
| 245 | + } |
| 246 | + res1 := pb.NewResource(pb.NewResourceArgs{ |
| 247 | + ResourceSchema: attributeSchema, |
| 248 | + }) |
| 249 | + |
| 250 | + schema2 := rschema.Schema{ |
| 251 | + Attributes: map[string]rschema.Attribute{ |
| 252 | + "key": rschema.DynamicAttribute{ |
| 253 | + Optional: true, |
| 254 | + }, |
| 255 | + }, |
| 256 | + Version: 1, |
| 257 | + } |
| 258 | + res2 := pb.NewResource(pb.NewResourceArgs{ |
| 259 | + ResourceSchema: schema2, |
| 260 | + UpgradeStateFunc: func(ctx context.Context) map[int64]resource.StateUpgrader { |
| 261 | + return map[int64]resource.StateUpgrader{ |
| 262 | + 0: { |
| 263 | + PriorSchema: &res1.ResourceSchema, |
| 264 | + StateUpgrader: func(ctx context.Context, usr1 resource.UpgradeStateRequest, usr2 *resource.UpgradeStateResponse) { |
| 265 | + usr2.State = *usr1.State |
| 266 | + }, |
| 267 | + }, |
| 268 | + } |
| 269 | + }, |
| 270 | + }) |
| 271 | + |
| 272 | + t.Run("no change", func(t *testing.T) { |
| 273 | + crosstests.Diff(t, res1, |
| 274 | + map[string]cty.Value{"key": cty.StringVal("value")}, |
| 275 | + map[string]cty.Value{"key": cty.StringVal("value")}, |
| 276 | + crosstests.DiffProviderUpgradedSchema( |
| 277 | + res2, |
| 278 | + ), |
| 279 | + ) |
| 280 | + }) |
| 281 | + |
| 282 | + t.Run("change", func(t *testing.T) { |
| 283 | + crosstests.Diff(t, res1, |
| 284 | + map[string]cty.Value{"key": cty.StringVal("value")}, |
| 285 | + map[string]cty.Value{"key": cty.StringVal("value1")}, |
| 286 | + crosstests.DiffProviderUpgradedSchema( |
| 287 | + res2, |
| 288 | + ), |
| 289 | + ) |
| 290 | + }) |
| 291 | +} |
0 commit comments