Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit 3d83748

Browse files
committed
fix: read config data with bytes (python3)
1 parent d68e456 commit 3d83748

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Diff for: config/kube_config.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,12 @@ def as_file(self):
100100
use_data_if_no_file = not self._file and self._data
101101
if use_data_if_no_file:
102102
if self._base64_file_content:
103+
if isinstance(self._data, str):
104+
content = self._data.encode()
105+
else:
106+
content = self._data
103107
self._file = _create_temp_file_with_content(
104-
base64.decodestring(self._data.encode()))
108+
base64.decodestring(content))
105109
else:
106110
self._file = _create_temp_file_with_content(self._data)
107111
if self._file and not os.path.isfile(self._file):

Diff for: config/kube_config_test.py

+15
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ def _raise_exception(st):
6565
TEST_DATA = "test-data"
6666
TEST_DATA_BASE64 = _base64(TEST_DATA)
6767

68+
TEST_DATA_BYTES = b"test-data"
69+
TEST_DATA_BASE64_BYTES = b"dGVzdC1kYXRh"
70+
6871
TEST_ANOTHER_DATA = "another-test-data"
6972
TEST_ANOTHER_DATA_BASE64 = _base64(TEST_ANOTHER_DATA)
7073

@@ -201,6 +204,18 @@ def test_create_temp_file_with_content(self):
201204
_create_temp_file_with_content(TEST_DATA)))
202205
_cleanup_temp_files()
203206

207+
def test_file_given_data_bytes(self):
208+
obj = {TEST_DATA_KEY: TEST_DATA_BASE64_BYTES}
209+
t = FileOrData(obj=obj, file_key_name=TEST_FILE_KEY,
210+
data_key_name=TEST_DATA_KEY)
211+
self.assertEqual(TEST_DATA, self.get_file_content(t.as_file()))
212+
213+
def test_file_given_data_bytes_no_base64(self):
214+
obj = {TEST_DATA_KEY: TEST_DATA_BYTES}
215+
t = FileOrData(obj=obj, file_key_name=TEST_FILE_KEY,
216+
data_key_name=TEST_DATA_KEY, base64_file_content=False)
217+
self.assertEqual(TEST_DATA, self.get_file_content(t.as_file()))
218+
204219

205220
class TestConfigNode(BaseTestCase):
206221

0 commit comments

Comments
 (0)