Skip to content
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

Add replace program utility #94

Merged
merged 10 commits into from
Oct 3, 2024
Merged

Conversation

VenelinMartinov
Copy link
Contributor

This adds a ReplaceProgram utility to pulumitest which replaces the Pulumi program used by the test.

I've written this quite a few times now, so it seems like a good candidate as a utility - should be useful when testing updates.

pulumitest/replaceProgram.go Outdated Show resolved Hide resolved
pulumitest/replaceProgram_test.go Outdated Show resolved Hide resolved
pulumitest/replaceProgram.go Outdated Show resolved Hide resolved
Copy link
Member

@danielrbradley danielrbradley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like where this is heading - to make working with inline YAML programs really easy. I think we could do with a little more design around the end-to-end process of testing YAML programs.

One assumption that's baked in right now is that we're always driving the test from a physical source directory. For YAML programs there's no real requirement for this. Perhaps we could introduce a new top-level test constructor for dealing with inline tests better? Something like this perhaps ...

  test := NewPulumiInlineTest(t, opttest.AttachProviderBinary("gcp", "../bin"))
  test.WritePulumiYaml(`
name: yaml_empty
runtime: yaml
outputs:
	output: "output"
`)
  test.InstallStack("my-stack")
  update := test.Up()
  t.Log(update.StdOut)

I expect the next step from here would be some additional utilities for YAML programs to be able to perform targeted updates.

  test := NewPulumiInlineTest(t, opttest.AttachProviderBinary("gcp", "../bin"))
  test.WritePulumiYaml(`
name: yaml_empty
runtime: yaml
outputs:
	output: "output"
`)
  test.Up()
  test.UpdatePulumiYaml(
    yaml.AddResource("my-resource", yaml.Resource{
      Type: "aws:s3:Bucket",
    })

A side question here: are there Go types available for the valid YAML constructs that we could leverage here instead of reverting to strings?

pulumitest/replaceProgram.go Outdated Show resolved Hide resolved
@mjeffryes mjeffryes added this to the 0.108 milestone Aug 16, 2024
@mikhailshilkov mikhailshilkov removed this from the 0.108 milestone Aug 21, 2024
@danielrbradley
Copy link
Member

I wonder if either instead of WritePulumiYaml, or in addition to it, we should use a method that allows us to interact with the existing file too. This could then be leveraged by a typed YAML helper if we want to later.

For example:

test.UpdatePulumiYaml(func(source string) string {
    return `
name: yaml_empty
runtime: yaml
outputs:
	output: "output"
`
})

Which then allows targeted updates:

test.UpdatePulumiYaml(func(source string) string {
    return strings.ReplaceAll(source, "toBeReplaced", "newValue")
})

@VenelinMartinov
Copy link
Contributor Author

I think that's for individual tests to implement as needed - it's easy enough to add something like

func testProgram(params interface{}) {

jsonParam := params.ToJson()

program := fmt.Sprintf(`
resource:
  myRes:
    myParam: %s
`, jsonParams)
}

I don't think we need to add all the possible methods to the testing library but I've found I am using WritePulumiYaml quite extensively, so I think it'd be useful to add here.

@VenelinMartinov
Copy link
Contributor Author

Discussed offline with @danielrbradley, I've added a ReadYaml utility and have removed the tab replacement in favour of an early error and a suggested program.

The tab replacement could be ugly with mixed spaces and tabs.

Copy link
Member

@danielrbradley danielrbradley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notes from sync review.

In a follow-up PR, we'll add a new NewInlinePulumiTest constructor which takes the pulumiYaml string instead of the source directory path.

pulumitest/writePulumiYAML.go Outdated Show resolved Hide resolved
pulumitest/writePulumiYAML.go Outdated Show resolved Hide resolved
pulumitest/writePulumiYAML.go Outdated Show resolved Hide resolved
Copy link
Member

@danielrbradley danielrbradley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good once the TestReadProgram is passing

@VenelinMartinov VenelinMartinov merged commit 387f6b5 into main Oct 3, 2024
2 checks passed
@VenelinMartinov VenelinMartinov deleted the vvm/add_replace_program branch October 3, 2024 14:35
@pulumi-bot
Copy link

This PR has been shipped in release v0.1.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants