First version of service exposure
[nonrtric.git] / service-exposure / rp_test.sh
1 #!/bin/bash
2 #
3 # ============LICENSE_START=======================================================
4 #  Copyright (C) 2022 Nordix Foundation.
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 #
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
20 #
21 INGRESS_HOST=$(minikube ip)
22 INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].nodePort}')
23 TESTS=0
24 PASSED=0
25 FAILED=0
26 TEST_TS=$(date +%F-%T)
27 TOKEN=""
28 ACCESS_TOKEN=""
29 REFRESH_TOKEN=""
30
31 function get_token
32 {
33     local prefix="${1}" 
34     url="http://192.168.49.2:31560/auth/realms"
35     #     echo $url
36          TOKEN=$(curl -s -X POST $url/provider/protocol/openid-connect/token -H \
37                  "Content-Type: application/x-www-form-urlencoded" -d client_secret=OwTCeahULA21G5TfEVMLG1iMloGiyH3i \
38                  -d 'grant_type=client_credentials' -d client_id=provider-cli) 
39         echo "TOKEN: $TOKEN"
40         ACCESS_TOKEN=$(echo $TOKEN | jq -r '.access_token')
41         #echo "ACCESS_TOKEN: $ACCESS_TOKEN"
42         REFRESH_TOKEN=$(echo $TOKEN | jq -r '.refresh_token')
43         #echo "REFRESH_TOKEN: $REFRESH_TOKEN"
44         # TOKEN2=$(curl -s -X POST $url/provider/protocol/openid-connect/token -H \
45         #        "Content-Type: application/x-www-form-urlencoded" -d client_secret= \
46         #        -d refresh_token=$REFRESH_TOKEN \
47         #        -d 'grant_type=refresh_token' -d client_id=provider-cli) 
48        #echo "TOKEN2 = $TOKEN2"
49          #ACCESS_TOKEN=""
50     echo $ACCESS_TOKEN
51 }
52
53 function run_test
54 {
55     local prefix="${1}" type=${2} msg="${3}" data=${4}
56     TESTS=$((TESTS+1))
57     echo "Test ${TESTS}: Testing $type /${prefix}"
58     get_token $prefix
59     url=$INGRESS_HOST:$INGRESS_PORT"/"$prefix
60     #echo $url
61     result=$(curl -s -X ${type} -H "Content-type: application/json" -H "Authorization: Bearer $ACCESS_TOKEN" $url)
62     echo $result
63     if [ "$result" != "$msg" ]; then
64             echo "FAIL"
65             FAILED=$((FAILED+1))
66     else
67             echo "PASS"
68             PASSED=$((PASSED+1))
69     fi
70     echo ""
71 }
72
73
74 run_test "rapp-provider" "GET" "Hello World!" ""
75
76 echo
77 echo "-----------------------------------------------------------------------"
78 echo "Number of Tests: $TESTS, Tests Passed: $PASSED, Tests Failed: $FAILED"
79 echo "Date: $TEST_TS"
80 echo "-----------------------------------------------------------------------"