@@ -1075,7 +1075,7 @@ async def load_from_exec_plugin(self):
1075
1075
1076
1076
1077
1077
class TestKubeConfigMerger (BaseTestCase ):
1078
- TEST_KUBE_CONFIG_PART1 = {
1078
+ TEST_KUBE_CONFIG_SET1 = [ {
1079
1079
"current-context" : "no_user" ,
1080
1080
"contexts" : [
1081
1081
{
@@ -1094,9 +1094,7 @@ class TestKubeConfigMerger(BaseTestCase):
1094
1094
},
1095
1095
],
1096
1096
"users" : []
1097
- }
1098
-
1099
- TEST_KUBE_CONFIG_PART2 = {
1097
+ }, {
1100
1098
"current-context" : "" ,
1101
1099
"contexts" : [
1102
1100
{
@@ -1134,10 +1132,8 @@ class TestKubeConfigMerger(BaseTestCase):
1134
1132
}
1135
1133
},
1136
1134
]
1137
- }
1138
-
1139
- TEST_KUBE_CONFIG_PART3 = {
1140
- "current-context" : "no_user" ,
1135
+ }, {
1136
+ "current-context" : "expired_oidc" ,
1141
1137
"contexts" : [
1142
1138
{
1143
1139
"name" : "expired_oidc" ,
@@ -1183,19 +1179,50 @@ class TestKubeConfigMerger(BaseTestCase):
1183
1179
}
1184
1180
},
1185
1181
]
1186
- }
1182
+ }]
1187
1183
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 ):
1189
1219
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 :
1194
1221
files .append (self ._create_temp_file (yaml .safe_dump (part )))
1195
1222
return ENV_KUBECONFIG_PATH_SEPARATOR .join (files )
1196
1223
1197
1224
def test_list_kube_config_contexts (self ):
1198
- kubeconfigs = self ._create_multi_config ()
1225
+ kubeconfigs = self ._create_multi_config (self . TEST_KUBE_CONFIG_SET1 )
1199
1226
expected_contexts = [
1200
1227
{'context' : {'cluster' : 'default' }, 'name' : 'no_user' },
1201
1228
{'context' : {'cluster' : 'ssl' , 'user' : 'ssl' }, 'name' : 'ssl' },
@@ -1207,18 +1234,34 @@ def test_list_kube_config_contexts(self):
1207
1234
config_file = kubeconfigs )
1208
1235
1209
1236
self .assertEqual (contexts , expected_contexts )
1210
- self .assertEqual (active_context , expected_contexts [0 ])
1237
+ self .assertEqual (active_context , expected_contexts [3 ])
1211
1238
1212
1239
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 )
1214
1241
client = await new_client_from_config (
1215
1242
config_file = kubeconfigs , context = "simple_token" )
1216
1243
self .assertEqual (TEST_HOST , client .configuration .host )
1217
1244
self .assertEqual (BEARER_TOKEN_FORMAT % TEST_DATA_BASE64 ,
1218
1245
client .configuration .api_key ['BearerToken' ])
1219
1246
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
+
1220
1263
def test_save_changes (self ):
1221
- kubeconfigs = self ._create_multi_config ()
1264
+ kubeconfigs = self ._create_multi_config (self . TEST_KUBE_CONFIG_SET1 )
1222
1265
1223
1266
# load configuration, update token, save config
1224
1267
kconf = KubeConfigMerger (kubeconfigs )
0 commit comments