From d2786296713de9f38b8c90f5e595e8ed2149f663 Mon Sep 17 00:00:00 2001 From: Joao M Date: Fri, 12 Aug 2022 01:00:18 +0000 Subject: [PATCH] Organizing code to refactory labels --- .vscode/launch.json | 19 +++++++ easymapping/__init__.py | 10 ++-- tests/test_parser.py | 113 +++++++++++++++++++++++++++++++++++----- 3 files changed, 124 insertions(+), 18 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..428608e --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,19 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal", + "cwd": "${workspaceFolder}", + "env": { + "PYTHONPATH": "${cwd}" + } + } + ] +} \ No newline at end of file diff --git a/easymapping/__init__.py b/easymapping/__init__.py index 7a00b43..ce616a1 100644 --- a/easymapping/__init__.py +++ b/easymapping/__init__.py @@ -41,10 +41,10 @@ class HaproxyConfigGenerator: os.makedirs(self.ssl_cert_folder, exist_ok=True) - def generate(self, lineList = []): + def generate(self, line_list = []): # static? - if len(lineList) > 0: - self.mapping["easymapping"] = self.__parse(lineList) + if len(line_list) > 0: + self.mapping["easymapping"] = self.parse(line_list) else: for d in self.mapping["easymapping"]: for name, hosts in d.get('hosts', {}).items(): @@ -63,10 +63,10 @@ class HaproxyConfigGenerator: return template.render(data=self.mapping) - def __parse(self, lineList): + def parse(self, line_list): easymapping = dict() - for line in lineList: + for line in line_list: line = line.strip() i = line.find("=") container = line[:i] diff --git a/tests/test_parser.py b/tests/test_parser.py index 465316d..85f926b 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -1,4 +1,4 @@ -from .context import easymapping +import easymapping import pytest import os import yaml @@ -7,20 +7,20 @@ 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() + line_list = content_file.readlines() - return lineList + return line_list def test_parser_doesnt_crash(): - lineList = load_fixture("no-services") + line_list = load_fixture("no-services") result = { "customerrors": False } cfg = easymapping.HaproxyConfigGenerator(result, "/tmp") - haproxy_config = cfg.generate(lineList) + haproxy_config = cfg.generate(line_list) assert len(haproxy_config) > 0 path = os.path.dirname(os.path.realpath(__file__)) @@ -29,7 +29,7 @@ def test_parser_doesnt_crash(): def test_parser_finds_services(): - lineList = load_fixture("services") + line_list = load_fixture("services") result = { "customerrors": False @@ -40,7 +40,7 @@ def test_parser_finds_services(): os.remove(cert_file) cfg = easymapping.HaproxyConfigGenerator(result, "/tmp") - haproxy_config = cfg.generate(lineList) + haproxy_config = cfg.generate(line_list) assert len(haproxy_config) > 0 path = os.path.dirname(os.path.realpath(__file__)) @@ -51,7 +51,7 @@ def test_parser_finds_services(): assert expected_file.read() == "Some PEM Certificate" def test_parser_finds_services_changed_label(): - lineList = load_fixture("services-changed-label") + line_list = load_fixture("services-changed-label") result = { "customerrors": False, @@ -63,7 +63,7 @@ def test_parser_finds_services_changed_label(): os.remove(cert_file) cfg = easymapping.HaproxyConfigGenerator(result, "/tmp") - haproxy_config = cfg.generate(lineList) + haproxy_config = cfg.generate(line_list) assert len(haproxy_config) > 0 path = os.path.dirname(os.path.realpath(__file__)) @@ -73,6 +73,91 @@ def test_parser_finds_services_changed_label(): with open(cert_file, 'r') as expected_file: assert expected_file.read() == "Some PEM Certificate" +def test_parser_finds_services_raw(): + line_list = load_fixture("services") + + result = { + "customerrors": False + } + + cert_file = "/tmp/www.somehost.com.br.1.pem" + if os.path.exists(cert_file): + os.remove(cert_file) + + cfg = easymapping.HaproxyConfigGenerator(result, "/tmp") + + parsed_object = [ + { + "mode":"tcp", + "health-check":"", + "port":"31339", + "hosts":{ + "agent.quantum.example.org":[ + "my-stack_agent:9001" + ] + }, + "redirect":{ + + } + }, + { + "mode":"http", + "health-check":"", + "port":"31337", + "hosts":{ + "cadvisor.quantum.example.org":[ + "my-stack_cadvisor:8080" + ], + "node-exporter.quantum.example.org":[ + "my-stack_node-exporter:9100" + ] + }, + "redirect":{ + + } + }, + { + "mode":"http", + "health-check":"", + "port":"80", + "hosts":{ + "www.somehost.com.br":[ + "some-service:80" + ] + }, + "redirect":{ + "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" + } + }, + { + "mode":"http", + "health-check":"", + "port":"443", + "hosts":{ + "www.somehost.com.br":[ + "some-service:80" + ] + }, + "redirect":{ + "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" + }, + "ssl_cert":"/tmp/www.somehost.com.br.1.pem" + } + ] + + processed = list(cfg.parse(line_list)) + + assert parsed_object == processed + + def test_parser_static(): path = os.path.dirname(os.path.realpath(__file__)) @@ -88,14 +173,14 @@ def test_parser_static(): def test_parser_tcp(): - lineList = load_fixture("services-tcp") + line_list = load_fixture("services-tcp") result = { "customerrors": False } cfg = easymapping.HaproxyConfigGenerator(result, "/tmp") - haproxy_config = cfg.generate(lineList) + haproxy_config = cfg.generate(line_list) # print(haproxy_config) assert len(haproxy_config) > 0 @@ -104,17 +189,19 @@ def test_parser_tcp(): assert expected_file.read() == haproxy_config def test_parser_multi_containers(): - lineList = load_fixture("services-multi-containers") + line_list = load_fixture("services-multi-containers") result = { "customerrors": False } cfg = easymapping.HaproxyConfigGenerator(result, "/tmp") - haproxy_config = cfg.generate(lineList) + haproxy_config = cfg.generate(line_list) assert len(haproxy_config) > 0 path = os.path.dirname(os.path.realpath(__file__)) with open(path + "/expected/services-multi-containers.txt", 'r') as expected_file: assert expected_file.read() == haproxy_config + +#test_parser_finds_services_raw()