-
Notifications
You must be signed in to change notification settings - Fork 899
/
test-job-run.sh
executable file
·130 lines (96 loc) · 2.74 KB
/
test-job-run.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
#!/bin/bash
#test /api/job/{id}/run
errorMsg() {
echo "$*" 1>&2
}
DIR=$(cd `dirname $0` && pwd)
# accept url argument on commandline, if '-' use default
url="$1"
if [ "-" == "$1" ] ; then
url='http://localhost:4440/api'
fi
apiurl="${url}/api"
VERSHEADER="X-RUNDECK-API-VERSION: 1.2"
# curl opts to use a cookie jar, and follow redirects, showing only errors
CURLOPTS="-s -S -L -c $DIR/cookies -b $DIR/cookies"
CURL="curl $CURLOPTS"
XMLSTARLET=xml
###
# setup: create a new job and acquire the ID
###
# job exec
args="echo hello there"
project=$2
if [ "" == "$2" ] ; then
project="test"
fi
#escape the string for xml
xmlargs=$($XMLSTARLET esc "$args")
xmlproj=$($XMLSTARLET esc "$project")
#produce job.xml content corresponding to the dispatch request
cat > $DIR/temp.out <<END
<joblist>
<job>
<name>cli job</name>
<group>api-test/job-run</group>
<description></description>
<loglevel>INFO</loglevel>
<context>
<project>$xmlproj</project>
</context>
<dispatch>
<threadcount>1</threadcount>
<keepgoing>true</keepgoing>
</dispatch>
<sequence>
<command>
<exec>$xmlargs</exec>
</command>
</sequence>
</job>
</joblist>
END
# now submit req
runurl="${apiurl}/jobs/import"
params=""
# specify the file for upload with curl, named "xmlBatch"
ulopts="-F xmlBatch=@$DIR/temp.out"
# get listing
$CURL $ulopts --header "$VERSHEADER" ${runurl}?${params} > $DIR/curl.out
if [ 0 != $? ] ; then
errorMsg "ERROR: failed query request"
exit 2
fi
sh $DIR/api-test-success.sh $DIR/curl.out || exit 2
#result will contain list of failed and succeeded jobs, in this
#case there should only be 1 failed or 1 succeeded since we submit only 1
succount=$($XMLSTARLET sel -T -t -v "/result/succeeded/@count" $DIR/curl.out)
jobid=$($XMLSTARLET sel -T -t -v "/result/succeeded/job/id" $DIR/curl.out)
if [ "1" != "$succount" -o "" == "$jobid" ] ; then
errorMsg "Upload was not successful."
exit
fi
###
# Run the chosen id, expect success message
###
echo "TEST: job/id/run should succeed"
# now submit req
runurl="${apiurl}/job/${jobid}/run"
params=""
# get listing
$CURL --header "$VERSHEADER" ${runurl}?${params} > $DIR/curl.out
if [ 0 != $? ] ; then
errorMsg "ERROR: failed query request"
exit 2
fi
sh $DIR/api-test-success.sh $DIR/curl.out || exit 2
#get execid
execcount=$($XMLSTARLET sel -T -t -v "/result/executions/@count" $DIR/curl.out)
execid=$($XMLSTARLET sel -T -t -v "/result/executions/execution/@id" $DIR/curl.out)
if [ "1" == "${execcount}" -a "" != "${execid}" ] ; then
echo "OK"
else
errorMsg "FAIL: expected run success message for execution id. (count: ${execcount}, id: ${execid})"
exit 2
fi
rm $DIR/curl.out