Пример #1
0
        public static async Task <ContainerListResponse> StartPgAdmin(DockerClient client)
        {
            const string ContainerName = "pgadmin-integration-tests";
            const string ImageName     = "dpage/pgadmin4";
            const string ImageTag      = "4.24";

            await DockerExtentions.EnsureImageExistsAndCleanupAsync(client, ImageName, ImageTag, ContainerName);

            var config = new Config();

            var hostConfig = new HostConfig
            {
                PortBindings = new Dictionary <string, IList <PortBinding> >
                {
                    { "80/tcp", new List <PortBinding> {
                          new PortBinding {
                              HostIP = "", HostPort = "5081"
                          }
                      } }
                },
                Binds = new List <string>
                {
                    "/tmp/var/lib/pgadmin:/var/lib/pgadmin",
                    "/tmp/servers.json:/servers.json"
                }
            };

            var response = await client.Containers.CreateContainerAsync(new CreateContainerParameters(config)
            {
                Image      = ImageName + ":" + ImageTag,
                Name       = ContainerName,
                Tty        = false,
                HostConfig = hostConfig,
                Env        = new List <string>
                {
                    "[email protected]",
                    "PGADMIN_DEFAULT_PASSWORD=@rr@gr0",
                }
            });

            var container = await DockerExtentions.GetContainerAsync(client, ContainerName);

            if (container == null)
            {
                throw new Exception("No PgAdmin container.");
            }

            if (container.State != "running")
            {
                var started = await client.Containers.StartContainerAsync(container.ID, new ContainerStartParameters());

                if (!started)
                {
                    throw new Exception("Cannot start the PgAdmin docker container.");
                }
            }

            return(container);
        }
Пример #2
0
        public static async Task <ContainerListResponse> StartMailDev(DockerClient client)
        {
            const string ContainerName = "maildev-integration-tests";
            const string ImageName     = "djfarrelly/maildev";
            const string ImageTag      = "latest";

            await DockerExtentions.EnsureImageExistsAndCleanupAsync(client, ImageName, ImageTag, ContainerName);

            var config = new Config();

            var hostConfig = new HostConfig
            {
                PortBindings = new Dictionary <string, IList <PortBinding> >
                {
                    { "80/tcp", new List <PortBinding> {
                          new PortBinding {
                              HostIP = "", HostPort = "5080"
                          }
                      } },
                    { "25/tcp", new List <PortBinding> {
                          new PortBinding {
                              HostIP = "", HostPort = "25"
                          }
                      } }
                }
            };

            var response = await client.Containers.CreateContainerAsync(new CreateContainerParameters(config)
            {
                Image      = ImageName + ":" + ImageTag,
                Name       = ContainerName,
                Tty        = false,
                HostConfig = hostConfig
            });

            var container = await DockerExtentions.GetContainerAsync(client, ContainerName);

            if (container == null)
            {
                throw new Exception("No MailDev container.");
            }

            if (container.State != "running")
            {
                var started = await client.Containers.StartContainerAsync(container.ID, new ContainerStartParameters());

                if (!started)
                {
                    throw new Exception("Cannot start the MailDev docker container.");
                }
                Thread.Sleep(100);
            }

            return(container);
        }
Пример #3
0
        public static async Task <ContainerListResponse> StartRedisCommander(DockerClient client)
        {
            const string ContainerName = "redis-commander-integration-tests";
            const string ImageName     = "rediscommander/redis-commander";
            const string ImageTag      = "latest";

            await DockerExtentions.EnsureImageExistsAndCleanupAsync(client, ImageName, ImageTag, ContainerName);

            var config = new Config();

            var hostConfig = new HostConfig
            {
                PortBindings = new Dictionary <string, IList <PortBinding> >
                {
                    { "8081/tcp", new List <PortBinding> {
                          new PortBinding {
                              HostIP = "", HostPort = "5082"
                          }
                      } }
                }
            };

            var response = await client.Containers.CreateContainerAsync(new CreateContainerParameters(config)
            {
                Image      = ImageName + ":" + ImageTag,
                Name       = ContainerName,
                Tty        = false,
                HostConfig = hostConfig,
                Env        = new List <string>
                {
                    "REDIS_HOSTS=local:localhost:6379"
                }
            });

            var container = await DockerExtentions.GetContainerAsync(client, ContainerName);

            if (container == null)
            {
                throw new Exception("No Redis Commander container.");
            }

            if (container.State != "running")
            {
                var started = await client.Containers.StartContainerAsync(container.ID, new ContainerStartParameters());

                if (!started)
                {
                    throw new Exception("Cannot start the Redis Commander docker container.");
                }
            }

            return(container);
        }
Пример #4
0
        public static async Task EnsureImageExistsAndCleanupAsync(DockerClient client, string imageName, string imageTag, string containerName)
        {
            await DockerExtentions.EnsureImageExistsAsync(client, imageName, imageTag);

            var container = await DockerExtentions.GetContainerAsync(client, containerName);

            if (container != null)
            {
                // await client.Containers.StopContainerAsync(container.ID, new ContainerStopParameters());
                await client.Containers.RemoveContainerAsync(container.ID, new ContainerRemoveParameters { Force = true });
            }
        }
Пример #5
0
        public static async Task <ContainerListResponse> StartLocalStripe(DockerClient client)
        {
            const string ContainerName = "localstripe";
            const string ImageName     = "mikejewell/localstripe";
            const string ImageTag      = "v0.0.6-alpha";

            await DockerExtentions.EnsureImageExistsAndCleanupAsync(client, ImageName, ImageTag, ContainerName);

            var config = new Config();

            var hostConfig = new HostConfig
            {
                PublishAllPorts = true,
                PortBindings    = new Dictionary <string, IList <PortBinding> >
                {
                    { "8420/tcp", new List <PortBinding> {
                          new PortBinding {
                              HostIP = "", HostPort = "8420"
                          }
                      } }
                }
            };

            await client.Containers.CreateContainerAsync(new CreateContainerParameters(config)
            {
                Image        = ImageName + ":" + ImageTag,
                Name         = ContainerName,
                ExposedPorts = new Dictionary <string, object>()
                {
                    { "8420/tcp", new { HostPort = 8420.ToString() } }
                } as IDictionary <string, EmptyStruct>,
                HostConfig = hostConfig
            });

            var container = await DockerExtentions.GetContainerAsync(client, ContainerName);

            if (container == null)
            {
                throw new Exception("No localstripe container.");
            }

            if (container.State != "running")
            {
                var started = await client.Containers.StartContainerAsync(container.ID, new ContainerStartParameters());

                if (!started)
                {
                    throw new Exception("Cannot start the localstripe docker container.");
                }
            }

            return(container);
        }
Пример #6
0
        public static async Task <ContainerListResponse> StartMinio(DockerClient client)
        {
            const string ContainerName = "minio";
            const string ImageName     = "minio/minio";
            const string ImageTag      = "RELEASE.2020-08-08T04-50-06Z";

            await DockerExtentions.EnsureImageExistsAndCleanupAsync(client, ImageName, ImageTag, ContainerName);

            var config = new Config();

            var hostConfig = new HostConfig
            {
                PublishAllPorts = true,
                PortBindings    = new Dictionary <string, IList <PortBinding> >
                {
                    { "9000/tcp", new List <PortBinding> {
                          new PortBinding {
                              HostIP = "", HostPort = "9000"
                          }
                      } }
                }
            };

            await client.Containers.CreateContainerAsync(new CreateContainerParameters(config)
            {
                Image        = ImageName + ":" + ImageTag,
                Name         = ContainerName,
                ExposedPorts = new Dictionary <string, object>()
                {
                    { "9000/tcp", new { HostPort = 9000.ToString() } }
                } as IDictionary <string, EmptyStruct>,
                HostConfig = hostConfig,
                Env        = new List <string>
                {
                    "MINIO_REGION=us-east-1",
                    "MINIO_ACCESS_KEY=minio_access_key",
                    "MINIO_SECRET_KEY=minio_secret_key"
                },
                Cmd = new List <string>
                {
                    "server",
                    "/data"
                }
            });

            var container = await DockerExtentions.GetContainerAsync(client, ContainerName);

            if (container == null)
            {
                throw new Exception("No minio container.");
            }

            if (container.State != "running")
            {
                var started = await client.Containers.StartContainerAsync(container.ID, new ContainerStartParameters());

                if (!started)
                {
                    throw new Exception("Cannot start the minio docker container.");
                }
            }

            return(container);
        }
Пример #7
0
        public static async Task <ContainerListResponse> StartAzurite(DockerClient client)
        {
            const string ContainerName = "azurite";
            const string ImageName     = "touchify/azurite";
            const string ImageTag      = "2.7.1";

            await DockerExtentions.EnsureImageExistsAndCleanupAsync(client, ImageName, ImageTag, ContainerName);

            var config = new Config();

            var hostConfig = new HostConfig
            {
                PublishAllPorts = true,
                PortBindings    = new Dictionary <string, IList <PortBinding> >
                {
                    { "10000/tcp", new List <PortBinding> {
                          new PortBinding {
                              HostIP = "", HostPort = "10000"
                          }
                      } },
                    { "10001/tcp", new List <PortBinding> {
                          new PortBinding {
                              HostIP = "", HostPort = "10001"
                          }
                      } },
                    { "10002/tcp", new List <PortBinding> {
                          new PortBinding {
                              HostIP = "", HostPort = "10002"
                          }
                      } }
                }
            };

            await client.Containers.CreateContainerAsync(new CreateContainerParameters(config)
            {
                Image        = ImageName + ":" + ImageTag,
                Name         = ContainerName,
                ExposedPorts = new Dictionary <string, object>()
                {
                    { "10000/tcp", new { HostPort = 10000.ToString() } },
                    { "10001/tcp", new { HostPort = 10001.ToString() } },
                    { "10002/tcp", new { HostPort = 10002.ToString() } }
                } as IDictionary <string, EmptyStruct>,
                HostConfig = hostConfig
            });

            var container = await DockerExtentions.GetContainerAsync(client, ContainerName);

            if (container == null)
            {
                throw new Exception("No azurite container.");
            }

            if (container.State != "running")
            {
                var started = await client.Containers.StartContainerAsync(container.ID, new ContainerStartParameters());

                if (!started)
                {
                    throw new Exception("Cannot start the azurite docker container.");
                }
            }

            return(container);
        }
Пример #8
0
        public static async Task <ContainerListResponse> StartPostgres(DockerClient client)
        {
            const string ContainerName = "postgres-integration-tests";
            const string ImageName     = "postgres";
            const string ImageTag      = "11.8-alpine";

            await DockerExtentions.EnsureImageExistsAndCleanupAsync(client, ImageName, ImageTag, ContainerName);

            var config = new Config();

            var hostConfig = new HostConfig
            {
                PortBindings = new Dictionary <string, IList <PortBinding> >
                {
                    { "5432/tcp", new List <PortBinding> {
                          new PortBinding {
                              HostIP = "", HostPort = "5432"
                          }
                      } }
                }
            };

            await client.Containers.CreateContainerAsync(new CreateContainerParameters(config)
            {
                Image      = ImageName + ":" + ImageTag,
                Name       = ContainerName,
                Tty        = false,
                HostConfig = hostConfig,
                Env        = new List <string>
                {
                    "POSTGRES_PASSWORD=password1"
                },
                Cmd = new List <string>
                {
                    "--max_prepared_transactions=100"
                }
            });

            var container = await DockerExtentions.GetContainerAsync(client, ContainerName);

            if (container == null)
            {
                throw new Exception("No Postgres container.");
            }

            if (container.State != "running")
            {
                var started = await client.Containers.StartContainerAsync(container.ID, new ContainerStartParameters());

                if (!started)
                {
                    throw new Exception("Cannot start the postgres docker container.");
                }
            }

            var isContainerReady = false;
            var isReadyCounter   = 0;

            while (!isContainerReady)
            {
                isReadyCounter++;
                var result = await client.Exec.RunCommandInContainerAsync(container.ID, "pg_isready -U postgres");

                if (result.stdout.TrimEnd('\n') == "/var/run/postgresql:5432 - accepting connections")
                {
                    isContainerReady = true;
                }

                if (isReadyCounter == 20)
                {
                    throw new Exception("Postgres container never ready.");
                }

                if (!isContainerReady)
                {
                    Thread.Sleep(1000);
                }
            }

            return(container);
        }
Пример #9
0
        public static async Task <ContainerListResponse> StartSqlServer(DockerClient client)
        {
            const string ContainerName = "sqlserver-integration-tests";
            const string ImageName     = "mcr.microsoft.com/mssql/server";
            const string ImageTag      = "2019-latest";

            await DockerExtentions.EnsureImageExistsAndCleanupAsync(client, ImageName, ImageTag, ContainerName);

            var config = new Config();

            var hostConfig = new HostConfig
            {
                PortBindings =
                    new Dictionary <string, IList <PortBinding> >
                {
                    { "1433/tcp", new List <PortBinding> {
                          new PortBinding {
                              HostIP = "127.0.0.1", HostPort = "1435"
                          }
                      } }
                }
            };

            await client.Containers.CreateContainerAsync(new CreateContainerParameters(config)
            {
                Image      = ImageName + ":" + ImageTag,
                Name       = ContainerName,
                Tty        = false,
                HostConfig = hostConfig,
                Env        = new List <string>
                {
                    "ACCEPT_EULA=Y",
                    "SA_PASSWORD=P@ssword123",
                }
            });

            var container = await DockerExtentions.GetContainerAsync(client, ContainerName);

            if (container == null)
            {
                throw new Exception("No SqlServer container.");
            }

            if (container.State != "running")
            {
                var started = await client.Containers.StartContainerAsync(container.ID, new ContainerStartParameters());

                if (!started)
                {
                    throw new Exception("Cannot start the SqlServer docker container.");
                }
            }

            var isContainerReady = false;
            var isReadyCounter   = 0;

            while (!isContainerReady)
            {
                isReadyCounter++;

                try
                {
                    var commandTokens = "/opt/mssql-tools/bin/sqlcmd -U sa -P P@ssword123 -Q".Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    commandTokens.Add("SELECT TOP 1 name FROM master.sys.databases");

                    var createdExec = await client.Exec.ExecCreateContainerAsync(container.ID, new ContainerExecCreateParameters
                    {
                        AttachStderr = true,
                        AttachStdout = true,
                        Cmd          = commandTokens
                    });

                    var multiplexedStream = await client.Exec.StartAndAttachContainerExecAsync(createdExec.ID, false);

                    var result = await multiplexedStream.ReadOutputToEndAsync(CancellationToken.None);

                    if (string.IsNullOrEmpty(result.stderr))
                    {
                        isContainerReady = true;
                    }
                }
                catch (Exception ex)
                {
                    var x = ex;
                }

                if (isReadyCounter == 20)
                {
                    throw new Exception("SqlServer container never ready.");
                }

                if (!isContainerReady)
                {
                    Thread.Sleep(1000);
                }
            }

            return(container);
        }