From c6bef882ca26a9e26240370fa3ecc3c86f86ff45 Mon Sep 17 00:00:00 2001 From: Joao M Date: Thu, 11 Aug 2022 22:18:28 +0000 Subject: [PATCH] Add unit tests --- .gitpod.yml | 12 ++++++++++++ README.md | 2 -- tests/fixtures/services-changed-label | 6 ++++++ tests/test_parser.py | 23 +++++++++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 .gitpod.yml create mode 100644 tests/fixtures/services-changed-label diff --git a/.gitpod.yml b/.gitpod.yml new file mode 100644 index 0000000..871d512 --- /dev/null +++ b/.gitpod.yml @@ -0,0 +1,12 @@ +# This configuration file was automatically generated by Gitpod. +# Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file) +# and commit this file to your remote git repository to share the goodness with others. + +tasks: + - init: |- + make + virtualenv -p /usr/bin/python3 venv + source venv/bin/activate + pip install -r requirements.txt + + diff --git a/README.md b/README.md index 59655bd..a10f5fa 100644 --- a/README.md +++ b/README.md @@ -170,8 +170,6 @@ stats: password: password port: 1936 # Optional (default 1936) -lookup_label: easyhaproxy # The key the system will try to find in the docker labels. - customerrors: true # Optional (default false) easymapping: diff --git a/tests/fixtures/services-changed-label b/tests/fixtures/services-changed-label new file mode 100644 index 0000000..983d431 --- /dev/null +++ b/tests/fixtures/services-changed-label @@ -0,0 +1,6 @@ +portainer-agent_agent={"com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"portainer-agent"} +my-stack_agent={"haproxy.definitions":"agent","haproxy.host.agent":"agent.quantum.example.org","haproxy.localport.agent":"9001","haproxy.mode.agent":"tcp","haproxy.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={"haproxy.definitions":"cadvisor","haproxy.host.cadvisor":"cadvisor.quantum.example.org","haproxy.localport.cadvisor":"8080","haproxy.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={"haproxy.definitions":"exp","haproxy.host.exp":"node-exporter.quantum.example.org","haproxy.localport.exp":"9100","haproxy.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"} +some-service={"haproxy.definitions":"http,https","haproxy.port.http":"80","haproxy.host.http":"www.somehost.com.br","haproxy.localport.http":"80","haproxy.redirect.http":"somehost.com.br--https://www.somehost.com.br,somehost.com--https://www.somehost.com.br,www.somehost.com--https://www.somehost.com.br,byjg.ca--https://www.somehost.com.br,www.byjg.ca--https://www.somehost.com.br","haproxy.port.https":"443","haproxy.host.https":"www.somehost.com.br","haproxy.localport.https":"80","haproxy.redirect.https":"somehost.com.br--https://www.somehost.com.br,somehost.com--https://www.somehost.com.br,www.somehost.com--https://www.somehost.com.br,byjg.ca--https://www.somehost.com.br,www.byjg.ca--https://www.somehost.com.br","haproxy.sslcert.https":"U29tZSBQRU0gQ2VydGlmaWNhdGU="} \ No newline at end of file diff --git a/tests/test_parser.py b/tests/test_parser.py index f80ac4a..465316d 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -50,6 +50,29 @@ def test_parser_finds_services(): with open(cert_file, 'r') as expected_file: assert expected_file.read() == "Some PEM Certificate" +def test_parser_finds_services_changed_label(): + lineList = load_fixture("services-changed-label") + + result = { + "customerrors": False, + "lookup_label": "haproxy" + } + + cert_file = "/tmp/www.somehost.com.br.1.pem" + if os.path.exists(cert_file): + os.remove(cert_file) + + cfg = easymapping.HaproxyConfigGenerator(result, "/tmp") + haproxy_config = cfg.generate(lineList) + + assert len(haproxy_config) > 0 + path = os.path.dirname(os.path.realpath(__file__)) + with open(path + "/expected/services.txt", 'r') as expected_file: + assert expected_file.read() == haproxy_config + + with open(cert_file, 'r') as expected_file: + assert expected_file.read() == "Some PEM Certificate" + def test_parser_static(): path = os.path.dirname(os.path.realpath(__file__))