mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-06-01 11:55:57 +00:00
test: Add more kubectx tests (#111)
This commit is contained in:
parent
845f3b690b
commit
acbf324464
22
test/common.bash
Normal file
22
test/common.bash
Normal file
@ -0,0 +1,22 @@
|
||||
#!/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_context() {
|
||||
kubectl config current-context
|
||||
}
|
@ -2,12 +2,88 @@
|
||||
|
||||
COMMAND="$BATS_TEST_DIRNAME/../kubectx"
|
||||
|
||||
load common
|
||||
|
||||
@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 "create one context and list contexts" {
|
||||
use_config config1
|
||||
|
||||
run ${COMMAND}
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" = "user1@cluster1" ]]
|
||||
}
|
||||
|
||||
@test "create two contexts and list contexts" {
|
||||
use_config config2
|
||||
|
||||
run ${COMMAND}
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" = *"user1@cluster1"* ]]
|
||||
[[ "$output" = *"user2@cluster1"* ]]
|
||||
}
|
||||
|
||||
@test "create 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 "create 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" ]]
|
||||
}
|
||||
|
18
test/testdata/config1
vendored
Normal file
18
test/testdata/config1
vendored
Normal 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
24
test/testdata/config2
vendored
Normal 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: {}
|
Loading…
Reference in New Issue
Block a user