Skip to content

Commit

Permalink
Cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tlunter committed Dec 7, 2015
1 parent 29e5a11 commit 64d8f10
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 919 deletions.
16 changes: 13 additions & 3 deletions lib/docker/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def copy(path, &block)
self
end

def archive_out(path, overwrite = false, &block)
def archive_out(path, &block)
connection.get(
path_for(:archive),
{ 'path' => path },
Expand All @@ -247,10 +247,20 @@ def archive_out(path, overwrite = false, &block)
self
end

def archive_in(path, overwrite = false, &block)
def archive_in(inputs, output_path, opts = {})
file_hash = Docker::Util.file_hash_from_paths([*inputs])
tar = StringIO.new(Docker::Util.create_tar(file_hash))
archive_in_stream(output_path, opts) do
tar.read(Excon.defaults[:chunk_size]).to_s
end
end

def archive_in_stream(output_path, opts = {}, &block)
overwrite = opts[:overwrite] || opts['overwrite'] || false

connection.put(
path_for(:archive),
{ 'path' => path, 'overwrite' => overwrite },
{ 'path' => output_path, 'noOverwriteDirNonDir' => !overwrite },
:headers => {
'Content-Type' => 'application/x-tar'
},
Expand Down
59 changes: 51 additions & 8 deletions spec/docker/container_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,36 +212,79 @@
end
end

describe '#archive_in' do
describe '#archive_in', :docker_1_8 do
let(:license_path) { File.absolute_path(File.join(__FILE__, '..', '..', '..', 'LICENSE')) }
subject { Docker::Container.create('Image' => 'debian:wheezy', 'Cmd' => ['/bin/sh']) }
let(:committed_image) { subject.commit }
let(:ls_container) { committed_image.run('ls /').tap(&:wait) }
let(:output) { ls_container.streaming_logs(stdout: true, stderr: true) }

after do
subject.remove
end

context 'when the input is a tar' do
after do
ls_container.remove
committed_image.remove
end

it 'file exists in the container', :vcr do
subject.archive_in(license_path, '/', overwrite: false)
expect(output).to include('LICENSE')
end
end
end

describe '#archive_in_stream', :docker_1_8 do
let(:tar) { StringIO.new(Docker::Util.create_tar('/lol' => 'TEST')) }
subject { Docker::Container.create('Image' => 'debian:wheezy', 'Cmd' => ['/bin/sh']) }
let(:committed_image) { subject.commit }
let(:ls_container) { committed_image.run('ls /').tap(&:wait) }
let(:output) { ls_container.streaming_logs(stdout: true, stderr: true) }

after(:each) { subject.remove }
after do
subject.remove
end

context 'when the input is a tar' do
after { ls_container.remove; committed_image.remove }
after do
ls_container.remove
committed_image.remove
end

it 'file exists in the container', :vcr do
subject.archive_in('/', false) { tar.read }
subject.archive_in_stream('/', overwrite: false) { tar.read }
expect(output).to include('lol')
end
end

context 'when the input would overwrite a directory with a file' do
let(:tar) { StringIO.new(Docker::Util.create_tar('/etc' => 'TEST')) }

it 'raises an error' do
# Docs say this should return a client error: clearly wrong
# https://docs.docker.com/engine/reference/api/docker_remote_api_v1.21/
# #extract-an-archive-of-files-or-folders-to-a-directory-in-a-container
expect {
subject.archive_in_stream('/', overwrite: false) { tar.read }
}.to raise_error(Docker::Error::ServerError)
end
end
end

describe '#archive_out' do
describe '#archive_out', :docker_1_8 do
subject { Docker::Container.create('Image' => 'debian:wheezy', 'Cmd' => ['touch','/test']) }

after(:each) { subject.remove }
after { subject.remove }

context 'when the file does not exist' do
it 'raises an error', :vcr do
subject.start; subject.wait
subject.start
subject.wait

expect { subject.archive_out('/lol') { |chunk| puts chunk } }
.to raise_error(Docker::Error::NotFoundError)
.to raise_error(Docker::Error::NotFoundError)
end
end

Expand Down
17 changes: 17 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,21 @@ def project_dir
config.color = true
config.formatter = :documentation
config.tty = true

case ENV['DOCKER_VERSION']
when /1\.5/
config.filter_run_including :docker_1_8 => false
when /1\.6/
config.filter_run_including :docker_1_8 => false
when /1\.7/
config.filter_run_including :docker_1_8 => false
when /1\.8/
config.filter_run_including :docker_1_8 => true
when /1\.9/
config.filter_run_including :docker_1_8 => true
config.filter_run_including :docker_1_9 => true
else
config.filter_run_including :docker_1_8 => true
config.filter_run_including :docker_1_9 => true
end
end
Loading

0 comments on commit 64d8f10

Please sign in to comment.