Class: Testcontainers::ComposeContainer
- Inherits:
-
Object
- Object
- Testcontainers::ComposeContainer
- Defined in:
- lib/testcontainers/compose.rb
Overview
ComposeContainer class is used to manage a large number of containers in a synchronous environment
Instance Attribute Summary collapse
-
#build ⇒ Object
Default image used by the container.
-
#compose_filenames ⇒ Object
Default image used by the container.
-
#env_file ⇒ Object
Default image used by the container.
-
#filepath ⇒ Object
Default image used by the container.
-
#pull ⇒ Object
Default image used by the container.
-
#services ⇒ Object
Default image used by the container.
Instance Method Summary collapse
-
#exec(service_name:, command:) ⇒ Object
Execute a command in the given service using the ‘docker-compose exec` command.
- #exited? ⇒ Boolean
-
#initialize(command: ["docker compose"], filepath: ".", compose_filenames: ["docker-compose.yml"], pull: false, build: false, env_file: nil, services: nil) ⇒ ComposeContainer
constructor
Initializes a new instance of ComposeContainer.
-
#logs ⇒ String
Return the logs of the containers using the ‘docker-compose logs` command.
- #running? ⇒ Boolean
-
#service_host(service: nil, port: 0) ⇒ Object
Return the host for a given service and port using the ‘docker-compose port` command.
-
#service_port(service: nil, port: 0) ⇒ Object
Return the mapped port for a given service and port using the ‘docker-compose port` command.
-
#start ⇒ ComposeContainer
Run the containers using the ‘docker-compose up` command.
-
#stop ⇒ ComposeContainer
Stop the containers using the ‘docker-compose down` command.
-
#wait_for_http(url:, timeout: 60, interval: 0.1, status: 200) ⇒ Boolean
Waits for the container to respond to HTTP requests.
-
#wait_for_logs(matcher:, timeout: 60, interval: 0.1) ⇒ Boolean
Waits for the container logs to match the given regex.
-
#wait_for_tcp_port(host:, port:, timeout: 60, interval: 0.1) ⇒ Boolean
Waits for the container to open the given port.
Constructor Details
#initialize(command: ["docker compose"], filepath: ".", compose_filenames: ["docker-compose.yml"], pull: false, build: false, env_file: nil, services: nil) ⇒ ComposeContainer
Initializes a new instance of ComposeContainer
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/testcontainers/compose.rb', line 27 def initialize(command: ["docker compose"], filepath: ".", compose_filenames: ["docker-compose.yml"], pull: false, build: false, env_file: nil, services: nil) @command = command @filepath = filepath @compose_filenames = compose_filenames @pull = pull @build = build @services = services @env_file = env_file @_container_started = false end |
Instance Attribute Details
#build ⇒ Object
Default image used by the container
16 17 18 |
# File 'lib/testcontainers/compose.rb', line 16 def build @build end |
#compose_filenames ⇒ Object
Default image used by the container
16 17 18 |
# File 'lib/testcontainers/compose.rb', line 16 def compose_filenames @compose_filenames end |
#env_file ⇒ Object
Default image used by the container
16 17 18 |
# File 'lib/testcontainers/compose.rb', line 16 def env_file @env_file end |
#filepath ⇒ Object
Default image used by the container
16 17 18 |
# File 'lib/testcontainers/compose.rb', line 16 def filepath @filepath end |
#pull ⇒ Object
Default image used by the container
16 17 18 |
# File 'lib/testcontainers/compose.rb', line 16 def pull @pull end |
#services ⇒ Object
Default image used by the container
16 17 18 |
# File 'lib/testcontainers/compose.rb', line 16 def services @services end |
Instance Method Details
#exec(service_name:, command:) ⇒ Object
Execute a command in the given service using the ‘docker-compose exec` command
97 98 99 100 101 102 |
# File 'lib/testcontainers/compose.rb', line 97 def exec(service_name:, command:) raise ContainerNotStartedError unless @_container_started exec_cmd = compose_command(["exec", "-T", service_name, command]) exec_command(exec_cmd) end |
#exited? ⇒ Boolean
78 79 80 |
# File 'lib/testcontainers/compose.rb', line 78 def exited? !running? end |
#logs ⇒ String
Return the logs of the containers using the ‘docker-compose logs` command
85 86 87 88 89 90 91 |
# File 'lib/testcontainers/compose.rb', line 85 def logs raise ContainerNotStartedError unless @_container_started logs_command = compose_command("logs") stdout, _, _ = exec_command(logs_command) stdout end |
#running? ⇒ Boolean
74 75 76 |
# File 'lib/testcontainers/compose.rb', line 74 def running? @_container_started end |
#service_host(service: nil, port: 0) ⇒ Object
Return the host for a given service and port using the ‘docker-compose port` command
118 119 120 121 122 |
# File 'lib/testcontainers/compose.rb', line 118 def service_host(service: nil, port: 0) raise ContainerNotStartedError unless @_container_started _service_info(service: service, port: port)["host"] end |
#service_port(service: nil, port: 0) ⇒ Object
Return the mapped port for a given service and port using the ‘docker-compose port` command
108 109 110 111 112 |
# File 'lib/testcontainers/compose.rb', line 108 def service_port(service: nil, port: 0) raise ContainerNotStartedError unless @_container_started _service_info(service: service, port: port)["port"] end |
#start ⇒ ComposeContainer
Run the containers using the ‘docker-compose up` command
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/testcontainers/compose.rb', line 43 def start return self if @_container_started if @pull pull_cmd = compose_command("pull") exec_command(pull_cmd) end args = ["up", "-d"] args << "--build" if @build args << @services.join(" ") if @services up_cmd = compose_command(args) _, _, status = exec_command(up_cmd) @_container_started = true if status.success? self end |
#stop ⇒ ComposeContainer
Stop the containers using the ‘docker-compose down` command
64 65 66 67 68 69 70 71 72 |
# File 'lib/testcontainers/compose.rb', line 64 def stop raise ContainerNotStartedError unless @_container_started down_cmd = compose_command("down -v") _, _, status = exec_command(down_cmd) @_container_started = false if status.success? self end |
#wait_for_http(url:, timeout: 60, interval: 0.1, status: 200) ⇒ Boolean
Waits for the container to respond to HTTP requests.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/testcontainers/compose.rb', line 187 def wait_for_http(url:, timeout: 60, interval: 0.1, status: 200) raise ContainerNotStartedError unless @_container_started Timeout.timeout(timeout) do loop do begin response = Excon.get(url) return true if response.status == status rescue Excon::Error::Socket # The container may not be ready to accept connections yet end sleep interval end end rescue Timeout::Error raise TimeoutError, "Timed out waiting for HTTP status #{status} on #{path}" end |
#wait_for_logs(matcher:, timeout: 60, interval: 0.1) ⇒ Boolean
Waits for the container logs to match the given regex.
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/testcontainers/compose.rb', line 133 def wait_for_logs(matcher:, timeout: 60, interval: 0.1) raise ContainerNotStartedError unless @_container_started Timeout.timeout(timeout) do loop do return true if logs&.match?(matcher) sleep interval end end rescue Timeout::Error raise TimeoutError, "Timed out waiting for logs to match #{matcher}" end |
#wait_for_tcp_port(host:, port:, timeout: 60, interval: 0.1) ⇒ Boolean
Waits for the container to open the given port.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/testcontainers/compose.rb', line 157 def wait_for_tcp_port(host:, port:, timeout: 60, interval: 0.1) raise ContainerNotStartedError unless @_container_started Timeout.timeout(timeout) do loop do Timeout.timeout(interval) do TCPSocket.new(host, port).close return true end rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Timeout::Error sleep interval end end rescue Timeout::Error raise TimeoutError, "Timed out waiting for port #{port} to open" end |