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

Make Pulumi.RunException assertable #203

Open
JasonWhall opened this issue Nov 7, 2023 · 1 comment
Open

Make Pulumi.RunException assertable #203

JasonWhall opened this issue Nov 7, 2023 · 1 comment
Labels
kind/enhancement Improvements or new features

Comments

@JasonWhall
Copy link
Contributor

Hello!

  • Vote on this issue by adding a 👍 reaction
  • If you want to implement this feature, comment to let us know (we'll work with you on design, scheduling, etc.)

Issue details

Currently when writing Unit tests for a stack that does Pulumi.Log.Error(""). The resulting stack will throw a Pulumi.RunException in the deployment runner. As this is an internal only type currently, we are unable to assert to check this error has been thrown. The workaround currently is to assert on the Exception type, but isn't accurate to the type of exception thrown. Below shows an example of a test that would check for this:

using FluentAssertions;
using NSubstitute;
using Pulumi;
using Pulumi.Testing;

public class UnitTest1
{

    [Fact]
    public async Task Stack_LoggingError_ShouldThrowExpectedException()
    {
        // Arrange
        var mocks = Substitute.For<IMocks>();
        var options = new TestOptions { IsPreview = false };
        static void act()
        {
            Log.Error("Message 1");
        };

        var sut = () => Deployment.TestAsync(mocks, options, act);

        // Act & Assert
        await sut.Should().ThrowAsync<Exception>()
            .WithMessage("Message 1");

        // Desired
        /*
            await sut.Should().ThrowAsync<Pulumi.RunException>()
                .WithMessage("Message 1");
        */
    }
}

Could the RunException be made public so we can assert on this?

Affected area/feature

Pulumi Core

@JasonWhall JasonWhall added kind/enhancement Improvements or new features needs-triage Needs attention from the triage team labels Nov 7, 2023
@justinvp justinvp removed the needs-triage Needs attention from the triage team label Nov 10, 2023
@justinvp
Copy link
Member

Thanks for the suggestion, @JasonWhall. I don't recall if we had a good reason for making RunException internal, other than not intending it to be thrown by users. But I understand why it'd be nice to write a test like this.

mmisztal1980 added a commit to mmisztal1980/pulumi-dotnet that referenced this issue Oct 16, 2024
github-merge-queue bot pushed a commit that referenced this issue Oct 18, 2024
…364)

As per discussion in #203 . This is a simple improvement in order to
make the exception assertable by testing frameworks such as
`FluentAssertions` using:

```csharp
var action = () => 
{
  // (...) // provision failing pulumi resources here
};

action.Should().Throw<Pulumi.RunException>();
```

This will significantly improve testing scenarios where exceptions are
being thrown, as everything gets wrapped in an internal
`Pulumi.RunException` while testing:

```csharp
// Disassembled code
internal static async Task<(ImmutableArray<Resource> Resources, Exception? Exception)> TryTestAsync(
            IMocks mocks, Func<IRunner, Task<int>> runAsync, TestOptions? options = null)
        {
            var engine = new MockEngine();
            var monitor = new MockMonitor(mocks);
            await CreateRunnerAndRunAsync(() => new Deployment(engine, monitor, options), runAsync).ConfigureAwait(false);
            Exception? err = engine.Errors.Count switch
            {
                1 => new RunException(engine.Errors.Single()), // <-- wrapping takes place here
                var v when v > 1 => new AggregateException(engine.Errors.Select(e => new RunException(e))),
                _ => null
            };
            return (Resources: monitor.Resources.ToImmutableArray(), Exception: err);
        }
```

---------

Co-authored-by: Thomas Gummerer <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement Improvements or new features
Projects
None yet
Development

No branches or pull requests

2 participants