Skip to content

Repro gitlab diff issue #2944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions pkg/pf/tests/diff_test/diff_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package tfbridgetests

import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
rschema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hexops/autogold/v2"
"github.com/zclconf/go-cty/cty"

Expand Down Expand Up @@ -59,6 +65,76 @@
`).Equal(t, diff.PulumiOut)
}

func TestPFGitlabDiffRepro(t *testing.T) {
t.Parallel()

getSchema := func(withNew bool) rschema.Schema {
attributes := map[string]rschema.Attribute{
"key": rschema.StringAttribute{Optional: true},
"project_id": rschema.Int64Attribute{
MarkdownDescription: "The id of the project for the hook.",
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.RequiresReplace(),
},
},
}
if withNew {
attributes["default_val"] = rschema.BoolAttribute{
Default: booldefault.StaticBool(false),
Computed: true,
Optional: true,
}
}
return rschema.Schema{
Attributes: attributes,
}
}

res := pb.NewResource(pb.NewResourceArgs{
CreateFunc: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
resp.State = tfsdk.State(req.Config)
resp.State.SetAttribute(ctx, path.Root("project_id"), 123)
resp.State.SetAttribute(ctx, path.Root("id"), "abc")
},
ResourceSchema: getSchema(false),
})
diff := crosstests.Diff(t, res,
map[string]cty.Value{"key": cty.StringVal("value")},
map[string]cty.Value{"key": cty.StringVal("value")},
crosstests.DiffUpdateResource(pb.NewResource(pb.NewResourceArgs{
ResourceSchema: getSchema(true),
ReadFunc: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
resp.State = tfsdk.State(req.State)

Check failure on line 108 in pkg/pf/tests/diff_test/diff_test.go

View workflow job for this annotation

GitHub Actions / Test and Lint / lint

unnecessary conversion (unconvert)
resp.State.SetAttribute(ctx, path.Root("default_val"), false)
},
})),
crosstests.DiffSkipUp(true),
)

autogold.Expect(`testprovider_test.res: Refreshing state... [id=abc]

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
`).Equal(t, diff.TFOut)
autogold.Expect(`Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
+-testprovider:index/test:Test: (replace)
[id=abc]
[urn=urn:pulumi:test::project::testprovider:index/test:Test::p]
+ defaultVal: false
~ id : "abc" => output<string>
key : "value"
~ projectId : 123 => output<string>
Resources:
+-1 to replace
1 unchanged
`).Equal(t, diff.PulumiOut)
}

func TestPFDetailedDiffStringAttribute(t *testing.T) {
t.Parallel()

Expand Down
54 changes: 42 additions & 12 deletions pkg/pf/tests/internal/cross-tests/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,23 @@ func Diff(t T, res pb.Resource, tfConfig1, tfConfig2 map[string]cty.Value, optio
f(&opts)
}

tmpDir := t.TempDir()
prov := pb.NewProvider(pb.NewProviderArgs{
AllResources: []pb.Resource{res},
})

shimProvider := tfbridge.ShimProvider(prov)

prov2 := *prov
shimProvider2 := shimProvider
if opts.updateResource != nil {
p2 := pb.NewProvider(pb.NewProviderArgs{
AllResources: []pb.Resource{*opts.updateResource},
})
prov2 = *p2
shimProvider2 = tfbridge.ShimProvider(&prov2)
}

// Run the TF part
var hcl1 bytes.Buffer

Expand All @@ -72,22 +83,25 @@ func Diff(t T, res pb.Resource, tfConfig1, tfConfig2 map[string]cty.Value, optio
hclwrite.WithCreateBeforeDestroy(true))
require.NoError(t, err)

driver := tfcheck.NewTfDriver(t, t.TempDir(), prov.TypeName, prov)
driver := tfcheck.NewTfDriver(t, tmpDir, prov.TypeName, prov)

driver.Write(t, hcl1.String())
plan, err := driver.Plan(t)
require.NoError(t, err)
err = driver.Apply(t, plan)
require.NoError(t, err)

sch2 := hclSchemaPFResource(opts.updateResource.ResourceSchema)
driver2 := tfcheck.NewTfDriver(t, tmpDir, prov2.TypeName, &prov2)
var hcl2 bytes.Buffer
err = hclwrite.WriteResource(&hcl2, sch, "testprovider_test", "res", tfConfig2,
err = hclwrite.WriteResource(&hcl2, sch2, "testprovider_test", "res", tfConfig2,
hclwrite.WithCreateBeforeDestroy(true))
require.NoError(t, err)
driver.Write(t, hcl2.String())
plan, err = driver.Plan(t)
driver2.Write(t, hcl2.String())
plan, err = driver2.Plan(t)
require.NoError(t, err)
tfChanges := driver.ParseChangesFromTFPlan(plan)
tfChanges := driver2.ParseChangesFromTFPlan(plan)
t.Logf("TF Changes: %v", tfChanges)

// Run the Pulumi part

Expand All @@ -99,7 +113,7 @@ func Diff(t T, res pb.Resource, tfConfig1, tfConfig2 map[string]cty.Value, optio
pulumiYaml1 := yamlResource(t, puConfig1)

puConfig2 := crosstestsimpl.InferPulumiValue(t,
shimProvider.ResourcesMap().Get("testprovider_test").Schema(),
shimProvider2.ResourcesMap().Get("testprovider_test").Schema(),
opts.resourceInfo,
cty.ObjectVal(tfConfig2),
)
Expand All @@ -112,17 +126,23 @@ func Diff(t T, res pb.Resource, tfConfig1, tfConfig2 map[string]cty.Value, optio
pt, err := pulcheck.PulCheck(t, prov.ToProviderInfo(), string(bytes))
require.NoError(t, err)
pt.Up(t)
state := pt.ExportStack(t)

bytes, err = yaml.Marshal(pulumiYaml2)
require.NoError(t, err)
t.Logf("Pulumi.yaml:\n%s", string(bytes))
pt.WritePulumiYaml(t, string(bytes))
pt2, err := pulcheck.PulCheck(t, prov2.ToProviderInfo(), string(bytes))
require.NoError(t, err)
pt2.ImportStack(t, state)

previewRes := pt.Preview(t, optpreview.Diff())
pulumiRes := pt.Up(t)
diffResponse := crosstestsimpl.GetPulumiDiffResponse(t, pt.GrpcLog(t).Entries)
previewRes := pt2.Preview(t, optpreview.Diff())
if !opts.skipUp {
pulumiRes := pt2.Up(t)
diffResponse := crosstestsimpl.GetPulumiDiffResponse(t, pt2.GrpcLog(t).Entries)
crosstestsimpl.VerifyBasicDiffAgreement(t, tfChanges.Actions, pulumiRes.Summary, diffResponse)
}

crosstestsimpl.VerifyBasicDiffAgreement(t, tfChanges.Actions, pulumiRes.Summary, diffResponse)
diffResponse := crosstestsimpl.GetPulumiDiffResponse(t, pt2.GrpcLog(t).Entries)

return crosstestsimpl.DiffResult{
TFDiff: tfChanges,
Expand All @@ -133,7 +153,9 @@ func Diff(t T, res pb.Resource, tfConfig1, tfConfig2 map[string]cty.Value, optio
}

type diffOpts struct {
resourceInfo map[string]*info.Schema
resourceInfo map[string]*info.Schema
updateResource *pb.Resource
skipUp bool
}

type DiffOption func(*diffOpts)
Expand All @@ -142,3 +164,11 @@ type DiffOption func(*diffOpts)
func DiffProviderInfo(info map[string]*info.Schema) DiffOption {
return func(o *diffOpts) { o.resourceInfo = info }
}

func DiffUpdateResource(res pb.Resource) DiffOption {
return func(o *diffOpts) { o.updateResource = &res }
}

func DiffSkipUp(skip bool) DiffOption {
return func(o *diffOpts) { o.skipUp = skip }
}
4 changes: 2 additions & 2 deletions pkg/tests/tfcheck/tfcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (d *TFDriver) Write(t pulcheck.T, program string) {

func (d *TFDriver) Plan(t pulcheck.T) (*TFPlan, error) {
planFile := filepath.Join(d.cwd, "test.tfplan")
planStdoutBytes, err := d.execTf(t, "plan", "-refresh=false", "-out", planFile, "-no-color")
planStdoutBytes, err := d.execTf(t, "plan", "-out", planFile, "-no-color")
if err != nil {
return nil, err
}
Expand All @@ -137,7 +137,7 @@ func (d *TFDriver) Plan(t pulcheck.T) (*TFPlan, error) {
}

func (d *TFDriver) Apply(t pulcheck.T, plan *TFPlan) error {
_, err := d.execTf(t, "apply", "-auto-approve", "-refresh=false", plan.PlanFile)
_, err := d.execTf(t, "apply", "-auto-approve", plan.PlanFile)
return err
}

Expand Down
Loading