Skip to content

Commit

Permalink
use default/1st namespace if there's no current (#668)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Dec 18, 2023
1 parent 12de722 commit 4b869df
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,12 @@ abstract class ActiveContext<N : HasMetadata, C : KubernetesClient>(

override fun getCurrentNamespace(): String? {
val current = client.namespace
return if (current.isNullOrEmpty()) {
null
} else {
return if (!current.isNullOrEmpty()) {
current
} else {
val allNamespaces = getAllResources(namespaceKind, NO_NAMESPACE)
val namespace = allNamespaces.find { namespace:HasMetadata -> "default" == namespace.metadata.name } ?: allNamespaces.firstOrNull()
return namespace?.metadata?.name
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ open class KubernetesContext(
) {

override val namespaceKind : ResourceKind<Namespace> = NamespacesOperator.KIND

private val replicasOperator = KubernetesReplicas(
NonCachingSingleResourceOperator(client),
object: ResourcesRetrieval {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.redhat.devtools.intellij.kubernetes.model.client.ClientAdapter
import com.redhat.devtools.intellij.kubernetes.model.client.KubeClientAdapter
import com.redhat.devtools.intellij.kubernetes.model.context.IActiveContext.ResourcesIn
import com.redhat.devtools.intellij.kubernetes.model.dashboard.IDashboard
import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks
import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks.NAMESPACE1
import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks.NAMESPACE2
import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks.NAMESPACE3
Expand All @@ -39,6 +40,7 @@ import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks.client
import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks.customResource
import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks.customResourceDefinition
import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks.namedContext
import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks.namespaceListOperation
import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks.resource
import com.redhat.devtools.intellij.kubernetes.model.mocks.Mocks.namespacedResourceOperator
import com.redhat.devtools.intellij.kubernetes.model.mocks.Mocks.nonNamespacedResourceOperator
Expand Down Expand Up @@ -71,8 +73,10 @@ import org.junit.Test

class KubernetesContextTest {

private val DEFAULT_NAMESPACE = resource<Namespace>("default", null, "defaultNsUid1", "v1", "1")

private val modelChange: ResourceModelObservable = mock()
private val allNamespaces = arrayOf(NAMESPACE1, NAMESPACE2, NAMESPACE3)
private val allNamespaces = arrayOf(NAMESPACE1, NAMESPACE2, DEFAULT_NAMESPACE, NAMESPACE3)
private val currentNamespace = NAMESPACE2
private val client = KubeClientAdapter(client(currentNamespace.metadata.name, allNamespaces))
private val namespaceWatchOperation: (watcher: Watcher<in Namespace>) -> Watch? = { null }
Expand Down Expand Up @@ -205,13 +209,40 @@ class KubernetesContextTest {
}

@Test
fun `#getCurrentNamespace should use null if no namespace set in client`() {
fun `#getCurrentNamespace should return 'default' namespace if no current namespace set in client`() {
// given
whenever(client.get().namespace)
.thenReturn(null)
// when
val namespace = context.getCurrentNamespace()
// then
assertThat(namespace).isEqualTo(DEFAULT_NAMESPACE.metadata.name)
}

@Test
fun `#getCurrentNamespace should return 1st namespace if no current namespace set in client and there's no 'default' namespace`() {
// given
val allNamespaces = arrayOf(NAMESPACE1, NAMESPACE2, NAMESPACE3)
whenever(namespacesOperator.allResources)
.thenReturn(allNamespaces.asList())
whenever(client.get().namespace)
.thenReturn(null)
// when
val namespace = context.getCurrentNamespace()
// then
assertThat(namespace).isEqualTo(NAMESPACE1.metadata.name)
}

@Test
fun `#getCurrentNamespace should return null if no current namespace set in client and there are no namespaces`() {
// given
whenever(namespacesOperator.allResources)
.thenReturn(emptyList())
whenever(client.get().namespace)
.thenReturn(null)
// when
val namespace = context.getCurrentNamespace()
// then
assertThat(namespace).isNull()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ object ClientMocks {
}
}

private fun namespaceListOperation(namespaces: Array<Namespace>): NamespaceListOperation {
fun namespaceListOperation(namespaces: Array<Namespace>): NamespaceListOperation {
val namespaceList: NamespaceList = mock()
doReturn(namespaces.asList())
.whenever(namespaceList).items
Expand Down

0 comments on commit 4b869df

Please sign in to comment.