Example #1
0
        public async Task RemoveAsync(string id, CancellationToken ct = default)
        {
            if (await DockerApiClientContainer.Instance.ExistsWithIdAsync(id))
            {
                await Docker.Containers.RemoveContainerAsync(id, new ContainerRemoveParameters { Force = true, RemoveVolumes = true }, ct);
            }

            TestcontainersRegistryService.Unregister(id);
        }
            public void RunTestcontainerWithoutCleanUp()
            {
                // Given
                const string id = nameof(this.RunTestcontainerWithoutCleanUp);

                // When
                TestcontainersRegistryService.Register(id);

                // Then
                Assert.DoesNotContain(id, TestcontainersRegistryService.GetRegisteredContainers());
            }
            public void RemoveTestcontainer()
            {
                // Given
                const string id = nameof(this.RemoveTestcontainer);

                // When
                TestcontainersRegistryService.Register(id, true);
                TestcontainersRegistryService.Unregister(id);

                // Then
                Assert.DoesNotContain(id, TestcontainersRegistryService.GetRegisteredContainers());
            }
Example #4
0
        private static void PurgeOrphanedContainers()
        {
            var args = string.Join(" ", TestcontainersRegistryService.GetRegisteredContainers());

            if (string.IsNullOrEmpty(args))
            {
                return;
            }

            new Process {
                StartInfo = { FileName = "docker", Arguments = $"rm --force {args}" }
            }.Start();
        }
        private TestcontainersClient(
            TestcontainersRegistryService registryService,
            IDockerContainerOperations containerOperations,
            IDockerImageOperations imageOperations,
            IDockerSystemOperations systemOperations)
        {
            this.registryService = registryService;
            this.containers      = containerOperations;
            this.images          = imageOperations;
            this.system          = systemOperations;

            AppDomain.CurrentDomain.ProcessExit += (sender, args) => this.PurgeOrphanedContainers();
            Console.CancelKeyPress += (sender, args) => this.PurgeOrphanedContainers();
        }
Example #6
0
        public async Task <string> RunAsync(TestcontainersConfiguration config, CancellationToken ct = default)
        {
            var image = config.Container.Image;

            var imageExistsTask = DockerApiClientImage.Instance.ExistsWithNameAsync(image);

            var pullImageTask = Task.CompletedTask;

            if (!await imageExistsTask)
            {
                pullImageTask = Docker.Images.CreateImageAsync(new ImagesCreateParameters {
                    FromImage = image
                }, null, DebugProgress.Instance, ct);
            }

            var name = config.Container.Name;

            var workingDir = config.Container.WorkingDirectory;

            var converter = new TestcontainersConfigurationConverter(config);

            var entrypoint = converter.Entrypoint;

            var cmd = converter.Command;

            var env = converter.Environments;

            var labels = converter.Labels;

            var exposedPorts = converter.ExposedPorts;

            var portBindings = converter.PortBindings;

            var mounts = converter.Mounts;

            var hostConfig = new HostConfig
            {
                PortBindings = portBindings,
                Mounts       = mounts,
            };

            var createParameters = new CreateContainerParameters
            {
                Image        = image,
                Name         = name,
                WorkingDir   = workingDir,
                Entrypoint   = entrypoint,
                Env          = env,
                Labels       = labels,
                Cmd          = cmd,
                ExposedPorts = exposedPorts,
                HostConfig   = hostConfig,
            };

            await pullImageTask;

            var id = (await Docker.Containers.CreateContainerAsync(createParameters, ct)).ID;

            TestcontainersRegistryService.Register(id, config.CleanUp);

            return(id);
        }