20 Commits

Author SHA1 Message Date
Ahmet Alp Balkan
a21638226f Release v0.7.0 2019-08-30 11:50:15 -07:00
Ahmet Alp Balkan
28e7c12f51 Introduce -c/--current options for kubectx/kubens (#171)
Per #127 the user community wants to have this feature, primarily as
-c and --current flags.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2019-08-30 11:49:49 -07:00
ferhat elmas
1652420a15 Enable shellcheck and fix issues (#170) 2019-08-25 12:05:01 -07:00
Ed Vinyard
543e035090 fix typo in "Customizing colors" example (#166) 2019-08-10 11:28:51 -07:00
Nils Breunese
a5e810b837 Add install instructions for MacPorts users (#159) 2019-07-15 10:29:52 -07:00
Christian Rebischke
62f3f27889 changed instructions for arch linux (#152)
I have pushed kubectx to the official repositories.
Users can install it via `pacman` now :)

Signed-off-by: Christian Rebischke <chris@nullday.de>
2019-05-22 17:01:08 +02:00
Ahmet Alp Balkan
4258f03446 Add shields.io badges 2019-05-08 19:39:34 -07:00
Ahmet Alp Balkan
b9614bd2e0 kubectx rename check if old_name is a valid ctx (#139)
Without this safeguard, when user runs `kubectx NEW_NAME=OLD_NAME` where
NEW_NAME is an existing context but OLD_NAME isn't, we end up deleting NEW_NAME
and not doing any renames (because OLD_NAME is not found).

Fixes #136.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2019-04-04 08:59:51 -07:00
Ahmet Alp Balkan
b3732b309e Update README.md 2019-02-06 10:20:55 -08:00
Tariq Ibrahim
a1bce92cc8 Fix alignment of kubectx help in USAGE text. (#129) 2019-02-04 14:21:45 -08:00
Tariq Ibrahim
1356c37cc0 Fix typos in readme doc. (#126)
* Fix typos in readme doc.

* fix typos
2019-01-30 12:20:37 -08:00
Ahmet Alp Balkan
10c9bd58ca v0.6.3
- FIX: Show current context/ns color in interactive (fzf) mode. (#109)
- TEST: Add integration tests for kubectx (#111, #113) and kubens (#105, #117)

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2019-01-28 10:11:52 -08:00
Jonathan Liuti
b6e918b084 Remove --with-short-names from doc (#120)
First measure to avoid confusing people.
see #112
2019-01-13 14:56:24 -08:00
Kumbirai Tanekha
402cc2c4b9 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
2019-01-03 10:06:13 -08:00
Philippe MARTIN
df557e4fa7 Add more cli tests for kubectx (#113) 2019-01-02 09:47:01 -08:00
Ahmet Alp Balkan
b584d14f90 Show color in interactive mode (#109)
This patch introduces an internal _KUBECTX_FORCE_COLOR environment variable
that overrides color output decision.

With this, fzf output shows the color indicators for ctx/ns and choosing the
option with the color works without any extra handling.

Fixes #89.
Fixes #98.
2018-12-29 11:05:00 -08:00
Philippe MARTIN
acbf324464 test: Add more kubectx tests (#111) 2018-12-25 11:38:45 -08:00
Ahmet Alp Balkan
845f3b690b test: enable travis-ci with bats (#108)
- add .travis.yml.
- move bats fixtures to .bats extension, since it allows detection of test
files automatically by file extension.
- use BATS_TEST_DIRNAME variable to compute location of COMMAND.
- IMPORTANT: use `echo "$output">&2` before final check so that we can debug
  the test cases by their output

Ref #2.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2018-12-22 13:22:08 -08:00
Philippe MARTIN
2b5bf4e429 Add simple tests for kubectx/kubens -h/--help (#105) 2018-12-22 13:02:20 -08:00
Eric Bailey
4a7d7cf025 README.md: add fish completion installation hint (#106)
This should work for most users anyway.
2018-12-21 20:30:46 -08:00
10 changed files with 566 additions and 30 deletions

11
.travis.yml Normal file
View File

@@ -0,0 +1,11 @@
before_install:
- sudo add-apt-repository ppa:duggan/bats --yes
- sudo apt-get update -qq
- sudo apt-get install -qq bats
- 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/kubectx.bats
- bats test/kubens.bats
- shellcheck kubectx
- shellcheck kubens

View File

@@ -1,3 +1,11 @@
# `kubectx` + `kubens`: Power tools for kubectl
![Latest GitHub release](https://img.shields.io/github/release/ahmetb/kubectx.svg)
![GitHub stars](https://img.shields.io/github/stars/ahmetb/kubectx.svg?label=github%20stars)
![Travis (.org) branch](https://img.shields.io/travis/ahmetb/kubectx/master.svg)
![Proudly written in Bash](https://img.shields.io/badge/written%20in-bash-ff69b4.svg)
This repository provides both `kubectx` and `kubens` tools.
@@ -9,13 +17,14 @@ This repository provides both `kubectx` and `kubens` tools.
# kubectx(1)
kubectx is an utility to manage and switch between kubectl(1) contexts.
kubectx is a utility to manage and switch between kubectl(1) contexts.
```
USAGE:
kubectx : list the contexts
kubectx <NAME> : switch to context <NAME>
kubectx - : switch to the previous context
kubectx -c, --current : show the current context name
kubectx <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
kubectx <NEW_NAME>=. : rename current-context to <NEW_NAME>
kubectx -d <NAME> : delete context <NAME> ('.' for current-context)
@@ -47,13 +56,14 @@ long context names. You don't have to remember full context names anymore.
# kubens(1)
kubens is an utility to switch between Kubernetes namespaces.
kubens is a utility to switch between Kubernetes namespaces.
```
USAGE:
kubens : list the namespaces
kubens <NAME> : change the active namespace
kubens - : switch to the previous namespace
kubens -c, --current : show the current namespace
```
@@ -77,22 +87,26 @@ Active namespace is "default".
### macOS
:confetti_ball: Use the [Homebrew](https://brew.sh/) package manager:
#### Homebrew
:confetti_ball: If you use [Homebrew](https://brew.sh/) you can install like this:
brew install kubectx
This command will set up bash/zsh/fish completion scripts automatically.
- Running `brew install` with `--with-short-names` will install tools with names
`kctx` and `kns` to prevent prefix collision with `kubectl` name.
- If you like to add context/namespace info to your shell prompt (`$PS1`),
I recommend trying out [kube-ps1](https://github.com/jonmosco/kube-ps1).
#### MacPorts
If you use [MacPorts](https://www.macports.org) you can install like this:
sudo port install kubectx
### Linux
Since `kubectx`/`kubens` are written in Bash, you should be able to instal
Since `kubectx`/`kubens` are written in Bash, you should be able to install
them to any POSIX environment that has Bash installed.
- Download the `kubectx`, and `kubens` scripts.
@@ -127,8 +141,13 @@ them to any POSIX environment that has Bash installed.
export PATH=~/.kubectx:\$PATH
FOE
```
- For fish: Figure out how to install completion scripts and please document here
- For fish:
```fish
mkdir -p ~/.config/fish/completions
ln -s /opt/kubectx/completion/kubectx.fish ~/.config/fish/completions/
ln -s /opt/kubectx/completion/kubens.fish ~/.config/fish/completions/
```
Example installation steps:
``` bash
@@ -139,9 +158,11 @@ sudo ln -s /opt/kubectx/kubens /usr/local/bin/kubens
#### Arch Linux
An unofficial [AUR package](https://aur.archlinux.org/packages/kubectx) `kubectx`
is available. Install instructions can be found on the [Arch
wiki](https://wiki.archlinux.org/index.php/Arch_User_Repository#Installing_packages).
Available as official Arch Linux package. Install it via:
```bash
sudo pacman -S kubectx
```
#### Debian/Ubuntu
@@ -172,7 +193,7 @@ If you like to customize the colors indicating the current namespace or context,
```
export KUBECTX_CURRENT_FGCOLOR=$(tput setaf 6) # blue text
export KUBECTX_CURRENT_BGCOLOR=$(tput setaf 7) # white background
export KUBECTX_CURRENT_BGCOLOR=$(tput setab 7) # white background
```
Colors in the output can be disabled by setting the
@@ -190,7 +211,8 @@ Colors in the output can be disabled by setting the
| _“Also using it on a daily basis. This and my zsh config that shows me the current k8s context 😉”_ [@puja108](https://twitter.com/puja108/status/928742521139810305) |
| _“Lately I've found myself using the kubens command more than kubectx. Both very useful though :-)”_ [@stuartleeks](https://twitter.com/stuartleeks/status/928562850464907264) |
| _“yeah kubens rocks!”_ [@embano1](https://twitter.com/embano1/status/928698440732815360) |
| _“Special thanks to Ahmet Alp Balkan for creating kubectx, kubens, and kubectl aliases, as these tools made my life better.”_ [@strebeld](https://medium.com/@strebeld/5-ways-to-enhance-kubectl-ux-97c8893227a)
| _“Special thanks to Ahmet Alp Balkan for creating kubectx, kubens, and kubectl aliases, as these tools made my life better.”_ [@strebeld](https://medium.com/@strebeld/5-ways-to-enhance-kubectl-ux-97c8893227a) |
| _“❤ this shell script @ahmetb wrote to help make switching between kubectl config contexts a breeze.”_ [@briandanowski](https://twitter.com/briandanowski/status/1085409568165896193) |
> If you liked `kubectx`, you may like my [`kubectl-aliases`](https://github.com/ahmetb/kubectl-aliases) project, too.

27
kubectx
View File

@@ -30,13 +30,14 @@ USAGE:
kubectx : list the contexts
kubectx <NAME> : switch to context <NAME>
kubectx - : switch to the previous context
kubectx -c, --current : show the current context name
kubectx <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
kubectx <NEW_NAME>=. : rename current-context to <NEW_NAME>
kubectx -d <NAME> [<NAME...>] : delete context <NAME> ('.' for current-context)
(this command won't delete the user/cluster entry
that is used by the context)
kubectx -h,--help : show this message
kubectx -h,--help : show this message
EOF
}
@@ -69,8 +70,14 @@ list_contexts() {
cur_ctx_bg=${KUBECTX_CURRENT_BGCOLOR:-$darkbg}
for c in $ctx_list; do
if [[ -t 1 && -z "${NO_COLOR:-}" && "${c}" = "${cur}" ]]; then
echo "${cur_ctx_bg}${cur_ctx_fg}${c}${normal}"
if [[ -n "${_KUBECTX_FORCE_COLOR:-}" || \
-t 1 && -z "${NO_COLOR:-}" ]]; then
# colored output mode
if [[ "${c}" = "${cur}" ]]; then
echo "${cur_ctx_bg}${cur_ctx_fg}${c}${normal}"
else
echo "${c}"
fi
else
echo "${c}"
fi
@@ -98,7 +105,9 @@ switch_context() {
choose_context_interactive() {
local choice
choice="$(FZF_DEFAULT_COMMAND="${SELF_CMD}" fzf --ansi || true)"
choice="$(_KUBECTX_FORCE_COLOR=1 \
FZF_DEFAULT_COMMAND="${SELF_CMD}" \
fzf --ansi || true)"
if [[ -z "${choice}" ]]; then
echo 2>&1 "error: you did not choose any of the options"
exit 1
@@ -140,6 +149,11 @@ rename_context() {
old_name="$(current_context)"
fi
if ! context_exists "${old_name}"; then
echo "error: Context \"${old_name}\" not found, can't rename it." >&2
exit 1
fi
if context_exists "${new_name}"; then
echo "Context \"${new_name}\" exists, deleting..." >&2
$KUBECTL config delete-context "${new_name}" 1>/dev/null 2>&1
@@ -194,6 +208,11 @@ main() {
elif [[ "$#" -eq 1 ]]; then
if [[ "${1}" == "-" ]]; then
swap_context
elif [[ "${1}" == '-c' || "${1}" == '--current' ]]; then
# we don't call current_context here for two reasons:
# - it does not fail when current-context property is not set
# - it does not return a trailing newline
kubectl config current-context
elif [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
usage
elif [[ "${1}" =~ ^-(.*) ]]; then

37
kubens
View File

@@ -30,6 +30,7 @@ USAGE:
kubens : list the namespaces in the current context
kubens <NAME> : change the active namespace of current context
kubens - : switch to the previous namespace in this context
kubens -c, --current : show the current namespace
kubens -h,--help : show this message
EOF
}
@@ -54,7 +55,7 @@ current_namespace() {
}
current_context() {
$KUBECTL config view -o=jsonpath='{.current-context}'
$KUBECTL config current-context
}
get_namespaces() {
@@ -66,7 +67,9 @@ escape_context_name() {
}
namespace_file() {
local ctx="$(escape_context_name "${1}")"
local ctx
ctx="$(escape_context_name "${1}")"
echo "${KUBENS_DIR}/${ctx}"
}
@@ -104,7 +107,9 @@ choose_namespace_interactive() {
fi
local choice
choice="$(FZF_DEFAULT_COMMAND="${SELF_CMD}" fzf --ansi || true)"
choice="$(_KUBECTX_FORCE_COLOR=1 \
FZF_DEFAULT_COMMAND="${SELF_CMD}" \
fzf --ansi || true)"
if [[ -z "${choice}" ]]; then
echo 2>&1 "error: you did not choose any of the options"
exit 1
@@ -145,11 +150,17 @@ list_namespaces() {
ns_list=$(get_namespaces) || exit_err "error getting namespace list"
for c in $ns_list; do
if [[ -t 1 && -z "${NO_COLOR:-}" && "${c}" = "${cur}" ]]; then
if [[ -n "${_KUBECTX_FORCE_COLOR:-}" || \
-t 1 && -z "${NO_COLOR:-}" ]]; then
# colored output mode
if [[ "${c}" = "${cur}" ]]; then
echo "${cur_ctx_bg}${cur_ctx_fg}${c}${normal}"
else
echo "${c}"
fi
else
echo "${c}"
fi
done
}
@@ -165,13 +176,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
@@ -185,6 +198,8 @@ main() {
usage
elif [[ "${1}" == "-" ]]; then
swap_namespace
elif [[ "${1}" == '-c' || "${1}" == '--current' ]]; then
current_namespace
elif [[ "${1}" =~ ^-(.*) ]]; then
echo "error: unrecognized flag \"${1}\"" >&2
usage

30
test/common.bash Normal file
View File

@@ -0,0 +1,30 @@
#!/usr/bin/env bats
# bats setup function
setup() {
export XDG_CACHE_HOME="$(mktemp -d)"
export KUBECONFIG="${XDG_CACHE_HOME}/config"
}
# bats teardown function
teardown() {
rm -rf "$XDG_CACHE_HOME"
}
use_config() {
cp "$BATS_TEST_DIRNAME/testdata/$1" $KUBECONFIG
}
# wrappers around "kubectl config" command
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}"
}

241
test/kubectx.bats Normal file
View File

@@ -0,0 +1,241 @@
#!/usr/bin/env bats
COMMAND="$BATS_TEST_DIRNAME/../kubectx"
load common
@test "no kubectl detected" {
OLDPATH="$PATH"
PATH=/bin
run ${COMMAND}
echo "$output"
[ "$status" -eq 1 ]
[[ "$output" = "kubectl is not installed" ]]
PATH="$OLDPATH"
}
@test "--help should not fail" {
run ${COMMAND} --help
echo "$output"
[ "$status" -eq 0 ]
}
@test "-h should not fail" {
run ${COMMAND} -h
echo "$output"
[ "$status" -eq 0 ]
}
@test "switch to previous context when no one exists" {
use_config config1
run ${COMMAND} -
echo "$output"
[ "$status" -eq 1 ]
[[ "$output" = "error: No previous context found." ]]
}
@test "list contexts when no kubeconfig exists" {
run ${COMMAND}
echo "$output"
[ "$status" -eq 0 ]
[[ "$output" = "" ]]
}
@test "get one context and list contexts" {
use_config config1
run ${COMMAND}
echo "$output"
[ "$status" -eq 0 ]
[[ "$output" = "user1@cluster1" ]]
}
@test "get two contexts and list contexts" {
use_config config2
run ${COMMAND}
echo "$output"
[ "$status" -eq 0 ]
[[ "$output" = *"user1@cluster1"* ]]
[[ "$output" = *"user2@cluster1"* ]]
}
@test "get two contexts and select contexts" {
use_config config2
run ${COMMAND} user1@cluster1
echo "$output"
[ "$status" -eq 0 ]
echo "$(get_context)"
[[ "$(get_context)" = "user1@cluster1" ]]
run ${COMMAND} user2@cluster1
echo "$output"
[ "$status" -eq 0 ]
echo "$(get_context)"
[[ "$(get_context)" = "user2@cluster1" ]]
}
@test "get two contexts and switch between contexts" {
use_config config2
run ${COMMAND} user1@cluster1
echo "$output"
[ "$status" -eq 0 ]
echo "$(get_context)"
[[ "$(get_context)" = "user1@cluster1" ]]
run ${COMMAND} user2@cluster1
echo "$output"
[ "$status" -eq 0 ]
echo "$(get_context)"
[[ "$(get_context)" = "user2@cluster1" ]]
run ${COMMAND} -
echo "$output"
[ "$status" -eq 0 ]
echo "$(get_context)"
[[ "$(get_context)" = "user1@cluster1" ]]
run ${COMMAND} -
echo "$output"
[ "$status" -eq 0 ]
echo "$(get_context)"
[[ "$(get_context)" = "user2@cluster1" ]]
}
@test "get one context and switch to non existent context" {
use_config config1
run ${COMMAND} "unknown-context"
echo "$output"
[ "$status" -eq 1 ]
}
@test "-c/--current fails when no context set" {
use_config config1
run "${COMMAND}" -c
echo "$output"
[ $status -eq 1 ]
run "${COMMAND}" --current
echo "$output"
[ $status -eq 1 ]
}
@test "-c/--current prints the current context" {
use_config config1
run "${COMMAND}" user1@cluster1
[ $status -eq 0 ]
run "${COMMAND}" -c
echo "$output"
[ $status -eq 0 ]
[[ "$output" = "user1@cluster1" ]]
run "${COMMAND}" --current
echo "$output"
[ $status -eq 0 ]
[[ "$output" = "user1@cluster1" ]]
}
@test "rename context" {
use_config config2
run ${COMMAND} "new-context=user1@cluster1"
echo "$output"
[ "$status" -eq 0 ]
run ${COMMAND}
echo "$output"
[ "$status" -eq 0 ]
[[ ! "$output" = *"user1@cluster1"* ]]
[[ "$output" = *"new-context"* ]]
[[ "$output" = *"user2@cluster1"* ]]
}
@test "rename current context" {
use_config config2
run ${COMMAND} user2@cluster1
echo "$output"
[ "$status" -eq 0 ]
run ${COMMAND} new-context=.
echo "$output"
[ "$status" -eq 0 ]
run ${COMMAND}
echo "$output"
[ "$status" -eq 0 ]
[[ ! "$output" = *"user2@cluster1"* ]]
[[ "$output" = *"user1@cluster1"* ]]
[[ "$output" = *"new-context"* ]]
}
@test "delete context" {
use_config config2
run ${COMMAND} -d "user1@cluster1"
echo "$output"
[ "$status" -eq 0 ]
run ${COMMAND}
echo "$output"
[ "$status" -eq 0 ]
[[ ! "$output" = "user1@cluster1" ]]
[[ "$output" = "user2@cluster1" ]]
}
@test "delete current context" {
use_config config2
run ${COMMAND} user2@cluster1
echo "$output"
[ "$status" -eq 0 ]
run ${COMMAND} -d .
echo "$output"
[ "$status" -eq 0 ]
run ${COMMAND}
echo "$output"
[ "$status" -eq 0 ]
[[ ! "$output" = "user2@cluster1" ]]
[[ "$output" = "user1@cluster1" ]]
}
@test "delete non existent context" {
use_config config1
run ${COMMAND} -d "unknown-context"
echo "$output"
[ "$status" -eq 1 ]
}
@test "delete several contexts" {
use_config config2
run ${COMMAND} -d "user1@cluster1" "user2@cluster1"
echo "$output"
[ "$status" -eq 0 ]
run ${COMMAND}
echo "$output"
[ "$status" -eq 0 ]
[[ "$output" = "" ]]
}
@test "delete several contexts including a non existent one" {
use_config config2
run ${COMMAND} -d "user1@cluster1" "non-existent" "user2@cluster1"
echo "$output"
[ "$status" -eq 1 ]
run ${COMMAND}
echo "$output"
[ "$status" -eq 0 ]
[[ "$output" = "user2@cluster1" ]]
}

144
test/kubens.bats Normal file
View File

@@ -0,0 +1,144 @@
#!/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 ]]
}
@test "-h should not fail" {
run ${COMMAND} -h
echo "$output">&2
[[ "$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"* ]]
}
@test "-c/--current works when no namespace is set on context" {
use_config config1
switch_context user1@cluster1
run ${COMMAND} "-c"
echo "$output"
[[ "$status" -eq 0 ]]
[[ "$output" = "default" ]]
run ${COMMAND} "--current"
echo "$output"
[[ "$status" -eq 0 ]]
[[ "$output" = "default" ]]
}
@test "-c/--current prints the namespace after it is set" {
use_config config1
switch_context user1@cluster1
${COMMAND} ns1
run ${COMMAND} "-c"
echo "$output"
[[ "$status" -eq 0 ]]
[[ "$output" = "ns1" ]]
run ${COMMAND} "--current"
echo "$output"
[[ "$status" -eq 0 ]]
[[ "$output" = "ns1" ]]
}
@test "-c/--current fails when current context is not set" {
use_config config1
run ${COMMAND} -c
echo "$output"
[[ "$status" -eq 1 ]]
run ${COMMAND} --current
echo "$output"
[[ "$status" -eq 1 ]]
}

12
test/mock-kubectl Executable file
View File

@@ -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

18
test/testdata/config1 vendored Normal file
View File

@@ -0,0 +1,18 @@
# config with one context
apiVersion: v1
clusters:
- cluster:
server: ""
name: cluster1
contexts:
- context:
cluster: cluster1
user: user1
name: user1@cluster1
current-context: ""
kind: Config
preferences: {}
users:
- name: user1
user: {}

24
test/testdata/config2 vendored Normal file
View File

@@ -0,0 +1,24 @@
# config with two contexts
apiVersion: v1
clusters:
- cluster:
server: ""
name: cluster1
contexts:
- context:
cluster: cluster1
user: user1
name: user1@cluster1
- context:
cluster: cluster1
user: user2
name: user2@cluster1
current-context: ""
kind: Config
preferences: {}
users:
- name: user1
user: {}
- name: user2
user: {}