19
19
import shutil
20
20
import tempfile
21
21
import unittest
22
+ from collections import namedtuple
22
23
23
24
import mock
24
25
import yaml
27
28
from kubernetes .client import Configuration
28
29
29
30
from .config_exception import ConfigException
30
- from .kube_config import (ENV_KUBECONFIG_PATH_SEPARATOR , ConfigNode ,
31
- FileOrData , KubeConfigLoader , KubeConfigMerger ,
32
- _cleanup_temp_files , _create_temp_file_with_content ,
31
+ from .dateutil import parse_rfc3339
32
+ from .kube_config import (ENV_KUBECONFIG_PATH_SEPARATOR , CommandTokenSource ,
33
+ ConfigNode , FileOrData , KubeConfigLoader ,
34
+ KubeConfigMerger , _cleanup_temp_files ,
35
+ _create_temp_file_with_content ,
33
36
list_kube_config_contexts , load_kube_config ,
34
37
new_client_from_config )
35
38
@@ -550,6 +553,27 @@ class TestKubeConfigLoader(BaseTestCase):
550
553
"user" : "exec_cred_user"
551
554
}
552
555
},
556
+ {
557
+ "name" : "contexttestcmdpath" ,
558
+ "context" : {
559
+ "cluster" : "clustertestcmdpath" ,
560
+ "user" : "usertestcmdpath"
561
+ }
562
+ },
563
+ {
564
+ "name" : "contexttestcmdpathempty" ,
565
+ "context" : {
566
+ "cluster" : "clustertestcmdpath" ,
567
+ "user" : "usertestcmdpathempty"
568
+ }
569
+ },
570
+ {
571
+ "name" : "contexttestcmdpathscope" ,
572
+ "context" : {
573
+ "cluster" : "clustertestcmdpath" ,
574
+ "user" : "usertestcmdpathscope"
575
+ }
576
+ }
553
577
],
554
578
"clusters" : [
555
579
{
@@ -588,6 +612,10 @@ class TestKubeConfigLoader(BaseTestCase):
588
612
"insecure-skip-tls-verify" : True ,
589
613
}
590
614
},
615
+ {
616
+ "name" : "clustertestcmdpath" ,
617
+ "cluster" : {}
618
+ }
591
619
],
592
620
"users" : [
593
621
{
@@ -661,7 +689,8 @@ class TestKubeConfigLoader(BaseTestCase):
661
689
"auth-provider" : {
662
690
"config" : {
663
691
"access-token" : TEST_AZURE_TOKEN ,
664
- "apiserver-id" : "00000002-0000-0000-c000-000000000000" ,
692
+ "apiserver-id" : "00000002-0000-0000-c000-"
693
+ "000000000000" ,
665
694
"environment" : "AzurePublicCloud" ,
666
695
"refresh-token" : "refreshToken" ,
667
696
"tenant-id" : "9d2ac018-e843-4e14-9e2b-4e0ddac75433"
@@ -676,7 +705,8 @@ class TestKubeConfigLoader(BaseTestCase):
676
705
"auth-provider" : {
677
706
"config" : {
678
707
"access-token" : TEST_AZURE_TOKEN ,
679
- "apiserver-id" : "00000002-0000-0000-c000-000000000000" ,
708
+ "apiserver-id" : "00000002-0000-0000-c000-"
709
+ "000000000000" ,
680
710
"environment" : "AzurePublicCloud" ,
681
711
"expires-in" : "0" ,
682
712
"expires-on" : "156207275" ,
@@ -693,7 +723,8 @@ class TestKubeConfigLoader(BaseTestCase):
693
723
"auth-provider" : {
694
724
"config" : {
695
725
"access-token" : TEST_AZURE_TOKEN ,
696
- "apiserver-id" : "00000002-0000-0000-c000-000000000000" ,
726
+ "apiserver-id" : "00000002-0000-0000-c000-"
727
+ "000000000000" ,
697
728
"environment" : "AzurePublicCloud" ,
698
729
"expires-in" : "0" ,
699
730
"expires-on" : "2018-10-18 00:52:29.044727" ,
@@ -710,7 +741,8 @@ class TestKubeConfigLoader(BaseTestCase):
710
741
"auth-provider" : {
711
742
"config" : {
712
743
"access-token" : TEST_AZURE_TOKEN ,
713
- "apiserver-id" : "00000002-0000-0000-c000-000000000000" ,
744
+ "apiserver-id" : "00000002-0000-0000-c000-"
745
+ "000000000000" ,
714
746
"environment" : "AzurePublicCloud" ,
715
747
"expires-in" : "0" ,
716
748
"expires-on" : "2018-10-18 00:52" ,
@@ -727,7 +759,8 @@ class TestKubeConfigLoader(BaseTestCase):
727
759
"auth-provider" : {
728
760
"config" : {
729
761
"access-token" : TEST_AZURE_TOKEN ,
730
- "apiserver-id" : "00000002-0000-0000-c000-000000000000" ,
762
+ "apiserver-id" : "00000002-0000-0000-c000-"
763
+ "000000000000" ,
731
764
"environment" : "AzurePublicCloud" ,
732
765
"expires-in" : "0" ,
733
766
"expires-on" : "-1" ,
@@ -877,6 +910,40 @@ class TestKubeConfigLoader(BaseTestCase):
877
910
}
878
911
}
879
912
},
913
+ {
914
+ "name" : "usertestcmdpath" ,
915
+ "user" : {
916
+ "auth-provider" : {
917
+ "name" : "gcp" ,
918
+ "config" : {
919
+ "cmd-path" : "cmdtorun"
920
+ }
921
+ }
922
+ }
923
+ },
924
+ {
925
+ "name" : "usertestcmdpathempty" ,
926
+ "user" : {
927
+ "auth-provider" : {
928
+ "name" : "gcp" ,
929
+ "config" : {
930
+ "cmd-path" : ""
931
+ }
932
+ }
933
+ }
934
+ },
935
+ {
936
+ "name" : "usertestcmdpathscope" ,
937
+ "user" : {
938
+ "auth-provider" : {
939
+ "name" : "gcp" ,
940
+ "config" : {
941
+ "cmd-path" : "cmd" ,
942
+ "scopes" : "scope"
943
+ }
944
+ }
945
+ }
946
+ }
880
947
]
881
948
}
882
949
@@ -1279,6 +1346,46 @@ def test_user_exec_auth(self, mock):
1279
1346
active_context = "exec_cred_user" ).load_and_set (actual )
1280
1347
self .assertEqual (expected , actual )
1281
1348
1349
+ def test_user_cmd_path (self ):
1350
+ A = namedtuple ('A' , ['token' , 'expiry' ])
1351
+ token = "dummy"
1352
+ return_value = A (token , parse_rfc3339 (datetime .datetime .now ()))
1353
+ CommandTokenSource .token = mock .Mock (return_value = return_value )
1354
+ expected = FakeConfig (api_key = {
1355
+ "authorization" : BEARER_TOKEN_FORMAT % token })
1356
+ actual = FakeConfig ()
1357
+ KubeConfigLoader (
1358
+ config_dict = self .TEST_KUBE_CONFIG ,
1359
+ active_context = "contexttestcmdpath" ).load_and_set (actual )
1360
+ expected .get_api_key_with_prefix = actual .get_api_key_with_prefix
1361
+ self .assertEqual (expected , actual )
1362
+
1363
+ def test_user_cmd_path_empty (self ):
1364
+ A = namedtuple ('A' , ['token' , 'expiry' ])
1365
+ token = "dummy"
1366
+ return_value = A (token , parse_rfc3339 (datetime .datetime .now ()))
1367
+ CommandTokenSource .token = mock .Mock (return_value = return_value )
1368
+ expected = FakeConfig (api_key = {
1369
+ "authorization" : BEARER_TOKEN_FORMAT % token })
1370
+ actual = FakeConfig ()
1371
+ with self .assertRaises (ConfigException ):
1372
+ KubeConfigLoader (
1373
+ config_dict = self .TEST_KUBE_CONFIG ,
1374
+ active_context = "contexttestcmdpathempty" ).load_and_set (actual )
1375
+
1376
+ def test_user_cmd_path_with_scope (self ):
1377
+ A = namedtuple ('A' , ['token' , 'expiry' ])
1378
+ token = "dummy"
1379
+ return_value = A (token , parse_rfc3339 (datetime .datetime .now ()))
1380
+ CommandTokenSource .token = mock .Mock (return_value = return_value )
1381
+ expected = FakeConfig (api_key = {
1382
+ "authorization" : BEARER_TOKEN_FORMAT % token })
1383
+ actual = FakeConfig ()
1384
+ with self .assertRaises (ConfigException ):
1385
+ KubeConfigLoader (
1386
+ config_dict = self .TEST_KUBE_CONFIG ,
1387
+ active_context = "contexttestcmdpathscope" ).load_and_set (actual )
1388
+
1282
1389
1283
1390
class TestKubernetesClientConfiguration (BaseTestCase ):
1284
1391
# Verifies properties of kubernetes.client.Configuration.
@@ -1394,6 +1501,7 @@ class TestKubeConfigMerger(BaseTestCase):
1394
1501
{
1395
1502
"name" : "expired_oidc" ,
1396
1503
"user" : {
1504
+ "name" : "gcp" ,
1397
1505
"auth-provider" : {
1398
1506
"name" : "oidc" ,
1399
1507
"config" : {
@@ -1421,14 +1529,46 @@ class TestKubeConfigMerger(BaseTestCase):
1421
1529
TEST_KUBE_CONFIG_PART4 = {
1422
1530
"current-context" : "no_user" ,
1423
1531
}
1532
+ # Config with user having cmd-path
1533
+ TEST_KUBE_CONFIG_PART5 = {
1534
+ "contexts" : [
1535
+ {
1536
+ "name" : "contexttestcmdpath" ,
1537
+ "context" : {
1538
+ "cluster" : "clustertestcmdpath" ,
1539
+ "user" : "usertestcmdpath"
1540
+ }
1541
+ }
1542
+ ],
1543
+ "clusters" : [
1544
+ {
1545
+ "name" : "clustertestcmdpath" ,
1546
+ "cluster" : {}
1547
+ }
1548
+ ],
1549
+ "users" : [
1550
+ {
1551
+ "name" : "usertestcmdpath" ,
1552
+ "user" : {
1553
+ "auth-provider" : {
1554
+ "name" : "gcp" ,
1555
+ "config" : {
1556
+ "cmd-path" : "cmdtorun"
1557
+ }
1558
+ }
1559
+ }
1560
+ }
1561
+ ]
1562
+ }
1424
1563
1425
1564
def _create_multi_config (self ):
1426
1565
files = []
1427
1566
for part in (
1428
1567
self .TEST_KUBE_CONFIG_PART1 ,
1429
1568
self .TEST_KUBE_CONFIG_PART2 ,
1430
1569
self .TEST_KUBE_CONFIG_PART3 ,
1431
- self .TEST_KUBE_CONFIG_PART4 ):
1570
+ self .TEST_KUBE_CONFIG_PART4 ,
1571
+ self .TEST_KUBE_CONFIG_PART5 ):
1432
1572
files .append (self ._create_temp_file (yaml .safe_dump (part )))
1433
1573
return ENV_KUBECONFIG_PATH_SEPARATOR .join (files )
1434
1574
@@ -1439,7 +1579,11 @@ def test_list_kube_config_contexts(self):
1439
1579
{'context' : {'cluster' : 'ssl' , 'user' : 'ssl' }, 'name' : 'ssl' },
1440
1580
{'context' : {'cluster' : 'default' , 'user' : 'simple_token' },
1441
1581
'name' : 'simple_token' },
1442
- {'context' : {'cluster' : 'default' , 'user' : 'expired_oidc' }, 'name' : 'expired_oidc' }]
1582
+ {'context' : {'cluster' : 'default' , 'user' : 'expired_oidc' },
1583
+ 'name' : 'expired_oidc' },
1584
+ {'context' : {'cluster' : 'clustertestcmdpath' ,
1585
+ 'user' : 'usertestcmdpath' },
1586
+ 'name' : 'contexttestcmdpath' }]
1443
1587
1444
1588
contexts , active_context = list_kube_config_contexts (
1445
1589
config_file = kubeconfigs )
0 commit comments