Tests: extend FTC850 benchmark test to include GETs
[nonrtric.git] / test / common / get_policies_process.py
1 #  ============LICENSE_START===============================================
2 #  Copyright (C) 2020-2023 Nordix Foundation. All rights reserved.
3 #  Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
4 #  ========================================================================
5 #  Licensed under the Apache License, Version 2.0 (the "License");
6 #  you may not use this file except in compliance with the License.
7 #  You may obtain a copy of the License at
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #  Unless required by applicable law or agreed to in writing, software
12 #  distributed under the License is distributed on an "AS IS" BASIS,
13 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #  See the License for the specific language governing permissions and
15 #  limitations under the License.
16 #  ============LICENSE_END=================================================
17 #
18
19 # This script gets policies spread over a number rics
20 # Intended for parallel processing
21 # Returns a string with result, either "0" for ok, or "1<fault description>"
22
23 import os
24 import json
25 import sys
26 import requests
27 import traceback
28
29 # disable warning about unverified https requests
30 from requests.packages import urllib3
31
32 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
33
34 #arg responsecode baseurl num_rics uuid startid count pids pid_id proxy
35
36 try:
37     if len(sys.argv) != 10:
38         print("1Expected 9 args, got "+str(len(sys.argv)-1)+ ". Args: responsecode baseurl num_rics uuid startid count pids pid_id proxy")
39         sys.exit()
40
41     responsecode=int(sys.argv[1])
42     baseurl=str(sys.argv[2])
43     num_rics=int(sys.argv[3])
44     uuid=str(sys.argv[4])
45     start=int(sys.argv[5])
46     count=int(sys.argv[6])
47     pids=int(sys.argv[7])
48     pid_id=int(sys.argv[8])
49     httpproxy=str(sys.argv[9])
50
51     proxydict=None
52     if httpproxy != "NOPROXY":
53         proxydict = {
54             "http" : httpproxy,
55             "https" : httpproxy
56         }
57     if uuid == "NOUUID":
58         uuid=""
59
60     http_retry_count=0
61     connect_retry_count=0
62
63     stop=count*num_rics+start
64     for i in range(start,stop):
65         if (i%pids == (pid_id-1)):
66             connect_ok=False
67             retry_cnt=5
68             while(retry_cnt>0):
69                 if ("/v2/policies/" in baseurl):
70                     url=str(baseurl+uuid+str(i))
71                 else:
72                     url=str(baseurl+"?id="+uuid+str(i))
73                 try:
74                     if proxydict is None:
75                         resp=requests.get(url, verify=False, timeout=90)
76                     else:
77                         resp=requests.get(url, verify=False, timeout=90, proxies=proxydict)
78                     cconnect_ok=True
79                 except Exception as e1:
80                     if (retry_cnt > 1):
81                         sleep(0.1)
82                         retry_cnt -= 1
83                         connect_retry_count += 1
84                     else:
85                         print("1Get failed for id:"+uuid+str(i)+ ", "+str(e1) + " "+traceback.format_exc())
86                         sys.exit()
87
88                 if (cconnect_ok == True):
89                     if (resp.status_code == None):
90                         print("1Get failed for id:"+uuid+str(i)+ ", expected response code: "+str(responsecode)+", got: None")
91                         sys.exit()
92                     if (resp.status_code != responsecode):
93                         if (resp.status_code >= 500) and (http_retry_count < 600 ) and (retry_cnt > 1):
94                             sleep(0.1)
95                             retry_cnt -= 1
96                             http_retry_count += 1
97                         else:
98                             print("1Get failed for id:"+uuid+str(i)+ ", expected response code: "+str(responsecode)+", got: "+str(resp.status_code)+str(resp.raw))
99                             sys.exit()
100                     else:
101                         retry_cnt=-1
102
103     print("0 http retries:"+str(http_retry_count) + ", connect retries: "+str(connect_retry_count))
104     sys.exit()
105
106 except Exception as e:
107     print("1"+str(e))
108     traceback.print_exc()
109 sys.exit()