17
17
import unittest
18
18
19
19
from .config_exception import ConfigException
20
- from .incluster_config import (_SERVICE_HOST_ENV_NAME , _SERVICE_PORT_ENV_NAME ,
21
- InClusterConfigLoader )
20
+ from .incluster_config import (SERVICE_HOST_ENV_NAME , SERVICE_PORT_ENV_NAME ,
21
+ InClusterConfigLoader , _join_host_port )
22
22
23
23
_TEST_TOKEN = "temp_token"
24
+ _TEST_CERT = "temp_cert"
24
25
_TEST_HOST = "127.0.0.1"
25
- _TEST_IPV6_HOST = "::1"
26
26
_TEST_PORT = "80"
27
- _TEST_ENVIRON = {_SERVICE_HOST_ENV_NAME : _TEST_HOST ,
28
- _SERVICE_PORT_ENV_NAME : _TEST_PORT }
29
- _TEST_IPV6_ENVIRON = {_SERVICE_HOST_ENV_NAME : _TEST_IPV6_HOST ,
30
- _SERVICE_PORT_ENV_NAME : _TEST_PORT }
27
+ _TEST_HOST_PORT = "127.0.0.1:80"
28
+ _TEST_IPV6_HOST = "::1"
29
+ _TEST_IPV6_HOST_PORT = "[::1]:80"
30
+
31
+ _TEST_ENVIRON = {SERVICE_HOST_ENV_NAME : _TEST_HOST ,
32
+ SERVICE_PORT_ENV_NAME : _TEST_PORT }
33
+ _TEST_IPV6_ENVIRON = {SERVICE_HOST_ENV_NAME : _TEST_IPV6_HOST ,
34
+ SERVICE_PORT_ENV_NAME : _TEST_PORT }
31
35
32
36
33
37
class InClusterConfigTest (unittest .TestCase ):
@@ -48,38 +52,29 @@ def _create_file_with_temp_content(self, content=""):
48
52
49
53
def get_test_loader (
50
54
self ,
51
- host_env_name = _SERVICE_HOST_ENV_NAME ,
52
- port_env_name = _SERVICE_PORT_ENV_NAME ,
53
55
token_filename = None ,
54
56
cert_filename = None ,
55
57
environ = _TEST_ENVIRON ):
56
58
if not token_filename :
57
59
token_filename = self ._create_file_with_temp_content (_TEST_TOKEN )
58
60
if not cert_filename :
59
- cert_filename = self ._create_file_with_temp_content ()
61
+ cert_filename = self ._create_file_with_temp_content (_TEST_CERT )
60
62
return InClusterConfigLoader (
61
- host_env_name = host_env_name ,
62
- port_env_name = port_env_name ,
63
63
token_filename = token_filename ,
64
64
cert_filename = cert_filename ,
65
65
environ = environ )
66
66
67
+ def test_join_host_port (self ):
68
+ self .assertEqual (_TEST_HOST_PORT ,
69
+ _join_host_port (_TEST_HOST , _TEST_PORT ))
70
+ self .assertEqual (_TEST_IPV6_HOST_PORT ,
71
+ _join_host_port (_TEST_IPV6_HOST , _TEST_PORT ))
72
+
67
73
def test_load_config (self ):
68
- cert_filename = self ._create_file_with_temp_content ()
74
+ cert_filename = self ._create_file_with_temp_content (_TEST_CERT )
69
75
loader = self .get_test_loader (cert_filename = cert_filename )
70
76
loader ._load_config ()
71
- self .assertEqual ("https://%s:%s" % (_TEST_HOST , _TEST_PORT ),
72
- loader .host )
73
- self .assertEqual (cert_filename , loader .ssl_ca_cert )
74
- self .assertEqual (_TEST_TOKEN , loader .token )
75
-
76
- def test_load_config_with_bracketed_hostname (self ):
77
- cert_filename = self ._create_file_with_temp_content ()
78
- loader = self .get_test_loader (cert_filename = cert_filename ,
79
- environ = _TEST_IPV6_ENVIRON )
80
- loader ._load_config ()
81
- self .assertEqual ("https://[%s]:%s" % (_TEST_IPV6_HOST , _TEST_PORT ),
82
- loader .host )
77
+ self .assertEqual ("https://" + _TEST_HOST_PORT , loader .host )
83
78
self .assertEqual (cert_filename , loader .ssl_ca_cert )
84
79
self .assertEqual (_TEST_TOKEN , loader .token )
85
80
@@ -92,21 +87,45 @@ def _should_fail_load(self, config_loader, reason):
92
87
pass
93
88
94
89
def test_no_port (self ):
95
- loader = self .get_test_loader (port_env_name = "not_exists_port" )
90
+ loader = self .get_test_loader (
91
+ environ = {SERVICE_HOST_ENV_NAME : _TEST_HOST })
96
92
self ._should_fail_load (loader , "no port specified" )
97
93
94
+ def test_empty_port (self ):
95
+ loader = self .get_test_loader (
96
+ environ = {SERVICE_HOST_ENV_NAME : _TEST_HOST ,
97
+ SERVICE_PORT_ENV_NAME : "" })
98
+ self ._should_fail_load (loader , "empty port specified" )
99
+
98
100
def test_no_host (self ):
99
- loader = self .get_test_loader (host_env_name = "not_exists_host" )
101
+ loader = self .get_test_loader (
102
+ environ = {SERVICE_PORT_ENV_NAME : _TEST_PORT })
100
103
self ._should_fail_load (loader , "no host specified" )
101
104
105
+ def test_empty_host (self ):
106
+ loader = self .get_test_loader (
107
+ environ = {SERVICE_HOST_ENV_NAME : "" ,
108
+ SERVICE_PORT_ENV_NAME : _TEST_PORT })
109
+ self ._should_fail_load (loader , "empty host specified" )
110
+
102
111
def test_no_cert_file (self ):
103
112
loader = self .get_test_loader (cert_filename = "not_exists_file_1123" )
104
113
self ._should_fail_load (loader , "cert file does not exists" )
105
114
115
+ def test_empty_cert_file (self ):
116
+ loader = self .get_test_loader (
117
+ cert_filename = self ._create_file_with_temp_content ())
118
+ self ._should_fail_load (loader , "empty cert file provided" )
119
+
106
120
def test_no_token_file (self ):
107
121
loader = self .get_test_loader (token_filename = "not_exists_file_1123" )
108
122
self ._should_fail_load (loader , "token file does not exists" )
109
123
124
+ def test_empty_token_file (self ):
125
+ loader = self .get_test_loader (
126
+ token_filename = self ._create_file_with_temp_content ())
127
+ self ._should_fail_load (loader , "empty token file provided" )
128
+
110
129
111
130
if __name__ == '__main__' :
112
131
unittest .main ()
0 commit comments