Skip to content

Commit

Permalink
Added an integration test for proxied accessory using Busybox and netcat
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-alexandrov committed Nov 26, 2024
1 parent f367ca8 commit eee47d1
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/kamal/cli/accessory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def boot(name, prepare: true)
execute *accessory.run

if accessory.running_proxy?
target = accessory.container_id_for(container_name: accessory.service_name, only_running: true)
target = capture_with_info(*accessory.container_id_for(container_name: accessory.service_name, only_running: true)).strip
execute *accessory.deploy(target: target)
end
end
Expand Down Expand Up @@ -81,7 +81,7 @@ def start(name)
execute *KAMAL.auditor.record("Started #{name} accessory"), verbosity: :debug
execute *accessory.start
if accessory.running_proxy?
target = container_id_for(container_name: service_name, only_running: true)
target = capture_with_info(*accessory.container_id_for(container_name: accessory.service_name, only_running: true)).strip
execute *accessory.deploy(target: target)
end
end
Expand Down
2 changes: 2 additions & 0 deletions test/integration/docker/deployer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ COPY *.sh .
COPY app/ app/
COPY app_with_roles/ app_with_roles/
COPY app_with_traefik/ app_with_traefik/
COPY app_with_proxied_accessory/ app_with_proxied_accessory/

RUN rm -rf /root/.ssh
RUN ln -s /shared/ssh /root/.ssh
Expand All @@ -30,6 +31,7 @@ RUN git config --global user.name "Deployer"
RUN cd app && git init && git add . && git commit -am "Initial version"
RUN cd app_with_roles && git init && git add . && git commit -am "Initial version"
RUN cd app_with_traefik && git init && git add . && git commit -am "Initial version"
RUN cd app_with_proxied_accessory && git init && git add . && git commit -am "Initial version"

HEALTHCHECK --interval=1s CMD pgrep sleep

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM registry:4443/nginx:1-alpine-slim

COPY default.conf /etc/nginx/conf.d/default.conf

ARG COMMIT_SHA
RUN echo $COMMIT_SHA > /usr/share/nginx/html/version
RUN mkdir -p /usr/share/nginx/html/versions && echo "version" > /usr/share/nginx/html/versions/$COMMIT_SHA
RUN mkdir -p /usr/share/nginx/html/versions && echo "hidden" > /usr/share/nginx/html/versions/.hidden
RUN echo "Up!" > /usr/share/nginx/html/up
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
service: app_with_proxied_accessory
image: app_with_proxied_accessory
servers:
- vm1
env:
clear:
CLEAR_TOKEN: 4321
CLEAR_TAG: ""
HOST_TOKEN: "${HOST_TOKEN}"
asset_path: /usr/share/nginx/html/versions
proxy:
host: 127.0.0.1
registry:
server: registry:4443
username: root
password: root
builder:
driver: docker
arch: <%= Kamal::Utils.docker_arch %>
args:
COMMIT_SHA: <%= `git rev-parse HEAD` %>
accessories:
busybox:
service: custom-busybox
image: registry:4443/busybox:1.36.0
cmd: sh -c 'echo "Starting busybox..."; trap exit term; while true; do sleep 1; done'
roles:
- web
netcat:
service: netcat
image: registry:4443/busybox:1.36.0
cmd: >
sh -c 'echo "Starting netcat..."; while true; do echo -e "HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nHello Ruby" | nc -l -p 80; done'
roles:
- web
port: 12345:80
proxy:
host: netcat
ssl: false
healthcheck:
interval: 1
timeout: 1
path: "/"

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
server {
listen 80;
listen [::]:80;
server_name localhost;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
63 changes: 63 additions & 0 deletions test/integration/proxied_accessory_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require_relative "integration_test"

class ProxiedAccessoryTest < IntegrationTest
test "boot, stop, start, restart, logs, remove" do
@app = "app_with_proxied_accessory"

kamal :deploy

kamal :accessory, :boot, :netcat
assert_accessory_running :netcat
assert_netcat_is_up

kamal :accessory, :stop, :netcat
assert_accessory_not_running :netcat
assert_netcat_not_found

kamal :accessory, :start, :netcat
assert_accessory_running :netcat
assert_netcat_is_up

kamal :accessory, :restart, :netcat
assert_accessory_running :netcat
assert_netcat_is_up

kamal :accessory, :remove, :netcat, "-y"
assert_accessory_not_running :netcat
assert_netcat_not_found
end

private
def assert_accessory_running(name)
assert_match /registry:4443\/busybox:1.36.0 "sh -c 'echo \\"Start/, accessory_details(name)
end

def assert_accessory_not_running(name)
assert_no_match /registry:4443\/busybox:1.36.0 "sh -c 'echo \\"Start/, accessory_details(name)
end

def accessory_details(name)
kamal :accessory, :details, name, capture: true
end

def assert_netcat_is_up
response = netcat_response
debug_response_code(response, "200")
assert_equal "200", response.code
end

def assert_netcat_not_found
response = netcat_response
debug_response_code(response, "404")
assert_equal "404", response.code
end

def netcat_response
uri = URI.parse("http://127.0.0.1:12345/up")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri)
request["Host"] = "netcat"

http.request(request)
end
end

0 comments on commit eee47d1

Please sign in to comment.