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