BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 1 | #!/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() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 27 | TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S") |
| 28 | echo "(${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 29 | echo " CMD: $3" >> $HTTPLOG |
| 30 | res="$($3)" |
| 31 | echo " RESP: $res" >> $HTTPLOG |
| 32 | retcode=$? |
| 33 | if [ $retcode -ne 0 ]; then |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 34 | __log_conf_fail_general " Fatal error when executing curl, response: "$retcode |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 35 | 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 |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 45 | if [ $1 == "TEST" ]; then |
| 46 | __log_test_fail_body |
| 47 | else |
| 48 | __log_conf_fail_body |
| 49 | fi |
| 50 | return 1 |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 51 | fi |
| 52 | fi |
| 53 | if [ $1 == "TEST" ]; then |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 54 | __log_test_pass |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 55 | else |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 56 | __log_conf_ok |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 57 | fi |
| 58 | return 0 |
| 59 | fi |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 60 | if [ $1 == "TEST" ]; then |
| 61 | __log_test_fail_status_code $2 $status |
| 62 | else |
| 63 | __log_conf_fail_status_code $2 $status |
| 64 | fi |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 65 | 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) |
BjornMagnussonXA | 39ad50e | 2020-10-22 09:55:25 +0200 | [diff] [blame] | 71 | prodstub_arm_producer() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 72 | __log_conf_start $@ |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 73 | if [ $# -ne 2 ] && [ $# -ne 3 ]; then |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 74 | __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" |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 84 | return $? |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 85 | } |
| 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) |
BjornMagnussonXA | 39ad50e | 2020-10-22 09:55:25 +0200 | [diff] [blame] | 90 | prodstub_arm_job_create() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 91 | __log_conf_start $@ |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 92 | if [ $# -ne 3 ] && [ $# -ne 4 ]; then |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 93 | __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" |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 103 | return $? |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 104 | } |
| 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) |
BjornMagnussonXA | 39ad50e | 2020-10-22 09:55:25 +0200 | [diff] [blame] | 109 | prodstub_arm_job_delete() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 110 | __log_conf_start $@ |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 111 | if [ $# -ne 3 ] && [ $# -ne 4 ]; then |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 112 | __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" |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 122 | return $? |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | # Prodstub API: Arm a type of a producer |
| 126 | # <response-code> <producer-id> <type-id> |
| 127 | # (Function for test scripts) |
| 128 | prodstub_arm_type() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 129 | __log_conf_start $@ |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 130 | if [ $# -ne 3 ]; then |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 131 | __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" |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 138 | return $? |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | # Prodstub API: Disarm a type in a producer |
| 142 | # <response-code> <producer-id> <type-id> |
| 143 | # (Function for test scripts) |
| 144 | prodstub_disarm_type() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 145 | __log_conf_start $@ |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 146 | if [ $# -ne 3 ]; then |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 147 | __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" |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 154 | return $? |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | # Prodstub API: Get job data for a job and compare with a target job json |
BjornMagnussonXA | 2138b63 | 2020-11-30 21:17:32 +0100 | [diff] [blame] | 158 | # <response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file> |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 159 | # (Function for test scripts) |
| 160 | prodstub_check_jobdata() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 161 | __log_test_start $@ |
BjornMagnussonXA | 2138b63 | 2020-11-30 21:17:32 +0100 | [diff] [blame] | 162 | if [ $# -ne 7 ]; then |
| 163 | __print_err "<response-code> <producer-id> <job-id> <type-id> <target-url> <job-owner> <template-job-file>" $@ |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 164 | return 1 |
| 165 | fi |
BjornMagnussonXA | 2138b63 | 2020-11-30 21:17:32 +0100 | [diff] [blame] | 166 | if [ -f $7 ]; then |
| 167 | jobfile=$(cat $7) |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 168 | jobfile=$(echo "$jobfile" | sed "s/XXXX/$3/g") |
| 169 | else |
BjornMagnussonXA | 2138b63 | 2020-11-30 21:17:32 +0100 | [diff] [blame] | 170 | _log_test_fail_general "Template file "$7" for jobdata, does not exist" |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 171 | return 1 |
| 172 | fi |
BjornMagnussonXA | 2138b63 | 2020-11-30 21:17:32 +0100 | [diff] [blame] | 173 | targetJson="{\"ei_job_identity\":\"$3\",\"ei_type_identity\":\"$4\",\"target_uri\":\"$5\",\"owner\":\"$6\", \"ei_job_data\":$jobfile}" |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 174 | 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 |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 180 | return $? |
| 181 | } |
| 182 | |
| 183 | # Prodstub API: Delete the job data |
| 184 | # <response-code> <producer-id> <job-id> |
| 185 | # (Function for test scripts) |
| 186 | prodstub_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 $? |
BjornMagnussonXA | 39ad50e | 2020-10-22 09:55:25 +0200 | [diff] [blame] | 196 | } |
| 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) |
| 205 | prodstub_equal() { |
| 206 | if [ $# -eq 2 ] || [ $# -eq 3 ]; then |
| 207 | __var_test "PRODSTUB" "$LOCALHOST$PROD_STUB_EXTERNAL_PORT/counter/" $1 "=" $2 $3 |
| 208 | else |
BjornMagnussonXA | 39ad50e | 2020-10-22 09:55:25 +0200 | [diff] [blame] | 209 | __print_err "Wrong args to prodstub_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@ |
| 210 | fi |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 211 | } |