Organizing code to refactory labels
This commit is contained in:
parent
e89a329781
commit
d278629671
3 changed files with 124 additions and 18 deletions
19
.vscode/launch.json
vendored
Normal file
19
.vscode/launch.json
vendored
Normal file
|
|
@ -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}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
@ -41,10 +41,10 @@ class HaproxyConfigGenerator:
|
||||||
os.makedirs(self.ssl_cert_folder, exist_ok=True)
|
os.makedirs(self.ssl_cert_folder, exist_ok=True)
|
||||||
|
|
||||||
|
|
||||||
def generate(self, lineList = []):
|
def generate(self, line_list = []):
|
||||||
# static?
|
# static?
|
||||||
if len(lineList) > 0:
|
if len(line_list) > 0:
|
||||||
self.mapping["easymapping"] = self.__parse(lineList)
|
self.mapping["easymapping"] = self.parse(line_list)
|
||||||
else:
|
else:
|
||||||
for d in self.mapping["easymapping"]:
|
for d in self.mapping["easymapping"]:
|
||||||
for name, hosts in d.get('hosts', {}).items():
|
for name, hosts in d.get('hosts', {}).items():
|
||||||
|
|
@ -63,10 +63,10 @@ class HaproxyConfigGenerator:
|
||||||
return template.render(data=self.mapping)
|
return template.render(data=self.mapping)
|
||||||
|
|
||||||
|
|
||||||
def __parse(self, lineList):
|
def parse(self, line_list):
|
||||||
easymapping = dict()
|
easymapping = dict()
|
||||||
|
|
||||||
for line in lineList:
|
for line in line_list:
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
i = line.find("=")
|
i = line.find("=")
|
||||||
container = line[:i]
|
container = line[:i]
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
from .context import easymapping
|
import easymapping
|
||||||
import pytest
|
import pytest
|
||||||
import os
|
import os
|
||||||
import yaml
|
import yaml
|
||||||
|
|
@ -7,20 +7,20 @@ import yaml
|
||||||
def load_fixture(file):
|
def load_fixture(file):
|
||||||
path = os.path.dirname(os.path.realpath(__file__))
|
path = os.path.dirname(os.path.realpath(__file__))
|
||||||
with open(path + "/fixtures/" + file, 'r') as content_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():
|
def test_parser_doesnt_crash():
|
||||||
lineList = load_fixture("no-services")
|
line_list = load_fixture("no-services")
|
||||||
|
|
||||||
result = {
|
result = {
|
||||||
"customerrors": False
|
"customerrors": False
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg = easymapping.HaproxyConfigGenerator(result, "/tmp")
|
cfg = easymapping.HaproxyConfigGenerator(result, "/tmp")
|
||||||
haproxy_config = cfg.generate(lineList)
|
haproxy_config = cfg.generate(line_list)
|
||||||
|
|
||||||
assert len(haproxy_config) > 0
|
assert len(haproxy_config) > 0
|
||||||
path = os.path.dirname(os.path.realpath(__file__))
|
path = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
|
@ -29,7 +29,7 @@ def test_parser_doesnt_crash():
|
||||||
|
|
||||||
|
|
||||||
def test_parser_finds_services():
|
def test_parser_finds_services():
|
||||||
lineList = load_fixture("services")
|
line_list = load_fixture("services")
|
||||||
|
|
||||||
result = {
|
result = {
|
||||||
"customerrors": False
|
"customerrors": False
|
||||||
|
|
@ -40,7 +40,7 @@ def test_parser_finds_services():
|
||||||
os.remove(cert_file)
|
os.remove(cert_file)
|
||||||
|
|
||||||
cfg = easymapping.HaproxyConfigGenerator(result, "/tmp")
|
cfg = easymapping.HaproxyConfigGenerator(result, "/tmp")
|
||||||
haproxy_config = cfg.generate(lineList)
|
haproxy_config = cfg.generate(line_list)
|
||||||
|
|
||||||
assert len(haproxy_config) > 0
|
assert len(haproxy_config) > 0
|
||||||
path = os.path.dirname(os.path.realpath(__file__))
|
path = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
|
@ -51,7 +51,7 @@ def test_parser_finds_services():
|
||||||
assert expected_file.read() == "Some PEM Certificate"
|
assert expected_file.read() == "Some PEM Certificate"
|
||||||
|
|
||||||
def test_parser_finds_services_changed_label():
|
def test_parser_finds_services_changed_label():
|
||||||
lineList = load_fixture("services-changed-label")
|
line_list = load_fixture("services-changed-label")
|
||||||
|
|
||||||
result = {
|
result = {
|
||||||
"customerrors": False,
|
"customerrors": False,
|
||||||
|
|
@ -63,7 +63,7 @@ def test_parser_finds_services_changed_label():
|
||||||
os.remove(cert_file)
|
os.remove(cert_file)
|
||||||
|
|
||||||
cfg = easymapping.HaproxyConfigGenerator(result, "/tmp")
|
cfg = easymapping.HaproxyConfigGenerator(result, "/tmp")
|
||||||
haproxy_config = cfg.generate(lineList)
|
haproxy_config = cfg.generate(line_list)
|
||||||
|
|
||||||
assert len(haproxy_config) > 0
|
assert len(haproxy_config) > 0
|
||||||
path = os.path.dirname(os.path.realpath(__file__))
|
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:
|
with open(cert_file, 'r') as expected_file:
|
||||||
assert expected_file.read() == "Some PEM Certificate"
|
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():
|
def test_parser_static():
|
||||||
path = os.path.dirname(os.path.realpath(__file__))
|
path = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
|
@ -88,14 +173,14 @@ def test_parser_static():
|
||||||
|
|
||||||
|
|
||||||
def test_parser_tcp():
|
def test_parser_tcp():
|
||||||
lineList = load_fixture("services-tcp")
|
line_list = load_fixture("services-tcp")
|
||||||
|
|
||||||
result = {
|
result = {
|
||||||
"customerrors": False
|
"customerrors": False
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg = easymapping.HaproxyConfigGenerator(result, "/tmp")
|
cfg = easymapping.HaproxyConfigGenerator(result, "/tmp")
|
||||||
haproxy_config = cfg.generate(lineList)
|
haproxy_config = cfg.generate(line_list)
|
||||||
# print(haproxy_config)
|
# print(haproxy_config)
|
||||||
|
|
||||||
assert len(haproxy_config) > 0
|
assert len(haproxy_config) > 0
|
||||||
|
|
@ -104,17 +189,19 @@ def test_parser_tcp():
|
||||||
assert expected_file.read() == haproxy_config
|
assert expected_file.read() == haproxy_config
|
||||||
|
|
||||||
def test_parser_multi_containers():
|
def test_parser_multi_containers():
|
||||||
lineList = load_fixture("services-multi-containers")
|
line_list = load_fixture("services-multi-containers")
|
||||||
|
|
||||||
result = {
|
result = {
|
||||||
"customerrors": False
|
"customerrors": False
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg = easymapping.HaproxyConfigGenerator(result, "/tmp")
|
cfg = easymapping.HaproxyConfigGenerator(result, "/tmp")
|
||||||
haproxy_config = cfg.generate(lineList)
|
haproxy_config = cfg.generate(line_list)
|
||||||
|
|
||||||
assert len(haproxy_config) > 0
|
assert len(haproxy_config) > 0
|
||||||
path = os.path.dirname(os.path.realpath(__file__))
|
path = os.path.dirname(os.path.realpath(__file__))
|
||||||
with open(path + "/expected/services-multi-containers.txt", 'r') as expected_file:
|
with open(path + "/expected/services-multi-containers.txt", 'r') as expected_file:
|
||||||
assert expected_file.read() == haproxy_config
|
assert expected_file.read() == haproxy_config
|
||||||
|
|
||||||
|
|
||||||
|
#test_parser_finds_services_raw()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue