Skip to content
This repository was archived by the owner on May 9, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions lib/hoosegow/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ def run_container(image, data, &block)
# Returns nothing.
def create_container(image)
@container = ::Docker::Container.create @container_options.merge(
:StdinOnce => true,
:OpenStdin => true,
:Volumes => volumes_for_create,
:StdinOnce => true,
:OpenStdin => true,
:HostConfig => {
:Binds => volumes_for_bind
},
:Image => image
)
callback @after_create
Expand All @@ -92,7 +94,7 @@ def create_container(image)
#
# Returns nothing.
def start_container
@container.start :Binds => volumes_for_bind
@container.start
callback @after_start
end

Expand Down Expand Up @@ -200,19 +202,7 @@ def docker_url(options)
end
end

# Private: Generate the `Volumes` argument for creating a container.
#
# Given a hash of container_path => local_path in @volumes, generate a
# hash of container_path => {}.
def volumes_for_create
result = {}
each_volume do |container_path, local_path, permissions|
result[container_path] = {}
end
result
end

# Private: Generate the `Binds` argument for starting a container.
# Private: Generate the `Binds` argument for creating a container.
#
# Given a hash of container_path => local_path in @volumes, generate an
# array of "local_path:container_path:rw".
Expand Down
33 changes: 27 additions & 6 deletions spec/hoosegow_docker_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'fileutils'
require_relative '../lib/hoosegow'

unless defined?(CONFIG)
Expand All @@ -18,13 +19,11 @@

context 'unspecified' do
subject { described_class.new }
its(:volumes_for_create) { should be_empty }
its(:volumes_for_bind) { should be_empty }
end

context 'empty' do
let(:volumes) { {} }
its(:volumes_for_create) { should be_empty }
its(:volumes_for_bind) { should be_empty }
end

Expand All @@ -33,17 +32,39 @@
"/inside/path" => "/home/burke/data-for-container:rw",
"/other/path" => "/etc/shared-config",
} }
its(:volumes_for_create) { should == {
"/inside/path" => {},
"/other/path" => {},
} }
its(:volumes_for_bind) { should == [
"/home/burke/data-for-container:/inside/path:rw",
"/etc/shared-config:/other/path:ro",
] }
end
end

context "volume is writable" do
it "calls after_create" do
test_dir = File.join(Dir.pwd, 'volume-test')
FileUtils.remove_dir(test_dir, true)
FileUtils.mkpath(test_dir)

config = CONFIG.merge(
:Entrypoint => ['/usr/bin/touch', '/volume-test/test'],
:volumes => { '/volume-test' => test_dir + ":rw"}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Barely remembering what is going on here... funny that this is :volumes instead of :Volumes – is that intentional?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. The api is such that lower case keys are processed by hoosegow and the rest are passed through to the docker api.

)
docker = Hoosegow::Docker.new(config)
begin
docker.create_container CONFIG[:image_name]
docker.start_container
ensure
docker.stop_container
docker.delete_container
end

exists = File.exists?(File.join(test_dir, 'test'))
expect(exists).to be_truthy
FileUtils.remove_dir(test_dir, true)
end
end


context 'docker_url' do
it "correctly generates TCP urls" do
hoosegow = Hoosegow::Docker.new CONFIG.merge(:host => "1.1.1.1", :port => 1234)
Expand Down