Skip to content

Commit

Permalink
small cleanup to kubens tests
Browse files Browse the repository at this point in the history
  • Loading branch information
doodlesbykumbi committed Jan 3, 2019
1 parent 1145b9e commit f7a193c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 37 deletions.
18 changes: 9 additions & 9 deletions kubens
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,15 @@ swap_namespace() {
}

main() {
if [[ "${KUBECTL:-}" ]]; then
true
elif 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
Expand Down
49 changes: 24 additions & 25 deletions test/kubens.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ 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" ]
[[ "$status" -eq 1 ]]
[[ "$output" = *"current-context is not set"* ]]
}

Expand All @@ -30,59 +30,58 @@ load common

run ${COMMAND}
echo "$output"
[ "$status" -eq 0 ]
[[ "$output" = *"default"* ]]
[[ "$output" = *"kube-public"* ]]
[[ "$output" = *"kube-system"* ]]
[[ "$status" -eq 0 ]]
[[ "$output" = *"ns1"* ]]
[[ "$output" = *"ns2"* ]]
}

@test "switch to existent namespace" {
@test "switch to existing namespace" {
use_config config1
switch_context user1@cluster1

run ${COMMAND} "kube-public"
run ${COMMAND} "ns1"
echo "$output"
[ "$status" -eq 0 ]
[[ "$output" = *'Active namespace is "kube-public"'* ]]
[[ "$status" -eq 0 ]]
[[ "$output" = *'Active namespace is "ns1"'* ]]
}

@test "switch to non-existent namespace" {
@test "switch to non-existing namespace" {
use_config config1
switch_context user1@cluster1

run ${COMMAND} "unknown-namespace"
echo "$output"
[ "$status" -eq 1 ]
[[ "$status" -eq 1 ]]
[[ "$output" = *'no namespace exists with name "unknown-namespace"'* ]]
}

@test "switch between namespaces" {
use_config config1
switch_context user1@cluster1

run ${COMMAND} kube-public
run ${COMMAND} ns1
echo "$output"
[ "$status" -eq 0 ]
[[ "$status" -eq 0 ]]
echo "$(get_namespace)"
[[ "$(get_namespace)" = "kube-public" ]]
[[ "$(get_namespace)" = "ns1" ]]

run ${COMMAND} kube-system
run ${COMMAND} ns2
echo "$output"
[ "$status" -eq 0 ]
[[ "$status" -eq 0 ]]
echo "$(get_namespace)"
[[ "$(get_namespace)" = "kube-system" ]]
[[ "$(get_namespace)" = "ns2" ]]

run ${COMMAND} -
echo "$output"
[ "$status" -eq 0 ]
[[ "$status" -eq 0 ]]
echo "$(get_namespace)"
[[ "$(get_namespace)" = "kube-public" ]]
[[ "$(get_namespace)" = "ns1" ]]

run ${COMMAND} -
echo "$output"
[ "$status" -eq 0 ]
[[ "$status" -eq 0 ]]
echo "$(get_namespace)"
[[ "$(get_namespace)" = "kube-system" ]]
[[ "$(get_namespace)" = "ns2" ]]
}

@test "switch to previous namespace when none exists" {
Expand All @@ -91,7 +90,7 @@ load common

run ${COMMAND} -
echo "$output"
[ "$status" -eq 1 ]
[[ "$status" -eq 1 ]]
[[ "$output" = *"No previous namespace found for current context"* ]]
}

Expand All @@ -100,6 +99,6 @@ load common

run ${COMMAND} -
echo "$output"
[ "$status" -eq 1 ]
[[ "$status" -eq 1 ]]
[[ "$output" = *"current-context is not set"* ]]
}
5 changes: 2 additions & 3 deletions test/mock-kubectl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
set -eou pipefail

if [[ $@ == *'get namespaces'* ]]; then
echo "default"
echo "kube-public"
echo "kube-system"
echo "ns1"
echo "ns2"
else
kubectl $@
fi

0 comments on commit f7a193c

Please sign in to comment.