Skip to content
Closed
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
19 changes: 16 additions & 3 deletions lib/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ def options=(new_options)
reset_connection!
end

def api_version
options.fetch(:api_version, SUPPORTED_API_VERSION)
end

def api_version=(version)
options[:api_version] = version.to_s
end

def connection
@connection ||= Connection.new(url, options)
end
Expand Down Expand Up @@ -132,14 +140,19 @@ def authenticate!(options = {}, connection = self.connection)
# When the correct version of Docker is installed, returns true. Otherwise,
# raises a VersionError.
def validate_version!
!api_version.empty? &&
Gem::Version.new(api_version) < Gem::Version.new(SUPPORTED_API_VERSION) &&
raise(Docker::Error::VersionError)

Docker.info
true
rescue Docker::Error::DockerError
raise Docker::Error::VersionError, "Expected API Version: #{API_VERSION}"
rescue Docker::Error::DockerError, Docker::Error::VersionError
raise Docker::Error::VersionError, "Expected API Version: #{SUPPORTED_API_VERSION}"
end

module_function :default_socket_url, :env_url, :url, :url=, :env_options,
:options, :options=, :creds, :creds=, :logger, :logger=,
:connection, :reset!, :reset_connection!, :version, :info,
:ping, :authenticate!, :validate_version!, :ssl_options
:ping, :authenticate!, :validate_version!, :ssl_options,
:api_version, :api_version=
end
3 changes: 2 additions & 1 deletion lib/docker/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ def compile_request_params(http_method, path, query = nil, opts = nil, &block)
query ||= {}
opts ||= {}
headers = opts.delete(:headers) || {}
remote_api_version = Docker.api_version.empty? ? '' : "/v#{Docker.api_version}"
content_type = opts[:body].nil? ? 'text/plain' : 'application/json'
user_agent = "Swipely/Docker-API #{Docker::VERSION}"
{
:method => http_method,
:path => "/v#{Docker::API_VERSION}#{path}",
:path => File.join(remote_api_version, path),
:query => query,
:headers => { 'Content-Type' => content_type,
'User-Agent' => user_agent,
Expand Down
2 changes: 1 addition & 1 deletion lib/docker/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ module Docker
VERSION = '1.33.1'

# The version of the compatible Docker remote API.
API_VERSION = '1.16'
SUPPORTED_API_VERSION = '1.16'
end
14 changes: 13 additions & 1 deletion spec/docker/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@
let(:body) { rand(10000000) }
let(:resource) { double(:resource) }
let(:response) { double(:response, :body => body) }
let(:path_with_version) { "/v#{Docker::SUPPORTED_API_VERSION}#{path}" }
let(:expected_hash) {
{
:method => method,
:path => "/v#{Docker::API_VERSION}#{path}",
:path => path_with_version,
:query => query,
:headers => { 'Content-Type' => 'text/plain',
'User-Agent' => "Swipely/Docker-API #{Docker::VERSION}",
Expand All @@ -99,6 +100,17 @@
it 'sends #request to #resource with the compiled params' do
expect(subject.request(method, path, query, options)).to eq body
end

context 'when api version is specified' do
before { Docker.api_version = '1.0.0' }
after { Docker.reset! }

let(:path_with_version) { "/v1.0.0#{path}" }

it 'sets api version it the :path option' do
expect(subject.request(method, path, query, options)).to eq body
end
end
end

[:get, :put, :post, :delete].each do |method|
Expand Down
14 changes: 12 additions & 2 deletions spec/docker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
describe '#ping' do
before { Docker.reset! }

let(:ping) { subject.ping}
let(:ping) { subject.ping }

it 'returns the status as a String' do
expect(ping).to eq('OK')
Expand Down Expand Up @@ -243,14 +243,24 @@
describe '#validate_version' do
before { Docker.reset! }

context 'when specified version is not supported by the client' do
before { Docker.api_version = '0.0.1' }
after { Docker.reset! }

it 'raises a Version Error' do
expect { subject.validate_version! }
.to raise_error(Docker::Error::VersionError)
end
end

context 'when a Docker Error is raised' do
before do
allow(Docker).to receive(:info).and_raise(Docker::Error::ClientError)
end

it 'raises a Version Error' do
expect { subject.validate_version! }
.to raise_error(Docker::Error::VersionError)
.to raise_error(Docker::Error::VersionError)
end
end

Expand Down