-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add more kubectx tests #111
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/env bats | ||
|
||
# bats setup function | ||
setup() { | ||
export KUBECONFIG=$(mktemp) | ||
export XDG_CACHE_HOME="$(mktemp -d)" | ||
} | ||
|
||
# bats teardown function | ||
teardown() { | ||
rm -f $KUBECONFIG | ||
rm -f $XDG_CACHE_HOME/kubectx | ||
rmdir $XDG_CACHE_HOME | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can combine all these into one |
||
} | ||
|
||
# wrappers around "kubectl config" command | ||
|
||
add_cluster() { | ||
kubectl config set-cluster ${1} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider putting "" around all variable references, this prevents splitting by space. |
||
} | ||
|
||
add_user() { | ||
kubectl config set-credentials ${1} | ||
} | ||
|
||
add_context() { | ||
kubectl config set-context ${1} --user=${2} --cluster=${3} | ||
} | ||
|
||
get_context() { | ||
kubectl config current-context | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,104 @@ | |
|
||
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" { | ||
add_cluster cluster1 | ||
add_user user1 | ||
add_context user1@cluster1 user1 cluster1 | ||
|
||
run ${COMMAND} - | ||
echo "$output" | ||
[ "$status" -eq 1 ] | ||
[[ "$output" = "error: No previous context found." ]] | ||
} | ||
|
||
@test "create one context and list contexts" { | ||
add_cluster cluster1 | ||
add_user user1 | ||
add_context user1@cluster1 user1 cluster1 | ||
|
||
run ${COMMAND} | ||
echo "$output" | ||
[ "$status" -eq 0 ] | ||
[[ "$output" = "user1@cluster1" ]] | ||
} | ||
|
||
@test "create two contexts and list contexts" { | ||
add_cluster cluster1 | ||
add_user user1 | ||
add_user user2 | ||
add_context user1@cluster1 user1 cluster1 | ||
add_context user2@cluster1 user2 cluster1 | ||
|
||
run ${COMMAND} | ||
echo "$output" | ||
[ "$status" -eq 0 ] | ||
[[ "$output" = *"user1@cluster1"* ]] | ||
[[ "$output" = *"user2@cluster1"* ]] | ||
} | ||
|
||
@test "create two contexts and select contexts" { | ||
add_cluster cluster1 | ||
add_user user1 | ||
add_user user2 | ||
add_context user1@cluster1 user1 cluster1 | ||
add_context user2@cluster1 user2 cluster1 | ||
|
||
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" { | ||
add_cluster cluster1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like we are gonna repeat this 2-context kubeconfig file a lot. I wish we had a way of automatically getting that. |
||
add_user user1 | ||
add_user user2 | ||
add_context user1@cluster1 user1 cluster1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you should modify $KUBECONFIG file in tests. You can consider doing a |
||
add_context user2@cluster1 user2 cluster1 | ||
|
||
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" ]] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why you don't do this like
you're gonna get a random dir every time anyway.