|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import os |
| 19 | +import sys |
| 20 | +import unittest |
| 21 | + |
| 22 | +HEALTH_CHECKS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), |
| 23 | + "..", "debian", "root", "health_checks") |
| 24 | +sys.path.insert(0, HEALTH_CHECKS_DIR) |
| 25 | + |
| 26 | +import haproxy_check # noqa: E402 |
| 27 | + |
| 28 | + |
| 29 | +SSL_BIND_OPTIONS = ("ssl crt /etc/cloudstack/ssl/10_0_62_24-443.pem alpn h2,http/1.1 " |
| 30 | + "ssl-min-ver TLSv1.2 no-tls-tickets") |
| 31 | + |
| 32 | + |
| 33 | +def build_lb_rule(): |
| 34 | + return { |
| 35 | + "sourceIp": "10.0.62.24", |
| 36 | + "sourcePortStart": "443", |
| 37 | + "sourcePortEnd": "443", |
| 38 | + "algorithm": "roundrobin", |
| 39 | + "keepAliveEnabled": "true", |
| 40 | + "stickiness": "", |
| 41 | + "vmIps": "10.1.1.10", |
| 42 | + "destPortStart": "80", |
| 43 | + "destPortEnd": "80", |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | +def build_haproxy_section(bind_value): |
| 48 | + return { |
| 49 | + "balance": ["roundrobin"], |
| 50 | + "bind": [bind_value], |
| 51 | + "server": ["10_0_62_24-443_1 10.1.1.10:80 check"], |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | +class TestHaproxyCheckBind(unittest.TestCase): |
| 56 | + |
| 57 | + def check(self, bind_value): |
| 58 | + haCfgSections = { |
| 59 | + "listen 10_0_62_24-443": build_haproxy_section(bind_value), |
| 60 | + } |
| 61 | + return haproxy_check.checkLoadBalance([build_lb_rule()], haCfgSections) |
| 62 | + |
| 63 | + def test_plain_bind_passes(self): |
| 64 | + self.assertTrue(self.check("10.0.62.24:443")) |
| 65 | + |
| 66 | + def test_ssl_bind_with_options_passes(self): |
| 67 | + self.assertTrue(self.check("10.0.62.24:443 " + SSL_BIND_OPTIONS)) |
| 68 | + |
| 69 | + def test_ssl_bind_with_wrong_port_fails(self): |
| 70 | + self.assertFalse(self.check("10.0.62.24:4430 " + SSL_BIND_OPTIONS)) |
| 71 | + |
| 72 | + def test_ssl_bind_with_wrong_ip_fails(self): |
| 73 | + self.assertFalse(self.check("10.0.62.25:443 " + SSL_BIND_OPTIONS)) |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + unittest.main() |
0 commit comments