Skip to content

Commit 0cf28b0

Browse files
Docs tweaks from feedback (#129)
1 parent 92d0f1b commit 0cf28b0

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

README.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Library for testing Pulumi providers by running Pulumi programs.
44

55
> [!NOTE]
6-
> The libraries in this repo are used internally by the Pulumi providers team, and are still evolving; you should expect incomplete documentation and breaking changes. If you do choose to use this library we strongly recommend starting with [providertest/pulumitest](https://github.com/pulumi/providertest/tree/main/pulumitest) which is our latest approach. The ProviderTest abstraction is deprecated and will removed in a future release.
6+
> The libraries in this repo are used internally by the Pulumi providers team, and are still evolving; you should expect incomplete documentation and breaking changes. If you do choose to use this library we strongly recommend starting with [providertest/pulumitest](https://github.com/pulumi/providertest/tree/main/pulumitest) which is our latest approach. The ProviderTest abstraction is deprecated and will be removed in a future release.
77
88
The library is composed of several modules. The most important of these is the [`pulumitest`](./pulumitest/) module. This is a library designed for testing any Pulumi program within a Go test. It extends the Go [Automation API](https://www.pulumi.com/automation/) with defaults appropriate for local testing such as using temporary directories for state.
99

@@ -25,9 +25,29 @@ func TestPulumiProgram(t *testing.T) {
2525

2626
Refer to [the full documentation](./pulumitest/README.md) for a complete walkthrough of the features.
2727

28+
## Attaching In-Process Providers
29+
30+
If your provider implementation is available in the context of your test, the provider can be started in a background goroutine and used within the test using the `opttest.AttachProviderServer`. This avoids needing to build a provider binary before running the test, and allows stepping through from the test to the provider implementation when attaching a debugger.
31+
32+
For bridged providers using the standard repository layout, this can be configured as such:
33+
34+
```go
35+
//go:embed cmd/pulumi-resource-example/schema.json
36+
var schemaBytes []byte // Embed the generated schema (this might need to be re-generated before re-running tests)
37+
38+
func exampleResourceProviderServerFactory(_ providers.PulumiTest) (pulumirpc.ResourceProviderServer, error) {
39+
ctx := context.Background()
40+
version.Version = "1.0.0" // Set the global version to a non-empty string
41+
info := Provider() // Call the function defined in resource.go
42+
return tfbridge.NewProvider(ctx, nil, "example", version.Version, info.P, info, schemaBytes), nil
43+
}
44+
```
45+
46+
For native providers this function just returns your implementation of [the `pulumirpc.ResourceProviderServer` interface](https://pkg.go.dev/github.com/pulumi/pulumi/sdk/v3/proto/go#ResourceProviderServer).
47+
2848
## Upgrade Testing
2949

30-
We perform "upgrade testing" on providers to indicate when there's a change in a new version of a provider which might cause resources to be re-created during an upgrade from a previous version.
50+
We perform "upgrade testing" on providers to fail when a resource might be re-created when updating to a new version of the provider.
3151

3252
In the root `providertest` module there is a function called `PreviewProviderUpgrade(..)`. This shows the result of a preview when upgrading from a **baseline** version of a provider to a new version of the provider. On first run it records the *baseline* state after running the program with the *baseline* version of the provider and writes it into a `testdata` directory. On subsequent runs, it restores the state from the recorded *baseline* before performing a preview operation with the new version.
3353

@@ -36,7 +56,7 @@ Here's an example of how to write an upgrade test:
3656
```go
3757
pt := pulumitest.NewPulumiTest(t, "path-to-a-pulumi-program-dir",
3858
// Use our local implementation for the new version
39-
opttest.AttachProviderServer("my-provider-name", myProviderServerFactory))
59+
opttest.AttachProviderServer("my-provider-name", exampleResourceProviderServerFactory))
4060
// Perform a preview of upgrading from v0.0.1 of my-provider-name to our new version.
4161
previewResult := providertest.PreviewProviderUpgrade(t, pt, "my-provider-name", "0.0.1")
4262
// Assert the preview shows no changes
@@ -47,9 +67,9 @@ It's expected that the preview operation does not perform actual network calls,
4767

4868
```go
4969
// Turn a server factory into a *resource provider* server factory
50-
resourceProviderFactory := providers.ResourceProviderFactory(myProviderServerFactory)
70+
resourceProviderFactory := providers.ResourceProviderFactory(exampleResourceProviderServerFactory)
5171
// Calculate the path to the baseline version recording
52-
upgradeCacheDir := providertest.GetUpgradeCacheDir("path-to-a-pulumi-program-dir", "5.60.0")
72+
upgradeCacheDir := providertest.GetUpgradeCacheDir("path-to-a-pulumi-program-dir", "0.0.1")
5373
// Create a new factory which will intercept and replay invokes from the recorded grpc.json
5474
factoryWithReplay := resourceProviderFactory.ReplayInvokes(filepath.Join(upgradeCacheDir, "grpc.json"), true)
5575

optproviderupgrade/optproviderupgrade.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ func CacheDir(elem ...string) PreviewProviderUpgradeOpt {
3838
})
3939
}
4040

41+
// NewSourcePath sets the path to new source code to use for the target version of the upgrade.
42+
// If not set, the original pulumitest program source is used.
43+
// This is useful for where it's expected for a user to perform code changes during a migration.
4144
func NewSourcePath(path string) PreviewProviderUpgradeOpt {
4245
return optionFunc(func(o *PreviewProviderUpgradeOptions) {
4346
o.NewSourcePath = path

0 commit comments

Comments
 (0)