|
| 1 | +# ========================================================================= |
| 2 | +# Copyright 2012-present Yunify, Inc. |
| 3 | +# ------------------------------------------------------------------------- |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this work except in compliance with the License. |
| 6 | +# You may obtain a copy of the License in the LICENSE file, or at: |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# ========================================================================= |
| 16 | + |
| 17 | +import unittest |
| 18 | + |
| 19 | +from qingcloud.iaas.lb_listener import LoadBalancerListener |
| 20 | + |
| 21 | +class LoadBalancerListenerTestCase(unittest.TestCase): |
| 22 | + |
| 23 | + def test_init_instance(self): |
| 24 | + port = 80 |
| 25 | + protocol = 'http' |
| 26 | + listener = LoadBalancerListener(port, listener_protocol=protocol, |
| 27 | + backend_protocol=protocol) |
| 28 | + json = listener.to_json() |
| 29 | + self.assertEqual(json['listener_port'], port) |
| 30 | + self.assertEqual(json['listener_protocol'], protocol) |
| 31 | + |
| 32 | + def test_init_forwardfor(self): |
| 33 | + port = 80 |
| 34 | + protocol = 'http' |
| 35 | + listener = LoadBalancerListener(port, listener_protocol=protocol, |
| 36 | + backend_protocol=protocol, forwardfor=1) |
| 37 | + json = listener.to_json() |
| 38 | + self.assertEqual(json['forwardfor'], 1) |
| 39 | + |
| 40 | + listener = LoadBalancerListener(port, listener_protocol=protocol, |
| 41 | + backend_protocol=protocol, headers=['QC-LBIP']) |
| 42 | + json = listener.to_json() |
| 43 | + self.assertEqual(json['forwardfor'], 4) |
| 44 | + |
| 45 | + listener = LoadBalancerListener(port, listener_protocol=protocol, |
| 46 | + backend_protocol=protocol, forwardfor=1, headers=['QC-LBIP']) |
| 47 | + json = listener.to_json() |
| 48 | + self.assertEqual(json['forwardfor'], 1) |
| 49 | + |
| 50 | + def test_get_forwardfor(self): |
| 51 | + headers = [] |
| 52 | + self.assertEqual(LoadBalancerListener.get_forwardfor(headers), 0) |
| 53 | + headers = ['wrong_header'] |
| 54 | + self.assertEqual(LoadBalancerListener.get_forwardfor(headers), 0) |
| 55 | + headers = ['X-FORWARD-FOR'] |
| 56 | + self.assertEqual(LoadBalancerListener.get_forwardfor(headers), 1) |
| 57 | + headers = ['QC-LBID'] |
| 58 | + self.assertEqual(LoadBalancerListener.get_forwardfor(headers), 2) |
| 59 | + headers = ['QC-LBIP'] |
| 60 | + self.assertEqual(LoadBalancerListener.get_forwardfor(headers), 4) |
| 61 | + headers = ['X-FORWARD-FOR', 'QC-LBID'] |
| 62 | + self.assertEqual(LoadBalancerListener.get_forwardfor(headers), 3) |
| 63 | + headers = ['X-FORWARD-FOR', 'QC-LBIP', 'QC-LBID'] |
| 64 | + self.assertEqual(LoadBalancerListener.get_forwardfor(headers), 7) |
| 65 | + |
| 66 | + def test_create_from_string(self): |
| 67 | + string = ''' |
| 68 | + [{"forwardfor":0,"loadbalancer_listener_id":"lbl-1234abcd", |
| 69 | + "balance_mode":"roundrobin","listener_protocol":"tcp", |
| 70 | + "backend_protocol":"tcp","healthy_check_method":"tcp", |
| 71 | + "session_sticky":"","loadbalancer_listener_name":"demo", |
| 72 | + "controller":"self","backends":[],"create_time":"2014-02-02T16:51:25Z", |
| 73 | + "healthy_check_option":"10|5|2|5","owner":"usr-1234abcd", |
| 74 | + "console_id":"qingcloud","loadbalancer_id":"lb-1234abcd", |
| 75 | + "listener_port":443}, |
| 76 | + {"forwardfor":0, |
| 77 | + "loadbalancer_listener_id":"lbl-1234abcd","balance_mode":"roundrobin", |
| 78 | + "listener_protocol":"http","backend_protocol":"http", |
| 79 | + "healthy_check_method":"tcp","session_sticky":"", |
| 80 | + "loadbalancer_listener_name":"demo","controller":"self", |
| 81 | + "backends":[],"create_time":"2014-02-02T16:51:19Z", |
| 82 | + "healthy_check_option":"10|5|2|5","owner":"usr-1234abcd", |
| 83 | + "console_id":"qingcloud","loadbalancer_id":"lb-1234abcd", |
| 84 | + "listener_port":80}] |
| 85 | + ''' |
| 86 | + listeners = LoadBalancerListener.create_from_string(string) |
| 87 | + self.assertEqual(len(listeners), 2) |
0 commit comments