Refactor: tried to refactor code
- move scripts to assets/scripts (and updated build) - put most logic into HaproxyConfigGenerator - created DockerLabelHandler for all label handling from previous code - added unit tests and fixtures to cover HaproxyConfigGenerator and DockerLabelHandler - added a Makefile with build (for docker build) and test targets
This commit is contained in:
parent
5f9eac4b8a
commit
08de132450
21 changed files with 387 additions and 131 deletions
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
5
tests/context.py
Normal file
5
tests/context.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import os
|
||||
import sys
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
|
||||
import easymapping
|
||||
5
tests/fixtures/no-services
vendored
Normal file
5
tests/fixtures/no-services
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
swarm-prom_caddy={"com.docker.stack.image":"stefanprodan/caddy","com.docker.stack.namespace":"swarm-prom"}
|
||||
swarm-prom_cadvisor={"com.docker.stack.image":"google/cadvisor","com.docker.stack.namespace":"swarm-prom"}
|
||||
swarm-prom_dockerd-exporter={"com.docker.stack.image":"stefanprodan/caddy","com.docker.stack.namespace":"swarm-prom"}
|
||||
swarm-prom_unsee={"com.docker.stack.image":"cloudflare/unsee:v0.8.0","com.docker.stack.namespace":"swarm-prom"}
|
||||
test_proxy={"com.docker.stack.image":"byjg/easy-haproxy","com.docker.stack.namespace":"test"}
|
||||
5
tests/fixtures/services
vendored
Normal file
5
tests/fixtures/services
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
portainer-agent_agent={"com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"portainer-agent"}
|
||||
my-stack_agent={"com.byjg.easyhaproxy.definitions":"agent","com.byjg.easyhaproxy.host.agent":"agent.quantum.example.org","com.byjg.easyhaproxy.localport.agent":"9001","com.byjg.easyhaproxy.mode.agent":"tcp","com.byjg.easyhaproxy.port.agent":"31339","com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"}
|
||||
my-stack_cadvisor={"com.byjg.easyhaproxy.definitions":"cadvisor","com.byjg.easyhaproxy.host.cadvisor":"cadvisor.quantum.example.org","com.byjg.easyhaproxy.localport.cadvisor":"8080","com.byjg.easyhaproxy.port.cadvisor":"31337","com.docker.stack.image":"gcr.io/google-containers/cadvisor:v0.34.0","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"}
|
||||
my-stack_node-exporter={"com.byjg.easyhaproxy.definitions":"exp","com.byjg.easyhaproxy.host.exp":"node-exporter.quantum.example.org","com.byjg.easyhaproxy.localport.exp":"9100","com.byjg.easyhaproxy.port.exp":"31337","com.docker.stack.image":"stefanprodan/swarmprom-node-exporter:v0.16.0","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"}
|
||||
my-stack_reverse-proxy={"com.docker.stack.image":"quay.io/pngmbh/easy-haproxy:tcp-mode","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"}
|
||||
2
tests/fixtures/services-tcp
vendored
Normal file
2
tests/fixtures/services-tcp
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
test_agent={"com.byjg.easyhaproxy.definitions":"agent","com.byjg.easyhaproxy.host.agent":"agent.quantum.local","com.byjg.easyhaproxy.localport.agent":"9001","com.byjg.easyhaproxy.mode.agent":"tcp","com.byjg.easyhaproxy.port.agent":"31339","com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"test","com.planetary-quantum":"monitoring"}
|
||||
test_proxy={"com.docker.stack.image":"byjg/easy-haproxy:local","com.docker.stack.namespace":"test"}
|
||||
23
tests/fixtures/static.yml
vendored
Normal file
23
tests/fixtures/static.yml
vendored
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
stats:
|
||||
username: admin
|
||||
password: test123
|
||||
port: 1936 # Optional (default 1936)
|
||||
|
||||
customerrors: true # Optional (default false)
|
||||
|
||||
easymapping:
|
||||
- port: 80
|
||||
hosts:
|
||||
host1.com.br: container:5000
|
||||
host2.com.br: other:3000
|
||||
redirect:
|
||||
www.host1.com.br: http://host1.com.br
|
||||
|
||||
- port: 443
|
||||
ssl_cert: BASE64_PEM_CERTIFICATE
|
||||
hosts:
|
||||
host1.com.br: container:80
|
||||
|
||||
- port: 8080
|
||||
hosts:
|
||||
host3.com.br: domain:8181
|
||||
32
tests/test_labels.py
Normal file
32
tests/test_labels.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
from .context import easymapping
|
||||
import json
|
||||
import pytest
|
||||
|
||||
def test_label_generation():
|
||||
label = easymapping.DockerLabelHandler("foo")
|
||||
|
||||
assert label.create("bar") == "foo.bar"
|
||||
assert label.create(["bar", "foobar"]) == "foo.bar.foobar"
|
||||
|
||||
|
||||
def test_label_data():
|
||||
label = easymapping.DockerLabelHandler("base")
|
||||
label.set_data(json.loads('{"base.definitions":"h2"}'))
|
||||
|
||||
label_name = label.create("definitions")
|
||||
assert label_name == "base.definitions"
|
||||
assert label.has_label(label_name)
|
||||
assert label.get(label_name) == "h2"
|
||||
|
||||
|
||||
def test_label_complex_key():
|
||||
label = easymapping.DockerLabelHandler("till")
|
||||
|
||||
data = dict()
|
||||
data["till.definitions"] = "h2"
|
||||
data["till.host.h2"] = "fqdn.example.org"
|
||||
data["till.mode.h2"] = "tcp"
|
||||
label.set_data(json.loads(json.dumps(data)))
|
||||
|
||||
assert label.get(label.create(["host", "h2"])) == "fqdn.example.org"
|
||||
assert label.get(label.create(["mode", "h2"])) == "tcp"
|
||||
70
tests/test_parser.py
Normal file
70
tests/test_parser.py
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
from .context import easymapping
|
||||
import pytest
|
||||
import os
|
||||
import yaml
|
||||
|
||||
|
||||
def load_fixture(file):
|
||||
path = os.path.dirname(os.path.realpath(__file__))
|
||||
with open(path + "/fixtures/" + file, 'r') as content_file:
|
||||
lineList = content_file.readlines()
|
||||
|
||||
return lineList
|
||||
|
||||
|
||||
def test_parser_doesnt_crash():
|
||||
lineList = load_fixture("no-services")
|
||||
|
||||
result = {
|
||||
"customerrors": False
|
||||
}
|
||||
|
||||
cfg = easymapping.HaproxyConfigGenerator(result)
|
||||
haproxy_config = cfg.generate(lineList)
|
||||
assert len(haproxy_config) > 0
|
||||
assert "frontend" not in haproxy_config
|
||||
assert "backend" not in haproxy_config
|
||||
|
||||
|
||||
def test_parser_finds_services():
|
||||
lineList = load_fixture("services")
|
||||
|
||||
result = {
|
||||
"customerrors": False
|
||||
}
|
||||
|
||||
cfg = easymapping.HaproxyConfigGenerator(result)
|
||||
haproxy_config = cfg.generate(lineList)
|
||||
assert len(haproxy_config) > 0
|
||||
assert "mode tcp" in haproxy_config
|
||||
assert "mode http" in haproxy_config
|
||||
|
||||
assert "frontend tcp_in_31339_1" in haproxy_config
|
||||
assert "frontend http_in_31337_2" in haproxy_config
|
||||
|
||||
|
||||
def test_parser_static():
|
||||
path = os.path.dirname(os.path.realpath(__file__))
|
||||
with open(path + "/fixtures/static.yml", 'r') as content_file:
|
||||
parsed = yaml.load(content_file.read(), Loader=yaml.FullLoader)
|
||||
|
||||
cfg = easymapping.HaproxyConfigGenerator(parsed)
|
||||
haproxy_config = cfg.generate()
|
||||
assert len(haproxy_config) > 0
|
||||
|
||||
# assert on auth on stats
|
||||
assert "stats auth admin:test123" in haproxy_config
|
||||
|
||||
# assert that we found redirect
|
||||
assert "redirect prefix http://host1.com.br code 301 if { hdr(host) -i www.host1.com.br }" in haproxy_config
|
||||
|
||||
# assert that we found the services
|
||||
assert "frontend http_in_80_1" in haproxy_config
|
||||
assert "bind *:80"
|
||||
assert "frontend http_in_443_2" in haproxy_config
|
||||
assert "bind *:443"
|
||||
assert "frontend http_in_8080_3" in haproxy_config
|
||||
assert "bind :*8080"
|
||||
|
||||
# verify ssl config
|
||||
assert "frontend http_in_443_2\n bind *:443 ssl crt BASE64_PEM_CERTIFICATE" in haproxy_config
|
||||
Loading…
Add table
Add a link
Reference in a new issue