Skip to content

Commit

Permalink
improve script readability
Browse files Browse the repository at this point in the history
  • Loading branch information
monopole committed May 17, 2018
1 parent a0ab7b0 commit 5bebfd0
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 74 deletions.
46 changes: 26 additions & 20 deletions demos/integration_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,49 +30,55 @@
# At time of writing, its 'call point' was in
# https://github.com/kubernetes/test-infra/blob/master/jobs/config.json

function exit_with {
function exitWith {
local msg=$1
echo >&2 ${msg}
exit 1
}
export -f exit_with
export -f exitWith

repo=kubernetes-sigs/kustomize
if [[ `pwd` != */$repo ]]; then
exit_with "Script must be run from $repo"
fi
function expectCommand {
command -v $1 >/dev/null 2>&1 || \
{ exitWith "Expected $1 on PATH."; }
}

echo pwd is `pwd`
echo GOPATH is $GOPATH
echo PATH is $PATH
function setUpEnv {
local repo=$(git rev-parse --show-toplevel)
cd $repo
[[ $? -eq 0 ]] || "Failed to cd to $repo"
echo "pwd is " `pwd`

go install . || \
{ exit_with "Failed to install kustomize."; }
local expectedRepo=kubernetes-sigs/kustomize
if [[ `pwd` != */$expectedRepo ]]; then
exitWith "Script must be run from $expectedRepo"
fi

export PATH=$GOPATH/bin:$PATH
go install . || \
{ exitWith "Failed to install kustomize."; }

command -v kustomize >/dev/null 2>&1 || \
{ exit_with "Require kustomize but it's not installed."; }
PATH=$GOPATH/bin:$PATH

command -v kubectl >/dev/null 2>&1 || \
{ exit_with "Require kubectl but it's not installed."; }
expectCommand kustomize
expectCommand kubectl
}

function runTest {
local script=$1
shift
local args=$@

if [ ! -x "$script" ]; then
exit_with "Unable to run $script"
exitWith "Unable to run $script"
fi

$script "$args"
if [ $? -ne 0 ]; then
exit_with "Failed: $script $args"
fi
[[ $? -eq 0 ]] || exitWith "Failed: $script $args"

echo "$script passed."
}

setUpEnv

pushd demos
runTest ldap/integration_test.sh ldap/base
popd
145 changes: 91 additions & 54 deletions demos/ldap/integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,68 +30,105 @@ set -x

target=$1

echo pwd is `pwd`
echo Kustomizing \"$target\"
echo Kustomizing: \"$target\"
ls $target
kustomize build $target > generatedResources.yaml
[[ $? -eq 0 ]] || { exit_with "Failed to kustomize build"; }

cat generatedResources.yaml
tmpDir=$(mktemp -d)

# Setting the namespace this way is a test-infra thing?
kubectl config set-context \
$(kubectl config current-context) --namespace=default
function configureCluster {
kustomize build $target > $tmpDir/my.yaml
[[ $? -eq 0 ]] || { exitWith "Failed to kustomize build"; }

kubectl apply -f generatedResources.yaml
[[ $? -eq 0 ]] || { exit_with "Failed to run kubectl apply"; }
cat $tmpDir/my.yaml

sleep 20
# Setting the namespace this way is a test-infra thing?
kubectl config set-context \
$(kubectl config current-context) --namespace=default

# get the pod and namespace
pod=$(kubectl get pods -l app=ldap -o jsonpath='{.items[0].metadata.name}')
namespace=$(kubectl get pods -l app=ldap -o jsonpath='{.items[0].metadata.namespace}')
container="ldap"
[[ -z ${pod} ]] && { exit_with "Pod is not started successfully"; }
[[ -z ${namespace} ]] && { exit_with "Couldn't get namespace for Pod ${pod}"; }
kubectl apply -f $tmpDir/my.yaml
[[ $? -eq 0 ]] || { exitWith "Failed to run kubectl apply"; }

# create a user ldif file locally
ldiffile="user.ldif"
cat <<EOF >$ldiffile
sleep 20
}

function tearDownCluster {
kubectl delete -f $tmpDir/my.yaml
rm -rf $tmpDir
}

function getPodField {
echo $(kubectl get pods -l app=ldap -o jsonpath=$1)
}

function podExec {
kubectl exec $podName -c $containerName -- "$@"
}

function addUser {
local namespace=`getPodField '{.items[0].metadata.namespace}'`
[[ -z $namespace ]] && { exitWith "Unable to get namespace"; }

# Create a user ldif file
local userFile="user.ldif"
cat <<EOF >$tmpDir/$userFile
dn: cn=The Postmaster,dc=example,dc=org
objectClass: organizationalRole
cn: The Postmaster
EOF
[[ -f ${ldiffile} ]] || { exit_with "Failed to create ldif file locally"; }

# add a user
pod_ldiffile="/tmp/user.ldif"
kubectl cp $ldiffile ${namespace}/${pod}:${pod_ldiffile} || \
{ exit_with "Failed to copy ldif file to Pod ${pod}"; }

kubectl exec ${pod} -c ${container} -- \
ldapadd -x -H ldap://localhost -D "cn=admin,dc=example,dc=org" -w admin \
-f ${pod_ldiffile} || { exit_with "Failed to add a user"; }

# query the added user
r=$(kubectl exec ${pod} -c ${container} -- \
ldapsearch -x -H ldap://localhost -b dc=example,dc=org \
-D "cn=admin,dc=example,dc=org" -w admin)

user_count=$(echo ${r} | grep "cn: The Postmaster" | wc -l)
[[ ${user_count} -eq 0 ]] && { exit_with "Couldn't find the new added user"; }

# delete the added user
kubectl exec ${pod} -c ${container} -- \
ldapdelete -v -x -H ldap://localhost "cn=The Postmaster,dc=example,dc=org" \
-D "cn=admin,dc=example,dc=org" -w admin || \
{ exit_with "Failed to delete the user"; }

r=$(kubectl exec ${pod} -c ${container} -- \
ldapsearch -x -H ldap://localhost -b dc=example,dc=org \
-D "cn=admin,dc=example,dc=org" -w admin)
user_count=$(echo ${r} | grep "cn: The Postmaster" | wc -l)
[[ ${user_count} -ne 0 ]] && { exit_with "The user hasn't been deleted."; }

# kubectl delete
kubectl delete -f generatedResources.yaml
rm $ldiffile
[[ -f $tmpDir/$userFile ]] || { exitWith "Failed to create $tmpDir/$userFile"; }

kubectl cp $tmpDir/$userFile $namespace/$podName:/tmp/$userFile || \
{ exitWith "Failed to copy ldif file to Pod $podName"; }

rm $tmpDir/$userFile

podExec \
ldapadd \
-x \
-w admin \
-H ldap://localhost \
-D "cn=admin,dc=example,dc=org" \
-f /tmp/$userFile
}

function getUserCount {
local result=$(\
podExec \
ldapsearch \
-x \
-w admin \
-H ldap://localhost \
-D "cn=admin,dc=example,dc=org" \
-b dc=example,dc=org \
)
return $(echo $result | grep "cn: The Postmaster" | wc -l)
}

function deleteAddedUser {
podExec \
ldapdelete \
-v \
-x \
-w admin \
-H ldap://localhost \
-D "cn=admin,dc=example,dc=org" \
"cn=The Postmaster,dc=example,dc=org"
}

configureCluster

podName=`getPodField '{.items[0].metadata.name}'`
[[ -z $podName ]] && { exitWith "Unable to get pod name"; }

echo pod is $podName
containerName="ldap"

getUserCount; [[ $? -eq 0 ]] || { exitWith "Expected no users."; }

addUser || { exitWith "Failed to add a user"; }
getUserCount; [[ $? -eq 1 ]] || { exitWith "Couldn't find the new added user"; }

deleteAddedUser || { exitWith "Failed to delete the user"; }
getUserCount; [[ $? -eq 0 ]] || { exitWith "User has not been deleted."; }

tearDownCluster

0 comments on commit 5bebfd0

Please sign in to comment.