14
14
15
15
import base64
16
16
import os
17
+ import shutil
17
18
import tempfile
18
19
import unittest
19
20
20
21
from .config_exception import ConfigException
21
22
from .kube_config import (ConfigNode , FileOrData , KubeConfigLoader ,
22
- _create_temp_file_with_content )
23
+ _create_temp_file_with_content , _cleanup_temp_files )
24
+
25
+ NON_EXISTING_FILE = "zz_non_existing_file_472398324"
23
26
24
27
25
28
def _base64 (string ):
@@ -67,6 +70,11 @@ def _create_temp_file(self, content=""):
67
70
os .close (handler )
68
71
return name
69
72
73
+ def expect_exception (self , func , message_part ):
74
+ with self .assertRaises (ConfigException ) as context :
75
+ func ()
76
+ self .assertIn (message_part , str (context .exception ))
77
+
70
78
71
79
class TestFileOrData (BaseTestCase ):
72
80
@@ -76,9 +84,16 @@ def get_file_content(filename):
76
84
return f .read ()
77
85
78
86
def test_file_given_file (self ):
79
- obj = {TEST_FILE_KEY : TEST_FILENAME }
87
+ temp_filename = _create_temp_file_with_content (TEST_DATA )
88
+ obj = {TEST_FILE_KEY : temp_filename }
80
89
t = FileOrData (obj = obj , file_key_name = TEST_FILE_KEY )
81
- self .assertEqual (TEST_FILENAME , t .as_file ())
90
+ self .assertEqual (TEST_DATA , self .get_file_content (t .as_file ()))
91
+
92
+ def test_file_given_non_existing_file (self ):
93
+ temp_filename = NON_EXISTING_FILE
94
+ obj = {TEST_FILE_KEY : temp_filename }
95
+ t = FileOrData (obj = obj , file_key_name = TEST_FILE_KEY )
96
+ self .expect_exception (t .as_file , "does not exists" )
82
97
83
98
def test_file_given_data (self ):
84
99
obj = {TEST_DATA_KEY : TEST_DATA_BASE64 }
@@ -116,10 +131,20 @@ def test_file_given_file_and_data(self):
116
131
data_key_name = TEST_DATA_KEY )
117
132
self .assertEqual (TEST_DATA , self .get_file_content (t .as_file ()))
118
133
134
+ def test_file_with_custom_dirname (self ):
135
+ tempfile = self ._create_temp_file (content = TEST_DATA )
136
+ tempfile_dir = os .path .dirname (tempfile )
137
+ tempfile_basename = os .path .basename (tempfile )
138
+ obj = {TEST_FILE_KEY : tempfile_basename }
139
+ t = FileOrData (obj = obj , file_key_name = TEST_FILE_KEY ,
140
+ file_base_path = tempfile_dir )
141
+ self .assertEqual (TEST_DATA , self .get_file_content (t .as_file ()))
142
+
119
143
def test_create_temp_file_with_content (self ):
120
144
self .assertEqual (TEST_DATA ,
121
145
self .get_file_content (
122
146
_create_temp_file_with_content (TEST_DATA )))
147
+ _cleanup_temp_files ()
123
148
124
149
125
150
class TestConfigNode (BaseTestCase ):
@@ -163,11 +188,6 @@ def test_get_with_name(self):
163
188
self .assertEqual ("test_obj/with_names[name=test_name3]" ,
164
189
node .get_with_name ("test_name3" ).name )
165
190
166
- def expect_exception (self , func , message_part ):
167
- with self .assertRaises (ConfigException ) as context :
168
- func ()
169
- self .assertIn (message_part , str (context .exception ))
170
-
171
191
def test_key_does_not_exists (self ):
172
192
self .expect_exception (lambda : self .node ['not-exists-key' ],
173
193
"Expected key not-exists-key in test_obj" )
@@ -281,6 +301,13 @@ class TestKubeConfigLoader(BaseTestCase):
281
301
"user" : "ssl-no_file"
282
302
}
283
303
},
304
+ {
305
+ "name" : "ssl-local-file" ,
306
+ "context" : {
307
+ "cluster" : "ssl-local-file" ,
308
+ "user" : "ssl-local-file"
309
+ }
310
+ },
284
311
],
285
312
"clusters" : [
286
313
{
@@ -296,6 +323,13 @@ class TestKubeConfigLoader(BaseTestCase):
296
323
"certificate-authority" : TEST_CERTIFICATE_AUTH ,
297
324
}
298
325
},
326
+ {
327
+ "name" : "ssl-local-file" ,
328
+ "cluster" : {
329
+ "server" : TEST_SSL_HOST ,
330
+ "certificate-authority" : "cert_test" ,
331
+ }
332
+ },
299
333
{
300
334
"name" : "ssl" ,
301
335
"cluster" : {
@@ -340,6 +374,14 @@ class TestKubeConfigLoader(BaseTestCase):
340
374
"client-key" : TEST_CLIENT_KEY ,
341
375
}
342
376
},
377
+ {
378
+ "name" : "ssl-local-file" ,
379
+ "user" : {
380
+ "tokenFile" : "token_file" ,
381
+ "client-certificate" : "client_cert" ,
382
+ "client-key" : "client_key" ,
383
+ }
384
+ },
343
385
{
344
386
"name" : "ssl" ,
345
387
"user" : {
@@ -420,11 +462,11 @@ def test_ssl_no_cert_files(self):
420
462
ssl_ca_cert = TEST_CERTIFICATE_AUTH
421
463
)
422
464
actual = FakeConfig ()
423
- KubeConfigLoader (
465
+ loader = KubeConfigLoader (
424
466
config_dict = self .TEST_KUBE_CONFIG ,
425
467
active_context = "ssl-no_file" ,
426
- client_configuration = actual ). load_and_set ()
427
- self .assertEqual ( expected , actual )
468
+ client_configuration = actual )
469
+ self .expect_exception ( loader . load_and_set , "does not exists" )
428
470
429
471
def test_ssl (self ):
430
472
expected = FakeConfig (
@@ -464,6 +506,34 @@ def test_set_active_context(self):
464
506
self .assertEqual (expected_contexts .get_with_name ("ssl" ).value ,
465
507
loader .current_context )
466
508
509
+ def test_ssl_with_relative_ssl_files (self ):
510
+ expected = FakeConfig (
511
+ host = TEST_SSL_HOST ,
512
+ token = TEST_DATA_BASE64 ,
513
+ cert_file = self ._create_temp_file (TEST_CLIENT_CERT ),
514
+ key_file = self ._create_temp_file (TEST_CLIENT_KEY ),
515
+ ssl_ca_cert = self ._create_temp_file (TEST_CERTIFICATE_AUTH )
516
+ )
517
+ try :
518
+ temp_dir = tempfile .mkdtemp ()
519
+ actual = FakeConfig ()
520
+ with open (os .path .join (temp_dir , "cert_test" ), "wb" ) as fd :
521
+ fd .write (TEST_CERTIFICATE_AUTH .encode ())
522
+ with open (os .path .join (temp_dir , "client_cert" ), "wb" ) as fd :
523
+ fd .write (TEST_CLIENT_CERT .encode ())
524
+ with open (os .path .join (temp_dir , "client_key" ), "wb" ) as fd :
525
+ fd .write (TEST_CLIENT_KEY .encode ())
526
+ with open (os .path .join (temp_dir , "token_file" ), "wb" ) as fd :
527
+ fd .write (TEST_DATA .encode ())
528
+ KubeConfigLoader (
529
+ config_dict = self .TEST_KUBE_CONFIG ,
530
+ active_context = "ssl-local-file" ,
531
+ config_base_path = temp_dir ,
532
+ client_configuration = actual ).load_and_set ()
533
+ self .assertEqual (expected , actual )
534
+ finally :
535
+ shutil .rmtree (temp_dir )
536
+
467
537
468
538
if __name__ == '__main__' :
469
539
unittest .main ()
0 commit comments