-
Notifications
You must be signed in to change notification settings - Fork 33
allow transforms to be registered in mocks #1139
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| // Copyright 2026, Pulumi Corporation | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Collections.Immutable; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Pulumi.Testing; | ||
| using Xunit; | ||
|
|
||
| namespace Pulumi.Tests.Mocks | ||
| { | ||
| public class TransformsTests | ||
| { | ||
| private sealed class RecordingMocks : IMocks | ||
| { | ||
| public readonly List<ResourceTransform> StackTransforms = new List<ResourceTransform>(); | ||
| public readonly List<InvokeTransform> InvokeTransforms = new List<InvokeTransform>(); | ||
| public readonly List<ResourceTransform> ResourceTransforms = new List<ResourceTransform>(); | ||
| public ImmutableDictionary<string, object>? Inputs; | ||
|
|
||
| public Task<(string? id, object state)> NewResourceAsync(MockResourceArgs args) | ||
| { | ||
| if (args.Name == "res") | ||
| { | ||
| Inputs = args.Inputs; | ||
| ResourceTransforms.AddRange(args.Transforms); | ||
| } | ||
| return Task.FromResult<(string?, object)>(($"{args.Name}_id", args.Inputs)); | ||
| } | ||
|
|
||
| public Task<object> CallAsync(MockCallArgs args) | ||
| => Task.FromResult<object>(args.Args); | ||
|
|
||
| public Task RegisterTransform(ResourceTransform transform) | ||
| { | ||
| StackTransforms.Add(transform); | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| public Task RegisterInvokeTransform(InvokeTransform transform) | ||
| { | ||
| InvokeTransforms.Add(transform); | ||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
|
|
||
| private sealed class TransformsTestResourceArgs : ResourceArgs | ||
| { | ||
| [Input("foo")] | ||
| public Input<string>? Foo { get; set; } | ||
| } | ||
|
|
||
| private sealed class TransformsTestResource : CustomResource | ||
| { | ||
| public TransformsTestResource(string name, TransformsTestResourceArgs args, CustomResourceOptions? options = null) | ||
| : base("test:index:TransformsTestResource", name, args, options) | ||
| { | ||
| } | ||
| } | ||
|
|
||
| private static Task<ResourceTransformResult?> StackTransform(ResourceTransformArgs args, CancellationToken cancellationToken) | ||
| { | ||
| var newArgs = args.Args.SetItem("foo", "stack"); | ||
| return Task.FromResult<ResourceTransformResult?>(new ResourceTransformResult(newArgs, args.Options)); | ||
| } | ||
|
|
||
| private static Task<ResourceTransformResult?> OwnTransform(ResourceTransformArgs args, CancellationToken cancellationToken) | ||
| { | ||
| var newArgs = args.Args.SetItem("foo", "own"); | ||
| return Task.FromResult<ResourceTransformResult?>(new ResourceTransformResult(newArgs, args.Options)); | ||
| } | ||
|
|
||
| private static Task<InvokeTransformResult?> AddExtraInvokeTransform(InvokeTransformArgs args, CancellationToken cancellationToken) | ||
| { | ||
| var newArgs = args.Args.SetItem("extra", "added"); | ||
| return Task.FromResult<InvokeTransformResult?>(new InvokeTransformResult(newArgs, args.Options)); | ||
| } | ||
|
|
||
| private sealed class TransformsStack : Stack | ||
| { | ||
| public TransformsStack() : base(new StackOptions | ||
| { | ||
| ResourceTransforms = { StackTransform }, | ||
| }) | ||
| { | ||
| _ = new TransformsTestResource("res", new TransformsTestResourceArgs { Foo = "orig" }, new CustomResourceOptions | ||
| { | ||
| ResourceTransforms = { OwnTransform }, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TransformsAreDeliveredButNotRun() | ||
| { | ||
| var mocks = new RecordingMocks(); | ||
| await Deployment.TestAsync<TransformsStack>(mocks, new TestOptions { IsPreview = false }); | ||
|
|
||
| Assert.NotNull(mocks.Inputs); | ||
| Assert.Equal("orig", mocks.Inputs!["foo"]); | ||
|
|
||
| Assert.Single(mocks.StackTransforms); | ||
| Assert.Single(mocks.ResourceTransforms); | ||
|
|
||
| var args = new ResourceTransformArgs( | ||
| "res", | ||
| "test:index:TransformsTestResource", | ||
| custom: true, | ||
| ImmutableDictionary<string, object?>.Empty.Add("foo", "orig"), | ||
| new CustomResourceOptions()); | ||
|
|
||
| var ownResult = await mocks.ResourceTransforms[0](args); | ||
| Assert.NotNull(ownResult); | ||
| Assert.Equal("own", ownResult!.Value.Args["foo"]); | ||
|
|
||
| var stackResult = await mocks.StackTransforms[0](args); | ||
| Assert.NotNull(stackResult); | ||
| Assert.Equal("stack", stackResult!.Value.Args["foo"]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task InvokeTransformsAreDelivered() | ||
| { | ||
| var mocks = new RecordingMocks(); | ||
| var (_, exception) = await Deployment.TryTestAsync(mocks, runner => runner.RunAsync(async () => | ||
| { | ||
| Deployment.Instance.RegisterInvokeTransform(AddExtraInvokeTransform); | ||
| await ((Deployment)Deployment.InternalInstance).AwaitPendingRegistrations().ConfigureAwait(false); | ||
| return (IDictionary<string, object?>)new Dictionary<string, object?>(); | ||
| }, null), new TestOptions { IsPreview = false }); | ||
|
|
||
| Assert.Null(exception); | ||
| Assert.Single(mocks.InvokeTransforms); | ||
|
|
||
| var result = await mocks.InvokeTransforms[0](new InvokeTransformArgs( | ||
| "test:index:MyFunction", | ||
| ImmutableDictionary<string, object?>.Empty.Add("orig", "value"), | ||
| new InvokeOptions())); | ||
| Assert.NotNull(result); | ||
| Assert.Equal("added", result!.Value.Args["extra"]); | ||
| Assert.Equal("value", result.Value.Args["orig"]); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.