beaker-docker 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 07fca4dada242e02c4caa429ebd0209cc3911fb9
4
- data.tar.gz: 9aa1e6633cfa7c267cb9b47f07d17eee0a540e0d
3
+ metadata.gz: 8a15b0037f264bd2e696038da349f7fc9ab219c7
4
+ data.tar.gz: a8a4d98c0cc5a0611938607362e334a2dd289d74
5
5
  SHA512:
6
- metadata.gz: c2eb49d22fb4827aa73774e4dcdbac5c5063f84662db010eb87ae440cfac083b1b7eb50152725225d6d3411f0249ce111497685127d79ec5bacc093359251ba0
7
- data.tar.gz: fbd2f2246972460cfb0c8efb0d5db8aac08fbb04f7012a95bb5ca391b2c6d7df32beb192e54b3e9d5b54082347d1b164fbab4cfabdb9a65615137f66bd4c69dc
6
+ metadata.gz: 7af3da434dbbe522bf039ff513c07d113bdb3da709e622bb9de5eba21a7262237b02e2063d42ef580d0dcd31bd2623a30c8fb897959973bc4dc9d1d7d1d4d33b
7
+ data.tar.gz: 833277de264b0000bda06bdbef7a072d0d53c1ac5dab68f0ba5f81dda2e34ca5a815657551a5dcee3a313e57be711bd4843bcd09cc1e9d85cb53a5a81dcc4784
@@ -1,3 +1,3 @@
1
1
  module BeakerDocker
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
@@ -12,7 +12,7 @@ module Beaker
12
12
  def initialize(hosts, options)
13
13
  require 'docker'
14
14
  @options = options
15
- @logger = options[:logger]
15
+ @logger = options[:logger] || Beaker::Logger.new
16
16
  @hosts = hosts
17
17
 
18
18
  # increase the http timeouts as provisioning images can be slow
@@ -52,44 +52,43 @@ module Beaker
52
52
 
53
53
  container_opts = {}
54
54
  @logger.debug("Creating image")
55
- if dockerfile = host['dockerfile']
56
- install_and_run_ssh = true
57
- container_opts['ExposedPorts'] = {'22/tcp' => {} }
58
- # assume that the dockerfile is in the repo and tests are running
59
- # from the root of the repo; maybe add support for external Dockerfiles
60
- # with external build dependencies later.
61
- if File.exist?(dockerfile)
62
- dir = File.expand_path(dockerfile).chomp(dockerfile)
63
- image = ::Docker::Image.build_from_dir(
64
- dir,
65
- { 'dockerfile' => dockerfile,
66
- :rm => true,
67
- :buildargs => buildargs_for(host)
68
- }
69
- )
70
- else
71
- raise "Unable to find dockerfile at #{dockerfile}"
72
- end
73
- elsif host['use_image_entry_point']
74
- install_and_run_ssh = true
75
- df = <<-DF
55
+
56
+ dockerfile = host['dockerfile']
57
+ if dockerfile
58
+ install_and_run_ssh = true
59
+ container_opts['ExposedPorts'] = {'22/tcp' => {} }
60
+ # assume that the dockerfile is in the repo and tests are running
61
+ # from the root of the repo; maybe add support for external Dockerfiles
62
+ # with external build dependencies later.
63
+ if File.exist?(dockerfile)
64
+ dir = File.expand_path(dockerfile).chomp(dockerfile)
65
+ image = ::Docker::Image.build_from_dir(
66
+ dir,
67
+ { 'dockerfile' => dockerfile,
68
+ :rm => true,
69
+ :buildargs => buildargs_for(host)
70
+ }
71
+ )
72
+ else
73
+ raise "Unable to find dockerfile at #{dockerfile}"
74
+ end
75
+ elsif host['use_image_entry_point']
76
+ install_and_run_ssh = true
77
+ df = <<-DF
76
78
  FROM #{host['image']}
77
79
  EXPOSE 22
78
80
  DF
79
81
 
80
- if cmd = host['docker_cmd']
81
- df += cmd
82
- end
83
- image = ::Docker::Image.build(df, {
84
- :rm => true, :buildargs => buildargs_for(host)
85
- })
82
+ cmd = host['docker_cmd']
83
+ df += cmd if cmd
86
84
 
87
- else
85
+ image = ::Docker::Image.build(df, { rm: true, buildargs: buildargs_for(host) })
88
86
 
89
- image = ::Docker::Image.build(dockerfile_for(host), {
90
- :rm => true, :buildargs => buildargs_for(host)
91
- })
92
- end
87
+ else
88
+
89
+ image = ::Docker::Image.build(dockerfile_for(host),
90
+ { rm: true, buildargs: buildargs_for(host) })
91
+ end
93
92
 
94
93
  if @docker_type == 'swarm'
95
94
  image_name = "#{@registry}/beaker/#{image.id}"
@@ -123,10 +122,13 @@ module Beaker
123
122
  container_opts[k] = v
124
123
  end
125
124
  end
125
+
126
126
  container = find_container(host)
127
127
 
128
- # If the specified container exists, then use it rather creating a new one
129
- if container.nil?
128
+ # Provisioning - Only provision if:
129
+ # - provisioning was explicitly requested via options, or
130
+ # - the host's container can't be found via its name or ID
131
+ if @options[:provision] || container.nil?
130
132
  unless host['mount_folders'].nil?
131
133
  container_opts['HostConfig'] ||= {}
132
134
  container_opts['HostConfig']['Binds'] = host['mount_folders'].values.map do |mount|
@@ -145,14 +147,12 @@ module Beaker
145
147
  container_opts['HostConfig']['CapAdd'] = host['docker_cap_add']
146
148
  end
147
149
 
148
- if @options[:provision]
149
- if host['docker_container_name']
150
- container_opts['name'] = host['docker_container_name']
151
- end
152
-
153
- @logger.debug("Creating container from image #{image_name}")
154
- container = ::Docker::Container.create(container_opts)
150
+ if host['docker_container_name']
151
+ container_opts['name'] = host['docker_container_name']
155
152
  end
153
+
154
+ @logger.debug("Creating container from image #{image_name}")
155
+ container = ::Docker::Container.create(container_opts)
156
156
  end
157
157
 
158
158
  if container.nil?
@@ -203,8 +203,8 @@ module Beaker
203
203
  }
204
204
 
205
205
  @logger.debug("node available as ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@#{ip} -p #{port}")
206
- host['docker_container'] = container
207
- host['docker_image'] = image
206
+ host['docker_container_id'] = container.id
207
+ host['docker_image_id'] = image.id
208
208
  host['vm_ip'] = container.json["NetworkSettings"]["IPAddress"].to_s
209
209
 
210
210
  end
@@ -260,7 +260,8 @@ module Beaker
260
260
  def cleanup
261
261
  @logger.notify "Cleaning up docker"
262
262
  @hosts.each do |host|
263
- if container = host['docker_container']
263
+ container = find_container(host)
264
+ if container
264
265
  @logger.debug("stop container #{container.id}")
265
266
  begin
266
267
  container.kill
@@ -276,15 +277,21 @@ module Beaker
276
277
  end
277
278
  end
278
279
 
279
- # Do not remove the image if docker_reserve_image is set to true, otherwise remove it
280
- if image = (host['docker_preserve_image'] ? nil : host['docker_image'])
281
- @logger.debug("delete image #{image.id}")
282
- begin
283
- image.delete
284
- rescue Excon::Errors::ClientError => e
285
- @logger.warn("deletion of image #{image.id} failed: #{e.response.body}")
286
- rescue ::Docker::Error::DockerError => e
287
- @logger.warn("deletion of image #{image.id} caused internal Docker error: #{e.message}")
280
+ # Do not remove the image if docker_preserve_image is set to true, otherwise remove it
281
+ unless host['docker_preserve_image']
282
+ image_id = host['docker_image_id']
283
+
284
+ if image_id
285
+ @logger.debug("deleting image #{image_id}")
286
+ begin
287
+ ::Docker::Image.remove(image_id)
288
+ rescue Excon::Errors::ClientError => e
289
+ @logger.warn("deletion of image #{image_id} failed: #{e.response.body}")
290
+ rescue ::Docker::Error::DockerError => e
291
+ @logger.warn("deletion of image #{image_id} caused internal Docker error: #{e.message}")
292
+ end
293
+ else
294
+ @logger.warn("Intended to delete the host's docker image, but host['docker_image_id'] was not set")
288
295
  end
289
296
  end
290
297
  end
@@ -446,12 +453,26 @@ module Beaker
446
453
  # return the existing container if we're not provisioning
447
454
  # and docker_container_name is set
448
455
  def find_container(host)
449
- return nil if host['docker_container_name'].nil? || @options[:provision]
450
- @logger.debug("Looking for an existing container called #{host['docker_container_name']}")
456
+ id = host['docker_container_id']
457
+ name = host['docker_container_name']
458
+ return unless id || name
459
+
460
+ containers = ::Docker::Container.all
461
+
462
+ if id
463
+ @logger.debug("Looking for an existing container with ID #{id}")
464
+ container = containers.select { |c| c.id == id }.first
465
+ end
466
+
467
+ if name && container.nil?
468
+ @logger.debug("Looking for an existing container with name #{name}")
469
+ container = containers.select do |c|
470
+ c.info['Names'].include? "/#{name}"
471
+ end.first
472
+ end
451
473
 
452
- ::Docker::Container.all.select do |c|
453
- c.info['Names'].include? "/#{host['docker_container_name']}"
454
- end.first
474
+ return container unless container.nil?
475
+ @logger.debug("Existing container not found")
455
476
  end
456
477
 
457
478
  # return true if we are inside a docker container
@@ -55,15 +55,14 @@ module Beaker
55
55
 
56
56
  let(:image) do
57
57
  image = double('Docker::Image')
58
- allow( image ).to receive(:id)
58
+ allow( image ).to receive(:id).and_return("zyxwvu")
59
59
  allow( image ).to receive(:tag)
60
- allow( image ).to receive(:delete)
61
60
  image
62
61
  end
63
62
 
64
63
  let(:container) do
65
64
  container = double('Docker::Container')
66
- allow( container ).to receive(:id)
65
+ allow( container ).to receive(:id).and_return('abcdef')
67
66
  allow( container ).to receive(:start)
68
67
  allow( container ).to receive(:info).and_return(
69
68
  *(0..2).map { |index| { 'Names' => ["/spec-container-#{index}"] } }
@@ -320,6 +319,7 @@ module Beaker
320
319
  container_name = "spec-container-#{index}"
321
320
  host['docker_container_name'] = container_name
322
321
 
322
+ allow(::Docker::Container).to receive(:all).and_return([])
323
323
  expect( ::Docker::Container ).to receive(:create).with({
324
324
  'Image' => image.id,
325
325
  'Hostname' => host.name,
@@ -492,8 +492,8 @@ module Beaker
492
492
  it 'should record the image and container for later' do
493
493
  docker.provision
494
494
 
495
- expect( hosts[0]['docker_image'] ).to be === image
496
- expect( hosts[0]['docker_container'] ).to be === container
495
+ expect( hosts[0]['docker_image_id'] ).to be === image.id
496
+ expect( hosts[0]['docker_container_id'] ).to be === container.id
497
497
  end
498
498
 
499
499
  context 'provision=false' do
@@ -533,6 +533,8 @@ module Beaker
533
533
  before :each do
534
534
  # get into a state where there's something to clean
535
535
  allow( ::Docker ).to receive(:validate_version!)
536
+ allow( ::Docker::Container ).to receive(:all).and_return([container])
537
+ allow( ::Docker::Image ).to receive(:remove).with(image.id)
536
538
  allow( docker ).to receive(:dockerfile_for)
537
539
  docker.provision
538
540
  end
@@ -551,7 +553,7 @@ module Beaker
551
553
 
552
554
  it 'should delete the images' do
553
555
  allow( docker ).to receive( :sleep ).and_return(true)
554
- expect( image ).to receive(:delete)
556
+ expect( ::Docker::Image ).to receive(:remove).with(image.id)
555
557
  docker.cleanup
556
558
  end
557
559
 
@@ -560,7 +562,7 @@ module Beaker
560
562
  hosts.each do |host|
561
563
  host['docker_preserve_image']=true
562
564
  end
563
- expect( image ).to_not receive(:delete)
565
+ expect( ::Docker::Image ).to_not receive(:remove)
564
566
  docker.cleanup
565
567
  end
566
568
 
@@ -569,7 +571,7 @@ module Beaker
569
571
  hosts.each do |host|
570
572
  host['docker_preserve_image']=false
571
573
  end
572
- expect( image ).to receive(:delete)
574
+ expect( ::Docker::Image ).to receive(:remove).with(image.id)
573
575
  docker.cleanup
574
576
  end
575
577
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beaker-docker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rishi Javia, Kevin Imber, Tony Vu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-26 00:00:00.000000000 Z
11
+ date: 2018-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec