1414
1515import base64
1616import os
17+ import shutil
1718import tempfile
1819import unittest
1920
2021from .config_exception import ConfigException
2122from .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"
2326
2427
2528def _base64 (string ):
@@ -67,6 +70,11 @@ def _create_temp_file(self, content=""):
6770 os .close (handler )
6871 return name
6972
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+
7078
7179class TestFileOrData (BaseTestCase ):
7280
@@ -76,9 +84,16 @@ def get_file_content(filename):
7684 return f .read ()
7785
7886 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 }
8089 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" )
8297
8398 def test_file_given_data (self ):
8499 obj = {TEST_DATA_KEY : TEST_DATA_BASE64 }
@@ -116,10 +131,20 @@ def test_file_given_file_and_data(self):
116131 data_key_name = TEST_DATA_KEY )
117132 self .assertEqual (TEST_DATA , self .get_file_content (t .as_file ()))
118133
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+
119143 def test_create_temp_file_with_content (self ):
120144 self .assertEqual (TEST_DATA ,
121145 self .get_file_content (
122146 _create_temp_file_with_content (TEST_DATA )))
147+ _cleanup_temp_files ()
123148
124149
125150class TestConfigNode (BaseTestCase ):
@@ -163,11 +188,6 @@ def test_get_with_name(self):
163188 self .assertEqual ("test_obj/with_names[name=test_name3]" ,
164189 node .get_with_name ("test_name3" ).name )
165190
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-
171191 def test_key_does_not_exists (self ):
172192 self .expect_exception (lambda : self .node ['not-exists-key' ],
173193 "Expected key not-exists-key in test_obj" )
@@ -281,6 +301,13 @@ class TestKubeConfigLoader(BaseTestCase):
281301 "user" : "ssl-no_file"
282302 }
283303 },
304+ {
305+ "name" : "ssl-local-file" ,
306+ "context" : {
307+ "cluster" : "ssl-local-file" ,
308+ "user" : "ssl-local-file"
309+ }
310+ },
284311 ],
285312 "clusters" : [
286313 {
@@ -296,6 +323,13 @@ class TestKubeConfigLoader(BaseTestCase):
296323 "certificate-authority" : TEST_CERTIFICATE_AUTH ,
297324 }
298325 },
326+ {
327+ "name" : "ssl-local-file" ,
328+ "cluster" : {
329+ "server" : TEST_SSL_HOST ,
330+ "certificate-authority" : "cert_test" ,
331+ }
332+ },
299333 {
300334 "name" : "ssl" ,
301335 "cluster" : {
@@ -340,6 +374,14 @@ class TestKubeConfigLoader(BaseTestCase):
340374 "client-key" : TEST_CLIENT_KEY ,
341375 }
342376 },
377+ {
378+ "name" : "ssl-local-file" ,
379+ "user" : {
380+ "tokenFile" : "token_file" ,
381+ "client-certificate" : "client_cert" ,
382+ "client-key" : "client_key" ,
383+ }
384+ },
343385 {
344386 "name" : "ssl" ,
345387 "user" : {
@@ -420,11 +462,11 @@ def test_ssl_no_cert_files(self):
420462 ssl_ca_cert = TEST_CERTIFICATE_AUTH
421463 )
422464 actual = FakeConfig ()
423- KubeConfigLoader (
465+ loader = KubeConfigLoader (
424466 config_dict = self .TEST_KUBE_CONFIG ,
425467 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" )
428470
429471 def test_ssl (self ):
430472 expected = FakeConfig (
@@ -464,6 +506,34 @@ def test_set_active_context(self):
464506 self .assertEqual (expected_contexts .get_with_name ("ssl" ).value ,
465507 loader .current_context )
466508
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+
467537
468538if __name__ == '__main__' :
469539 unittest .main ()
0 commit comments