Example #1
0
        public async Task WhenCommandSucceeds_ReturnsDockerContainer()
        {
            // Arrange
            var mockDockerClient = BuildMockDockerClient(true, true);

            var mockCommand = MotherFor.CommandWrapper
                              .ThatSucceeds()
                              .WithOutput("thisisacontaineridthatisover12chars")
                              .Build();

            var mockCommandFactory = MotherFor.CommandFactory
                                     .ForWorkingDirectory(_workingDirectory)
                                     .ForCommandReturn(DockerCommands.Image.RunContainer(ExpectedId, null), mockCommand)
                                     .Build();

            var dockerImage = new DockerImageResult
            {
                Id         = ExpectedId,
                Repository = ExpectedRepository,
                Tag        = ExpectedTag
            };

            var sut = new DockerImage(mockDockerClient, mockCommandFactory, dockerImage);

            // Act
            var result = await sut.StartContainerAsync();

            // Assert
            result.Should().BeOfType <DockerContainer>();
        }
Example #2
0
        public void WhenImageExistsFails_ItDoesNotSetDeletedState()
        {
            // Arrange
            var mockDockerClient = BuildMockDockerClient(false, true);

            var mockCommand = MotherFor.CommandWrapper.ThatFails().WithExitCode(-1).Build();

            var mockCommandFactory = MotherFor.CommandFactory
                                     .ForWorkingDirectory(_workingDirectory)
                                     .ForCommandReturn(DockerCommands.Image.RunContainer(ExpectedId, null), mockCommand)
                                     .Build();

            var dockerImage = new DockerImageResult
            {
                Id         = ExpectedId,
                Repository = ExpectedRepository,
                Tag        = ExpectedTag
            };

            var sut = new DockerImage(mockDockerClient, mockCommandFactory, dockerImage);

            // Act
            Assert.CatchAsync(async() => await sut.StartContainerAsync());

            // Assert
            sut.Deleted.Should().BeFalse();
        }
Example #3
0
        public void WhenCommandSucceeds_ButNoValidContainerIdInResponse(params string[] output)
        {
            // Arrange
            var mockDockerClient = BuildMockDockerClient(true, true);

            var mockCommand = MotherFor.CommandWrapper.ThatSucceeds().WithOutput(output).Build();

            var mockCommandFactory = MotherFor.CommandFactory
                                     .ForWorkingDirectory(_workingDirectory)
                                     .ForCommandReturn(DockerCommands.Image.RunContainer(ExpectedId, null), mockCommand)
                                     .Build();

            var dockerImage = new DockerImageResult
            {
                Id         = ExpectedId,
                Repository = ExpectedRepository,
                Tag        = ExpectedTag
            };

            var sut = new DockerImage(mockDockerClient, mockCommandFactory, dockerImage);

            // Act
            var exception = Assert.CatchAsync(async() => await sut.StartContainerAsync());

            // Assert
            exception.Should().BeOfType <DockerCommandUnexpectedOutputException>();
        }
Example #4
0
        public void WhenCommandFails_ThrowsDockerCommandException()
        {
            // Arrange
            var mockDockerClient = BuildMockDockerClient(true, true);

            var mockCommand = MotherFor.CommandWrapper.ThatFails().WithExitCode(-1).Build();

            var mockCommandFactory = MotherFor.CommandFactory
                                     .ForWorkingDirectory(_workingDirectory)
                                     .ForCommandReturn(DockerCommands.Image.RunContainer(ExpectedId, null), mockCommand)
                                     .Build();

            var dockerImage = new DockerImageResult
            {
                Id         = ExpectedId,
                Repository = ExpectedRepository,
                Tag        = ExpectedTag
            };

            var sut = new DockerImage(mockDockerClient, mockCommandFactory, dockerImage);

            // Act
            var exception = Assert.CatchAsync(async() => await sut.StartContainerAsync());

            // Assert
            exception.Should().BeOfType <DockerCommandException>();
        }
Example #5
0
        public async Task WhenNotDeleted_CallsStartContainer()
        {
            // Arrange
            var mockDockerClient = BuildMockDockerClient(true, true);

            var mockCommand = MotherFor.CommandWrapper.ThatSucceeds().WithOutput("twelvecharac").Build();

            var mockCommandFactory = MotherFor.CommandFactory
                                     .ForWorkingDirectory(_workingDirectory)
                                     .ForCommandReturn(DockerCommands.Image.RunContainer(ExpectedId, null), mockCommand)
                                     .Build();

            var dockerImage = new DockerImageResult
            {
                Id         = ExpectedId,
                Repository = ExpectedRepository,
                Tag        = ExpectedTag
            };

            var sut = new DockerImage(mockDockerClient, mockCommandFactory, dockerImage);

            // Act
            await sut.StartContainerAsync();

            // Assert
            mockCommandFactory.Received(1).RunCommand(DockerCommands.Image.RunContainer(ExpectedId, null), _workingDirectory, Arg.Any <CancellationToken?>());
        }
Example #6
0
        public async Task WhenCommandFails_CallsImageExists()
        {
            // Arrange
            var mockDockerClient = BuildMockDockerClient(true, true);

            var mockCommand = MotherFor.CommandWrapper.ThatFails().WithExitCode(-1).Build();

            var mockCommandFactory = MotherFor.CommandFactory
                                     .ForWorkingDirectory(_workingDirectory)
                                     .ForCommandReturn(DockerCommands.Image.RunContainer(ExpectedId, null), mockCommand)
                                     .Build();

            var dockerImage = new DockerImageResult
            {
                Id         = ExpectedId,
                Repository = ExpectedRepository,
                Tag        = ExpectedTag
            };

            var sut = new DockerImage(mockDockerClient, mockCommandFactory, dockerImage);

            // Act
            Assert.CatchAsync(async() => await sut.StartContainerAsync());

            // Assert
            await mockDockerClient.Received(1).ImageExistsAsync(Arg.Any <string>());
        }
Example #7
0
        public void WhenContainerCpuLimitSpecifiedIsTooHigh()
        {
            // Arrange
            var aboveMaxCpuCount = Environment.ProcessorCount + 0.1m;

            var mockDockerClient = BuildMockDockerClient(true, true);

            var mockCommand = MotherFor.CommandWrapper.ThatFails().WithExitCode(-1).Build();

            var mockCommandFactory = MotherFor.CommandFactory
                                     .ForWorkingDirectory(_workingDirectory)
                                     .ForCommandReturn(DockerCommands.Image.RunContainer(ExpectedId, new StartContainerOptions().WithCpuLimit(aboveMaxCpuCount)), mockCommand)
                                     .Build();

            var dockerImage = new DockerImageResult
            {
                Id         = ExpectedId,
                Repository = ExpectedRepository,
                Tag        = ExpectedTag
            };

            var sut = new DockerImage(mockDockerClient, mockCommandFactory, dockerImage);

            // Act
            var exception = Assert.CatchAsync(async() => await sut.StartContainerAsync(o => o.WithCpuLimit(aboveMaxCpuCount)));

            // Assert
            exception.Should().BeOfType <ArgumentException>();
        }
Example #8
0
        public async Task WhenCommandSucceeds_ItDoesNotChangeDeletedState()
        {
            // Arrange
            var mockDockerClient = BuildMockDockerClient(true, true);

            var mockCommand = MotherFor.CommandWrapper
                              .ThatSucceeds()
                              .WithOutput("avalidcontainerid")
                              .Build();

            var mockCommandFactory = MotherFor.CommandFactory
                                     .ForWorkingDirectory(_workingDirectory)
                                     .ForCommandReturn(DockerCommands.Image.RunContainer(ExpectedId, null), mockCommand)
                                     .Build();

            var dockerImage = new DockerImageResult
            {
                Id         = ExpectedId,
                Repository = ExpectedRepository,
                Tag        = ExpectedTag
            };

            var sut = new DockerImage(mockDockerClient, mockCommandFactory, dockerImage);
            var initialDeletedState = sut.Deleted;

            // Act
            await sut.StartContainerAsync();

            // Assert
            sut.Deleted.Should().Be(initialDeletedState);
        }
Example #9
0
        public async Task WhenCommandSucceeds_DoesNotCallImageExists()
        {
            // Arrange
            var mockDockerClient = BuildMockDockerClient(true, true);

            var mockCommand = MotherFor.CommandWrapper
                              .ThatSucceeds()
                              .WithOutput("avalidcontainerid")
                              .Build();

            var mockCommandFactory = MotherFor.CommandFactory
                                     .ForWorkingDirectory(_workingDirectory)
                                     .ForCommandReturn(DockerCommands.Image.RunContainer(ExpectedId, null), mockCommand)
                                     .Build();

            var dockerImage = new DockerImageResult
            {
                Id         = ExpectedId,
                Repository = ExpectedRepository,
                Tag        = ExpectedTag
            };

            var sut = new DockerImage(mockDockerClient, mockCommandFactory, dockerImage);

            // Act
            await sut.StartContainerAsync();

            // Assert
            await mockDockerClient.DidNotReceive().ImageExistsAsync(Arg.Any <string>());
        }
Example #10
0
        public async Task WhenCommandSucceeds_ResultContainsTruncatedContainerId()
        {
            // Arrange
            var mockDockerClient = BuildMockDockerClient(true, true);
            var fullContainerId  = "thisislongerthantwelvecharacters";

            var mockCommand = MotherFor.CommandWrapper
                              .ThatSucceeds()
                              .WithOutput(fullContainerId)
                              .Build();

            var mockCommandFactory = MotherFor.CommandFactory
                                     .ForWorkingDirectory(_workingDirectory)
                                     .ForCommandReturn(DockerCommands.Image.RunContainer(ExpectedId, null), mockCommand)
                                     .Build();

            var dockerImage = new DockerImageResult
            {
                Id         = ExpectedId,
                Repository = ExpectedRepository,
                Tag        = ExpectedTag
            };

            var expectedResult = new DockerContainer(mockDockerClient, mockCommandFactory, "thisislonger", new DockerPortMapping[0]);

            var sut = new DockerImage(mockDockerClient, mockCommandFactory, dockerImage);

            // Act
            var result = await sut.StartContainerAsync();

            // Assert
            result.Should().BeEquivalentTo(expectedResult);
        }
Example #11
0
        public void WhenDeleted_DoesNotCallImageExists()
        {
            // Arrange
            var mockDockerClient   = Substitute.For <IDockerClient>();
            var mockCommandFactory = Substitute.For <IRunCommands>();

            var dockerImage = new DockerImageResult
            {
                Id         = ExpectedId,
                Repository = ExpectedRepository,
                Tag        = ExpectedTag
            };

            var sut = new DockerImage(mockDockerClient, mockCommandFactory, dockerImage)
            {
                Deleted = true
            };

            // Act
            Assert.CatchAsync(async() => await sut.StartContainerAsync());

            // Assert
            mockDockerClient.DidNotReceive().ImageExistsAsync(Arg.Any <string>());
        }
Example #12
0
        public void WhenDeleted_ThrowsDockerImageDeletedException()
        {
            // Arrange
            var mockDockerClient   = Substitute.For <IDockerClient>();
            var mockCommandFactory = Substitute.For <IRunCommands>();

            var dockerImage = new DockerImageResult
            {
                Id         = ExpectedId,
                Repository = ExpectedRepository,
                Tag        = ExpectedTag
            };

            var sut = new DockerImage(mockDockerClient, mockCommandFactory, dockerImage)
            {
                Deleted = true
            };

            // Act
            var exception = Assert.CatchAsync(async() => await sut.StartContainerAsync());

            // Assert
            exception.Should().BeOfType <DockerImageDeletedException>();
        }