X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=src%2Ftest%2Fresources%2Fhttp-cache%2Fthird_party_proxy.py;h=5ec9f46c14404f97e0993a8ff6e26c36426bd245;hb=a610d12a1d2542cefb509294cc81a2b4fa48ddfb;hp=a1092412820bbddcd8f3d300ad67f5624ba758ab;hpb=d72d0d05d74f4125e8f36beea096aa7769d19eab;p=clamp.git diff --git a/src/test/resources/http-cache/third_party_proxy.py b/src/test/resources/http-cache/third_party_proxy.py index a1092412..5ec9f46c 100755 --- a/src/test/resources/http-cache/third_party_proxy.py +++ b/src/test/resources/http-cache/third_party_proxy.py @@ -43,7 +43,7 @@ parser.add_argument("--temp", "-t", default=tempfile.mkdtemp(), type=str, he parser.add_argument("--proxy" , type=str, help="Url of the Act as a proxy. If not set, this script only uses the cache and will return a 404 if files aren't found") parser.add_argument("--port", "-P", type=int, default="8081", help="Port on which the proxy should listen to") parser.add_argument("--verbose", "-v", type=bool, help="Print more information in case of error") -parser.add_argument("--proxyaddress","-a", type=str, help="Address of this proxy, generally either third_party_proxy:8085 or localhost:8085 depending if started with docker-compose or not") +parser.add_argument("--proxyaddress","-a", type=str, help="Address of this proxy, generally either third-party-proxy:8085 or localhost:8085 depending if started with docker-compose or not") options = parser.parse_args() @@ -127,10 +127,28 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): with open(cached_file_content, 'w') as f: f.write(jsonGenerated) return True - elif self.path.startswith("/dcae-operationstatus") and http_type == "GET": + elif self.path.startswith("/dcae-operationstatus/install") and http_type == "GET": if not _file_available: - print "self.path start with /dcae-operationstatus, generating response json..." - jsonGenerated = "{\"operationType\": \"operationType1\", \"status\": \"succeeded\"}" + print "self.path start with /dcae-operationstatus/install, generating response json..." + jsonGenerated = "{\"operationType\": \"install\", \"status\": \"succeeded\"}" + print "jsonGenerated: " + jsonGenerated + + try: + os.makedirs(cached_file_folder, 0777) + except OSError as e: + if e.errno != errno.EEXIST: + raise + print(cached_file_folder+" already exists") + + with open(cached_file_header, 'w') as f: + f.write("{\"Content-Length\": \"" + str(len(jsonGenerated)) + "\", \"Content-Type\": \"application/json\"}") + with open(cached_file_content, 'w') as f: + f.write(jsonGenerated) + return True + elif self.path.startswith("/dcae-operationstatus/uninstall") and http_type == "GET": + if not _file_available: + print "self.path start with /dcae-operationstatus/uninstall, generating response json..." + jsonGenerated = "{\"operationType\": \"uninstall\", \"status\": \"succeeded\"}" print "jsonGenerated: " + jsonGenerated try: @@ -157,19 +175,79 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): f.write("{\"Content-Length\": \"" + str(len(jsonGenerated)) + "\", \"Content-Type\": \"application/json\"}") with open(cached_file_content, 'w') as f: f.write(jsonGenerated) - return True; - elif self.path.startswith("/dcae-deployments/") and (http_type == "PUT" or http_type == "DELETE"): - if not _file_available: - print "self.path start with /dcae-deployments/, generating response json..." + return True + elif self.path.startswith("/dcae-deployments/") and http_type == "PUT": + print "self.path start with /dcae-deployments/ DEPLOY, generating response json..." #jsondata = json.loads(self.data_string) - jsonGenerated = "{\"links\":{\"status\":\"http:\/\/" + PROXY_ADDRESS + "\/dcae-operationstatus\",\"test2\":\"test2\"}}" + jsonGenerated = "{\"operationType\":\"install\",\"status\":\"processing\",\"links\":{\"status\":\"http:\/\/" + PROXY_ADDRESS + "\/dcae-operationstatus/install\"}}" print "jsonGenerated: " + jsonGenerated - - os.makedirs(cached_file_folder, 0777) - with open(cached_file_header, 'w') as f: + if not os.path.exists(cached_file_folder): + os.makedirs(cached_file_folder, 0777) + with open(cached_file_header, 'w+') as f: f.write("{\"Content-Length\": \"" + str(len(jsonGenerated)) + "\", \"Content-Type\": \"application/json\"}") - with open(cached_file_content, 'w') as f: + with open(cached_file_content, 'w+') as f: + f.write(jsonGenerated) + return True + elif self.path.startswith("/dcae-deployments/") and http_type == "DELETE": + print "self.path start with /dcae-deployments/ UNDEPLOY, generating response json..." + #jsondata = json.loads(self.data_string) + jsonGenerated = "{\"operationType\":\"uninstall\",\"status\":\"processing\",\"links\":{\"status\":\"http:\/\/" + PROXY_ADDRESS + "\/dcae-operationstatus/uninstall\"}}" + print "jsonGenerated: " + jsonGenerated + if not os.path.exists(cached_file_folder): + os.makedirs(cached_file_folder, 0777) + with open(cached_file_header, 'w+') as f: + f.write("{\"Content-Length\": \"" + str(len(jsonGenerated)) + "\", \"Content-Type\": \"application/json\"}") + with open(cached_file_content, 'w+') as f: f.write(jsonGenerated) + return True + elif (self.path.startswith("/pdp/api/") and (http_type == "PUT" or http_type == "DELETE")) or (self.path.startswith("/pdp/api/policyEngineImport") and http_type == "POST"): + print "self.path start with /pdp/api/, copying body to response ..." + if not os.path.exists(cached_file_folder): + os.makedirs(cached_file_folder, 0777) + with open(cached_file_header, 'w+') as f: + f.write("{\"Content-Length\": \"" + str(len(self.data_string)) + "\", \"Content-Type\": \""+str(self.headers['Content-Type'])+"\"}") + with open(cached_file_content, 'w+') as f: + f.write(self.data_string) + return True + elif self.path.startswith("/policy/api/v1/policytypes/") and http_type == "POST": + print "self.path start with POST new policy API /pdp/api/, copying body to response ..." + if not os.path.exists(cached_file_folder): + os.makedirs(cached_file_folder, 0777) + with open(cached_file_header, 'w+') as f: + f.write("{\"Content-Length\": \"" + str(len(self.data_string)) + "\", \"Content-Type\": \""+str(self.headers['Content-Type'])+"\"}") + with open(cached_file_content, 'w+') as f: + f.write(self.data_string) + return True + elif self.path.startswith("/policy/api/v1/policytypes/") and http_type == "DELETE": + print "self.path start with DELETE new policy API /policy/api/v1/policytypes/ ..." + if not os.path.exists(cached_file_folder): + os.makedirs(cached_file_folder, 0777) + + with open(cached_file_header, 'w+') as f: + f.write("{\"Content-Length\": \"" + str(len("")) + "\", \"Content-Type\": \""+str("")+"\"}") + with open(cached_file_content, 'w+') as f: + f.write(self.data_string) + return True + elif self.path.startswith("/policy/pap/v1/pdps/policies") and http_type == "POST": + print "self.path start with POST new policy API /policy/pap/v1/pdps/ ..." + if not os.path.exists(cached_file_folder): + os.makedirs(cached_file_folder, 0777) + with open(cached_file_header, 'w+') as f: + f.write("{\"Content-Length\": \"" + str(len("")) + "\", \"Content-Type\": \""+str("")+"\"}") + with open(cached_file_content, 'w+') as f: + f.write(self.data_string) + return True + elif self.path.startswith("/policy/api/v1/policytypes/") and http_type == "GET": + print "self.path start with /policy/api/v1/policytypes/, generating response json..." + jsonGenerated = "{\"policyTypeId\": \"onap.policies.controlloop.operational\",\"policyTypeVersion\": \"1.0.0\",\"policyId\": \"OPERATIONAL_z711F_v1_0_ResourceInstanceName1_tca\"}" + print "jsonGenerated: " + jsonGenerated + if not os.path.exists(cached_file_folder): + os.makedirs(cached_file_folder, 0777) + + with open(cached_file_header, 'w') as f: + f.write("{\"Content-Length\": \"" + str(len(jsonGenerated)) + "\", \"Content-Type\": \"application/json\"}") + with open(cached_file_content, 'w') as f: + f.write(jsonGenerated) return True else: return False @@ -201,7 +279,9 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): if not HOST: self.send_response(404) - return "404 Not found" + self.end_headers() + self.wfile.write('404 Not found, no remote HOST specified on the emulator !!!') + return "404 Not found, no remote HOST specified on the emulator !!!" url = '%s%s' % (HOST, self.path) response = requests.get(url, auth=AUTH, headers=HEADERS, stream=True) @@ -214,6 +294,8 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): print('Status code : %s' % (response.status_code,)) print('Content : %s' % (response.content,)) self.send_response(response.status_code) + self.end_headers() + self.wfile.write('404 Not found, nothing found on the remote server !!!') return response.content else: print("Request for data currently present in cache: %s" % (cached_file_folder,)) @@ -252,7 +334,9 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): if not HOST: self.send_response(404) - return "404 Not found" + self.end_headers() + self.wfile.write('404 Not found, no remote HOST specified on the emulator !!!') + return "404 Not found, no remote HOST specified on the emulator !!!" print("Request for data currently not present in cache: %s" % (cached_file_folder,)) @@ -268,6 +352,8 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): print('Status code : %s' % (response.status_code,)) print('Content : %s' % (response.content,)) self.send_response(response.status_code) + self.end_headers() + self.wfile.write('404 Not found, nothing found on the remote server !!!') return response.content else: print("Request for data present in cache: %s" % (cached_file_folder,)) @@ -299,7 +385,9 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): if not _file_available: if not HOST: self.send_response(404) - return "404 Not found" + self.end_headers() + self.wfile.write('404 Not found, no remote HOST specified on the emulator !!!') + return "404 Not found, no remote HOST specified on the emulator !!!" print("Request for data currently not present in cache: %s" % (cached_file_folder,)) @@ -315,6 +403,8 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): print('Status code : %s' % (response.status_code,)) print('Content : %s' % (response.content,)) self.send_response(response.status_code) + self.end_headers() + self.wfile.write('404 Not found, nothing found on the remote server !!!') return response.content else: print("Request for data present in cache: %s" % (cached_file_folder,)) @@ -328,6 +418,10 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): cached_file_header="" print("\n\n\nGot a DELETE for %s " % self.path) self.check_credentials() + if self.headers.get('Content-Length') is not None: + self.data_string = self.rfile.read(int(self.headers['Content-Length'])) + else: + self.data_string = "empty generated" print("self.headers:\n %s" % self.headers) is_special = self._execute_content_generated_cases("DELETE") @@ -345,7 +439,9 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): if not _file_available: if not HOST: self.send_response(404) - return "404 Not found" + self.end_headers() + self.wfile.write('404 Not found, no remote HOST specified on the emulator !!!') + return "404 Not found, no remote HOST specified on the emulator !!!" print("Request for data currently not present in cache: %s" % (cached_file_folder,)) @@ -361,6 +457,8 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): print('Status code : %s' % (response.status_code,)) print('Content : %s' % (response.content,)) self.send_response(response.status_code) + self.end_headers() + self.wfile.write('404 Not found, nothing found on the remote server !!!') return response.content else: print("Request for data present in cache: %s" % (cached_file_folder,))