From 50af051a3344b7be2776c0cec9aef139bb70a1fb Mon Sep 17 00:00:00 2001 From: Joao Gilberto Magalhaes Date: Sun, 27 Mar 2022 18:52:52 -0500 Subject: [PATCH] Added tests for multi-container --- .github/workflows/build.yml | 2 +- .../docker-compose-multi-containers.yml | 2 +- tests/expected/services-multi-containers.txt | 29 +++++++++++++++++++ tests/fixtures/services-multi-containers | 2 ++ tests/test_parser.py | 16 ++++++++++ 5 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 tests/expected/services-multi-containers.txt create mode 100644 tests/fixtures/services-multi-containers diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e136c5f..6abd00c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -27,7 +27,7 @@ jobs: uses: actions/checkout@v2 - run: pip install -r requirements.txt - - run: pytest -s tests/ + - run: pytest -s tests/ -vv Build: runs-on: ubuntu-latest diff --git a/examples/docker/docker-compose-multi-containers.yml b/examples/docker/docker-compose-multi-containers.yml index ee180ea..cb0df8f 100644 --- a/examples/docker/docker-compose-multi-containers.yml +++ b/examples/docker/docker-compose-multi-containers.yml @@ -19,7 +19,7 @@ services: #image: nginx image: stenote/nginx-hostname deploy: - replicas: 2 + replicas: 2 labels: easyhaproxy.definitions: "http" diff --git a/tests/expected/services-multi-containers.txt b/tests/expected/services-multi-containers.txt new file mode 100644 index 0000000..3afa9ef --- /dev/null +++ b/tests/expected/services-multi-containers.txt @@ -0,0 +1,29 @@ +global + log stdout format raw local0 info + maxconn 2000 + tune.ssl.default-dh-param 2048 + +defaults + log global + + timeout connect 3s + timeout client 10s + timeout server 10m + + +frontend http_in_19901_1 + bind *:19901 + mode http + + acl is_rule_www_helloworld_com_19901_1_1 hdr(host) -i www.helloworld.com + acl is_rule_www_helloworld_com_19901_1_2 hdr(host) -i www.helloworld.com:19901 + use_backend srv_www_helloworld_com_19901_1 if is_rule_www_helloworld_com_19901_1_1 OR is_rule_www_helloworld_com_19901_1_2 + +backend srv_www_helloworld_com_19901_1 + balance roundrobin + mode http + option forwardfor + http-request set-header X-Forwarded-Port %[dst_port] + http-request add-header X-Forwarded-Proto https if { ssl_fc } + server srv-0 test_nginx.2.t5r94mjlced7m3t5orfjbowmm:80 check weight 1 + server srv-1 test_nginx.1.p552hqxkdx88narjrp5kouwb2:80 check weight 1 diff --git a/tests/fixtures/services-multi-containers b/tests/fixtures/services-multi-containers new file mode 100644 index 0000000..c9b7de7 --- /dev/null +++ b/tests/fixtures/services-multi-containers @@ -0,0 +1,2 @@ +test_nginx.2.t5r94mjlced7m3t5orfjbowmm={"easyhaproxy.definitions":"http","easyhaproxy.host.http":"www.helloworld.com","easyhaproxy.localport.http":"80","easyhaproxy.port.http":"19901","com.docker.stack.image":"stenote/nginx-hostname","com.docker.stack.namespace":"test"} +test_nginx.1.p552hqxkdx88narjrp5kouwb2={"easyhaproxy.definitions":"http","easyhaproxy.host.http":"www.helloworld.com","easyhaproxy.localport.http":"80","easyhaproxy.port.http":"19901","com.docker.stack.image":"stenote/nginx-hostname","com.docker.stack.namespace":"test"} \ No newline at end of file diff --git a/tests/test_parser.py b/tests/test_parser.py index 4f076be..f80ac4a 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -79,3 +79,19 @@ def test_parser_tcp(): path = os.path.dirname(os.path.realpath(__file__)) with open(path + "/expected/services-tcp.txt", 'r') as expected_file: assert expected_file.read() == haproxy_config + +def test_parser_multi_containers(): + lineList = load_fixture("services-multi-containers") + + result = { + "customerrors": False + } + + 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-multi-containers.txt", 'r') as expected_file: + assert expected_file.read() == haproxy_config +