From 63bf121227db3b80e026973ed45932ce5c54b5ba Mon Sep 17 00:00:00 2001 From: Igor Alexandrov Date: Fri, 27 Sep 2024 12:24:54 +0400 Subject: [PATCH] Added tests for accessory deploy and remove commands --- lib/kamal/cli/accessory.rb | 17 ++++++++++++++--- lib/kamal/commands/accessory.rb | 10 ++++------ test/commands/accessory_test.rb | 17 ++++++++++++++++- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/lib/kamal/cli/accessory.rb b/lib/kamal/cli/accessory.rb index 6538ed42..e1beba9a 100644 --- a/lib/kamal/cli/accessory.rb +++ b/lib/kamal/cli/accessory.rb @@ -16,7 +16,11 @@ def boot(name, prepare: true) execute *accessory.ensure_env_directory upload! accessory.secrets_io, accessory.secrets_path, mode: "0600" execute *accessory.run - execute *accessory.deploy if accessory.running_proxy? + + if accessory.running_proxy? + target = accessory.container_id_for(container_name: accessory.service_name, only_running: true) + execute *accessory.deploy(target: target) + end end end end @@ -74,7 +78,10 @@ def start(name) on(hosts) do execute *KAMAL.auditor.record("Started #{name} accessory"), verbosity: :debug execute *accessory.start - execute *accessory.deploy if accessory.running_proxy? + if accessory.running_proxy? + target = container_id_for(container_name: service_name, only_running: true) + execute *accessory.deploy(target: target) + end end end end @@ -87,7 +94,11 @@ def stop(name) on(hosts) do execute *KAMAL.auditor.record("Stopped #{name} accessory"), verbosity: :debug execute *accessory.stop, raise_on_non_zero_exit: false - execute *accessory.remove if accessory.running_proxy? + + if accessory.running_proxy? + target = accessory.container_id_for(container_name: accessory.service_name, only_running: true) + execute *accessory.remove(target: target) + end end end end diff --git a/lib/kamal/commands/accessory.rb b/lib/kamal/commands/accessory.rb index 41c8b174..a68b5d6e 100644 --- a/lib/kamal/commands/accessory.rb +++ b/lib/kamal/commands/accessory.rb @@ -40,14 +40,12 @@ def info docker :ps, *service_filter end - def deploy - target = container_id_for(container_name: service_name, only_running: true) - proxy_exec :deploy, service_name, *accessory_config.proxy.deploy_command_args(target: target) if target + def deploy(target:) + proxy_exec :deploy, service_name, *accessory_config.proxy.deploy_command_args(target: target) end - def remove - target = container_id_for(container_name: service_name, only_running: true) - proxy_exec :remove, service_name, *accessory_config.proxy.remove_command_args(target: target) if target + def remove(target:) + proxy_exec :remove, service_name, *accessory_config.proxy.remove_command_args(target: target) end diff --git a/test/commands/accessory_test.rb b/test/commands/accessory_test.rb index f3d71ffd..626d8ebe 100644 --- a/test/commands/accessory_test.rb +++ b/test/commands/accessory_test.rb @@ -39,7 +39,10 @@ class CommandsAccessoryTest < ActiveSupport::TestCase "busybox" => { "service" => "custom-busybox", "image" => "busybox:latest", - "host" => "1.1.1.7" + "host" => "1.1.1.7", + "proxy" => { + "host" => "busybox.example.com" + } } } } @@ -158,6 +161,18 @@ class CommandsAccessoryTest < ActiveSupport::TestCase new_command(:mysql).remove_image.join(" ") end + test "deploy" do + assert_equal \ + "docker exec kamal-proxy kamal-proxy deploy custom-busybox --target \"172.1.0.2:80\" --deploy-timeout \"30s\" --drain-timeout \"30s\" --buffer-requests --buffer-responses --log-request-header \"Cache-Control\" --log-request-header \"Last-Modified\" --log-request-header \"User-Agent\"", + new_command(:busybox).deploy(target: "172.1.0.2").join(" ") + end + + test "remove" do + assert_equal \ + "docker exec kamal-proxy kamal-proxy remove custom-busybox --target \"172.1.0.2:80\"", + new_command(:busybox).remove(target: "172.1.0.2").join(" ") + end + private def new_command(accessory) Kamal::Commands::Accessory.new(Kamal::Configuration.new(@config), name: accessory)