12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ import datetime
15
16
import os
16
17
import tempfile
17
- import unittest
18
- import datetime
19
18
import time
19
+ import unittest
20
20
21
21
from kubernetes .client import Configuration
22
22
33
33
_TEST_IPV6_HOST = "::1"
34
34
_TEST_IPV6_HOST_PORT = "[::1]:80"
35
35
36
- _TEST_ENVIRON = {SERVICE_HOST_ENV_NAME : _TEST_HOST ,
37
- SERVICE_PORT_ENV_NAME : _TEST_PORT }
38
- _TEST_IPV6_ENVIRON = {SERVICE_HOST_ENV_NAME : _TEST_IPV6_HOST ,
39
- SERVICE_PORT_ENV_NAME : _TEST_PORT }
36
+ _TEST_ENVIRON = {
37
+ SERVICE_HOST_ENV_NAME : _TEST_HOST ,
38
+ SERVICE_PORT_ENV_NAME : _TEST_PORT
39
+ }
40
+ _TEST_IPV6_ENVIRON = {
41
+ SERVICE_HOST_ENV_NAME : _TEST_IPV6_HOST ,
42
+ SERVICE_PORT_ENV_NAME : _TEST_PORT
43
+ }
40
44
41
45
42
46
class InClusterConfigTest (unittest .TestCase ):
43
-
44
47
def setUp (self ):
45
48
self ._temp_files = []
46
49
@@ -55,25 +58,18 @@ def _create_file_with_temp_content(self, content=""):
55
58
os .close (handler )
56
59
return name
57
60
58
- def _overwrite_file_with_content (self , name , content = "" ):
59
- handler = os .open (name , os .O_RDWR )
60
- os .truncate (name , 0 )
61
- os .write (handler , str .encode (content ))
62
- os .close (handler )
63
-
64
- def get_test_loader (
65
- self ,
66
- token_filename = None ,
67
- cert_filename = None ,
68
- environ = _TEST_ENVIRON ):
61
+ def get_test_loader (self ,
62
+ token_filename = None ,
63
+ cert_filename = None ,
64
+ environ = _TEST_ENVIRON ):
69
65
if not token_filename :
70
66
token_filename = self ._create_file_with_temp_content (_TEST_TOKEN )
71
67
if not cert_filename :
72
68
cert_filename = self ._create_file_with_temp_content (_TEST_CERT )
73
- return InClusterConfigLoader (
74
- token_filename = token_filename ,
75
- cert_filename = cert_filename ,
76
- environ = environ )
69
+ return InClusterConfigLoader (token_filename = token_filename ,
70
+ cert_filename = cert_filename ,
71
+ try_refresh_token = True ,
72
+ environ = environ )
77
73
78
74
def test_join_host_port (self ):
79
75
self .assertEqual (_TEST_HOST_PORT ,
@@ -87,25 +83,29 @@ def test_load_config(self):
87
83
loader ._load_config ()
88
84
self .assertEqual ("https://" + _TEST_HOST_PORT , loader .host )
89
85
self .assertEqual (cert_filename , loader .ssl_ca_cert )
90
- self .assertEqual (_TEST_TOKEN , loader .token )
86
+ self .assertEqual ('bearer ' + _TEST_TOKEN , loader .token )
91
87
92
88
def test_refresh_token (self ):
93
89
loader = self .get_test_loader ()
94
- loader ._token_refresh_period = datetime .timedelta (seconds = 5 )
95
- loader .load_and_set ()
96
90
config = Configuration ()
91
+ loader .load_and_set (config )
97
92
98
- self .assertEqual ('bearer ' + _TEST_TOKEN , config .get_api_key_with_prefix ('authorization' ))
99
- self .assertEqual (_TEST_TOKEN , loader .token )
93
+ self .assertEqual ('bearer ' + _TEST_TOKEN ,
94
+ config .get_api_key_with_prefix ('authorization' ))
95
+ self .assertEqual ('bearer ' + _TEST_TOKEN , loader .token )
100
96
self .assertIsNotNone (loader .token_expires_at )
101
97
102
98
old_token = loader .token
103
99
old_token_expires_at = loader .token_expires_at
104
- self ._overwrite_file_with_content (loader ._token_filename , _TEST_NEW_TOKEN )
105
- time .sleep (5 )
106
-
107
- self .assertEqual ('bearer ' + _TEST_NEW_TOKEN , config .get_api_key_with_prefix ('authorization' ))
108
- self .assertEqual (_TEST_NEW_TOKEN , loader .token )
100
+ loader ._token_filename = self ._create_file_with_temp_content (
101
+ _TEST_NEW_TOKEN )
102
+ self .assertEqual ('bearer ' + _TEST_TOKEN ,
103
+ config .get_api_key_with_prefix ('authorization' ))
104
+
105
+ loader .token_expires_at = datetime .datetime .now ()
106
+ self .assertEqual ('bearer ' + _TEST_NEW_TOKEN ,
107
+ config .get_api_key_with_prefix ('authorization' ))
108
+ self .assertEqual ('bearer ' + _TEST_NEW_TOKEN , loader .token )
109
109
self .assertGreater (loader .token_expires_at , old_token_expires_at )
110
110
111
111
def _should_fail_load (self , config_loader , reason ):
@@ -122,9 +122,10 @@ def test_no_port(self):
122
122
self ._should_fail_load (loader , "no port specified" )
123
123
124
124
def test_empty_port (self ):
125
- loader = self .get_test_loader (
126
- environ = {SERVICE_HOST_ENV_NAME : _TEST_HOST ,
127
- SERVICE_PORT_ENV_NAME : "" })
125
+ loader = self .get_test_loader (environ = {
126
+ SERVICE_HOST_ENV_NAME : _TEST_HOST ,
127
+ SERVICE_PORT_ENV_NAME : ""
128
+ })
128
129
self ._should_fail_load (loader , "empty port specified" )
129
130
130
131
def test_no_host (self ):
@@ -133,9 +134,10 @@ def test_no_host(self):
133
134
self ._should_fail_load (loader , "no host specified" )
134
135
135
136
def test_empty_host (self ):
136
- loader = self .get_test_loader (
137
- environ = {SERVICE_HOST_ENV_NAME : "" ,
138
- SERVICE_PORT_ENV_NAME : _TEST_PORT })
137
+ loader = self .get_test_loader (environ = {
138
+ SERVICE_HOST_ENV_NAME : "" ,
139
+ SERVICE_PORT_ENV_NAME : _TEST_PORT
140
+ })
139
141
self ._should_fail_load (loader , "empty host specified" )
140
142
141
143
def test_no_cert_file (self ):
0 commit comments