1 # ============LICENSE_START=======================================================
3 # ================================================================================
4 # Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
21 def test_parse_interval():
22 from k8sclient.k8sclient import _parse_interval
24 good_intervals = [{"in": input, "ex": expected}
25 for (input, expected) in [
31 ("24h", 24 * 60 * 60),
35 (1234567890123456789012345678901234567890L,1234567890123456789012345678901234567890L),
36 ("1234567890123456789012345678901234567890",1234567890123456789012345678901234567890L),
37 ("1234567890123456789012345678901234567890s",1234567890123456789012345678901234567890L),
39 ("00000000000000000000000000000000005m", 5 * 60)
64 "i want an interval of 30s",
81 for test_case in good_intervals:
82 assert _parse_interval(test_case["in"]) == test_case["ex"]
84 for interval in bad_intervals:
85 with pytest.raises(ValueError):
86 _parse_interval(interval)
88 def test_parse_ports():
89 from k8sclient.k8sclient import _parse_ports
91 good_ports = [{"in": input, "ex": expected}
92 for (input, expected) in [
93 ("9101:0", (9101, 0, "TCP")),
94 ("9101/TCP:0", (9101, 0, "TCP")),
95 ("9101/tcp:0", (9101, 0, "TCP")),
96 ("9101/UDP:0", (9101, 0, "UDP")),
97 ("9101/udp:0", (9101, 0, "UDP")),
98 ("9101:31043", (9101, 31043, "TCP")),
99 ("9101/TCP:31043", (9101, 31043, "TCP")),
100 ("9101/tcp:31043", (9101, 31043, "TCP")),
101 ("9101/UDP:31043", (9101, 31043, "UDP")),
102 ("9101/udp:31043", (9101, 31043, "UDP"))
126 expected_port_map = {
130 (9661,"TCP") : 19661,
131 (9661,"UDP") : 19661,
135 for test_case in good_ports:
136 container_ports, port_map = _parse_ports([test_case["in"]])
137 (cport, hport, proto) = test_case["ex"]
138 assert container_ports == [(cport, proto)]
139 assert port_map == {(cport, proto) : hport}
141 for port in bad_ports:
142 with pytest.raises(ValueError):
145 container_ports, port_map = _parse_ports(port_list)
146 assert port_map == expected_port_map
148 def test_create_container():
149 from k8sclient.k8sclient import _create_container_object
150 from kubernetes import client
152 container = _create_container_object("c1","nginx",False, container_ports=[(80, "TCP"), (53, "UDP")])
154 assert container.ports[0].container_port == 80 and container.ports[0].protocol == "TCP"
155 assert container.ports[1].container_port == 53 and container.ports[1].protocol == "UDP"