Skip to content

Instantly share code, notes, and snippets.

@iaintshine
Created October 30, 2017 13:26
Show Gist options
  • Save iaintshine/81093dd39d64355a1eea9ce27f1f2a27 to your computer and use it in GitHub Desktop.
Save iaintshine/81093dd39d64355a1eea9ce27f1f2a27 to your computer and use it in GitHub Desktop.
Use docker-compose in Ruby tests
module Docker::Compose
class Session
def wait(timeout = 10)
Timeout.timeout(timeout) do
loop do
containers = ps
return if containers
.map { |container| running?(container.id) }
.all?
end
end
end
def running?(container_id)
cmd = @shell.run('docker', 'inspect', container_id, format: "{{.State.Running}}").join
status, out, err = cmd.status, cmd.captured_output, cmd.captured_error
unless status.success?
raise Error.new("docker inspect #{container_id} -f {{.State.Running}}", status, out+err)
end
lines = out.split(/[\r\n]+/)
return false if lines.empty?
lines.first == "true"
end
end
end
gem.add_development_dependency('docker-compose', '~> 1.1')
compose = Docker::Compose.new
compose.up(detached: true)
compose.wait
at_exit { compose.down }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment