From 44ed75af23aca2d67e551eefe3fc11e37e2d26e9 Mon Sep 17 00:00:00 2001 From: Frank Macreery Date: Mon, 19 May 2014 16:03:22 -0400 Subject: [PATCH 1/2] Better support for named containers This modifies container matching to look only at the container name for cases in which a container_name is explicitly provided. --- providers/container.rb | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/providers/container.rb b/providers/container.rb index 7cdf2fb00e..a20de09fec 100644 --- a/providers/container.rb +++ b/providers/container.rb @@ -4,11 +4,7 @@ def load_current_resource @current_resource = Chef::Resource::DockerContainer.new(new_resource) wait_until_ready! docker_containers.each do |ps| - unless container_id_matches?(ps['id']) - next unless container_image_matches?(ps['image']) - next unless container_command_matches_if_exists?(ps['command']) - next unless container_name_matches_if_exists?(ps['names']) - end + next unless container_matches?(ps) Chef::Log.debug('Matched docker container: ' + ps['line'].squeeze(' ')) @current_resource.container_name(ps['names']) @current_resource.created(ps['created']) @@ -134,6 +130,15 @@ def commit docker_cmd!("commit #{commit_args} #{current_resource.id} #{commit_end_args}") end +def container_matches?(ps) + return true if container_id_matches?(ps['id']) + return true if container_name_matches?(ps['names']) + return false unless container_image_matches?(ps['image']) + return false unless container_command_matches_if_exists?(ps['command']) + return false unless container_name_matches_if_exists?(ps['names']) + true +end + def container_command_matches_if_exists?(command) return true if new_resource.command.nil? # try the exact command but also the command with the ' and " stripped out, since docker will @@ -150,6 +155,10 @@ def container_image_matches?(image) image.include?(new_resource.image) end +def container_name_matches?(names) + new_resource.container_name && new_resource.container_name == names +end + def container_name_matches_if_exists?(names) return false if new_resource.container_name && new_resource.container_name != names true From 4d1c28cec9cfe1706c35061e0e2bc26e2c3b8d70 Mon Sep 17 00:00:00 2001 From: Frank Macreery Date: Tue, 20 May 2014 17:02:17 -0400 Subject: [PATCH 2/2] Specs for 44ed75af23aca2d67e551eefe3fc11e37e2d26e9 --- .../tests/minitest/container_lwrp_test.rb | 5 +++++ .../default/tests/minitest/support/helpers.rb | 6 ++++++ .../docker_test/recipes/container_lwrp.rb | 16 ++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/test/cookbooks/docker_test/files/default/tests/minitest/container_lwrp_test.rb b/test/cookbooks/docker_test/files/default/tests/minitest/container_lwrp_test.rb index 7f8bd3a8f0..00cfb70d94 100644 --- a/test/cookbooks/docker_test/files/default/tests/minitest/container_lwrp_test.rb +++ b/test/cookbooks/docker_test/files/default/tests/minitest/container_lwrp_test.rb @@ -38,4 +38,9 @@ assert container_running?('bflad/testcontainerd') service('testcontainerd').must_be_running end + + it "has a named busybox-container running sleep 8888" do + cmd = container_info("busybox-container").first['Config']['Cmd'] + assert cmd.grep(/8888/).count > 0 + end end diff --git a/test/cookbooks/docker_test/files/default/tests/minitest/support/helpers.rb b/test/cookbooks/docker_test/files/default/tests/minitest/support/helpers.rb index f4d13fdaf7..10a98a9fa5 100644 --- a/test/cookbooks/docker_test/files/default/tests/minitest/support/helpers.rb +++ b/test/cookbooks/docker_test/files/default/tests/minitest/support/helpers.rb @@ -1,3 +1,5 @@ +require 'json' + module Helpers module DockerTest require 'chef/mixin/shell_out' @@ -29,5 +31,9 @@ def image_exists?(image) di.stdout.include?(image) end + def container_info(name) + JSON.parse(shell_out("docker inspect #{name}").stdout) + end + end end diff --git a/test/cookbooks/docker_test/recipes/container_lwrp.rb b/test/cookbooks/docker_test/recipes/container_lwrp.rb index b2a9c625d4..49675235ba 100644 --- a/test/cookbooks/docker_test/recipes/container_lwrp.rb +++ b/test/cookbooks/docker_test/recipes/container_lwrp.rb @@ -76,3 +76,19 @@ detach true port '9999:9999' end + +docker_container "busybox-container" do + image "busybox" + container_name "busybox-container" + command "sleep 7777" + detach true + init_type false +end + +docker_container "busybox-container" do + image "busybox" + container_name "busybox-container" + command "sleep 8888" + init_type false + action :redeploy +end