@@ -1653,7 +1653,7 @@ def refresh_api_key_hook(client_config):
1653
1653
1654
1654
1655
1655
class TestKubeConfigMerger (BaseTestCase ):
1656
- TEST_KUBE_CONFIG_PART1 = {
1656
+ TEST_KUBE_CONFIG_SET1 = [ {
1657
1657
"current-context" : "no_user" ,
1658
1658
"contexts" : [
1659
1659
{
@@ -1672,9 +1672,7 @@ class TestKubeConfigMerger(BaseTestCase):
1672
1672
},
1673
1673
],
1674
1674
"users" : []
1675
- }
1676
-
1677
- TEST_KUBE_CONFIG_PART2 = {
1675
+ }, {
1678
1676
"current-context" : "" ,
1679
1677
"contexts" : [
1680
1678
{
@@ -1712,9 +1710,7 @@ class TestKubeConfigMerger(BaseTestCase):
1712
1710
}
1713
1711
},
1714
1712
]
1715
- }
1716
-
1717
- TEST_KUBE_CONFIG_PART3 = {
1713
+ }, {
1718
1714
"current-context" : "no_user" ,
1719
1715
"contexts" : [
1720
1716
{
@@ -1761,12 +1757,10 @@ class TestKubeConfigMerger(BaseTestCase):
1761
1757
}
1762
1758
},
1763
1759
]
1764
- }
1765
- TEST_KUBE_CONFIG_PART4 = {
1760
+ }, {
1766
1761
"current-context" : "no_user" ,
1767
- }
1768
- # Config with user having cmd-path
1769
- TEST_KUBE_CONFIG_PART5 = {
1762
+ }, {
1763
+ # Config with user having cmd-path
1770
1764
"contexts" : [
1771
1765
{
1772
1766
"name" : "contexttestcmdpath" ,
@@ -1795,8 +1789,7 @@ class TestKubeConfigMerger(BaseTestCase):
1795
1789
}
1796
1790
}
1797
1791
]
1798
- }
1799
- TEST_KUBE_CONFIG_PART6 = {
1792
+ }, {
1800
1793
"current-context" : "no_user" ,
1801
1794
"contexts" : [
1802
1795
{
@@ -1815,22 +1808,49 @@ class TestKubeConfigMerger(BaseTestCase):
1815
1808
},
1816
1809
],
1817
1810
"users" : None
1818
- }
1811
+ }]
1812
+ # 3 parts with different keys/data to merge
1813
+ TEST_KUBE_CONFIG_SET2 = [{
1814
+ "clusters" : [
1815
+ {
1816
+ "name" : "default" ,
1817
+ "cluster" : {
1818
+ "server" : TEST_HOST
1819
+ }
1820
+ },
1821
+ ],
1822
+ }, {
1823
+ "current-context" : "simple_token" ,
1824
+ "contexts" : [
1825
+ {
1826
+ "name" : "simple_token" ,
1827
+ "context" : {
1828
+ "cluster" : "default" ,
1829
+ "user" : "simple_token"
1830
+ }
1831
+ },
1832
+ ],
1833
+ }, {
1834
+ "users" : [
1835
+ {
1836
+ "name" : "simple_token" ,
1837
+ "user" : {
1838
+ "token" : TEST_DATA_BASE64 ,
1839
+ "username" : TEST_USERNAME ,
1840
+ "password" : TEST_PASSWORD ,
1841
+ }
1842
+ },
1843
+ ]
1844
+ }]
1819
1845
1820
- def _create_multi_config (self ):
1846
+ def _create_multi_config (self , parts ):
1821
1847
files = []
1822
- for part in (
1823
- self .TEST_KUBE_CONFIG_PART1 ,
1824
- self .TEST_KUBE_CONFIG_PART2 ,
1825
- self .TEST_KUBE_CONFIG_PART3 ,
1826
- self .TEST_KUBE_CONFIG_PART4 ,
1827
- self .TEST_KUBE_CONFIG_PART5 ,
1828
- self .TEST_KUBE_CONFIG_PART6 ):
1848
+ for part in parts :
1829
1849
files .append (self ._create_temp_file (yaml .safe_dump (part )))
1830
1850
return ENV_KUBECONFIG_PATH_SEPARATOR .join (files )
1831
1851
1832
1852
def test_list_kube_config_contexts (self ):
1833
- kubeconfigs = self ._create_multi_config ()
1853
+ kubeconfigs = self ._create_multi_config (self . TEST_KUBE_CONFIG_SET1 )
1834
1854
expected_contexts = [
1835
1855
{'context' : {'cluster' : 'default' }, 'name' : 'no_user' },
1836
1856
{'context' : {'cluster' : 'ssl' , 'user' : 'ssl' }, 'name' : 'ssl' },
@@ -1849,15 +1869,31 @@ def test_list_kube_config_contexts(self):
1849
1869
self .assertEqual (active_context , expected_contexts [0 ])
1850
1870
1851
1871
def test_new_client_from_config (self ):
1852
- kubeconfigs = self ._create_multi_config ()
1872
+ kubeconfigs = self ._create_multi_config (self . TEST_KUBE_CONFIG_SET1 )
1853
1873
client = new_client_from_config (
1854
1874
config_file = kubeconfigs , context = "simple_token" )
1855
1875
self .assertEqual (TEST_HOST , client .configuration .host )
1856
1876
self .assertEqual (BEARER_TOKEN_FORMAT % TEST_DATA_BASE64 ,
1857
1877
client .configuration .api_key ['authorization' ])
1858
1878
1879
+ def test_merge_with_context_in_different_file (self ):
1880
+ kubeconfigs = self ._create_multi_config (self .TEST_KUBE_CONFIG_SET2 )
1881
+ client = new_client_from_config (config_file = kubeconfigs )
1882
+
1883
+ expected_contexts = [
1884
+ {'context' : {'cluster' : 'default' , 'user' : 'simple_token' },
1885
+ 'name' : 'simple_token' }
1886
+ ]
1887
+ contexts , active_context = list_kube_config_contexts (
1888
+ config_file = kubeconfigs )
1889
+ self .assertEqual (contexts , expected_contexts )
1890
+ self .assertEqual (active_context , expected_contexts [0 ])
1891
+ self .assertEqual (TEST_HOST , client .configuration .host )
1892
+ self .assertEqual (BEARER_TOKEN_FORMAT % TEST_DATA_BASE64 ,
1893
+ client .configuration .api_key ['authorization' ])
1894
+
1859
1895
def test_save_changes (self ):
1860
- kubeconfigs = self ._create_multi_config ()
1896
+ kubeconfigs = self ._create_multi_config (self . TEST_KUBE_CONFIG_SET1 )
1861
1897
1862
1898
# load configuration, update token, save config
1863
1899
kconf = KubeConfigMerger (kubeconfigs )
0 commit comments