1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import datetime
1516import os
1617import tempfile
17- import unittest
18- import datetime
1918import time
19+ import unittest
2020
2121from kubernetes .client import Configuration
2222
3333_TEST_IPV6_HOST = "::1"
3434_TEST_IPV6_HOST_PORT = "[::1]:80"
3535
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+ }
4044
4145
4246class InClusterConfigTest (unittest .TestCase ):
43-
4447 def setUp (self ):
4548 self ._temp_files = []
4649
@@ -55,25 +58,18 @@ def _create_file_with_temp_content(self, content=""):
5558 os .close (handler )
5659 return name
5760
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 ):
6965 if not token_filename :
7066 token_filename = self ._create_file_with_temp_content (_TEST_TOKEN )
7167 if not cert_filename :
7268 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 )
7773
7874 def test_join_host_port (self ):
7975 self .assertEqual (_TEST_HOST_PORT ,
@@ -87,25 +83,29 @@ def test_load_config(self):
8783 loader ._load_config ()
8884 self .assertEqual ("https://" + _TEST_HOST_PORT , loader .host )
8985 self .assertEqual (cert_filename , loader .ssl_ca_cert )
90- self .assertEqual (_TEST_TOKEN , loader .token )
86+ self .assertEqual ('bearer ' + _TEST_TOKEN , loader .token )
9187
9288 def test_refresh_token (self ):
9389 loader = self .get_test_loader ()
94- loader ._token_refresh_period = datetime .timedelta (seconds = 5 )
95- loader .load_and_set ()
9690 config = Configuration ()
91+ loader .load_and_set (config )
9792
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 )
10096 self .assertIsNotNone (loader .token_expires_at )
10197
10298 old_token = loader .token
10399 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 )
109109 self .assertGreater (loader .token_expires_at , old_token_expires_at )
110110
111111 def _should_fail_load (self , config_loader , reason ):
@@ -122,9 +122,10 @@ def test_no_port(self):
122122 self ._should_fail_load (loader , "no port specified" )
123123
124124 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+ })
128129 self ._should_fail_load (loader , "empty port specified" )
129130
130131 def test_no_host (self ):
@@ -133,9 +134,10 @@ def test_no_host(self):
133134 self ._should_fail_load (loader , "no host specified" )
134135
135136 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+ })
139141 self ._should_fail_load (loader , "empty host specified" )
140142
141143 def test_no_cert_file (self ):
0 commit comments