2019-12-23 08:31:12 +01:00
|
|
|
|
// Copyright 2016-2019, Pulumi Corporation
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Moq;
|
|
|
|
|
using Pulumi.Serialization;
|
|
|
|
|
using Xunit;
|
|
|
|
|
using Xunit.Sdk;
|
|
|
|
|
|
|
|
|
|
namespace Pulumi.Tests
|
|
|
|
|
{
|
2020-05-06 00:09:50 +02:00
|
|
|
|
public class StackTests : IDisposable
|
2019-12-23 08:31:12 +01:00
|
|
|
|
{
|
|
|
|
|
private class ValidStack : Stack
|
|
|
|
|
{
|
|
|
|
|
[Output("foo")]
|
2020-03-11 23:10:01 +01:00
|
|
|
|
public Output<string> ExplicitName { get; set; }
|
2019-12-23 08:31:12 +01:00
|
|
|
|
|
|
|
|
|
[Output]
|
2020-03-11 23:10:01 +01:00
|
|
|
|
public Output<string> ImplicitName { get; set; }
|
2019-12-23 08:31:12 +01:00
|
|
|
|
|
|
|
|
|
public ValidStack()
|
|
|
|
|
{
|
|
|
|
|
this.ExplicitName = Output.Create("bar");
|
|
|
|
|
this.ImplicitName = Output.Create("buzz");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task ValidStackInstantiationSucceeds()
|
|
|
|
|
{
|
|
|
|
|
var (stack, outputs) = await Run<ValidStack>();
|
|
|
|
|
Assert.Equal(2, outputs.Count);
|
|
|
|
|
Assert.Same(stack.ExplicitName, outputs["foo"]);
|
|
|
|
|
Assert.Same(stack.ImplicitName, outputs["ImplicitName"]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class NullOutputStack : Stack
|
|
|
|
|
{
|
|
|
|
|
[Output("foo")]
|
|
|
|
|
public Output<string>? Foo { get; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task StackWithNullOutputsThrows()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var (stack, outputs) = await Run<NullOutputStack>();
|
|
|
|
|
}
|
|
|
|
|
catch (RunException ex)
|
|
|
|
|
{
|
2020-03-11 23:10:01 +01:00
|
|
|
|
Assert.Contains("Output(s) 'foo' have no value assigned", ex.ToString());
|
2019-12-23 08:31:12 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new XunitException("Should not come here");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class InvalidOutputTypeStack : Stack
|
|
|
|
|
{
|
|
|
|
|
[Output("foo")]
|
2020-03-11 23:10:01 +01:00
|
|
|
|
public string Foo { get; set; }
|
2019-12-23 08:31:12 +01:00
|
|
|
|
|
|
|
|
|
public InvalidOutputTypeStack()
|
|
|
|
|
{
|
|
|
|
|
this.Foo = "bar";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task StackWithInvalidOutputTypeThrows()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var (stack, outputs) = await Run<InvalidOutputTypeStack>();
|
|
|
|
|
}
|
|
|
|
|
catch (RunException ex)
|
|
|
|
|
{
|
2020-03-11 23:10:01 +01:00
|
|
|
|
Assert.Contains("Output(s) 'foo' have incorrect type", ex.ToString());
|
2019-12-23 08:31:12 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new XunitException("Should not come here");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<(T, IDictionary<string, object?>)> Run<T>() where T : Stack, new()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
Output<IDictionary<string, object?>>? outputs = null;
|
|
|
|
|
|
|
|
|
|
var mock = new Mock<IDeploymentInternal>(MockBehavior.Strict);
|
|
|
|
|
mock.Setup(d => d.ProjectName).Returns("TestProject");
|
|
|
|
|
mock.Setup(d => d.StackName).Returns("TestStack");
|
|
|
|
|
mock.SetupSet(content => content.Stack = It.IsAny<Stack>());
|
2020-10-06 19:19:22 +02:00
|
|
|
|
mock.Setup(d => d.ReadOrRegisterResource(It.IsAny<Stack>(), It.IsAny<bool>(),
|
|
|
|
|
It.IsAny<Func<string, Resource>>(), It.IsAny<ResourceArgs>(), It.IsAny<ResourceOptions>()));
|
2019-12-23 08:31:12 +01:00
|
|
|
|
mock.Setup(d => d.RegisterResourceOutputs(It.IsAny<Stack>(), It.IsAny<Output<IDictionary<string, object?>>>()))
|
|
|
|
|
.Callback((Resource _, Output<IDictionary<string, object?>> o) => outputs = o);
|
|
|
|
|
|
2020-04-14 10:30:25 +02:00
|
|
|
|
Deployment.Instance = new DeploymentInstance(mock.Object);
|
2019-12-23 08:31:12 +01:00
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var stack = new T();
|
|
|
|
|
stack.RegisterPropertyOutputs();
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.NotNull(outputs);
|
|
|
|
|
var values = await outputs!.DataTask;
|
|
|
|
|
return (stack, values.Value);
|
|
|
|
|
}
|
2020-05-06 00:09:50 +02:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
// Always reset the instance after each of these tests as other tests elsewhere
|
|
|
|
|
// expect it to be initially null.
|
|
|
|
|
Deployment.Instance = null!;
|
|
|
|
|
}
|
2019-12-23 08:31:12 +01:00
|
|
|
|
}
|
|
|
|
|
}
|