-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_test.sh
executable file
·136 lines (111 loc) · 3.32 KB
/
integration_test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
# Copyright 2018 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ----------------------------------------------------
#
# This script tests the ldap kustomization demo
# against a real cluster.
#
# - deploy a ldap server by the output of kustomize
# - add a user
# - query a user
# - delete a user
#
# This script is a test that (passes|fails) if exit
# code is (0|1).
set -x
function configureCluster {
local tmpDir=$1
local target=$2
echo Kustomizing: \"$target\"
ls $target
kustomize build $target > $tmpDir/my.yaml
[[ $? -eq 0 ]] || { exitWith "Failed to kustomize build"; }
# Setting the namespace this way is a test-infra thing?
kubectl config set-context \
$(kubectl config current-context) --namespace=default
kubectl apply -f $tmpDir/my.yaml
[[ $? -eq 0 ]] || { exitWith "Failed to run kubectl apply"; }
sleep 20
}
function tearDownCluster {
local tmpDir=$1
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 tmpDir=$1
local namespace=`getPodField '{.items[0].metadata.namespace}'`
[[ -z $namespace ]] && { exitWith "Unable to get namespace"; }
local myUserFile=$tmpDir/user.ldif
local podUserFile=/tmp/user.ldif
cat <<EOF >$myUserFile
dn: cn=The Postmaster,dc=example,dc=org
objectClass: organizationalRole
cn: The Postmaster
EOF
[[ -f $myUserFile ]] || \
{ exitWith "Failed to create $myUserFile"; }
kubectl cp $myUserFile \
$namespace/$podName:$podUserFile || \
{ exitWith "Failed to copy $myUserFile to pod $podName"; }
rm $myUserFile
podExec \
ldapadd \
-x \
-w admin \
-H ldap://localhost \
-D "cn=admin,dc=example,dc=org" \
-f $podUserFile
}
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"
}
tmpDir=$(mktemp -d)
configureCluster $tmpDir $1
podName=`getPodField '{.items[0].metadata.name}'`
[[ -z $podName ]] && { exitWith "Unable to get pod name"; }
containerName="ldap"
getUserCount; [[ $? -eq 0 ]] || { exitWith "Expected no users."; }
addUser $tmpDir || { 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 $tmpDir