From 4bbf27282daffc238a2f5cbcedda66d5f63c3754 Mon Sep 17 00:00:00 2001 From: Todd Lunter Date: Wed, 8 Apr 2015 11:08:40 -0400 Subject: [PATCH] Verify cache support works --- lib/docker/util.rb | 29 +++++- spec/docker/image_spec.rb | 32 ++++++- spec/support/vcr.rb | 2 +- ...alls_the_block_and_passes_build_output.yml | 96 +++++++++++++++++++ 4 files changed, 151 insertions(+), 8 deletions(-) create mode 100644 spec/vcr/Docker_Image/_build_from_dir/with_a_valid_Dockerfile/with_a_block_capturing_build_output/uses_a_cached_version_the_second_time/calls_the_block_and_passes_build_output.yml diff --git a/lib/docker/util.rb b/lib/docker/util.rb index 73a2f40c8..c8d763494 100644 --- a/lib/docker/util.rb +++ b/lib/docker/util.rb @@ -138,8 +138,8 @@ def create_relative_dir_tar(directory, output) next unless stat.file? unprefixed_file_name = prefixed_file_name[directory.length..-1] - tar.add_file_simple( - unprefixed_file_name, stat.mode, stat.size + add_file_to_tar( + tar, unprefixed_file_name, stat.mode, stat.size, stat.mtime ) do |tar_file| IO.copy_stream(File.open(prefixed_file_name, 'rb'), tar_file) end @@ -147,6 +147,31 @@ def create_relative_dir_tar(directory, output) end end + def add_file_to_tar(tar, name, mode, size, mtime) + tar.check_closed + + io = tar.instance_variable_get(:@io) + + name, prefix = tar.split_name(name) + + header = Gem::Package::TarHeader.new(:name => name, :mode => mode, + :size => size, :prefix => prefix, + :mtime => mtime).to_s + + io.write header + os = Gem::Package::TarWriter::BoundedStream.new io, size + + yield os if block_given? + + min_padding = size - os.written + io.write("\0" * min_padding) + + remainder = (512 - (size % 512)) % 512 + io.write("\0" * remainder) + + tar + end + def create_temp_file tempfile_name = Dir::Tmpname.create('out') {} File.open(tempfile_name, 'wb+') diff --git a/spec/docker/image_spec.rb b/spec/docker/image_spec.rb index 02985ad75..ce5ce427b 100644 --- a/spec/docker/image_spec.rb +++ b/spec/docker/image_spec.rb @@ -564,30 +564,37 @@ Docker::Container.create('Image' => image.id, 'Cmd' => %w[cat /Dockerfile]) end - let!(:output) { - container.tap(&:start).streaming_logs(stdout: true) - } + let(:output) { container.streaming_logs(stdout: true) } + after(:each) do - container.tap(&:wait).remove image.remove(:noprune => true) end context 'with no query parameters' do it 'builds the image', :vcr do - container.wait + container.tap(&:wait).start expect(output).to eq(docker_file.read) end + + after do + container.remove + end end context 'with specifying a repo in the query parameters' do let(:opts) { { "t" => "#{ENV['DOCKER_API_USER']}/debian:from_dir" } } it 'builds the image and tags it', :vcr do + container.tap(&:wait).start expect(output).to eq(docker_file.read) image.refresh! expect(image.info["RepoTags"]).to eq( ["#{ENV['DOCKER_API_USER']}/debian:from_dir"] ) end + + after do + container.remove + end end context 'with a block capturing build output' do @@ -598,6 +605,21 @@ image # Create the image variable, which is lazy-loaded by Rspec expect(build_output).to match(/Step 0 : FROM debian:wheezy/) end + + context 'uses a cached version the second time' do + let(:build_output_two) { "" } + let(:block_two) { Proc.new { |chunk| build_output_two << chunk } } + let(:image_two) { subject.build_from_dir(dir, opts, &block_two) } + + it 'calls the block and passes build output', :vcr do + image # Create the image variable, which is lazy-loaded by Rspec + expect(build_output).to match(/Step 0 : FROM debian:wheezy/) + expect(build_output).to_not match(/Using cache/) + + image_two # Create the image_two variable, which is lazy-loaded by Rspec + expect(build_output_two).to match(/Using cache/) + end + end end context 'with credentials passed' do diff --git a/spec/support/vcr.rb b/spec/support/vcr.rb index 00ac3c5f3..07cb88345 100644 --- a/spec/support/vcr.rb +++ b/spec/support/vcr.rb @@ -4,7 +4,7 @@ VCR.configure do |c| c.allow_http_connections_when_no_cassette = false - c.filter_sensitive_data('') { Docker.url.sub(/tcp\:/, 'https:') } + c.filter_sensitive_data('') { Docker.url } c.filter_sensitive_data('') { ENV['DOCKER_API_USER'] } c.filter_sensitive_data('') { ENV['DOCKER_API_PASS'] } c.filter_sensitive_data('') { ENV['DOCKER_API_EMAIL'] } diff --git a/spec/vcr/Docker_Image/_build_from_dir/with_a_valid_Dockerfile/with_a_block_capturing_build_output/uses_a_cached_version_the_second_time/calls_the_block_and_passes_build_output.yml b/spec/vcr/Docker_Image/_build_from_dir/with_a_valid_Dockerfile/with_a_block_capturing_build_output/uses_a_cached_version_the_second_time/calls_the_block_and_passes_build_output.yml new file mode 100644 index 000000000..516d99881 --- /dev/null +++ b/spec/vcr/Docker_Image/_build_from_dir/with_a_valid_Dockerfile/with_a_block_capturing_build_output/uses_a_cached_version_the_second_time/calls_the_block_and_passes_build_output.yml @@ -0,0 +1,96 @@ +--- +http_interactions: +- request: + method: post + uri: "/v1.16/build" + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Swipely/Docker-API 1.21.0 + Content-Type: + - application/tar + Transfer-Encoding: + - chunked + X-Registry-Config: + - eyJjb25maWdzIjp7IiI6eyJ1c2VybmFtZSI6IiIsInBhc3N3b3JkIjoiIiwiZW1haWwiOiIifX19 + response: + status: + code: 200 + message: + headers: + Content-Type: + - application/json + Date: + - Fri, 03 Apr 2015 20:19:34 GMT + body: + encoding: UTF-8 + string: "{\"stream\":\"Step 0 : FROM debian:wheezy\\n\"}\r\n{\"stream\":\" ---\\u003e + 1265e16d0c28\\n\"}\r\n{\"stream\":\"Step 1 : ADD . /\\n\"}\r\n{\"stream\":\" + ---\\u003e 1df5fb96c8b6\\n\"}\r\n{\"stream\":\"Removing intermediate container + 31c097a22066\\n\"}\r\n{\"stream\":\"Successfully built 1df5fb96c8b6\\n\"}\r\n" + http_version: + recorded_at: Wed, 08 Apr 2015 15:07:59 GMT +- request: + method: post + uri: "/v1.16/build" + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Swipely/Docker-API 1.21.0 + Content-Type: + - application/tar + Transfer-Encoding: + - chunked + X-Registry-Config: + - eyJjb25maWdzIjp7IiI6eyJ1c2VybmFtZSI6IiIsInBhc3N3b3JkIjoiIiwiZW1haWwiOiIifX19 + response: + status: + code: 200 + message: + headers: + Content-Type: + - application/json + Date: + - Fri, 03 Apr 2015 20:19:36 GMT + body: + encoding: UTF-8 + string: "{\"stream\":\"Step 0 : FROM debian:wheezy\\n\"}\r\n{\"stream\":\" ---\\u003e + 1265e16d0c28\\n\"}\r\n{\"stream\":\"Step 1 : ADD . /\\n\"}\r\n{\"stream\":\" + ---\\u003e Using cache\\n\"}\r\n{\"stream\":\" ---\\u003e 1df5fb96c8b6\\n\"}\r\n{\"stream\":\"Successfully + built 1df5fb96c8b6\\n\"}\r\n" + http_version: + recorded_at: Wed, 08 Apr 2015 15:07:59 GMT +- request: + method: delete + uri: "/v1.16/images/1df5fb96c8b6?noprune=true" + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Swipely/Docker-API 1.21.0 + Content-Type: + - text/plain + response: + status: + code: 200 + message: + headers: + Content-Type: + - application/json + Date: + - Fri, 03 Apr 2015 20:19:37 GMT + Content-Length: + - '81' + body: + encoding: UTF-8 + string: |- + [{"Deleted":"1df5fb96c8b6f0f21cf5898ad17867b80f0b58d2db5859fcc997dfde7d903b56"} + ] + http_version: + recorded_at: Wed, 08 Apr 2015 15:07:59 GMT +recorded_with: VCR 2.9.2