public async Task UpdateAsync_IpAddressAndNoAuthenticationParametersProvided_DoesNotSignInToDockerOnInstance()
        {
            //Arrange
            var fakeDockerComposeParser = Substitute.For <IDockerComposeParser>();

            var fakeDockerComposeParserFactory = Substitute.For <IDockerComposeParserFactory>();

            fakeDockerComposeParserFactory
            .Create(Arg.Any <string>())
            .Returns(fakeDockerComposeParser);

            var fakeMediator = Substitute.For <IMediator>();

            var fakeSshClientFactory = Substitute.For <ISshClientFactory>();

            var fakeSshClient = Substitute.For <ISshClient>();

            fakeSshClientFactory
            .CreateForLightsailInstanceAsync("ip-address")
            .Returns(fakeSshClient);

            var fakeProvisioningStateFactory = Substitute.For <IProvisioningStateFactory>();

            fakeProvisioningStateFactory
            .Create <CompleteInstanceSetupState>()
            .Returns(new CompleteInstanceSetupState(
                         fakeSshClientFactory,
                         Substitute.For <IMediator>()));

            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup(services =>
            {
                services.AddSingleton(fakeDockerComposeParserFactory);
                services.AddSingleton(fakeProvisioningStateFactory);
                services.AddSingleton(fakeSshClientFactory);
                services.AddSingleton(fakeMediator);
            });

            var state = serviceProvider.GetRequiredService <RunDockerComposeOnInstanceState>();

            state.IpAddress    = "ip-address";
            state.InstanceName = "some-instance-name";
            state.DockerComposeYmlFilePaths = new[]
            {
                "some-docker-compose-yml-contents"
            };
            state.Authentication = null;

            await state.InitializeAsync();

            //Act
            await state.UpdateAsync();

            //Assert
            await fakeSshClient
            .DidNotReceive()
            .ExecuteCommandAsync(
                Arg.Any <SshRetryPolicy>(),
                Arg.Is <string>(arg =>
                                arg.Contains("docker login")));
        }
示例#2
0
        public async Task GetJobById_JobExists_JobHasInitialStateOfFlow()
        {
            //Arrange
            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup();

            ConfigureFakeLightsailClient(serviceProvider);

            var provisioningService = serviceProvider.GetRequiredService <IProvisioningService>();

            var fakeInitialState = Substitute.For <IProvisioningState>();

            var fakeFlow = Substitute.For <IProvisioningStateFlow>();

            fakeFlow
            .GetInitialStateAsync(Arg.Any <InitialStateContext>())
            .Returns(fakeInitialState);

            var createdJob = await provisioningService.ScheduleJobAsync(fakeFlow);

            //Act
            var job = await provisioningService.GetJobByIdAsync(createdJob.Id);

            //Assert
            var state = job.CurrentState;

            Assert.AreSame(fakeInitialState, state);
        }
        public async Task Update_StateCompleted_PublishesServerProvisionedEvent()
        {
            //Arrange
            var fakeLightsailClient  = Substitute.For <IAmazonLightsail>();
            var fakeSshClientFactory = Substitute.For <ISshClientFactory>();
            var fakeMediator         = Substitute.For <IMediator>();

            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup(services =>
            {
                services.AddSingleton(fakeMediator);
                services.AddSingleton(fakeLightsailClient);
                services.AddSingleton(fakeSshClientFactory);
            });

            var state = serviceProvider.GetRequiredService <CompleteInstanceSetupState>();

            state.IpAddress    = "some-ip-address";
            state.InstanceName = "some-instance-name";

            await state.InitializeAsync();

            //Act
            await state.UpdateAsync();

            //Assert
            await fakeMediator
            .Received(1)
            .Send(
                Arg.Is <RegisterInstanceAsProvisionedCommand>(arg =>
                                                              arg.InstanceName == "some-instance-name"),
                default);
        }
示例#4
0
        public async Task GetNextState_FromRunDockerComposeOnInstanceState_ReturnsNull()
        {
            //Arrange
            var flow = new DeployToClusterStateFlow(
                "127.0.0.1",
                new[]
            {
                "some-docker-compose-contents"
            });

            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup();
            var stateFactory    = new ProvisioningStateFactory(serviceProvider);

            var fakeState    = Substitute.For <IRunDockerComposeOnInstanceState>();
            var fakeMediator = Substitute.For <IMediator>();

            //Act
            var state = await flow.GetNextStateAsync(new NextStateContext(
                                                         fakeMediator,
                                                         stateFactory,
                                                         fakeState));

            //Assert
            Assert.IsNull(state);
        }
        public async Task Initialize_ConnectionCantBeMadeToSsh_ThrowsStateUpdateException()
        {
            //Arrange
            var fakeLightsailClient = Substitute.For <IAmazonLightsail>();

            var fakeSshClientFactory = Substitute.For <ISshClientFactory>();

            fakeSshClientFactory
            .CreateForLightsailInstanceAsync("some-ip-address")
            .Throws(new SshConnectionException());

            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup(services =>
            {
                services.AddSingleton(fakeLightsailClient);
                services.AddSingleton(fakeSshClientFactory);
            });

            var state = serviceProvider.GetRequiredService <CompleteInstanceSetupState>();

            state.IpAddress = "some-ip-address";

            //Act
            var exception = await Assert.ThrowsExceptionAsync <StateUpdateException>(async() =>
                                                                                     await state.InitializeAsync());

            //Assert
            Assert.IsNotNull(exception);
        }
        public async Task GetInitialState_ValuesGiven_TransfersValuesToInitialState()
        {
            //Arrange
            var flow = new ProvisionInstanceStateFlow(
                "some-plan-id",
                new Dogger.Domain.Models.Instance()
            {
                Name = "some-instance-name"
            });

            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup();
            var stateFactory    = new ProvisioningStateFactory(serviceProvider);

            var fakeMediator = Substitute.For <IMediator>();

            //Act
            var state = await flow.GetInitialStateAsync(new InitialStateContext(
                                                            fakeMediator,
                                                            stateFactory)) as ICreateLightsailInstanceState;

            //Assert
            Assert.IsNotNull(state);

            Assert.AreEqual("some-plan-id", state.PlanId);
            Assert.AreEqual("some-instance-name", state.DatabaseInstance.Name);
        }
        public async Task GetNextState_FromUnknownState_ThrowsException()
        {
            //Arrange
            var flow = new ProvisionInstanceStateFlow(
                "some-plan-id",
                new Dogger.Domain.Models.Instance()
            {
                Name = "some-instance-name"
            });

            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup();
            var stateFactory    = new ProvisioningStateFactory(serviceProvider);

            var fakeState    = Substitute.For <IProvisioningState>();
            var fakeMediator = Substitute.For <IMediator>();

            //Act
            var exception = await Assert.ThrowsExceptionAsync <UnknownFlowStateException>(async() =>
                                                                                          await flow.GetNextStateAsync(new NextStateContext(
                                                                                                                           fakeMediator,
                                                                                                                           stateFactory,
                                                                                                                           fakeState)));

            //Assert
            Assert.IsNotNull(exception);
        }
        public async Task GetNextState_FromCompleteInstanceSetupState_ReturnsNull()
        {
            //Arrange
            var flow = new ProvisionInstanceStateFlow(
                "some-plan-id",
                new Dogger.Domain.Models.Instance()
            {
                Name = "some-instance-name"
            });

            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup();
            var stateFactory    = new ProvisioningStateFactory(serviceProvider);

            var fakeState    = Substitute.For <ICompleteInstanceSetupState>();
            var fakeMediator = Substitute.For <IMediator>();

            //Act
            var state = await flow.GetNextStateAsync(new NextStateContext(
                                                         fakeMediator,
                                                         stateFactory,
                                                         fakeState));

            //Assert
            Assert.IsNull(state);
        }
示例#9
0
        public async Task StartAsync_JobAvailableWithFinishedState_UpdatesJobStateToCompletedState()
        {
            //Arrange
            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup();

            ConfigureFakeLightsailClient(serviceProvider);

            var provisioningService = serviceProvider.GetRequiredService <IProvisioningService>();

            var fakeJobState = Substitute.For <IProvisioningState>();

            fakeJobState
            .UpdateAsync()
            .Returns(ProvisioningStateUpdateResult.Succeeded);

            var fakeFlow = Substitute.For <IProvisioningStateFlow>();

            fakeFlow
            .GetInitialStateAsync(Arg.Any <InitialStateContext>())
            .Returns(fakeJobState);

            fakeFlow
            .GetNextStateAsync(Arg.Any <NextStateContext>())
            .Returns((IProvisioningState)null);

            var createdJob = await provisioningService.ScheduleJobAsync(fakeFlow);

            //Act
            await provisioningService.StartAsync(new CancellationToken(true));

            //Assert
            Assert.IsTrue(createdJob.IsSucceeded);
        }
示例#10
0
        public async Task StartAsync_UpdatingThrowsStateUpdateException_SetsExceptionOnJob()
        {
            //Arrange
            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup();

            ConfigureFakeLightsailClient(serviceProvider);

            var provisioningService = serviceProvider.GetRequiredService <IProvisioningService>();

            var createdJob = await provisioningService.ScheduleJobAsync(Substitute.For <IProvisioningStateFlow>());

            var fakeJobState = Substitute.For <IProvisioningState>();

            fakeJobState
            .UpdateAsync()
            .Throws(new StateUpdateException("Test error"));

            createdJob.CurrentState = fakeJobState;

            //Act
            await provisioningService.StartAsync(new CancellationToken(true));

            //Assert
            Assert.IsInstanceOfType(createdJob.Exception, typeof(StateUpdateException));
        }
示例#11
0
        public async Task StopAsync_CancellationTokenFired_RespectsCancellation()
        {
            //Arrange
            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup();

            var provisioningService = serviceProvider.GetRequiredService <IProvisioningService>();

            //Act
            await provisioningService.StopAsync(new CancellationToken(true));
        }
        public async Task UpdateAsync_IpAddressProvided_ReturnsSucceeded()
        {
            //Arrange
            var fakeDockerComposeParser = Substitute.For <IDockerComposeParser>();

            var fakeDockerComposeParserFactory = Substitute.For <IDockerComposeParserFactory>();

            fakeDockerComposeParserFactory
            .Create(Arg.Any <string>())
            .Returns(fakeDockerComposeParser);

            var fakeMediator = Substitute.For <IMediator>();

            var fakeSshClientFactory = Substitute.For <ISshClientFactory>();

            var fakeSshClient = Substitute.For <ISshClient>();

            fakeSshClientFactory
            .CreateForLightsailInstanceAsync("ip-address")
            .Returns(fakeSshClient);

            var fakeProvisioningStateFactory = Substitute.For <IProvisioningStateFactory>();

            fakeProvisioningStateFactory
            .Create <CompleteInstanceSetupState>()
            .Returns(new CompleteInstanceSetupState(
                         fakeSshClientFactory,
                         Substitute.For <IMediator>()));

            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup(services =>
            {
                services.AddSingleton(fakeDockerComposeParserFactory);
                services.AddSingleton(fakeProvisioningStateFactory);
                services.AddSingleton(fakeSshClientFactory);
                services.AddSingleton(fakeMediator);
            });

            var state = serviceProvider.GetRequiredService <RunDockerComposeOnInstanceState>();

            state.IpAddress    = "ip-address";
            state.InstanceName = "some-instance-name";
            state.DockerComposeYmlFilePaths = new[]
            {
                "some-docker-compose-yml-contents"
            };

            await state.InitializeAsync();

            //Act
            var newState = await state.UpdateAsync();

            //Assert
            Assert.AreEqual(ProvisioningStateUpdateResult.Succeeded, newState);
        }
        public void GetMapperFromServiceProvider_NoValidationExceptionsThrown()
        {
            //Arrange
            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup();

            //Act
            var mapper = serviceProvider.GetRequiredService <IMapper>();

            //Assert
            Assert.IsNotNull(mapper);
        }
示例#14
0
        public async Task StartAsync_JobAvailableWithCompletedState_InitializesNextJob()
        {
            //Arrange
            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup();

            ConfigureFakeLightsailClient(serviceProvider);

            var provisioningService = serviceProvider.GetRequiredService <IProvisioningService>();

            var startCancellationTokenSource = new CancellationTokenSource();

            var fakeInitialState = Substitute.For <IProvisioningState>();

            fakeInitialState
            .UpdateAsync()
            .Returns(ProvisioningStateUpdateResult.Succeeded);

            var fakeNextState = Substitute.For <IProvisioningState>();

            fakeNextState
            .UpdateAsync()
            .Returns(call =>
            {
                startCancellationTokenSource.Cancel();
                return(ProvisioningStateUpdateResult.Succeeded);
            });

            var fakeFlow = Substitute.For <IProvisioningStateFlow>();

            fakeFlow
            .GetInitialStateAsync(Arg.Any <InitialStateContext>())
            .Returns(fakeInitialState);

            fakeFlow
            .GetNextStateAsync(Arg.Is <NextStateContext>(args =>
                                                         args.CurrentState == fakeInitialState))
            .Returns(
                fakeNextState);

            fakeFlow
            .GetNextStateAsync(Arg.Is <NextStateContext>(args =>
                                                         args.CurrentState == fakeNextState))
            .Returns((IProvisioningState)null);

            await provisioningService.ScheduleJobAsync(fakeFlow);

            //Act
            await provisioningService.StartAsync(startCancellationTokenSource.Token);

            //Assert
            await fakeNextState
            .Received(1)
            .InitializeAsync();
        }
示例#15
0
        public async Task GetJobById_JobDoesNotExists_ReturnsNull()
        {
            //Arrange
            var serviceProvider     = TestServiceProviderFactory.CreateUsingStartup();
            var provisioningService = serviceProvider.GetRequiredService <IProvisioningService>();

            //Act
            var job = await provisioningService.GetJobByIdAsync("non-existing-id");

            //Assert
            Assert.IsNull(job);
        }
示例#16
0
        public async Task StartAsync_JobAvailableWithInProgressState_ReschedulesJob()
        {
            //Arrange
            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup();

            ConfigureFakeLightsailClient(serviceProvider);

            var provisioningService = serviceProvider.GetRequiredService <IProvisioningService>();

            var updateCallCount = 0;
            var startCancellationTokenSource = new CancellationTokenSource();

            var fakeJobState = Substitute.For <IProvisioningState>();

            fakeJobState
            .UpdateAsync()
            .Returns(callInfo => {
                var updateResults = new[]
                {
                    ProvisioningStateUpdateResult.InProgress,
                    ProvisioningStateUpdateResult.Succeeded,
                    ProvisioningStateUpdateResult.Succeeded
                };

                var updateResult = updateResults[updateCallCount++];
                if (updateResult == ProvisioningStateUpdateResult.Succeeded)
                {
                    startCancellationTokenSource.Cancel();
                }

                return(updateResult);
            });

            var fakeFlow = Substitute.For <IProvisioningStateFlow>();

            fakeFlow
            .GetInitialStateAsync(Arg.Any <InitialStateContext>())
            .Returns(fakeJobState);

            fakeFlow
            .GetNextStateAsync(Arg.Any <NextStateContext>())
            .Returns(
                fakeJobState,
                (IProvisioningState)null);

            var createdJob = await provisioningService.ScheduleJobAsync(fakeFlow);

            //Act
            await provisioningService.StartAsync(startCancellationTokenSource.Token);

            //Assert
            Assert.IsTrue(createdJob.IsSucceeded);
        }
示例#17
0
        public async Task UpdateAsync_NoIpAddressSet_ThrowsException()
        {
            //Arrange
            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup();

            var state = serviceProvider.GetRequiredService <InstallSoftwareOnInstanceState>();

            //Act
            var exception = await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() =>
                                                                                          await state.UpdateAsync());

            //Assert
            Assert.IsNotNull(exception);
        }
        public async Task Update_HasFailedOperation_ThrowsStateUpdateException()
        {
            //Arrange
            var fakeAmazonLightsail = Substitute.For <IAmazonLightsail>();

            fakeAmazonLightsail
            .CreateInstancesAsync(Arg.Any <CreateInstancesRequest>())
            .Returns(new CreateInstancesResponse()
            {
                Operations = new List <Operation>()
            });

            var fakeLightsailOperationService = Substitute.For <ILightsailOperationService>();

            fakeLightsailOperationService
            .GetOperationsFromIdsAsync(Arg.Any <IEnumerable <string> >())
            .Returns(new[]
            {
                new Operation()
                {
                    Status = OperationStatus.Failed
                }
            });

            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup(services =>
            {
                services.AddSingleton(fakeAmazonLightsail);
                services.AddSingleton(fakeLightsailOperationService);
            });

            var state = serviceProvider.GetRequiredService <CreateLightsailInstanceState>();

            state.PlanId           = "dummy";
            state.DatabaseInstance = new Dogger.Domain.Models.Instance()
            {
                Name    = "dummy",
                Cluster = new Cluster()
            };

            await state.InitializeAsync();

            //Act
            var exception = await Assert.ThrowsExceptionAsync <StateUpdateException>(async() =>
                                                                                     await state.UpdateAsync());

            //Assert
            Assert.IsNotNull(exception);
        }
        public async Task Initialize_DatabaseInstanceWithClusterSet_CreatesNecessaryTags()
        {
            //Arrange
            var fakeUserId     = Guid.NewGuid();
            var fakeClusterId  = Guid.NewGuid();
            var fakeInstanceId = Guid.NewGuid();

            var fakeAmazonLightsail = Substitute.For <IAmazonLightsail>();

            fakeAmazonLightsail
            .CreateInstancesAsync(Arg.Any <CreateInstancesRequest>())
            .Returns(new CreateInstancesResponse()
            {
                Operations = new List <Operation>()
            });

            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup(services =>
            {
                services.AddSingleton(fakeAmazonLightsail);
            });

            var state = serviceProvider.GetRequiredService <CreateLightsailInstanceState>();

            state.PlanId           = "some-plan-id";
            state.DatabaseInstance = new Dogger.Domain.Models.Instance()
            {
                Id      = fakeInstanceId,
                Name    = "some-instance-name",
                Cluster = new Cluster()
                {
                    Id     = fakeClusterId,
                    UserId = fakeUserId
                }
            };

            //Act
            await state.InitializeAsync();

            //Assert
            await fakeAmazonLightsail
            .Received(1)
            .CreateInstancesAsync(Arg.Is <CreateInstancesRequest>(args =>
                                                                  args.Tags.Any(x => x.Key == "UserId" && x.Value == fakeUserId.ToString()) &&
                                                                  args.Tags.Any(x => x.Key == "StripePlanId" && x.Value == "some-plan-id") &&
                                                                  args.Tags.Any(x => x.Key == "ClusterId" && x.Value == fakeClusterId.ToString()) &&
                                                                  args.Tags.Any(x => x.Key == "InstanceId" && x.Value == fakeInstanceId.ToString())));
        }
示例#20
0
        public async Task StartAsync_JobAvailableWithNoState_ThrowsException()
        {
            //Arrange
            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup();

            ConfigureFakeLightsailClient(serviceProvider);

            var provisioningService = serviceProvider.GetRequiredService <IProvisioningService>();

            var createdJob = await provisioningService.ScheduleJobAsync(Substitute.For <IProvisioningStateFlow>());

            createdJob.CurrentState = null;

            //Act & assert
            await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() =>
                                                                          await provisioningService.StartAsync(new CancellationToken(true)));
        }
示例#21
0
        public async Task GetCompletedJob_Always_ReturnsCompletedJob()
        {
            //Arrange
            var serviceProvider     = TestServiceProviderFactory.CreateUsingStartup();
            var provisioningService = serviceProvider.GetRequiredService <IProvisioningService>();

            //Act
            var job = provisioningService.GetCompletedJob();

            //Assert
            Assert.IsNotNull(job);

            Assert.IsTrue(job.IsEnded);
            Assert.IsTrue(job.IsSucceeded);

            Assert.IsFalse(job.IsFailed);
        }
示例#22
0
        public async Task GetJobById_JobExists_JobIsReturned()
        {
            //Arrange
            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup();

            ConfigureFakeLightsailClient(serviceProvider);

            var provisioningService = serviceProvider.GetRequiredService <IProvisioningService>();

            var createdJob = await provisioningService.ScheduleJobAsync(Substitute.For <IProvisioningStateFlow>());

            //Act
            var job = await provisioningService.GetJobByIdAsync(createdJob.Id);

            //Assert
            Assert.IsNotNull(job);
        }
示例#23
0
        public async Task UpdateAsync_IpAddressProvided_CreatesProperSshClient()
        {
            //Arrange
            var fakeSshClientFactory = Substitute.For <ISshClientFactory>();

            var fakeSshClient = Substitute.For <ISshClient>();

            fakeSshClientFactory
            .CreateForLightsailInstanceAsync("ip-address")
            .Returns(fakeSshClient);

            var fakeMediator = Substitute.For <IMediator>();

            var fakeProvisioningStateFactory = Substitute.For <IProvisioningStateFactory>();

            fakeProvisioningStateFactory
            .Create <CompleteInstanceSetupState>()
            .Returns(new CompleteInstanceSetupState(
                         fakeSshClientFactory,
                         fakeMediator));

            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup(services => {
                services.AddSingleton(fakeSshClientFactory);
            });

            var state = serviceProvider.GetRequiredService <InstallSoftwareOnInstanceState>();

            state.IpAddress = "ip-address";

            await state.InitializeAsync();

            //Act
            await state.UpdateAsync();

            //Assert
            await fakeSshClient
            .Received()
            .ExecuteCommandAsync(
                Arg.Any <SshRetryPolicy>(),
                Arg.Any <string>());
        }
示例#24
0
        public async Task GetInitialState_ValuesGiven_TransfersValuesToInitialState()
        {
            //Arrange
            var buildArguments = new Dictionary <string, string>();

            var flow = new DeployToClusterStateFlow(
                "127.0.0.1",
                new[]
            {
                "some-docker-compose-contents"
            })
            {
                BuildArguments = buildArguments
            };

            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup();
            var stateFactory    = new ProvisioningStateFactory(serviceProvider);

            var fakeMediator = Substitute.For <IMediator>();

            fakeMediator
            .Send(Arg.Any <GetLightsailInstanceByNameQuery>())
            .Returns(new Instance()
            {
                PublicIpAddress = "127.0.0.1"
            });

            //Act
            var state = await flow.GetInitialStateAsync(new InitialStateContext(
                                                            fakeMediator,
                                                            stateFactory)) as IRunDockerComposeOnInstanceState;

            //Assert
            Assert.IsNotNull(state);

            Assert.AreEqual("127.0.0.1", state.IpAddress);
            Assert.AreEqual("some-docker-compose-contents", state.DockerComposeYmlFilePaths?.Single());

            Assert.AreSame(buildArguments, state.BuildArguments);
        }
        public async Task GetNextState_FromCreateLightsailInstanceState_ReturnsInstallDockerOnInstanceStateWithValuesTransferred()
        {
            //Arrange
            var fakeUserId = Guid.NewGuid();

            var flow = new ProvisionInstanceStateFlow(
                "some-plan-id",
                new Dogger.Domain.Models.Instance()
            {
                Name = "some-instance-name"
            })
            {
                UserId = fakeUserId
            };

            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup();
            var stateFactory    = new ProvisioningStateFactory(serviceProvider);

            var fakeMediator = Substitute.For <IMediator>();

            var fakeState = Substitute.For <ICreateLightsailInstanceState>();

            fakeState.CreatedLightsailInstance.Returns(new Instance()
            {
                PublicIpAddress = "127.0.0.1"
            });

            //Act
            var state = await flow.GetNextStateAsync(new NextStateContext(
                                                         fakeMediator,
                                                         stateFactory,
                                                         fakeState)) as IInstallSoftwareOnInstanceState;

            //Assert
            Assert.IsNotNull(state);

            Assert.AreEqual("127.0.0.1", state.IpAddress);
            Assert.AreEqual(fakeUserId, state.UserId);
        }
示例#26
0
        public async Task UpdateAsync_IpAddressProvided_ReturnsCompleted()
        {
            //Arrange
            var fakeSshClientFactory = Substitute.For <ISshClientFactory>();

            var fakeSshClient = Substitute.For <ISshClient>();

            fakeSshClientFactory
            .CreateForLightsailInstanceAsync("ip-address")
            .Returns(fakeSshClient);

            var fakeMediator = Substitute.For <IMediator>();

            var fakeProvisioningStateFactory = Substitute.For <IProvisioningStateFactory>();

            fakeProvisioningStateFactory
            .Create <CompleteInstanceSetupState>()
            .Returns(new CompleteInstanceSetupState(
                         fakeSshClientFactory,
                         fakeMediator));

            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup(services => {
                services.AddSingleton(fakeSshClientFactory);
            });

            var state = serviceProvider.GetRequiredService <InstallSoftwareOnInstanceState>();

            state.IpAddress = "ip-address";

            await state.InitializeAsync();

            //Act
            var newState = await state.UpdateAsync();

            //Assert
            Assert.AreEqual(ProvisioningStateUpdateResult.Succeeded, newState);
        }
示例#27
0
        public void AllControllersCanBeResolved()
        {
            //Arrange
            var controllerTypes = typeof(Startup)
                                  .Assembly
                                  .GetTypes()
                                  .Where(x => x.IsClass)
                                  .Where(x => x.Name.EndsWith(nameof(Controller)));

            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup();

            //Act
            var controllerInstances = controllerTypes
                                      .Select(stateType => serviceProvider.GetRequiredService(stateType))
                                      .ToArray();

            //Assert
            Assert.AreNotEqual(0, controllerInstances.Length);

            foreach (var stateInstance in controllerInstances)
            {
                Assert.IsNotNull(stateInstance);
            }
        }
        public async Task UpdateAsync_IpAddressAndEnvironmentFilesProvided_SetsUpEnvironmentFilesOnInstance()
        {
            //Arrange
            var fakeDockerComposeParser = Substitute.For <IDockerComposeParser>();

            var fakeDockerComposeParserFactory = Substitute.For <IDockerComposeParserFactory>();

            fakeDockerComposeParserFactory
            .Create(Arg.Any <string>())
            .Returns(fakeDockerComposeParser);

            var fakeMediator = Substitute.For <IMediator>();

            var fakeSshClientFactory = Substitute.For <ISshClientFactory>();

            var fakeSshClient = Substitute.For <ISshClient>();

            fakeSshClientFactory
            .CreateForLightsailInstanceAsync("ip-address")
            .Returns(fakeSshClient);

            var fakeProvisioningStateFactory = Substitute.For <IProvisioningStateFactory>();

            fakeProvisioningStateFactory
            .Create <CompleteInstanceSetupState>()
            .Returns(new CompleteInstanceSetupState(
                         fakeSshClientFactory,
                         Substitute.For <IMediator>()));

            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup(services =>
            {
                services.AddSingleton(fakeDockerComposeParserFactory);
                services.AddSingleton(fakeProvisioningStateFactory);
                services.AddSingleton(fakeSshClientFactory);
                services.AddSingleton(fakeMediator);
            });

            var state = serviceProvider.GetRequiredService <RunDockerComposeOnInstanceState>();

            state.IpAddress    = "ip-address";
            state.InstanceName = "some-instance-name";
            state.Files        = new[]
            {
                new InstanceDockerFile(
                    "some-file-name",
                    Encoding.UTF8.GetBytes("some-file-contents"))
            };
            state.DockerComposeYmlFilePaths = new[]
            {
                "some-docker-compose-yml-contents"
            };

            await state.InitializeAsync();

            //Act
            await state.UpdateAsync();

            //Assert
            await fakeSshClient
            .Received()
            .ExecuteCommandAsync(
                Arg.Any <SshRetryPolicy>(),
                Arg.Is <string>(arg => arg.Contains("some-file-name")),
                Arg.Any <Dictionary <string, string> >());
        }
        public async Task UpdateAsync_IpAddressAndDockerComposeYmlWithPortsProvided_OpensNecessaryPortsAndDockerComposePorts()
        {
            //Arrange
            var fakeSshClientFactory = Substitute.For <ISshClientFactory>();

            var fakeDockerComposeParser = Substitute.For <IDockerComposeParser>();

            fakeDockerComposeParser
            .GetExposedHostPorts()
            .Returns(new[]
            {
                new ExposedPort()
                {
                    Protocol = SocketProtocol.Tcp,
                    Port     = 1337
                }
            });

            var fakeDockerComposeParserFactory = Substitute.For <IDockerComposeParserFactory>();

            fakeDockerComposeParserFactory
            .Create(Arg.Any <string>())
            .Returns(fakeDockerComposeParser);

            var fakeMediator = Substitute.For <IMediator>();

            var fakeSshClient = Substitute.For <ISshClient>();

            fakeSshClientFactory
            .CreateForLightsailInstanceAsync("ip-address")
            .Returns(fakeSshClient);

            var fakeProvisioningStateFactory = Substitute.For <IProvisioningStateFactory>();

            fakeProvisioningStateFactory
            .Create <CompleteInstanceSetupState>()
            .Returns(new CompleteInstanceSetupState(
                         fakeSshClientFactory,
                         Substitute.For <IMediator>()));

            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup(services =>
            {
                services.AddSingleton(fakeDockerComposeParserFactory);
                services.AddSingleton(fakeProvisioningStateFactory);
                services.AddSingleton(fakeSshClientFactory);
                services.AddSingleton(fakeMediator);
            });

            var state = serviceProvider.GetRequiredService <RunDockerComposeOnInstanceState>();

            state.IpAddress      = "ip-address";
            state.InstanceName   = "some-instance-name";
            state.Authentication = new [] {
                new DockerAuthenticationArguments(
                    "some-username",
                    "some-password")
                {
                    RegistryHostName = "some-registry"
                }
            };
            state.DockerComposeYmlFilePaths = new[]
            {
                "some-docker-compose-yml-contents"
            };

            await state.InitializeAsync();

            //Act
            await state.UpdateAsync();

            //Assert
            await fakeMediator
            .Received(1)
            .Send(Arg.Is <OpenFirewallPortsCommand>(arg =>
                                                    arg.Ports.Any(p => p.ToPort == 1337 && p.FromPort == 1337 && p.Protocol == SocketProtocol.Tcp)));
        }
        public async Task Update_AllOperationsSucceededAndInstanceNotRunning_ReturnsInProgress()
        {
            //Arrange
            var fakeAmazonLightsail = Substitute.For <IAmazonLightsail>();

            fakeAmazonLightsail
            .CreateInstancesAsync(Arg.Any <CreateInstancesRequest>())
            .Returns(new CreateInstancesResponse()
            {
                Operations = new List <Operation>()
            });

            var fakeLightsailOperationService = Substitute.For <ILightsailOperationService>();

            fakeLightsailOperationService
            .GetOperationsFromIdsAsync(Arg.Any <IEnumerable <string> >())
            .Returns(new[]
            {
                new Operation()
                {
                    Status = OperationStatus.Succeeded
                }
            });

            var fakeMediator = Substitute.For <IMediator>();

            fakeMediator
            .Send(Arg.Is <GetLightsailInstanceByNameQuery>(arg =>
                                                           arg.Name == "some-instance-name"))
            .Returns(new Instance()
            {
                State = new InstanceState()
                {
                    Name = "not-running"
                }
            });

            var fakeProvisioningStateFactory = Substitute.For <IProvisioningStateFactory>();

            fakeProvisioningStateFactory
            .Create <InstallSoftwareOnInstanceState>()
            .Returns(new InstallSoftwareOnInstanceState(
                         Substitute.For <ISshClientFactory>(),
                         Substitute.For <IMediator>()));

            var serviceProvider = TestServiceProviderFactory.CreateUsingStartup(services =>
            {
                services.AddSingleton(fakeAmazonLightsail);
                services.AddSingleton(fakeLightsailOperationService);
                services.AddSingleton(fakeMediator);
            });

            var state = serviceProvider.GetRequiredService <CreateLightsailInstanceState>();

            state.PlanId           = "dummy";
            state.DatabaseInstance = new Dogger.Domain.Models.Instance()
            {
                Name    = "some-instance-name",
                Cluster = new Cluster()
            };

            await state.InitializeAsync();

            //Act
            var newState = await state.UpdateAsync();

            //Assert
            Assert.AreEqual(ProvisioningStateUpdateResult.InProgress, newState);
        }