Skip to content

Commit

Permalink
Merge pull request #4 from predatorray/0.0.x
Browse files Browse the repository at this point in the history
Added option `detach` and unit tests
  • Loading branch information
predatorray authored Mar 14, 2020
2 parents 140e6c6 + 6521d18 commit 8e8bfa1
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 6 deletions.
21 changes: 20 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
language: bash
sudo: required

env:
global:
- CHANGE_MINIKUBE_NONE_USER=true
- MINIKUBE_WANTUPDATENOTIFICATION=false
- MINIKUBE_WANTREPORTERRORPROMPT=false
- MINIKUBE_HOME=$HOME
- CHANGE_MINIKUBE_NONE_USER=true
- KUBECONFIG=$HOME/.kube/config

before_script:
- curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/v1.16.0/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/
- curl -Lo minikube https://storage.googleapis.com/minikube/releases/v1.4.0/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
- mkdir -p $HOME/.kube $HOME/.minikube
- touch $KUBECONFIG
- sudo minikube start --vm-driver=none --kubernetes-version=v1.16.0
- "sudo chown -R travis: /home/travis/.minikube/"

script:
- make
- export PATH="$(pwd)/bin:$PATH"
- make
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ else
OS_UNAME := $(shell uname -s)
endif

.PHONY: build sign checksum clean mk-output-dir
.PHONY: build sign checksum clean mk-output-dir test

all: $(RELEASE_FILE_PATH) $(CHECKSUM_FILE_PATH)
all: test $(RELEASE_FILE_PATH) $(CHECKSUM_FILE_PATH)

build: $(RELEASE_FILE_PATH)

test:
bats test/

checksum: $(CHECKSUM_FILE_PATH)

sign: $(SIG_FILE_PATH)
Expand Down
15 changes: 12 additions & 3 deletions bin/kubectl-tmux_exec
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Options:
-t, --tty=true: Stdin is a TTY (deprecated, since it's enabled by default)
-l, --selector: Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)
-f, --file: Read pod names line-by-line from a file
-d, --detach=false: Make the Tmux session detached
--remain-on-exit=false: Remain Tmux window on exit
--select-layout=tiled: One of the five Tmux preset layouts: even-horizontal, even-vertical, main-horizontal,
main-vertical, or tiled.
Expand Down Expand Up @@ -136,8 +137,8 @@ function array_contains() {

function main() {
local opts
opts=$(ggetopt -o hitc:l:f:"$(printf '%s:' "${KUBECTL_SHORT_OPTS[@]}")" --long \
help,stdin,tty,container:,selector:,remain-on-exit,select-layout:,file:,"$(printf '%s:,' "${KUBECTL_LONG_OPTS[@]}")","$(printf '%s,' "${KUBECTL_NOARG_LONG_OPTS[@]}")" -- "$@")
opts=$(ggetopt -o hitdc:l:f:"$(printf '%s:' "${KUBECTL_SHORT_OPTS[@]}")" --long \
help,stdin,tty,detach,container:,selector:,remain-on-exit,select-layout:,file:,"$(printf '%s:,' "${KUBECTL_LONG_OPTS[@]}")","$(printf '%s,' "${KUBECTL_NOARG_LONG_OPTS[@]}")" -- "$@")
eval set -- $opts

local selector
Expand All @@ -146,6 +147,7 @@ function main() {
local remain_on_exit=0
local tmux_layout='tiled'
local pod_list_file
local tmux_detach=0
while [[ $# -gt 0 ]]; do
local opt="$1"
case "${opt}" in
Expand All @@ -167,6 +169,9 @@ function main() {
shift
selector="$1"
;;
-d|--detach)
tmux_detach=1
;;
--remain-on-exit)
remain_on_exit=1
;;
Expand Down Expand Up @@ -268,7 +273,11 @@ function main() {
for pod in "${pods[@]}"; do
local cmd="kubectl ${kubectl_opts[@]:-} exec ${kubectl_exec_opts} ${pod} -- ${exec_cmd_str}"
if [[ "${#tmux_commands[@]}" -eq 0 ]]; then
tmux_commands+=('new-session' "${cmd}" ';')
if [[ "${tmux_detach}" -eq 0 ]]; then
tmux_commands+=('new-session' "${cmd}" ';')
else
tmux_commands+=('new-session' '-d' "${cmd}" ';')
fi
else
tmux_commands+=('split-window' "${cmd}" ';')
fi
Expand Down
11 changes: 11 additions & 0 deletions test/basic.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bats

@test "Print usage" {
bin/kubectl-tmux_exec --help
[ $? -eq 0 ]
}

@test "Use as a plugin" {
kubectl tmux-exec --help
[ $? -eq 0 ]
}
65 changes: 65 additions & 0 deletions test/exec-one-pod.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env bats

load test-helper

readonly POD_NAME='alpine'
readonly POD_APP_LABEL='alpine'

function setup() {
kubectl apply -f - << EOF
apiVersion: v1
kind: Pod
metadata:
name: ${POD_NAME}
labels:
app: ${POD_APP_LABEL}
spec:
containers:
- name: alpine
image: alpine
command:
- sleep
- infinite
EOF
local pod_status=''
local retries=30
while [[ "${pod_status}" != 'Running' ]]; do
sleep 1
pod_status="$(kubectl get pods "${POD_NAME}" -o custom-columns=':status.phase' --no-headers)"
echo "The pod status is ${pod_status}."
(( --retries )) || {
echo 'Timed out.'
exit 1
}
done
}

function teardown() {
kubectl delete pod "${POD_NAME}"
sleep 1
}

@test "Exec one pod by label" {
local written='foobar-label'
bin/kubectl-tmux_exec -d -l app="${POD_APP_LABEL}" -- sh -c "echo ${written} > /tmp/foobar"

wait_until_no_sessions

local file_content=
file_content="$(kubectl exec "${POD_NAME}" cat /tmp/foobar)"

[ "${file_content}" == "${written}" ]
}

@test "Exec one pod by file" {
# set -x
local written='foobar-file'
bin/kubectl-tmux_exec -d -f - -- sh -c "echo ${written} > /tmp/foobar" <<< "${POD_NAME}"

wait_until_no_sessions

local file_content
file_content="$(kubectl exec "${POD_NAME}" cat /tmp/foobar)"

[ "${file_content}" == "${written}" ]
}
12 changes: 12 additions & 0 deletions test/test-helper.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

function wait_until_no_sessions() {
local retries=30
while tmux ls 2>&1 >/dev/null; do
(( --retries )) || {
echo 'Timed out.'
return 1
}
sleep 1
done
}

0 comments on commit 8e8bfa1

Please sign in to comment.