From 9a3df7e1747a098b09c71e8664c22cebcbf15154 Mon Sep 17 00:00:00 2001 From: Jakob Durstberger Date: Thu, 22 Aug 2024 05:56:59 +0200 Subject: [PATCH] Add support for 'Cmd' argument on container tasks --- lib/rake_docker/container.rb | 31 ++++++++---- lib/rake_docker/tasks/provision.rb | 2 + spec/rake_docker/container_spec.rb | 64 ++++++++++++++++++++++++ spec/rake_docker/tasks/provision_spec.rb | 3 ++ 4 files changed, 90 insertions(+), 10 deletions(-) diff --git a/lib/rake_docker/container.rb b/lib/rake_docker/container.rb index 3cc193b..956bb95 100644 --- a/lib/rake_docker/container.rb +++ b/lib/rake_docker/container.rb @@ -145,7 +145,13 @@ def enhance_with_name(container, name) class Provisioner include Utilities - attr_reader :name, :image, :ports, :environment, :ready, :reporter + attr_reader :name, + :image, + :ports, + :environment, + :ready, + :reporter, + :command def initialize(name, image, opts = {}) @name = name @@ -154,6 +160,7 @@ def initialize(name, image, opts = {}) @ports = opts[:ports] || [] @ready = opts[:ready?] @reporter = opts[:reporter] || NullReporter.new + @command = opts[:command] end # rubocop:disable Metrics/AbcSize @@ -165,7 +172,7 @@ def execute ensure_container_running(container) else reporter.container_does_not_exist(name) - start_new_container(name, image, ports, environment) + start_new_container(name, image, ports, environment, command) end reporter.done end @@ -173,9 +180,9 @@ def execute private - def start_new_container(name, image, ports, environment) + def start_new_container(name, image, ports, environment, command) ensure_image_available(image) - create_and_start_container(name, image, ports, environment) + create_and_start_container(name, image, ports, environment, command) end def ensure_image_available(image) @@ -206,16 +213,17 @@ def ensure_container_running(container) end end - def create_and_start_container(name, image, ports, environment) - start_container(create_container(image, name, ports, environment)) + def create_and_start_container(name, image, ports, environment, command) + start_container(create_container(image, name, ports, environment, + command)) end - def create_container(image, name, ports, environment) + def create_container(image, name, ports, environment, command) exposed_ports, port_bindings = process_ports(ports) reporter.creating_container(name, image) container = Docker::Container.create( make_container_options( - name, image, exposed_ports, port_bindings, environment + name, image, exposed_ports, port_bindings, environment, command ) ) container = enhance_with_name(container, name) @@ -223,17 +231,20 @@ def create_container(image, name, ports, environment) container end + # rubocop:disable Metrics/ParameterLists def make_container_options( - name, image, exposed_ports, port_bindings, environment + name, image, exposed_ports, port_bindings, environment, command ) { name:, Image: image, ExposedPorts: exposed_ports, HostConfig: { PortBindings: port_bindings }, - Env: environment + Env: environment, + Cmd: command } end + # rubocop:enable Metrics/ParameterLists def start_container(container) reporter.starting_container(container) diff --git a/lib/rake_docker/tasks/provision.rb b/lib/rake_docker/tasks/provision.rb index f476224..3526966 100644 --- a/lib/rake_docker/tasks/provision.rb +++ b/lib/rake_docker/tasks/provision.rb @@ -16,6 +16,7 @@ class Provision < RakeFactory::Task parameter :image, required: true parameter :ports parameter :environment + parameter :command parameter :ready_check @@ -28,6 +29,7 @@ class Provision < RakeFactory::Task t.image, ports: t.ports, environment: t.environment, + command: t.command, ready?: t.ready_check, reporter: t.reporter ) diff --git a/spec/rake_docker/container_spec.rb b/spec/rake_docker/container_spec.rb index 6d4fb57..8992b5d 100644 --- a/spec/rake_docker/container_spec.rb +++ b/spec/rake_docker/container_spec.rb @@ -254,6 +254,70 @@ )) end + it 'passes the provided command when creating the container' do + name = 'my-container' + image = 'nginx:latest' + command = ['ls', '-l'] + underlying_image = instance_double(Docker::Image, image) + underlying_container = MockDockerContainer.created(name) + + allow(Docker::Container) + .to(receive(:get).with(name) + .and_raise(Docker::Error::NotFoundError)) + allow(Docker::Image) + .to(receive(:all).with(filters: filters(image)) + .and_return([underlying_image])) + allow(underlying_container).to(receive(:start)) + allow(Docker::Container) + .to(receive(:create) + .and_return(underlying_container)) + + reporter = MockReporter.new + provisioner = RakeDocker::Container::Provisioner + .new( + name, image, + reporter:, + command: + ) + + provisioner.execute + + expect(Docker::Container) + .to(have_received(:create) + .with(hash_including(Cmd: command))) + end + + it 'passes nil for the command when creating container with no command' do + name = 'my-container' + image = 'nginx:latest' + underlying_image = instance_double(Docker::Image, image) + underlying_container = MockDockerContainer.created(name) + + allow(Docker::Container) + .to(receive(:get).with(name) + .and_raise(Docker::Error::NotFoundError)) + allow(Docker::Image) + .to(receive(:all).with(filters: filters(image)) + .and_return([underlying_image])) + allow(underlying_container).to(receive(:start)) + allow(Docker::Container) + .to(receive(:create) + .and_return(underlying_container)) + + reporter = MockReporter.new + provisioner = RakeDocker::Container::Provisioner + .new( + name, image, + reporter: + ) + + provisioner.execute + + expect(Docker::Container) + .to(have_received(:create) + .with(hash_including(Cmd: nil))) + end + # rubocop:disable RSpec/MultipleExpectations it 'calls the supplied readiness poller when provided' do name = 'my-container' diff --git a/spec/rake_docker/tasks/provision_spec.rb b/spec/rake_docker/tasks/provision_spec.rb index aa474fb..aa96bac 100644 --- a/spec/rake_docker/tasks/provision_spec.rb +++ b/spec/rake_docker/tasks/provision_spec.rb @@ -97,6 +97,7 @@ 'THING1' => 'one', 'THING2' => 'two' } + command = ['ls', '-l'] reporter = RakeDocker::Container::NullReporter.new ready_check = proc { true } @@ -111,6 +112,7 @@ image:, ports:, environment:, + command:, reporter:, ready_check: ) @@ -126,6 +128,7 @@ image, ports:, environment:, + command:, reporter:, ready?: ready_check ))