@@ -1075,7 +1075,7 @@ async def load_from_exec_plugin(self):
10751075
10761076
10771077class TestKubeConfigMerger (BaseTestCase ):
1078- TEST_KUBE_CONFIG_PART1 = {
1078+ TEST_KUBE_CONFIG_SET1 = [ {
10791079 "current-context" : "no_user" ,
10801080 "contexts" : [
10811081 {
@@ -1094,9 +1094,7 @@ class TestKubeConfigMerger(BaseTestCase):
10941094 },
10951095 ],
10961096 "users" : []
1097- }
1098-
1099- TEST_KUBE_CONFIG_PART2 = {
1097+ }, {
11001098 "current-context" : "" ,
11011099 "contexts" : [
11021100 {
@@ -1134,10 +1132,8 @@ class TestKubeConfigMerger(BaseTestCase):
11341132 }
11351133 },
11361134 ]
1137- }
1138-
1139- TEST_KUBE_CONFIG_PART3 = {
1140- "current-context" : "no_user" ,
1135+ }, {
1136+ "current-context" : "expired_oidc" ,
11411137 "contexts" : [
11421138 {
11431139 "name" : "expired_oidc" ,
@@ -1183,19 +1179,50 @@ class TestKubeConfigMerger(BaseTestCase):
11831179 }
11841180 },
11851181 ]
1186- }
1182+ }]
11871183
1188- def _create_multi_config (self ):
1184+ # 3 parts with different keys/data to merge
1185+ TEST_KUBE_CONFIG_SET2 = [{
1186+ "clusters" : [
1187+ {
1188+ "name" : "default" ,
1189+ "cluster" : {
1190+ "server" : TEST_HOST
1191+ }
1192+ },
1193+ ],
1194+ }, {
1195+ "current-context" : "simple_token" ,
1196+ "contexts" : [
1197+ {
1198+ "name" : "simple_token" ,
1199+ "context" : {
1200+ "cluster" : "default" ,
1201+ "user" : "simple_token"
1202+ }
1203+ },
1204+ ],
1205+ }, {
1206+ "users" : [
1207+ {
1208+ "name" : "simple_token" ,
1209+ "user" : {
1210+ "token" : TEST_DATA_BASE64 ,
1211+ "username" : TEST_USERNAME ,
1212+ "password" : TEST_PASSWORD ,
1213+ }
1214+ },
1215+ ]
1216+ }]
1217+
1218+ def _create_multi_config (self , parts ):
11891219 files = []
1190- for part in (
1191- self .TEST_KUBE_CONFIG_PART1 ,
1192- self .TEST_KUBE_CONFIG_PART2 ,
1193- self .TEST_KUBE_CONFIG_PART3 ):
1220+ for part in parts :
11941221 files .append (self ._create_temp_file (yaml .safe_dump (part )))
11951222 return ENV_KUBECONFIG_PATH_SEPARATOR .join (files )
11961223
11971224 def test_list_kube_config_contexts (self ):
1198- kubeconfigs = self ._create_multi_config ()
1225+ kubeconfigs = self ._create_multi_config (self . TEST_KUBE_CONFIG_SET1 )
11991226 expected_contexts = [
12001227 {'context' : {'cluster' : 'default' }, 'name' : 'no_user' },
12011228 {'context' : {'cluster' : 'ssl' , 'user' : 'ssl' }, 'name' : 'ssl' },
@@ -1207,18 +1234,34 @@ def test_list_kube_config_contexts(self):
12071234 config_file = kubeconfigs )
12081235
12091236 self .assertEqual (contexts , expected_contexts )
1210- self .assertEqual (active_context , expected_contexts [0 ])
1237+ self .assertEqual (active_context , expected_contexts [3 ])
12111238
12121239 async def test_new_client_from_config (self ):
1213- kubeconfigs = self ._create_multi_config ()
1240+ kubeconfigs = self ._create_multi_config (self . TEST_KUBE_CONFIG_SET1 )
12141241 client = await new_client_from_config (
12151242 config_file = kubeconfigs , context = "simple_token" )
12161243 self .assertEqual (TEST_HOST , client .configuration .host )
12171244 self .assertEqual (BEARER_TOKEN_FORMAT % TEST_DATA_BASE64 ,
12181245 client .configuration .api_key ['BearerToken' ])
12191246
1247+ async def test_merge_with_context_in_different_file (self ):
1248+ kubeconfigs = self ._create_multi_config (self .TEST_KUBE_CONFIG_SET2 )
1249+ client = await new_client_from_config (config_file = kubeconfigs )
1250+
1251+ expected_contexts = [
1252+ {'context' : {'cluster' : 'default' , 'user' : 'simple_token' },
1253+ 'name' : 'simple_token' }
1254+ ]
1255+ contexts , active_context = list_kube_config_contexts (
1256+ config_file = kubeconfigs )
1257+ self .assertEqual (contexts , expected_contexts )
1258+ self .assertEqual (active_context , expected_contexts [0 ])
1259+ self .assertEqual (TEST_HOST , client .configuration .host )
1260+ self .assertEqual (BEARER_TOKEN_FORMAT % TEST_DATA_BASE64 ,
1261+ client .configuration .api_key ['BearerToken' ])
1262+
12201263 def test_save_changes (self ):
1221- kubeconfigs = self ._create_multi_config ()
1264+ kubeconfigs = self ._create_multi_config (self . TEST_KUBE_CONFIG_SET1 )
12221265
12231266 # load configuration, update token, save config
12241267 kconf = KubeConfigMerger (kubeconfigs )
0 commit comments