예제 #1
0
        /// <summary>
        /// Entry point to test a Pulumi application. Deployment will
        /// instantiate a new stack instance based on the type passed as TStack
        /// type parameter. This method creates no real resources.
        /// Note: Currently, unit tests that call <see cref="TestAsync{TStack}"/>
        /// must run serially; parallel execution is not supported.
        /// </summary>
        /// <param name="mocks">Hooks to mock the engine calls.</param>
        /// <param name="options">Optional settings for the test run.</param>
        /// <typeparam name="TStack">The type of the stack to test.</typeparam>
        /// <returns>Test result containing created resources and errors, if any.</returns>
        public static async Task <ImmutableArray <Resource> > TestAsync <TStack>(IMocks mocks, TestOptions?options = null) where TStack : Stack, new()
        {
            var        engine  = new MockEngine();
            var        monitor = new MockMonitor(mocks);
            Deployment deployment;

            lock (_instanceLock)
            {
                if (_instance != null)
                {
                    throw new NotSupportedException($"Multiple executions of {nameof(TestAsync)} must run serially. Please configure your unit test suite to run tests one-by-one.");
                }

                deployment = new Deployment(engine, monitor, options);
                Instance   = new DeploymentInstance(deployment);
            }

            try
            {
                await deployment._runner.RunAsync <TStack>();

                return(engine.Errors.Count switch
                {
                    1 => throw new RunException(engine.Errors.Single()),
                    int v when v > 1 => throw new AggregateException(engine.Errors.Select(e => new RunException(e))),
                    _ => monitor.Resources.ToImmutableArray()
                });
            }
예제 #2
0
파일: TestHelpers.cs 프로젝트: t0yv0/pulumi
        public static async Task <T> Run <T>(IMocks mocks, Func <Output <T> > outputGenerator, TestOptions?options = null)
        {
            options             = options ?? new TestOptions();
            options.ProjectName = HelperStack.RegisterBuilderAsProjectName(outputGenerator);
            var resources = await Deployment.TestAsync <HelperStack>(mocks, options);

            var stack = resources.Where(x => x is HelperStack).First() as HelperStack;

            if (stack != null)
            {
                var result = await AwaitOutput(stack.Result);

                if (result is T)
                {
                    return((T)result);
                }
                else
                {
                    throw new Exception($"The output did not resolve to the correct type: {result}");
                }
            }
            else
            {
                throw new Exception("Did not find stack");
            }
        }
예제 #3
0
        private static async Task <ImmutableArray <Resource> > TestAsync(IMocks mocks, Func <IRunner, Task <int> > runAsync, TestOptions?options = null)
        {
            var result = await TryTestAsync(mocks, runAsync, options);

            if (result.Exception != null)
            {
                throw result.Exception;
            }
            return(result.Resources);
        }
예제 #4
0
        private static async Task <ImmutableArray <Resource> > TestAsync(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);

            return(engine.Errors.Count switch
            {
                1 => throw new RunException(engine.Errors.Single()),
                var v when v > 1 => throw new AggregateException(engine.Errors.Select(e => new RunException(e))),
                _ => monitor.Resources.ToImmutableArray()
            });
예제 #5
0
 public MockMonitor(IMocks mocks)
 {
     _mocks = mocks;
 }
예제 #6
0
 /// <summary>
 /// Entry point to test a Pulumi application. Deployment will
 /// instantiate a new stack instance based on the type passed as TStack
 /// type parameter. This method creates no real resources.
 /// Note: Currently, unit tests that call <see cref="TestAsync{TStack}(IMocks, TestOptions)"/>
 /// must run serially; parallel execution is not supported.
 /// </summary>
 /// <param name="mocks">Hooks to mock the engine calls.</param>
 /// <param name="options">Optional settings for the test run.</param>
 /// <typeparam name="TStack">The type of the stack to test.</typeparam>
 /// <returns>Test result containing created resources and errors, if any.</returns>
 public static Task <ImmutableArray <Resource> > TestAsync <TStack>(IMocks mocks, TestOptions?options = null)
     where TStack : Stack, new()
 => TestAsync(mocks, runner => runner.RunAsync <TStack>(), options);
예제 #7
0
 /// <summary>
 /// Entry point to test a Pulumi application. Deployment will
 /// instantiate a new stack instance based on the type passed as TStack
 /// type parameter using the given service provider. This method creates
 /// no real resources.
 /// Note: Currently, unit tests that call
 /// <see cref="TestWithServiceProviderAsync{TStack}(IMocks, IServiceProvider, TestOptions)"/>
 /// must run serially; parallel execution is not supported.
 /// </summary>
 /// <param name="mocks">Hooks to mock the engine calls.</param>
 /// <param name="serviceProvider"></param>
 /// <param name="options">Optional settings for the test run.</param>
 /// <typeparam name="TStack">The type of the stack to test.</typeparam>
 /// <returns>Test result containing created resources and errors, if any.</returns>
 public static Task <ImmutableArray <Resource> > TestWithServiceProviderAsync <TStack>(IMocks mocks, IServiceProvider serviceProvider, TestOptions?options = null)
     where TStack : Stack
 => TestAsync(mocks, runner => runner.RunAsync <TStack>(serviceProvider), options);
예제 #8
0
 public MockTests(IMocks mocks)
 {
     _mocks = mocks;
 }
 public DefaultController(ILogger <DefaultController> logger, IMocks mocks)
 {
     _logger = logger;
     _mocks  = mocks;
 }
예제 #10
0
 /// <summary>
 /// Entry point to test a Pulumi application. Deployment will
 /// instantiate a new stack instance based on the type passed as TStack
 /// type parameter. This method creates no real resources.
 /// Note: Currently, unit tests that call <see cref="TestAsync{TStack}(IMocks, TestOptions)"/>
 /// must run serially; parallel execution is not supported.
 /// </summary>
 /// <param name="mocks">Hooks to mock the engine calls.</param>
 /// <param name="options">Optional settings for the test run.</param>
 /// <typeparam name="TStack">The type of the stack to test.</typeparam>
 /// <returns>Test result containing created resources and errors, if any.</returns>
 public static Task <ImmutableArray <Resource> > TestAsync <TStack>(IMocks mocks, TestOptions?options = null)
     where TStack : Stack, new()
 {
     return(TestAsync(mocks, (deployment) => deployment._runner.RunAsync <TStack>(), options));
 }
예제 #11
0
 /// <summary>
 /// Entry point to test a Pulumi application. Deployment will
 /// instantiate a new stack instance based on the type passed as TStack
 /// type parameter using the given service provider. This method creates
 /// no real resources.
 /// Note: Currently, unit tests that call
 /// <see cref="TestWithServiceProviderAsync{TStack}(IMocks, IServiceProvider, TestOptions)"/>
 /// must run serially; parallel execution is not supported.
 /// </summary>
 /// <param name="mocks">Hooks to mock the engine calls.</param>
 /// <param name="serviceProvider"></param>
 /// <param name="options">Optional settings for the test run.</param>
 /// <typeparam name="TStack">The type of the stack to test.</typeparam>
 /// <returns>Test result containing created resources and errors, if any.</returns>
 public static Task <ImmutableArray <Resource> > TestWithServiceProviderAsync <TStack>(IMocks mocks, IServiceProvider serviceProvider, TestOptions?options = null)
     where TStack : Stack
 {
     return(TestAsync(mocks, (deployment) => deployment._runner.RunAsync <TStack>(serviceProvider), options));
 }
예제 #12
0
 public ControllerTests(IMocks mocks)
 {
     _mocks = mocks;
 }