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

Commit bd507f4

Browse files
committed
Addressing comments
1 parent 50f717d commit bd507f4

File tree

4 files changed

+71
-14
lines changed

4 files changed

+71
-14
lines changed

Diff for: config/dateutil.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def dst(self, dt):
4646

4747

4848
def parse_rfc3339(s):
49-
if type(s) == datetime.datetime:
49+
if isinstance(s, datetime.datetime):
5050
# no need to parse it, just make sure it has a timezone.
5151
if not s.tzinfo:
5252
return s.replace(tzinfo=UTC)

Diff for: config/dateutil_test.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2016 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest
16+
from datetime import datetime
17+
18+
from .dateutil import UTC, TimezoneInfo, format_rfc3339, parse_rfc3339
19+
20+
21+
class DateUtilTest(unittest.TestCase):
22+
23+
def _parse_rfc3339_test(self, st, y, m, d, h, mn, s):
24+
actual = parse_rfc3339(st)
25+
expected = datetime(y, m, d, h, mn, s, 0, UTC)
26+
self.assertEqual(expected, actual)
27+
28+
def test_parse_rfc3339(self):
29+
self._parse_rfc3339_test("2017-07-25T04:44:21Z",
30+
2017, 7, 25, 4, 44, 21)
31+
self._parse_rfc3339_test("2017-07-25 04:44:21Z",
32+
2017, 7, 25, 4, 44, 21)
33+
self._parse_rfc3339_test("2017-07-25T04:44:21",
34+
2017, 7, 25, 4, 44, 21)
35+
self._parse_rfc3339_test("2017-07-25T04:44:21z",
36+
2017, 7, 25, 4, 44, 21)
37+
self._parse_rfc3339_test("2017-07-25T04:44:21+03:00",
38+
2017, 7, 25, 1, 44, 21)
39+
self._parse_rfc3339_test("2017-07-25T04:44:21-03:00",
40+
2017, 7, 25, 7, 44, 21)
41+
42+
def test_format_rfc3339(self):
43+
self.assertEqual(
44+
format_rfc3339(datetime(2017, 7, 25, 4, 44, 21, 0, UTC)),
45+
"2017-07-25T04:44:21Z")
46+
self.assertEqual(
47+
format_rfc3339(datetime(2017, 7, 25, 4, 44, 21, 0,
48+
TimezoneInfo(2, 0))),
49+
"2017-07-25T02:44:21Z")
50+
self.assertEqual(
51+
format_rfc3339(datetime(2017, 7, 25, 4, 44, 21, 0,
52+
TimezoneInfo(-2, 30))),
53+
"2017-07-25T07:14:21Z")

Diff for: config/kube_config.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
import yaml
2525

2626
from kubernetes.client import ApiClient, ConfigurationObject, configuration
27-
from .dateutil import parse_rfc3339, format_rfc3339, UTC
27+
2828
from .config_exception import ConfigException
29+
from .dateutil import UTC, format_rfc3339, parse_rfc3339
2930

3031
EXPIRY_SKEW_PREVENTION_DELAY = datetime.timedelta(minutes=5)
3132
KUBE_CONFIG_DEFAULT_LOCATION = os.environ.get('KUBECONFIG', '~/.kube/config')
@@ -320,11 +321,6 @@ def _get_kube_config_loader_for_yaml_file(filename, **kwargs):
320321
**kwargs)
321322

322323

323-
def _save_kube_config(filename, config_map):
324-
with open(filename, 'w') as f:
325-
yaml.safe_dump(config_map, f, default_flow_style=False)
326-
327-
328324
def list_kube_config_contexts(config_file=None):
329325

330326
if config_file is None:
@@ -354,8 +350,11 @@ def load_kube_config(config_file=None, context=None,
354350

355351
config_persister = None
356352
if persist_config:
357-
config_persister = lambda config_map, config_file=config_file: (
358-
_save_kube_config(config_file, config_map))
353+
def _save_kube_config(config_map):
354+
with open(config_file, 'w') as f:
355+
yaml.safe_dump(config_map, f, default_flow_style=False)
356+
config_persister = _save_kube_config
357+
359358
_get_kube_config_loader_for_yaml_file(
360359
config_file, active_context=context,
361360
client_configuration=client_configuration,

Diff for: config/kube_config_test.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
from six import PY3
2424

2525
from .config_exception import ConfigException
26+
from .dateutil import parse_rfc3339
2627
from .kube_config import (ConfigNode, FileOrData, KubeConfigLoader,
2728
_cleanup_temp_files, _create_temp_file_with_content,
2829
list_kube_config_contexts, load_kube_config,
2930
new_client_from_config)
30-
from .rfc3339 import timestamp_from_tf
3131

3232
BEARER_TOKEN_FORMAT = "Bearer %s"
3333

@@ -38,6 +38,10 @@ def _base64(string):
3838
return base64.encodestring(string.encode()).decode()
3939

4040

41+
def _raise_exception(st):
42+
raise Exception(st)
43+
44+
4145
TEST_FILE_KEY = "file"
4246
TEST_DATA_KEY = "data"
4347
TEST_FILENAME = "test-filename"
@@ -422,7 +426,7 @@ class TestKubeConfigLoader(BaseTestCase):
422426
"name": "gcp",
423427
"config": {
424428
"access-token": TEST_DATA_BASE64,
425-
"expiry": timestamp_from_tf(0),
429+
"expiry": "2000-01-01T12:00:00Z", # always in past
426430
}
427431
},
428432
"token": TEST_DATA_BASE64, # should be ignored
@@ -499,15 +503,16 @@ def test_gcp_no_refresh(self):
499503
config_dict=self.TEST_KUBE_CONFIG,
500504
active_context="gcp",
501505
client_configuration=actual,
502-
get_google_credentials=lambda: "SHOULD NOT BE CALLED") \
503-
.load_and_set()
506+
get_google_credentials=lambda: _raise_exception(
507+
"SHOULD NOT BE CALLED")).load_and_set()
504508
self.assertEqual(expected, actual)
505509

506510
def test_load_gcp_token_no_refresh(self):
507511
loader = KubeConfigLoader(
508512
config_dict=self.TEST_KUBE_CONFIG,
509513
active_context="gcp",
510-
get_google_credentials=lambda: "SHOULD NOT BE CALLED")
514+
get_google_credentials=lambda: _raise_exception(
515+
"SHOULD NOT BE CALLED"))
511516
self.assertTrue(loader._load_gcp_token())
512517
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
513518
loader.token)

0 commit comments

Comments
 (0)