Skip to content
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

Merged
merged 3 commits into from
Dec 25, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions test/common.bash
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)
Copy link
Owner

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

KUBECONFIG="$XDG_CACHE_HOME/kubeconfig"

you're gonna get a random dir every time anyway.

export XDG_CACHE_HOME="$(mktemp -d)"
}

# bats teardown function
teardown() {
rm -f $KUBECONFIG
rm -f $XDG_CACHE_HOME/kubectx
rmdir $XDG_CACHE_HOME
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can combine all these into one rm -rf $XDG_CACHE_HOME, if you put kubeconfig in that dir, too.

}

# wrappers around "kubectl config" command

add_cluster() {
kubectl config set-cluster ${1}
Copy link
Owner

Choose a reason for hiding this comment

The 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
}
92 changes: 92 additions & 0 deletions test/kubectx.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The 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
Copy link
Owner

Choose a reason for hiding this comment

The 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 cp testdata/kubeconfig "$(mktemp)" for each test, and use that copy. Otherwise we'd be mutating kubeconfig, which can cause problems when we reorder tests. Ideally each test should be idempotent/isolated.

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" ]]
}