From 402cc2c4b908a0aca1c461f89c0a70f9958a9948 Mon Sep 17 00:00:00 2001 From: Kumbirai Tanekha Date: Thu, 3 Jan 2019 18:06:13 +0000 Subject: [PATCH] add cli tests for kubens (#117) * split bats test invocation by executable * add more cli tests for kubens * clean up kubens tests * small cleanup to kubens tests --- .travis.yml | 3 +- kubens | 18 +++++---- test/common.bash | 12 +++++- test/kubens.bats | 93 ++++++++++++++++++++++++++++++++++++++++++++++- test/mock-kubectl | 12 ++++++ 5 files changed, 125 insertions(+), 13 deletions(-) create mode 100755 test/mock-kubectl diff --git a/.travis.yml b/.travis.yml index 05535f8..5ec9c8f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,4 +5,5 @@ before_install: - sudo curl -fsSL -o /usr/bin/kubectl https://storage.googleapis.com/kubernetes-release/release/v1.13.1/bin/linux/amd64/kubectl - sudo chmod +x /usr/bin/kubectl script: - - bats test/ + - bats test/kubectx.bats + - bats test/kubens.bats diff --git a/kubens b/kubens index a1e3b25..361e3b3 100755 --- a/kubens +++ b/kubens @@ -54,7 +54,7 @@ current_namespace() { } current_context() { - $KUBECTL config view -o=jsonpath='{.current-context}' + $KUBECTL config current-context } get_namespaces() { @@ -173,13 +173,15 @@ swap_namespace() { } main() { - if hash kubectl 2>/dev/null; then - KUBECTL=kubectl - elif hash kubectl.exe 2>/dev/null; then - KUBECTL=kubectl.exe - else - echo >&2 "kubectl is not installed" - exit 1 + if [[ -z "${KUBECTL:-}" ]]; then + if hash kubectl 2>/dev/null; then + KUBECTL=kubectl + elif hash kubectl.exe 2>/dev/null; then + KUBECTL=kubectl.exe + else + echo >&2 "kubectl is not installed" + exit 1 + fi fi if [[ "$#" -eq 0 ]]; then diff --git a/test/common.bash b/test/common.bash index d70826a..a028f2c 100644 --- a/test/common.bash +++ b/test/common.bash @@ -17,6 +17,14 @@ use_config() { # wrappers around "kubectl config" command -get_context() { - kubectl config current-context +get_namespace() { + kubectl config view -o=jsonpath="{.contexts[?(@.name==\"$(get_context)\")].context.namespace}" +} + +get_context() { + kubectl config current-context +} + +switch_context() { + kubectl config use-context "${1}" } diff --git a/test/kubens.bats b/test/kubens.bats index e3aae96..0978e6b 100644 --- a/test/kubens.bats +++ b/test/kubens.bats @@ -1,15 +1,104 @@ #!/usr/bin/env bats COMMAND="${BATS_TEST_DIRNAME}/../kubens" +export KUBECTL="$BATS_TEST_DIRNAME/../test/mock-kubectl" + +load common @test "--help should not fail" { run ${COMMAND} --help echo "$output">&2 - [ "$status" -eq 0 ] + [[ "$status" -eq 0 ]] } @test "-h should not fail" { run ${COMMAND} -h echo "$output">&2 - [ "$status" -eq 0 ] + [[ "$status" -eq 0 ]] +} + +@test "list namespaces when no kubeconfig exists" { + run ${COMMAND} + echo "$output" + [[ "$status" -eq 1 ]] + [[ "$output" = *"current-context is not set"* ]] +} + +@test "list namespaces" { + use_config config1 + switch_context user1@cluster1 + + run ${COMMAND} + echo "$output" + [[ "$status" -eq 0 ]] + [[ "$output" = *"ns1"* ]] + [[ "$output" = *"ns2"* ]] +} + +@test "switch to existing namespace" { + use_config config1 + switch_context user1@cluster1 + + run ${COMMAND} "ns1" + echo "$output" + [[ "$status" -eq 0 ]] + [[ "$output" = *'Active namespace is "ns1"'* ]] +} + +@test "switch to non-existing namespace" { + use_config config1 + switch_context user1@cluster1 + + run ${COMMAND} "unknown-namespace" + echo "$output" + [[ "$status" -eq 1 ]] + [[ "$output" = *'no namespace exists with name "unknown-namespace"'* ]] +} + +@test "switch between namespaces" { + use_config config1 + switch_context user1@cluster1 + + run ${COMMAND} ns1 + echo "$output" + [[ "$status" -eq 0 ]] + echo "$(get_namespace)" + [[ "$(get_namespace)" = "ns1" ]] + + run ${COMMAND} ns2 + echo "$output" + [[ "$status" -eq 0 ]] + echo "$(get_namespace)" + [[ "$(get_namespace)" = "ns2" ]] + + run ${COMMAND} - + echo "$output" + [[ "$status" -eq 0 ]] + echo "$(get_namespace)" + [[ "$(get_namespace)" = "ns1" ]] + + run ${COMMAND} - + echo "$output" + [[ "$status" -eq 0 ]] + echo "$(get_namespace)" + [[ "$(get_namespace)" = "ns2" ]] +} + +@test "switch to previous namespace when none exists" { + use_config config1 + switch_context user1@cluster1 + + run ${COMMAND} - + echo "$output" + [[ "$status" -eq 1 ]] + [[ "$output" = *"No previous namespace found for current context"* ]] +} + +@test "switch to namespace when current context is empty" { + use_config config1 + + run ${COMMAND} - + echo "$output" + [[ "$status" -eq 1 ]] + [[ "$output" = *"current-context is not set"* ]] } diff --git a/test/mock-kubectl b/test/mock-kubectl new file mode 100755 index 0000000..2535c92 --- /dev/null +++ b/test/mock-kubectl @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +[[ -n $DEBUG ]] && set -x + +set -eou pipefail + +if [[ $@ == *'get namespaces'* ]]; then + echo "ns1" + echo "ns2" +else + kubectl $@ +fi