Skip to content

Commit 13647e4

Browse files
committed
tests: improve test-samba-ad-server-kubernetes
- move deployment setup to its own script - add coredns patching to deployment - Make deletion of deployment optional with KEEP=1 Signed-off-by: Michael Adam <[email protected]>
1 parent ab2e92d commit 13647e4

File tree

3 files changed

+109
-45
lines changed

3 files changed

+109
-45
lines changed

tests/files/coredns-snippet.template

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
domain1.sink.test:53 {
2+
errors
3+
cache 30
4+
forward . AD_SERVER_IP
5+
}

tests/test-deploy-ad-server.sh

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env bash
2+
3+
4+
SCRIPT_DIR="$(cd "$(dirname "${0}")" && pwd)"
5+
BASE_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
6+
DEPLOYMENT_YAML="${BASE_DIR}/tests/files/samba-ad-server-deployment.yml"
7+
DEPLOYMENT_NAME="samba-ad-server"
8+
9+
_error() {
10+
echo "$@"
11+
exit 1
12+
}
13+
14+
echo "Creating ad server deployment..."
15+
ERROR_MSG=$(kubectl create -f "${DEPLOYMENT_YAML}" 2>&1 1>/dev/null)
16+
if [ $? -ne 0 ] ; then
17+
if [[ "${ERROR_MSG}" =~ "AlreadyExists" ]] ; then
18+
echo "Deployment exists already. Continuing."
19+
else
20+
_error "Error creating ad server deployment."
21+
fi
22+
fi
23+
24+
kubectl get deployment
25+
26+
replicaset="$(kubectl describe deployment ${DEPLOYMENT_NAME} | grep -s "NewReplicaSet:" | awk '{ print $2 }')"
27+
[ $? -eq 0 ] || _error "Error getting replicaset"
28+
29+
podname="$(kubectl get pod | grep $replicaset | awk '{ print $1 }')"
30+
[ $? -eq 0 ] || _error "Error getting podname"
31+
32+
echo "Samba ad pod is $podname"
33+
34+
echo "waiting for pod to be in Running state"
35+
tries=0
36+
podstatus="none"
37+
until [ $tries -ge 120 ] || echo $podstatus | grep -q 'Running'; do
38+
sleep 1
39+
echo -n "."
40+
tries=$(( tries + 1 ))
41+
podstatus="$(kubectl get pod $podname -o go-template='{{.status.phase}}')"
42+
done
43+
echo
44+
kubectl get pod
45+
echo
46+
echo $podstatus | grep -q 'Running' || _error "Pod did not reach Running state"
47+
48+
echo "waiting for samba to become reachable"
49+
tries=0
50+
rc=1
51+
while [ $tries -lt 120 ] && [ $rc -ne 0 ]; do
52+
sleep 1
53+
tries=$(( tries + 1 ))
54+
kubectl exec "${podname}" -- smbclient -N -L 127.0.0.1 2>/dev/null 1>/dev/null
55+
rc=$?
56+
echo -n "."
57+
done
58+
echo
59+
[ $rc -eq 0 ] || _error "Error: samba ad did not become reachable"
60+
61+
62+
echo "patching coredns zonefile"
63+
64+
AD_POD_IP=$(kubectl get pod -o jsonpath='{ .items[*].status.podIP }')
65+
[ $? -eq 0 ] || _error "Error getting ad server pod IP"
66+
67+
echo "AD pod IP: ${AD_POD_IP}"
68+
69+
TMPFILE=$(mktemp)
70+
71+
cat > "${TMPFILE}" <<EOF
72+
data:
73+
Corefile: |
74+
EOF
75+
76+
kubectl get cm -n kube-system coredns -o jsonpath='{ .data.Corefile }' \
77+
| sed -e 's/^/ /g' \
78+
>> "${TMPFILE}"
79+
80+
echo >> "${TMPFILE}"
81+
82+
# don't repeat an existing block for our domain
83+
FIRSTLINE="$(head -1 ./tests/files/coredns-snippet.template)"
84+
LASTLINE=" }"
85+
86+
sed -i .backup -e "/$FIRSTLINE/,/$LASTLINE/d" ${TMPFILE}
87+
88+
cat tests/files/coredns-snippet.template \
89+
| sed -e "s/AD_SERVER_IP/${AD_POD_IP}/" \
90+
>> "${TMPFILE}"
91+
92+
echo >> "${TMPFILE}"
93+
94+
kubectl patch cm -n kube-system coredns -p "$(cat ${TMPFILE})"
95+
[ $? -eq 0 ] || _error "Error patching coredns config map"
96+
97+
echo "ad setup done"

tests/test-samba-ad-server-kubernetes.sh

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,18 @@ _error() {
1010
exit 1
1111
}
1212

13-
kubectl create -f "${DEPLOYMENT_YAML}"
14-
[ $? -eq 0 ] || _error "Error creating deployment"
15-
16-
kubectl get deployment
17-
18-
replicaset="$(kubectl describe deployment ${DEPLOYMENT_NAME} | grep -s "NewReplicaSet:" | awk '{ print $2 }')"
19-
[ $? -eq 0 ] || _error "Error getting replicaset"
20-
21-
podname="$(kubectl get pod | grep $replicaset | awk '{ print $1 }')"
22-
[ $? -eq 0 ] || _error "Error getting podname"
23-
24-
echo "Samba pod is $podname"
25-
26-
echo "waiting for pod to be in Running state"
27-
tries=0
28-
podstatus="none"
29-
until [ $tries -ge 120 ] || echo $podstatus | grep -q 'Running'; do
30-
sleep 1
31-
echo -n "."
32-
tries=$(( tries + 1 ))
33-
podstatus="$(kubectl get pod $podname -o go-template='{{.status.phase}}')"
34-
done
35-
echo
36-
kubectl get pod
37-
echo
38-
echo $podstatus | grep -q 'Running' || _error "Pod did not reach Running state"
39-
40-
echo "waiting for samba to become reachable"
41-
tries=0
42-
rc=1
43-
while [ $tries -lt 120 ] && [ $rc -ne 0 ]; do
44-
sleep 1
45-
tries=$(( tries + 1 ))
46-
kubectl exec "${podname}" -- smbclient -N -L 127.0.0.1 2>/dev/null 1>/dev/null
47-
rc=$?
48-
echo -n "."
49-
done
50-
echo
51-
[ $rc -eq 0 ] || _error "Error listing samba shares"
52-
53-
kubectl exec "${podname}" -- smbclient -N -L 127.0.0.1
54-
echo
13+
. ${BASE_DIR}/tests/test-deploy-ad-server.sh
5514

5615
kubectl exec "${podname}" -- samba-tool domain info 127.0.0.1
5716
[ $? -eq 0 ] || _error "Error listing domain info"
5817
echo
5918

60-
kubectl delete deployment "samba-ad-server"
61-
[ $? -eq 0 ] || _error "Error deleting deployment"
62-
echo
19+
if [ ${KEEP} -eq 0 ]; then
20+
echo "removing ad server deployment again..."
21+
kubectl delete deployment "samba-ad-server"
22+
[ $? -eq 0 ] || _error "Error deleting deployment"
23+
echo
24+
fi
6325

6426
echo "Success"
6527
exit 0

0 commit comments

Comments
 (0)