blob: a27f3b4b71d6bb5a36672d4331ee3e15078fd490 [file] [log] [blame]
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001#!/bin/bash
2
3# ============LICENSE_START===============================================
4# Copyright (C) 2020 Nordix Foundation. All rights reserved.
5# ========================================================================
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17# ============LICENSE_END=================================================
18#
19
20
21### Admin API functions producer stub
22
23
24# Excute a curl cmd towards the prodstub simulator and check the response code.
25# args: TEST|CONF <expected-response-code> <curl-cmd-string> [<json-file-to-compare-output>]
26__execute_curl_to_prodstub() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +010027 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
28 echo "(${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +020029 echo " CMD: $3" >> $HTTPLOG
30 res="$($3)"
31 echo " RESP: $res" >> $HTTPLOG
32 retcode=$?
33 if [ $retcode -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +010034 __log_conf_fail_general " Fatal error when executing curl, response: "$retcode
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +020035 return 1
36 fi
37 status=${res:${#res}-3}
38 if [ $status -eq $2 ]; then
39 if [ $# -eq 4 ]; then
40 body=${res:0:${#res}-3}
41 jobfile=$(cat $4)
42 echo " TARGET JSON: $jobfile" >> $HTTPLOG
43 res=$(python3 ../common/compare_json.py "$jobfile" "$body")
44 if [ $res -ne 0 ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +010045 if [ $1 == "TEST" ]; then
46 __log_test_fail_body
47 else
48 __log_conf_fail_body
49 fi
50 return 1
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +020051 fi
52 fi
53 if [ $1 == "TEST" ]; then
BjornMagnussonXA7b36db62020-11-23 10:57:57 +010054 __log_test_pass
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +020055 else
BjornMagnussonXA7b36db62020-11-23 10:57:57 +010056 __log_conf_ok
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +020057 fi
58 return 0
59 fi
BjornMagnussonXA7b36db62020-11-23 10:57:57 +010060 if [ $1 == "TEST" ]; then
61 __log_test_fail_status_code $2 $status
62 else
63 __log_conf_fail_status_code $2 $status
64 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +020065 return 1
66}
67
68# Prodstub API: Set (or reset) response code for producer supervision
69# <response-code> <producer-id> [<forced_response_code>]
70# (Function for test scripts)
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +020071prodstub_arm_producer() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +010072 __log_conf_start $@
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +020073 if [ $# -ne 2 ] && [ $# -ne 3 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +020074 __print_err "<response-code> <producer-id> [<forced_response_code>]" $@
75 return 1
76 fi
77
78 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_LOCALHOST/arm/supervision/"$2
79 if [ $# -eq 3 ]; then
80 curlString=$curlString"?response="$3
81 fi
82
83 __execute_curl_to_prodstub CONF $1 "$curlString"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +010084 return $?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +020085}
86
87# Prodstub API: Set (or reset) response code job create
88# <response-code> <producer-id> <job-id> [<forced_response_code>]
89# (Function for test scripts)
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +020090prodstub_arm_job_create() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +010091 __log_conf_start $@
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +020092 if [ $# -ne 3 ] && [ $# -ne 4 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +020093 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
94 return 1
95 fi
96
97 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_LOCALHOST/arm/create/$2/$3"
98 if [ $# -eq 4 ]; then
99 curlString=$curlString"?response="$4
100 fi
101
102 __execute_curl_to_prodstub CONF $1 "$curlString"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100103 return $?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200104}
105
106# Prodstub API: Set (or reset) response code job delete
107# <response-code> <producer-id> <job-id> [<forced_response_code>]
108# (Function for test scripts)
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200109prodstub_arm_job_delete() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100110 __log_conf_start $@
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200111 if [ $# -ne 3 ] && [ $# -ne 4 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200112 __print_err "<response-code> <producer-id> <job-id> [<forced_response_code>]" $@
113 return 1
114 fi
115
116 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_LOCALHOST/arm/delete/$2/$3"
117 if [ $# -eq 4 ]; then
118 curlString=$curlString"?response="$4
119 fi
120
121 __execute_curl_to_prodstub CONF $1 "$curlString"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100122 return $?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200123}
124
125# Prodstub API: Arm a type of a producer
126# <response-code> <producer-id> <type-id>
127# (Function for test scripts)
128prodstub_arm_type() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100129 __log_conf_start $@
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200130 if [ $# -ne 3 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200131 __print_err "<response-code> <producer-id> <type-id>" $@
132 return 1
133 fi
134
135 curlString="curl -X PUT -skw %{http_code} $PROD_STUB_LOCALHOST/arm/type/$2/$3"
136
137 __execute_curl_to_prodstub CONF $1 "$curlString"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100138 return $?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200139}
140
141# Prodstub API: Disarm a type in a producer
142# <response-code> <producer-id> <type-id>
143# (Function for test scripts)
144prodstub_disarm_type() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100145 __log_conf_start $@
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200146 if [ $# -ne 3 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200147 __print_err "<response-code> <producer-id> <type-id>" $@
148 return 1
149 fi
150
151 curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_LOCALHOST/arm/type/$2/$3"
152
153 __execute_curl_to_prodstub CONF $1 "$curlString"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100154 return $?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200155}
156
157# Prodstub API: Get job data for a job and compare with a target job json
BjornMagnussonXA2138b632020-11-30 21:17:32 +0100158# <response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200159# (Function for test scripts)
160prodstub_check_jobdata() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100161 __log_test_start $@
BjornMagnussonXA2138b632020-11-30 21:17:32 +0100162 if [ $# -ne 7 ]; then
163 __print_err "<response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>" $@
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200164 return 1
165 fi
BjornMagnussonXA2138b632020-11-30 21:17:32 +0100166 if [ -f $7 ]; then
167 jobfile=$(cat $7)
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200168 jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g")
169 else
BjornMagnussonXA2138b632020-11-30 21:17:32 +0100170 _log_test_fail_general "Template file "$7" for jobdata, does not exist"
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200171 return 1
172 fi
BjornMagnussonXA2138b632020-11-30 21:17:32 +0100173 targetJson="{\"ei_job_identity\":\"$3\",\"ei_type_identity\":\"$4\",\"target_uri\":\"$5\",\"owner\":\"$6\", \"ei_job_data\":$jobfile}"
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200174 file="./tmp/.p.json"
175 echo "$targetJson" > $file
176
177 curlString="curl -X GET -skw %{http_code} $PROD_STUB_LOCALHOST/jobdata/$2/$3"
178
179 __execute_curl_to_prodstub TEST $1 "$curlString" $file
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100180 return $?
181}
182
183# Prodstub API: Delete the job data
184# <response-code> <producer-id> <job-id>
185# (Function for test scripts)
186prodstub_delete_jobdata() {
187 __log_conf_start
188 if [ $# -ne 3 ]; then
189 __print_err "<response-code> <producer-id> <job-id> " $@
190 return 1
191 fi
192 curlString="curl -X DELETE -skw %{http_code} $PROD_STUB_LOCALHOST/jobdata/$2/$3"
193
194 __execute_curl_to_prodstub CONF $1 "$curlString"
195 return $?
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200196}
197
198# Tests if a variable value in the prod stub is equal to a target value and and optional timeout.
199# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
200# equal to the target or not.
201# Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
202# before setting pass or fail depending on if the variable value becomes equal to the target
203# value or not.
204# (Function for test scripts)
205prodstub_equal() {
206 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
207 __var_test "PRODSTUB" "$LOCALHOST$PROD_STUB_EXTERNAL_PORT/counter/" $1 "=" $2 $3
208 else
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200209 __print_err "Wrong args to prodstub_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
210 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200211}