From 95353863b1d58ba411580e811f287ad43c14da2a Mon Sep 17 00:00:00 2001 From: Chai Feng Date: Sun, 20 Oct 2019 17:18:13 +0200 Subject: [PATCH] Add testing in Vagrant --- Vagrantfile | 85 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 76 insertions(+), 9 deletions(-) diff --git a/Vagrantfile b/Vagrantfile index 29651ba..3fe0bed 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -15,7 +15,7 @@ Vagrant.configure('2') do |config| ip_prefix="192.168.56" config.vm.provision 'docker-daemon-config', type: 'shell', inline: <<-SHELL - set -exu + set -eu if [[ ! -f /etc/docker/daemon.json ]]; then echo '{' >> /etc/docker/daemon.json echo ' "insecure-registries": ["localhost:5000", "#{ip_prefix}.130:5000"]' >> /etc/docker/daemon.json @@ -31,14 +31,16 @@ Vagrant.configure('2') do |config| SHELL config.vm.provision 'ufw-docker', type: 'shell', inline: <<-SHELL - set -exu + set -euo pipefail export DEBUG=true lsb_release -is | grep -Fi ubuntu /vagrant/ufw-docker check || { ufw allow OpenSSH ufw allow from #{ip_prefix}.128/28 to any - yes | ufw enable + yes | ufw enable || true + ufw status | grep '^Status: active' + /vagrant/ufw-docker install sed -i -e 's,192\.168\.0\.0/16,#{ip_prefix}.128/28,' /etc/ufw/after.rules @@ -51,13 +53,15 @@ Vagrant.configure('2') do |config| } SHELL + private_registry="#{ip_prefix}.130:5000" + config.vm.define "master" do |master| master.vm.hostname = "master" master.vm.network "private_network", ip: "#{ip_prefix}.130" master.vm.provision "unit-testing", type: 'shell', inline: <<-SHELL - /vagrant/test/ufw-docker.test.sh - /vagrant/test/ufw-docker-service.test.sh + set -euo pipefail + /vagrant/test.sh SHELL master.vm.provision "docker-registry", type: 'docker' do |d| @@ -68,10 +72,10 @@ Vagrant.configure('2') do |config| daemonize: true end - ufw_docker_agent_image = "192.168.56.130:5000/chaifeng/ufw-docker-agent:test" + ufw_docker_agent_image = "#{private_registry}/chaifeng/ufw-docker-agent:test" master.vm.provision "docker-build-ufw-docker-agent", type: 'shell', inline: <<-SHELL - set -exu + set -euo pipefail docker build -t #{ufw_docker_agent_image} /vagrant docker push #{ufw_docker_agent_image} @@ -83,12 +87,55 @@ Vagrant.configure('2') do |config| SHELL master.vm.provision "swarm-init", type: 'shell', inline: <<-SHELL - set -exuo pipefail + set -euo pipefail docker info | fgrep 'Swarm: active' && exit 0 docker swarm init --advertise-addr eth1 docker swarm join-token worker --quiet > /vagrant/.vagrant/docker-join-token SHELL + + master.vm.provision "build-webapp", type: 'shell', inline: <<-SHELL + set -euo pipefail + docker build -t #{private_registry}/chaifeng/hostname-webapp - <<\\DOCKERFILE +FROM httpd:alpine + +RUN { echo '#!/bin/sh'; \\ + echo 'set -e; (echo -n "${name:-Hi} "; hostname;) > /usr/local/apache2/htdocs/index.html'; \\ + echo 'exec "$@"'; \\ + } > /entrypoint.sh; chmod +x /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] +CMD ["httpd-foreground"] +DOCKERFILE + docker push #{private_registry}/chaifeng/hostname-webapp + SHELL + + master.vm.provision "local-webapp", type: 'shell', inline: <<-SHELL + set -euo pipefail + for name in public:8080 local:8000; do + webapp="${name%:*}_webapp" + port="${name#*:}" + if docker inspect "$webapp" &>/dev/null; then docker stop "$webapp"; fi + docker run -d --rm --name "$webapp" \ + -p "$port:80" --env name="$webapp" #{private_registry}/chaifeng/hostname-webapp + sleep 1 + done + + ufw-docker allow public_webapp + SHELL + + master.vm.provision "swarm-webapp", type: 'shell', inline: <<-SHELL + set -euo pipefail + for name in public:9090 local:9000; do + webapp="${name%:*}_service" + port="${name#*:}" + if docker service inspect "$webapp" &>/dev/null; then docker service rm "$webapp"; fi + docker service create --name "$webapp" \ + --publish "${port}:80" --env name="$webapp" --replicas 3 #{private_registry}/chaifeng/hostname-webapp + done + + ufw-docker service allow public_service 80/tcp + SHELL end 1.upto 2 do |ip| @@ -97,7 +144,7 @@ Vagrant.configure('2') do |config| node.vm.network "private_network", ip: "#{ip_prefix}.#{ 130 + ip }" node.vm.provision "swarm-join", type: 'shell', inline: <<-SHELL - set -exuo pipefail + set -euo pipefail docker info | fgrep 'Swarm: active' && exit 0 [[ -f /vagrant/.vagrant/docker-join-token ]] && @@ -105,4 +152,24 @@ Vagrant.configure('2') do |config| SHELL end end + + config.vm.define "external" do |external| + external.vm.hostname = "external" + external.vm.network "private_network", ip: "#{ip_prefix}.127" + + external.vm.provision "testing", type: 'shell', inline: <<-SHELL + set -euo pipefail + server="http://#{ip_prefix}.130" + function test-webapp() { timeout 3 curl --silent "$@"; } + test-webapp "$server:8080" + ! test-webapp "$server:8000" + + test-webapp "$server:9090" + ! test-webapp "$server:9000" + + echo "=====================" + echo " TEST DONE " + echo "=====================" + SHELL + end end