Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the ENV variables used by boot2docker. #210

Merged
merged 1 commit into from
Nov 14, 2014
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
25 changes: 19 additions & 6 deletions lib/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,24 @@ def default_socket_url
end

def env_url
ENV['DOCKER_URL']
ENV['DOCKER_URL'] || ENV['DOCKER_HOST']
end

def env_options
if cert_path = ENV['DOCKER_CERT_PATH']
{
client_cert: File.join(cert_path, 'cert.pem'),
client_key: File.join(cert_path, 'key.pem'),
ssl_ca_file: File.join(cert_path, 'ca.pem'),
scheme: 'https'
}
else
{}
end
end

def url
@url ||= ENV['DOCKER_URL'] || ENV['DOCKER_HOST'] || default_socket_url
@url ||= env_url || default_socket_url
# docker uses a default notation tcp:// which means tcp://localhost:2375
if @url == 'tcp://'
@url = 'tcp://localhost:2375'
Expand All @@ -48,7 +61,7 @@ def url
end

def options
@options ||= {}
@options ||= env_options
end

def url=(new_url)
Expand All @@ -57,7 +70,7 @@ def url=(new_url)
end

def options=(new_options)
@options = new_options
@options = env_options.merge(new_options || {})
reset_connection!
end

Expand Down Expand Up @@ -98,8 +111,8 @@ def validate_version!
raise Docker::Error::VersionError, "Expected API Version: #{API_VERSION}"
end

module_function :default_socket_url, :env_url, :url, :url=, :options,
:options=, :creds, :creds=, :logger, :logger=,
module_function :default_socket_url, :env_url, :url, :url=, :env_options,
:options, :options=, :creds, :creds=, :logger, :logger=,
:connection, :reset_connection!, :version, :info,
:authenticate!, :validate_version!
end
37 changes: 30 additions & 7 deletions spec/docker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
before do
ENV['DOCKER_URL'] = nil
ENV['DOCKER_HOST'] = nil
ENV['DOCKER_CERT_PATH'] = nil
end

it { should be_a Module }
Expand All @@ -17,37 +18,40 @@
end

context "when the DOCKER_* ENV variables aren't set" do
its(:options) { {} }
its(:options) { should == {} }
its(:url) { should == 'unix:///var/run/docker.sock' }
its(:connection) { should be_a Docker::Connection }
end

context "when the DOCKER_* ENV variables are set" do
before do
ENV['DOCKER_URL'] = 'unixs:///var/run/not-docker.sock'
Docker.options = nil
end

its(:options) { {} }
its(:options) { should == {} }
its(:url) { should == 'unixs:///var/run/not-docker.sock' }
its(:connection) { should be_a Docker::Connection }
end

context "when the DOCKER_HOST is set and uses default tcp://" do
before do
ENV['DOCKER_HOST'] = 'tcp://'
Docker.options = nil
end

its(:options) { {} }
its(:options) { should == {} }
its(:url) { should == 'tcp://localhost:2375' }
its(:connection) { should be_a Docker::Connection }
end

context "when the DOCKER_HOST ENV variables is set" do
context "when the DOCKER_HOST ENV variable is set" do
before do
ENV['DOCKER_HOST'] = 'tcp://someserver:8103'
Docker.options = nil
end

its(:options) { {} }
its(:options) { should == {} }
its(:url) { should == 'tcp://someserver:8103' }
its(:connection) { should be_a Docker::Connection }
end
Expand All @@ -56,13 +60,32 @@
before do
ENV['DOCKER_HOST'] = 'tcp://someserver:8103'
ENV['DOCKER_URL'] = 'tcp://someotherserver:8103'

Docker.options = nil
end

its(:options) { {} }
its(:options) { should == {} }
its(:url) { should == 'tcp://someotherserver:8103' }
its(:connection) { should be_a Docker::Connection }
end

context "when the DOCKER_CERT_PATH and DOCKER_HOST ENV variables are set" do
before do
ENV['DOCKER_HOST'] = 'tcp://someserver:8103'
ENV['DOCKER_CERT_PATH'] = '/boot2dockert/cert/path'
Docker.options = nil
end

its(:options) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should have a should == before the hash, correct? I don't think /boot2dockert/cert/path/https is a correct value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for catching that!

Yup, I assumed that the test case is taking advantage of some RSpec shorthand.

I fixed this pattern everywhere in the spec for the Docker object.

Also, I had to copy-paste Docker.options = nil to every before block that changes environment vars, because the docker's before block executes before the context-specific before blocks, so options never actually changed before.

Previous tests didn't catch this because they were all testing for the same result, namely the empty hash.

should == {
client_cert: '/boot2dockert/cert/path/cert.pem',
client_key: '/boot2dockert/cert/path/key.pem',
ssl_ca_file: '/boot2dockert/cert/path/ca.pem',
scheme: 'https'
}
}
its(:url) { should == 'tcp://someserver:8103' }
its(:connection) { should be_a Docker::Connection }
end
end

describe '#reset_connection!' do
Expand Down