Skip to content

Commit

Permalink
Added tests for accessory deploy and remove commands
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-alexandrov committed Sep 27, 2024
1 parent 041b849 commit 63bf121
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
17 changes: 14 additions & 3 deletions lib/kamal/cli/accessory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 4 additions & 6 deletions lib/kamal/commands/accessory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
17 changes: 16 additions & 1 deletion test/commands/accessory_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 63bf121

Please sign in to comment.