diff --git a/src/main/kotlin/com/redhat/devtools/gateway/kubeconfig/KubeConfigUpdate.kt b/src/main/kotlin/com/redhat/devtools/gateway/kubeconfig/KubeConfigUpdate.kt index 8fc399ef..286d2356 100644 --- a/src/main/kotlin/com/redhat/devtools/gateway/kubeconfig/KubeConfigUpdate.kt +++ b/src/main/kotlin/com/redhat/devtools/gateway/kubeconfig/KubeConfigUpdate.kt @@ -51,7 +51,39 @@ abstract class KubeConfigUpdate private constructor( abstract fun apply() - protected fun save( + protected fun setCurrentContext(currentContextName: String, currentContextConfig: KubeConfig?) { + currentContextConfig?.setContext(currentContextName) + } + + protected fun saveConfigs( + primaryConfig: KubeConfig, + currentContextConfig: KubeConfig?, + currentContextName: String? + ) { + when { + currentContextConfig == null -> + saveConfig(primaryConfig) + primaryConfig.path == currentContextConfig.path -> + saveConfig(primaryConfig, currentContextName ?: primaryConfig.currentContext) + else -> { + saveConfig(primaryConfig) + saveConfig(currentContextConfig, currentContextName ?: currentContextConfig.currentContext) + } + } + } + + protected fun saveConfig(config: KubeConfig, currentContext: String? = config.currentContext) { + saveConfig( + config.contexts, + config.clusters, + config.users, + config.preferences, + currentContext, + config.path + ) + } + + private fun saveConfig( contexts: ArrayList?, clusters: ArrayList?, users: ArrayList?, @@ -73,6 +105,76 @@ abstract class KubeConfigUpdate private constructor( ) } + protected fun addNewContext(user: KubeConfigNamedUser): KubeConfig? { + val config = allConfigs.firstOrNull() ?: return null + + val users = config.users ?: ArrayList() + users.add(user.toMap()) + + val cluster = createCluster(allConfigs) + val clusters = config.clusters ?: ArrayList() + clusters.add(cluster.toMap()) + + val context = createContext(user, cluster, allConfigs) + val contexts = config.contexts ?: ArrayList() + contexts.add(context.toMap()) + + config.setContext(context.name) + + return config + } + + protected fun uniqueUserName(allConfigs: List): String { + val existingUserNames = getAllExistingNames(allConfigs) { it.users } + return UniqueNameGenerator.generateUniqueName(clusterName, existingUserNames) + } + + private fun createCluster(allConfigs: List): KubeConfigNamedCluster { + val existingClusterNames = getAllExistingNames(allConfigs) { it.clusters } + val uniqueClusterName = UniqueNameGenerator.generateUniqueName(clusterName, existingClusterNames) + + return KubeConfigNamedCluster( + KubeConfigCluster(clusterUrl), + uniqueClusterName + ) + } + + private fun createContext( + user: KubeConfigNamedUser, + cluster: KubeConfigNamedCluster, + allConfigs: List + ): KubeConfigNamedContext { + val existingContextNames = getAllExistingNames(allConfigs) { it.contexts } + val tempContext = KubeConfigNamedContext( + KubeConfigContext( + user.name, + cluster.name + ) + ) + val uniqueContextName = UniqueNameGenerator.generateUniqueName(tempContext.name, existingContextNames) + + return KubeConfigNamedContext( + KubeConfigContext( + user.name, + cluster.name + ), + uniqueContextName + ) + } + + private fun getAllExistingNames( + allConfigs: List, + extractList: (KubeConfig) -> List<*>? + ): Set { + return allConfigs + .flatMap { config -> extractList(config) ?: emptyList() } + .mapNotNull { entryObject -> + val entryMap = entryObject as? Map<*, *> ?: return@mapNotNull null + entryMap["name"] as? String + } + .toSet() + } + class UpdateToken( clusterName: String, clusterUrl: String, @@ -82,128 +184,38 @@ abstract class KubeConfigUpdate private constructor( ) : KubeConfigUpdate(clusterName, clusterUrl, token, allConfigs) { override fun apply() { - updateToken(context.context.user) - updateCurrentContext(context.name) + val config = KubeConfigUtils.getConfigByUser(context, allConfigs) ?: return + setTokenFor(context.context.user, config) + val currentContextConfig = KubeConfigUtils.getConfigWithCurrentContext(allConfigs) + val currentContextName = context.name + setCurrentContext(currentContextName, currentContextConfig) + saveConfigs(config, currentContextConfig, currentContextName) } - private fun updateToken(username: String) { - val config = KubeConfigUtils.getConfigByUser(context, allConfigs) ?: return + private fun setTokenFor(username: String, config: KubeConfig) { config.users?.find { user -> username == Utils.getValue(user, arrayOf("name")) }?.apply { Utils.setValue(this, token, arrayOf("user", "token")) } - - save( - config.contexts, - config.clusters, - config.users, - config.preferences, - config.currentContext, - config.path - ) } - private fun updateCurrentContext(contextName: String) { - val config = KubeConfigUtils.getConfigWithCurrentContext(allConfigs) ?: return - save( - config.contexts, - config.clusters, - config.users, - config.preferences, - contextName, - config.path - ) - } } class CreateContext( clusterName: String, clusterUrl: String, - token: String, + private val authToken: String, allConfigs: List - ) : KubeConfigUpdate(clusterName, clusterUrl, token, allConfigs) { - override fun apply() { - // create new context in first config - val config = allConfigs.firstOrNull() ?: return - - val user = createUser(allConfigs) - val users = config.users ?: ArrayList() - users.add(user.toMap()) - - val cluster = createCluster(allConfigs) - val clusters = config.clusters ?: ArrayList() - clusters.add(cluster.toMap()) - - val context = createContext(user, cluster, allConfigs) - val contexts = config.contexts ?: ArrayList() - contexts.add(context.toMap()) - - config.setContext(context.name) - - save( - contexts, - clusters, - users, - config.preferences, - config.currentContext, - config.path - ) - } + ) : KubeConfigUpdate(clusterName, clusterUrl, authToken, allConfigs) { - private fun createUser(allConfigs: List): KubeConfigNamedUser { - val existingUserNames = getAllExistingNames(allConfigs) { it.users } - val uniqueUserName = UniqueNameGenerator.generateUniqueName(clusterName, existingUserNames) - return KubeConfigNamedUser( - KubeConfigUser(token), - uniqueUserName - ) - } - - private fun createCluster(allConfigs: List): KubeConfigNamedCluster { - val existingClusterNames = getAllExistingNames(allConfigs) { it.clusters } - val uniqueClusterName = UniqueNameGenerator.generateUniqueName(clusterName, existingClusterNames) - - return KubeConfigNamedCluster( - KubeConfigCluster(clusterUrl), - uniqueClusterName - ) - } - - private fun createContext( - user: KubeConfigNamedUser, - cluster: KubeConfigNamedCluster, - allConfigs: List - ): KubeConfigNamedContext { - val existingContextNames = getAllExistingNames(allConfigs) { it.contexts } - val tempContext = KubeConfigNamedContext( - KubeConfigContext( - user.name, - cluster.name - ) - ) - val uniqueContextName = UniqueNameGenerator.generateUniqueName(tempContext.name, existingContextNames) - - return KubeConfigNamedContext( - KubeConfigContext( - user.name, - cluster.name - ), - uniqueContextName + override fun apply() { + val user = KubeConfigNamedUser( + KubeConfigUser(authToken), + uniqueUserName(allConfigs) ) - } - - private fun getAllExistingNames( - allConfigs: List, - extractList: (KubeConfig) -> List<*>? - ): Set { - return allConfigs - .flatMap { config -> extractList(config) ?: emptyList() } - .mapNotNull { entryObject -> - val entryMap = entryObject as? Map<*, *> ?: return@mapNotNull null - entryMap["name"] as? String - } - .toSet() + val config = addNewContext(user) ?: return + saveConfig(config) } } @@ -217,13 +229,15 @@ abstract class KubeConfigUpdate private constructor( ) : KubeConfigUpdate(clusterName, clusterUrl, "", allConfigs) { override fun apply() { - updateClientCert(context.context.user) - updateCurrentContext(context.name) - } - - private fun updateClientCert(username: String) { val config = KubeConfigUtils.getConfigByUser(context, allConfigs) ?: return + setClientCert(config, context.context.user) + val currentContextConfig = KubeConfigUtils.getConfigWithCurrentContext(allConfigs) + val currentContextName = context.name + setCurrentContext(currentContextName, currentContextConfig) + saveConfigs(config, currentContextConfig, currentContextName) + } + private fun setClientCert(config: KubeConfig, username: String) { config.users?.find { user -> username == Utils.getValue(user, arrayOf("name")) }?.apply { @@ -233,27 +247,6 @@ abstract class KubeConfigUpdate private constructor( // remove token if present Utils.removeValue(this, arrayOf("user", "token")) } - - save( - config.contexts, - config.clusters, - config.users, - config.preferences, - config.currentContext, - config.path - ) - } - - private fun updateCurrentContext(contextName: String) { - val config = KubeConfigUtils.getConfigWithCurrentContext(allConfigs) ?: return - save( - config.contexts, - config.clusters, - config.users, - config.preferences, - contextName, - config.path - ) } } @@ -266,89 +259,16 @@ abstract class KubeConfigUpdate private constructor( ) : KubeConfigUpdate(clusterName, clusterUrl, "", allConfigs) { override fun apply() { - val config = allConfigs.firstOrNull() ?: return - - val user = createUser(allConfigs) - val users = config.users ?: ArrayList() - users.add(user.toMap()) - - val cluster = createCluster(allConfigs) - val clusters = config.clusters ?: ArrayList() - clusters.add(cluster.toMap()) - - val context = createContext(user, cluster, allConfigs) - val contexts = config.contexts ?: ArrayList() - contexts.add(context.toMap()) - - config.setContext(context.name) - - save( - contexts, - clusters, - users, - config.preferences, - config.currentContext, - config.path - ) - } - - private fun createUser(allConfigs: List): KubeConfigNamedUser { - val existingUserNames = getAllExistingNames(allConfigs) { it.users } - val uniqueUserName = - UniqueNameGenerator.generateUniqueName(clusterName, existingUserNames) - - return KubeConfigNamedUser( + val user = KubeConfigNamedUser( KubeConfigUser( token = null, clientCertificate = CertificateSource.fromData(clientCertPem), clientKey = CertificateSource.fromData(clientKeyPem) ), - uniqueUserName + uniqueUserName(allConfigs) ) - } - - private fun createCluster(allConfigs: List): KubeConfigNamedCluster { - val existingClusterNames = getAllExistingNames(allConfigs) { it.clusters } - val uniqueClusterName = - UniqueNameGenerator.generateUniqueName(clusterName, existingClusterNames) - - return KubeConfigNamedCluster( - KubeConfigCluster(clusterUrl), - uniqueClusterName - ) - } - - private fun createContext( - user: KubeConfigNamedUser, - cluster: KubeConfigNamedCluster, - allConfigs: List - ): KubeConfigNamedContext { - val existingContextNames = getAllExistingNames(allConfigs) { it.contexts } - - val tempContext = KubeConfigNamedContext( - KubeConfigContext(user.name, cluster.name) - ) - - val uniqueContextName = - UniqueNameGenerator.generateUniqueName(tempContext.name, existingContextNames) - - return KubeConfigNamedContext( - KubeConfigContext(user.name, cluster.name), - uniqueContextName - ) - } - - private fun getAllExistingNames( - allConfigs: List, - extractList: (KubeConfig) -> List<*>? - ): Set { - return allConfigs - .flatMap { config -> extractList(config) ?: emptyList() } - .mapNotNull { entryObject -> - val entryMap = entryObject as? Map<*, *> ?: return@mapNotNull null - entryMap["name"] as? String - } - .toSet() + val config = addNewContext(user) ?: return + saveConfig(config) } } } diff --git a/src/test/kotlin/com/redhat/devtools/gateway/kubeconfig/KubeConfigUpdateTest.kt b/src/test/kotlin/com/redhat/devtools/gateway/kubeconfig/KubeConfigUpdateTest.kt index dd2abf9b..34da98e1 100644 --- a/src/test/kotlin/com/redhat/devtools/gateway/kubeconfig/KubeConfigUpdateTest.kt +++ b/src/test/kotlin/com/redhat/devtools/gateway/kubeconfig/KubeConfigUpdateTest.kt @@ -11,6 +11,7 @@ */ package com.redhat.devtools.gateway.kubeconfig +import com.redhat.devtools.gateway.auth.tls.PemUtils import com.redhat.devtools.gateway.openshift.Utils import io.kubernetes.client.util.KubeConfig import io.mockk.* @@ -76,6 +77,101 @@ class KubeConfigUpdateTest { } } + @Test + fun `#apply CreateContextWithClientCert creates the context if it does not exist`() { + // given + val data = CreateContextWithClientCertTestData() + val config = KubeConfigTestHelpers.createMockKubeConfig(tempKubeConfigFile) + val allConfigs = listOf(config) + setupCreateContextMocks(data.clusterName, allConfigs, tempKubeConfigFile) + + val update = KubeConfigUpdate.CreateContextWithClientCert( + data.clusterName, + data.clusterUrl, + data.clientCertPem, + data.clientKeyPem, + allConfigs + ) + + // when + update.apply() + + // then + verify { + anyConstructed().save( + match { contexts -> + assertThat(contexts).hasSize(1) + verifyContext( + contexts[0] as Map<*, *>, + "${data.clusterName}/${data.clusterName}", + data.clusterName, + data.clusterName + ) + }, + match { clusters -> + assertThat(clusters).hasSize(1) + verifyCluster(clusters[0] as Map<*, *>, data.clusterName, data.clusterUrl) + }, + match { users -> + assertThat(users).hasSize(1) + verifyUserWithClientCert( + users[0] as Map<*, *>, + data.clusterName, + data.clientCertPem, + data.clientKeyPem + ) + }, + any(), + any(), + ) + } + } + + @Test + fun `#apply CreateContextWithClientCert generates unique user name if user name already exists`() { + // given + val data = CreateContextWithClientCertTestData() + val existingUserMap = KubeConfigTestHelpers.createUserMap(data.clusterName, "existing-token") + + val config = KubeConfigTestHelpers.createMockKubeConfig( + tempKubeConfigFile, + users = listOf(existingUserMap) + ) + val allConfigs = listOf(config) + setupCreateContextMocks(data.clusterName, allConfigs, tempKubeConfigFile) + + val update = KubeConfigUpdate.CreateContextWithClientCert( + data.clusterName, + data.clusterUrl, + data.clientCertPem, + data.clientKeyPem, + allConfigs + ) + + // when + update.apply() + + // then + verify { + anyConstructed().save( + any(), + any(), + match { users -> + verifyNewEntryInList(users, 2, "${data.clusterName}2") { userMap -> + verifyUserWithClientCert( + userMap, + "${data.clusterName}2", + data.clientCertPem, + data.clientKeyPem + ) + } + }, + any(), + any(), + ) + } + } + @Test fun `#apply updates the token if context already exists`() { // given @@ -83,7 +179,7 @@ class KubeConfigUpdateTest { val (existingUserMap, existingClusterMap, existingContextMap) = createUpdateTokenTestMaps(data) val config = KubeConfigTestHelpers.createMockKubeConfig(tempKubeConfigFile, existingUserMap, existingClusterMap, existingContextMap) val allConfigs = listOf(config) - val mockContext = setupUpdateTokenMocks(data, allConfigs, config, null) + val mockContext = setupUpdateExistingContextMocks(data.clusterName, data.userName, data.contextName, allConfigs, config, null) val update = KubeConfigUpdate.UpdateToken(data.clusterName, data.clusterUrl, data.newToken, mockContext, allConfigs) @@ -119,45 +215,146 @@ class KubeConfigUpdateTest { val configForToken = KubeConfigTestHelpers.createMockKubeConfig(tempKubeConfigFile, existingUserMap, existingClusterMap, existingContextMap, "other-context") val configForCurrentContext = KubeConfigTestHelpers.createMockKubeConfig(tempKubeConfigFile, existingUserMap, existingClusterMap, existingContextMap, "other-context") val allConfigs = listOf(configForToken, configForCurrentContext) - val mockContext = setupUpdateTokenMocks(data, allConfigs, configForToken, configForCurrentContext) + val mockContext = setupUpdateExistingContextMocks(data.clusterName, data.userName, data.contextName, allConfigs, configForToken, configForCurrentContext) val update = KubeConfigUpdate.UpdateToken(data.clusterName, data.clusterUrl, data.newToken, mockContext, allConfigs) // when update.apply() - // then - verify that save is called twice: once for token update, once for current context update - verify(exactly = 2) { + // then - token and current-context changes are persisted in a single save + verify(exactly = 1) { anyConstructed().save(any(), any(), any(), any(), any()) } - - // Verify both calls were made: first with original context, second with context name - // The match function returns boolean - MockK will try calls until one matches - verify(atLeast = 1) { + + verify { anyConstructed().save( any(), any(), + match { users -> + assertThat(users).hasSize(1) + verifyUser(users[0] as Map<*, *>, data.userName, data.newToken) + }, any(), - any(), - match { currentContext -> - currentContext == "other-context" + match { currentContext -> + currentContext == data.contextName }, ) } - - verify(atLeast = 1) { + } + + @Test + fun `#apply UpdateClientCert updates client cert if context already exists`() { + // given + val data = UpdateClientCertTestData() + val (existingUserMap, existingClusterMap, existingContextMap) = createUpdateClientCertTestMaps(data) + val config = KubeConfigTestHelpers.createMockKubeConfig(tempKubeConfigFile, existingUserMap, existingClusterMap, existingContextMap) + val allConfigs = listOf(config) + val mockContext = setupUpdateExistingContextMocks(data.clusterName, data.userName, data.contextName, allConfigs, config, null) + + val update = KubeConfigUpdate.UpdateClientCert( + data.clusterName, + data.clusterUrl, + data.clientCertPem, + data.clientKeyPem, + mockContext, + allConfigs + ) + + // when + update.apply() + + // then + verify { anyConstructed().save( + match { contexts -> + assertThat(contexts).hasSize(1) + verifyContext(contexts[0] as Map<*, *>, data.clusterName, data.clusterName, data.userName) + }, + match { clusters -> + assertThat(clusters).hasSize(1) + verifyCluster(clusters[0] as Map<*, *>, data.clusterName, data.clusterUrl) + }, + match { users -> + assertThat(users).hasSize(1) + verifyUserWithClientCert(users[0] as Map<*, *>, data.userName, data.clientCertPem, data.clientKeyPem) + }, + any(), any(), + ) + } + } + + @Test + fun `#apply UpdateClientCert sets current context when updating client cert`() { + // given + val data = UpdateClientCertTestData() + val (existingUserMap, existingClusterMap, existingContextMap) = createUpdateClientCertTestMaps(data) + val configForUser = KubeConfigTestHelpers.createMockKubeConfig(tempKubeConfigFile, existingUserMap, existingClusterMap, existingContextMap, "other-context") + val configForCurrentContext = KubeConfigTestHelpers.createMockKubeConfig(tempKubeConfigFile, existingUserMap, existingClusterMap, existingContextMap, "other-context") + val allConfigs = listOf(configForUser, configForCurrentContext) + val mockContext = setupUpdateExistingContextMocks(data.clusterName, data.userName, data.contextName, allConfigs, configForUser, configForCurrentContext) + + val update = KubeConfigUpdate.UpdateClientCert( + data.clusterName, + data.clusterUrl, + data.clientCertPem, + data.clientKeyPem, + mockContext, + allConfigs + ) + + // when + update.apply() + + // then - cert and current-context changes are persisted in a single save + verify(exactly = 1) { + anyConstructed().save(any(), any(), any(), any(), any()) + } + + verify { + anyConstructed().save( any(), any(), + match { users -> + assertThat(users).hasSize(1) + verifyUserWithClientCert(users[0] as Map<*, *>, data.userName, data.clientCertPem, data.clientKeyPem) + }, any(), - match { currentContext -> + match { currentContext -> currentContext == data.contextName }, ) } } + @Test + fun `#apply UpdateClientCert does not set current context when no config has current context`() { + // given + val data = UpdateClientCertTestData() + val (existingUserMap, existingClusterMap, existingContextMap) = createUpdateClientCertTestMaps(data) + val config = KubeConfigTestHelpers.createMockKubeConfig(tempKubeConfigFile, existingUserMap, existingClusterMap, existingContextMap, "") + val allConfigs = listOf(config) + val mockContext = setupUpdateExistingContextMocks(data.clusterName, data.userName, data.contextName, allConfigs, config, null) + + val update = KubeConfigUpdate.UpdateClientCert( + data.clusterName, + data.clusterUrl, + data.clientCertPem, + data.clientKeyPem, + mockContext, + allConfigs + ) + + // when + update.apply() + + // then + verify(exactly = 1) { + anyConstructed().save(any(), any(), any(), any(), any()) + } + } + @Test fun `#apply does not set current context when no config has current context`() { // given @@ -165,7 +362,7 @@ class KubeConfigUpdateTest { val (existingUserMap, existingClusterMap, existingContextMap) = createUpdateTokenTestMaps(data) val config = KubeConfigTestHelpers.createMockKubeConfig(tempKubeConfigFile, existingUserMap, existingClusterMap, existingContextMap, "") val allConfigs = listOf(config) - val mockContext = setupUpdateTokenMocks(data, allConfigs, config, null) + val mockContext = setupUpdateExistingContextMocks(data.clusterName, data.userName, data.contextName, allConfigs, config, null) val update = KubeConfigUpdate.UpdateToken(data.clusterName, data.clusterUrl, data.newToken, mockContext, allConfigs) @@ -472,7 +669,7 @@ class KubeConfigUpdateTest { every { config.preferences } returns null val allConfigs = listOf(config) - val mockContext = setupUpdateTokenMocks(data, allConfigs, config, null) + val mockContext = setupUpdateExistingContextMocks(data.clusterName, data.userName, data.contextName, allConfigs, config, null) val update = KubeConfigUpdate.UpdateToken(data.clusterName, data.clusterUrl, data.newToken, mockContext, allConfigs) @@ -491,6 +688,41 @@ class KubeConfigUpdateTest { } } + @Test + fun `#apply UpdateClientCert updates client cert when preferences are null`() { + // given + val data = UpdateClientCertTestData() + val (existingUserMap, existingClusterMap, existingContextMap) = createUpdateClientCertTestMaps(data) + val config = KubeConfigTestHelpers.createMockKubeConfig(tempKubeConfigFile, existingUserMap, existingClusterMap, existingContextMap) + every { config.preferences } returns null + + val allConfigs = listOf(config) + val mockContext = setupUpdateExistingContextMocks(data.clusterName, data.userName, data.contextName, allConfigs, config, null) + + val update = KubeConfigUpdate.UpdateClientCert( + data.clusterName, + data.clusterUrl, + data.clientCertPem, + data.clientKeyPem, + mockContext, + allConfigs + ) + + // when + update.apply() + + // then + verify { + anyConstructed().save( + any(), + any(), + any(), + null, + any() + ) + } + } + private data class UpdateTokenTestData( val oldToken: String = "use-the-force", val newToken: String = "may-the-force-be-with-you", @@ -506,6 +738,23 @@ class KubeConfigUpdateTest { val token: String = "join-the-dark-side" ) + private data class CreateContextWithClientCertTestData( + val clusterName: String = "death-star", + val clusterUrl: String = "https://death-star.com", + val clientCertPem: String = "empire-client-cert", + val clientKeyPem: String = "empire-client-key" + ) + + private data class UpdateClientCertTestData( + val oldToken: String = "use-the-force", + val clientCertPem: String = "rebel-client-cert", + val clientKeyPem: String = "rebel-client-key", + val clusterName: String = "yavin-4", + val clusterUrl: String = "https://yavin-4.com", + val userName: String = "leia-organa", + val contextName: String = clusterName + ) + // Helper functions for test verification and data creation private fun findEntryByName(list: List<*>, expectedName: String): Map<*, *>? { @@ -543,6 +792,22 @@ class KubeConfigUpdateTest { return true } + private fun verifyUserWithClientCert( + user: Map<*, *>, + expectedName: String, + expectedCertPem: String, + expectedKeyPem: String + ): Boolean { + val name = Utils.getValue(user, arrayOf("name")) as String + val cert = Utils.getValue(user, arrayOf("user", "client-certificate-data")) as String + val key = Utils.getValue(user, arrayOf("user", "client-key-data")) as String + assertThat(name).isEqualTo(expectedName) + assertThat(cert).isEqualTo(PemUtils.toBase64(expectedCertPem)) + assertThat(key).isEqualTo(PemUtils.toBase64(expectedKeyPem)) + assertThat(Utils.getValue(user, arrayOf("user", "token"))).isNull() + return true + } + private fun verifyCurrentContext(currentContext: String?, expectedContextName: String): Boolean { assertThat(currentContext).isEqualTo(expectedContextName) return true @@ -568,18 +833,26 @@ class KubeConfigUpdateTest { return Triple(existingUserMap, existingClusterMap, existingContextMap) } + private fun createUpdateClientCertTestMaps(data: UpdateClientCertTestData): Triple, MutableMap, MutableMap> { + val existingUserMap = KubeConfigTestHelpers.createUserMap(data.userName, data.oldToken) + val existingClusterMap = KubeConfigTestHelpers.createClusterMap(data.clusterName, data.clusterUrl) + val existingContextMap = KubeConfigTestHelpers.createContextMap(data.contextName, data.clusterName, data.userName) + return Triple(existingUserMap, existingClusterMap, existingContextMap) + } - private fun setupUpdateTokenMocks( - data: UpdateTokenTestData, + private fun setupUpdateExistingContextMocks( + clusterName: String, + userName: String, + contextName: String, allConfigs: List, configForUser: KubeConfig, configForCurrentContext: KubeConfig? ): KubeConfigNamedContext { mockkObject(KubeConfigNamedContext) val mockContext = mockk(relaxed = true) - every { mockContext.context } returns KubeConfigContext(data.userName, data.clusterName) - every { mockContext.name } returns data.contextName - every { KubeConfigNamedContext.getByClusterName(data.clusterName, allConfigs) } returns mockContext + every { mockContext.context } returns KubeConfigContext(userName, clusterName) + every { mockContext.name } returns contextName + every { KubeConfigNamedContext.getByClusterName(clusterName, allConfigs) } returns mockContext every { KubeConfigUtils.getAllConfigs(any()) } returns allConfigs every { KubeConfigUtils.getAllConfigFiles() } returns listOf(tempKubeConfigFile) every { KubeConfigUtils.getConfigByUser(mockContext, allConfigs) } returns configForUser