diff options
author | sebdet <sebastien.determe@intl.att.com> | 2018-12-01 15:09:54 +0100 |
---|---|---|
committer | sebdet <sebastien.determe@intl.att.com> | 2018-12-05 13:18:19 +0100 |
commit | d72d0d05d74f4125e8f36beea096aa7769d19eab (patch) | |
tree | 18e76a08b52de40a77288a14f20448efede92d7d /src/test/resources/http-cache | |
parent | d1779040e9f6aeaff20a8c0f4da64f4ac7774ae0 (diff) |
Additional code for Tosca
Tosca code for policy dynamic configuration
Issue-ID: CLAMP-252,CLAMP-251
Change-Id: Icd96f833567050c1dd4730a61765507ad24ebd2e
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
Diffstat (limited to 'src/test/resources/http-cache')
-rwxr-xr-x | src/test/resources/http-cache/start_http_cache.sh | 2 | ||||
-rwxr-xr-x | src/test/resources/http-cache/third_party_proxy.py | 393 |
2 files changed, 223 insertions, 172 deletions
diff --git a/src/test/resources/http-cache/start_http_cache.sh b/src/test/resources/http-cache/start_http_cache.sh index a8ff96ac1..178ac80af 100755 --- a/src/test/resources/http-cache/start_http_cache.sh +++ b/src/test/resources/http-cache/start_http_cache.sh @@ -49,4 +49,4 @@ done echo 'Installing requests packages for Python' pip install requests echo 'Executing the Http proxy in Cache mode only' -python third_party_proxy.py --port 8080 --root /usr/src/http-cache-app/data-cache $python_proxyaddress +python -u third_party_proxy.py --port 8080 --root /usr/src/http-cache-app/data-cache $python_proxyaddress diff --git a/src/test/resources/http-cache/third_party_proxy.py b/src/test/resources/http-cache/third_party_proxy.py index fdc4fab34..a10924128 100755 --- a/src/test/resources/http-cache/third_party_proxy.py +++ b/src/test/resources/http-cache/third_party_proxy.py @@ -25,6 +25,7 @@ import json import requests import os +import errno import sys import SimpleHTTPServer import SocketServer @@ -37,7 +38,8 @@ import shutil parser = argparse.ArgumentParser(description="3rd party Cache & Replay") parser.add_argument("--username", "-u", type=str, help="Set the username for contacting 3rd party - only used for GET") parser.add_argument("--password", "-p", type=str, help="Set the password for contacting 3rd party - only used for GET") -parser.add_argument("--root", "-r", default=tempfile.mkdtemp, type=str, help="Root folder for the proxy cache") +parser.add_argument("--root", "-r", default=tempfile.mkdtemp(), type=str, help="Root folder for the proxy cache") +parser.add_argument("--temp", "-t", default=tempfile.mkdtemp(), type=str, help="Temp folder for the generated content") 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") @@ -49,8 +51,9 @@ PORT = options.port HOST = options.proxy AUTH = (options.username, options.password) HEADERS = {'X-ECOMP-InstanceID':'CLAMP'} -CACHE_ROOT = options.root -PROXY_ADDRESS=options.proxyaddress +CACHE_ROOT = str(options.root) +TMP_ROOT = str(options.temp) +PROXY_ADDRESS=str(options.proxyaddress) def signal_handler(signal_sent, frame): global httpd @@ -61,7 +64,6 @@ def signal_handler(signal_sent, frame): httpd.server_close() class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): - def print_headers(self): for header,value in self.headers.items(): print("header: %s : %s" % (header, value)) @@ -82,238 +84,286 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): fc = f.read() self.wfile.write(fc) - def _write_cache(self,cached_file, header_file, content_file, response): - os.makedirs(cached_file, True) + def _write_cache(self,cached_file_folder, header_file, content_file, response): + os.makedirs(cached_file_folder, 0777) with open(content_file, 'w') as f: f.write(response.raw.read()) with open(header_file, 'w') as f: json.dump(dict(response.raw.headers), f) # Entry point of the code + def _get_cached_file_folder_name(self,folder): + cached_file_folder = '%s/%s' % (folder, self.path,) + print("Cached file name before escaping : %s" % cached_file_folder) + cached_file_folder = cached_file_folder.replace('<','<').replace('>','>').replace('?','?').replace('*','*').replace('\\','*').replace(':',':').replace('|','|') + print("Cached file name after escaping (used for cache storage) : %s" % cached_file_folder) + return cached_file_folder + + def _get_cached_content_file_name(self,cached_file_folder): + return "%s/.file" % (cached_file_folder,) + + def _get_cached_header_file_name(self,cached_file_folder): + return "%s/.header" % (cached_file_folder,) + + def _execute_content_generated_cases(self,http_type): + print("Testing special cases, cache files will be sent to :" +TMP_ROOT) + cached_file_folder = self._get_cached_file_folder_name(TMP_ROOT) + cached_file_content = self._get_cached_content_file_name(cached_file_folder) + cached_file_header = self._get_cached_header_file_name(cached_file_folder) + _file_available = os.path.exists(cached_file_content) + + if self.path.startswith("/dcae-service-types?asdcResourceId=") and http_type == "GET": + if not _file_available: + print "self.path start with /dcae-service-types?asdcResourceId=, generating response json..." + uuidGenerated = str(uuid.uuid4()) + typeId = "typeId-" + uuidGenerated + typeName = "typeName-" + uuidGenerated + print "typeId generated: " + typeName + " and typeName: "+ typeId + jsonGenerated = "{\"totalCount\":1, \"items\":[{\"typeId\":\"" + typeId + "\", \"typeName\":\"" + typeName +"\"}]}" + print "jsonGenerated: " + jsonGenerated + + 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("/dcae-operationstatus") and http_type == "GET": + if not _file_available: + print "self.path start with /dcae-operationstatus, generating response json..." + jsonGenerated = "{\"operationType\": \"operationType1\", \"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("/sdc/v1/catalog/services/") and http_type == "POST": + if not _file_available: + print "self.path start with /sdc/v1/catalog/services/, generating response json..." + jsondata = json.loads(self.data_string) + jsonGenerated = "{\"artifactName\":\"" + jsondata['artifactName'] + "\",\"artifactType\":\"" + jsondata['artifactType'] + "\",\"artifactURL\":\"" + self.path + "\",\"artifactDescription\":\"" + jsondata['description'] + "\",\"artifactChecksum\":\"ZjJlMjVmMWE2M2M1OTM2MDZlODlmNTVmZmYzNjViYzM=\",\"artifactUUID\":\"" + str(uuid.uuid4()) + "\",\"artifactVersion\":\"1\"}" + print "jsonGenerated: " + jsonGenerated + + 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("/dcae-deployments/") and (http_type == "PUT" or http_type == "DELETE"): + if not _file_available: + print "self.path start with /dcae-deployments/, generating response json..." + #jsondata = json.loads(self.data_string) + jsonGenerated = "{\"links\":{\"status\":\"http:\/\/" + PROXY_ADDRESS + "\/dcae-operationstatus\",\"test2\":\"test2\"}}" + print "jsonGenerated: " + jsonGenerated + + 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 + + def do_GET(self): + cached_file_folder = "" + cached_file_content ="" + cached_file_header="" print("\n\n\nGot a GET request for %s " % self.path) self.print_headers() self.check_credentials() - - cached_file = '%s/%s' % (CACHE_ROOT, self.path,) - print("Cached file name before escaping : %s" % cached_file) - cached_file = cached_file.replace('<','<').replace('>','>').replace('?','?').replace('*','*').replace('\\','*').replace(':',':').replace('|','|') - print("Cached file name after escaping (used for cache storage) : %s" % cached_file) - cached_file_content = "%s/.file" % (cached_file,) - cached_file_header = "%s/.header" % (cached_file,) + # Verify if it's a special case + is_special = self._execute_content_generated_cases("GET") + if is_special: + cached_file_folder = self._get_cached_file_folder_name(TMP_ROOT) + cached_file_content = self._get_cached_content_file_name(cached_file_folder) + cached_file_header = self._get_cached_header_file_name(cached_file_folder) + else: + cached_file_folder = self._get_cached_file_folder_name(CACHE_ROOT) + cached_file_content = self._get_cached_content_file_name(cached_file_folder) + cached_file_header = self._get_cached_header_file_name(cached_file_folder) _file_available = os.path.exists(cached_file_content) if not _file_available: - print("Request for data currently not present in cache: %s" % (cached_file,)) - - if self.path.startswith("/dcae-service-types?asdcResourceId="): - print "self.path start with /dcae-service-types?asdcResourceId=, generating response json..." - uuidGenerated = str(uuid.uuid4()) - typeId = "typeId-" + uuidGenerated - typeName = "typeName-" + uuidGenerated - print "typeId generated: " + typeName + " and typeName: "+ typeId - jsonGenerated = "{\"totalCount\":1, \"items\":[{\"typeId\":\"" + typeId + "\", \"typeName\":\"" + typeName +"\"}]}" - print "jsonGenerated: " + jsonGenerated - - os.makedirs(cached_file, True) - 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) - elif self.path.startswith("/dcae-operationstatus"): - print "self.path start with /dcae-operationstatus, generating response json..." - jsonGenerated = "{\"operationType\": \"operationType1\", \"status\": \"succeeded\"}" - print "jsonGenerated: " + jsonGenerated - - os.makedirs(cached_file, True) - 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) + print("Request for data currently not present in cache: %s" % (cached_file_folder,)) + + if not HOST: + self.send_response(404) + return "404 Not found" + + url = '%s%s' % (HOST, self.path) + response = requests.get(url, auth=AUTH, headers=HEADERS, stream=True) + + if response.status_code == 200: + self._write_cache(cached_file_folder, cached_file_header, cached_file_content, response) else: - if not HOST: - self.send_response(404) - return "404 Not found" - - url = '%s%s' % (HOST, self.path) - response = requests.get(url, auth=AUTH, headers=HEADERS, stream=True) - - if response.status_code == 200: - self._write_cache(cached_file, cached_file_header, cached_file_content, response) - else: - print('Error when requesting file :') - print('Requested url : %s' % (url,)) - print('Status code : %s' % (response.status_code,)) - print('Content : %s' % (response.content,)) - self.send_response(response.status_code) - return response.content + print('Error when requesting file :') + print('Requested url : %s' % (url,)) + print('Status code : %s' % (response.status_code,)) + print('Content : %s' % (response.content,)) + self.send_response(response.status_code) + return response.content else: - print("Request for data currently present in cache: %s" % (cached_file,)) + print("Request for data currently present in cache: %s" % (cached_file_folder,)) self._send_content(cached_file_header, cached_file_content) if self.path.startswith("/dcae-service-types?asdcResourceId="): - print "DCAE case deleting folder created " + cached_file - shutil.rmtree(cached_file, ignore_errors=False, onerror=None) + print "DCAE case deleting folder created " + cached_file_folder + shutil.rmtree(cached_file_folder, ignore_errors=False, onerror=None) else: - print "NOT in DCAE case deleting folder created " + cached_file + print "NOT in DCAE case deleting folder created " + cached_file_folder def do_POST(self): + cached_file_folder = "" + cached_file_content ="" + cached_file_header="" print("\n\n\nGot a POST for %s" % self.path) self.check_credentials() self.data_string = self.rfile.read(int(self.headers['Content-Length'])) print("data-string:\n %s" % self.data_string) print("self.headers:\n %s" % self.headers) - cached_file = '%s/%s' % (CACHE_ROOT, self.path,) - print("Cached file name before escaping : %s" % cached_file) - cached_file = cached_file.replace('<','<').replace('>','>').replace('?','?').replace('*','*').replace('\\','*').replace(':',':').replace('|','|') - print("Cached file name after escaping (used for cache storage) : %s" % cached_file) - cached_file_content = "%s/.file" % (cached_file,) - cached_file_header = "%s/.header" % (cached_file,) + is_special = self._execute_content_generated_cases("POST") + if is_special: + cached_file_folder = self._get_cached_file_folder_name(TMP_ROOT) + cached_file_content = self._get_cached_content_file_name(cached_file_folder) + cached_file_header = self._get_cached_header_file_name(cached_file_folder) + else: + cached_file_folder = self._get_cached_file_folder_name(CACHE_ROOT) + cached_file_content = self._get_cached_content_file_name(cached_file_folder) + cached_file_header = self._get_cached_header_file_name(cached_file_folder) _file_available = os.path.exists(cached_file_content) if not _file_available: - if self.path.startswith("/sdc/v1/catalog/services/"): - print "self.path start with /sdc/v1/catalog/services/, generating response json..." - jsondata = json.loads(self.data_string) - jsonGenerated = "{\"artifactName\":\"" + jsondata['artifactName'] + "\",\"artifactType\":\"" + jsondata['artifactType'] + "\",\"artifactURL\":\"" + self.path + "\",\"artifactDescription\":\"" + jsondata['description'] + "\",\"artifactChecksum\":\"ZjJlMjVmMWE2M2M1OTM2MDZlODlmNTVmZmYzNjViYzM=\",\"artifactUUID\":\"" + str(uuid.uuid4()) + "\",\"artifactVersion\":\"1\"}" - print "jsonGenerated: " + jsonGenerated - - os.makedirs(cached_file, True) - 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) + + if not HOST: + self.send_response(404) + return "404 Not found" + + print("Request for data currently not present in cache: %s" % (cached_file_folder,)) + + url = '%s%s' % (HOST, self.path) + print("url: %s" % (url,)) + response = requests.post(url, data=self.data_string, headers=self.headers, stream=True) + + if response.status_code == 200: + self._write_cache(cached_file_folder, cached_file_header, cached_file_content, response) else: - if not HOST: - self.send_response(404) - return "404 Not found" - - print("Request for data currently not present in cache: %s" % (cached_file,)) - - url = '%s%s' % (HOST, self.path) - print("url: %s" % (url,)) - response = requests.post(url, data=self.data_string, headers=self.headers, stream=True) - - if response.status_code == 200: - self._write_cache(cached_file, cached_file_header, cached_file_content, response) - else: - print('Error when requesting file :') - print('Requested url : %s' % (url,)) - print('Status code : %s' % (response.status_code,)) - print('Content : %s' % (response.content,)) - self.send_response(response.status_code) - return response.content + print('Error when requesting file :') + print('Requested url : %s' % (url,)) + print('Status code : %s' % (response.status_code,)) + print('Content : %s' % (response.content,)) + self.send_response(response.status_code) + return response.content else: - print("Request for data present in cache: %s" % (cached_file,)) + print("Request for data present in cache: %s" % (cached_file_folder,)) self._send_content(cached_file_header, cached_file_content) def do_PUT(self): + cached_file_folder = "" + cached_file_content ="" + cached_file_header="" print("\n\n\nGot a PUT for %s " % self.path) self.check_credentials() self.data_string = self.rfile.read(int(self.headers['Content-Length'])) print("data-string:\n %s" % self.data_string) print("self.headers:\n %s" % self.headers) - cached_file = '%s/%s' % (CACHE_ROOT, self.path,) - print("Cached file name before escaping : %s" % cached_file) - cached_file = cached_file.replace('<','<').replace('>','>').replace('?','?').replace('*','*').replace('\\','*').replace(':',':').replace('|','|') - print("Cached file name after escaping (used for cache storage) : %s" % cached_file) - cached_file_content = "%s/.file" % (cached_file,) - cached_file_header = "%s/.header" % (cached_file,) + is_special = self._execute_content_generated_cases("PUT") + if is_special: + cached_file_folder = self._get_cached_file_folder_name(TMP_ROOT) + cached_file_content = self._get_cached_content_file_name(cached_file_folder) + cached_file_header = self._get_cached_header_file_name(cached_file_folder) + else: + cached_file_folder = self._get_cached_file_folder_name(CACHE_ROOT) + cached_file_content = self._get_cached_content_file_name(cached_file_folder) + cached_file_header = self._get_cached_header_file_name(cached_file_folder) _file_available = os.path.exists(cached_file_content) if not _file_available: - if self.path.startswith("/dcae-deployments/"): - print "self.path start with /dcae-deployments/, generating response json..." - #jsondata = json.loads(self.data_string) - jsonGenerated = "{\"links\":{\"status\":\"http:\/\/" + PROXY_ADDRESS + "\/dcae-operationstatus\",\"test2\":\"test2\"}}" - print "jsonGenerated: " + jsonGenerated - - os.makedirs(cached_file, True) - 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) + if not HOST: + self.send_response(404) + return "404 Not found" + + print("Request for data currently not present in cache: %s" % (cached_file_folder,)) + + url = '%s%s' % (HOST, self.path) + print("url: %s" % (url,)) + response = requests.put(url, data=self.data_string, headers=self.headers, stream=True) + + if response.status_code == 200: + self._write_cache(cached_file_folder, cached_file_header, cached_file_content, response) else: - if not HOST: - self.send_response(404) - return "404 Not found" - - print("Request for data currently not present in cache: %s" % (cached_file,)) - - url = '%s%s' % (HOST, self.path) - print("url: %s" % (url,)) - response = requests.put(url, data=self.data_string, headers=self.headers, stream=True) - - if response.status_code == 200: - self._write_cache(cached_file, cached_file_header, cached_file_content, response) - else: - print('Error when requesting file :') - print('Requested url : %s' % (url,)) - print('Status code : %s' % (response.status_code,)) - print('Content : %s' % (response.content,)) - self.send_response(response.status_code) - return response.content + print('Error when requesting file :') + print('Requested url : %s' % (url,)) + print('Status code : %s' % (response.status_code,)) + print('Content : %s' % (response.content,)) + self.send_response(response.status_code) + return response.content else: - print("Request for data present in cache: %s" % (cached_file,)) + print("Request for data present in cache: %s" % (cached_file_folder,)) self._send_content(cached_file_header, cached_file_content) def do_DELETE(self): + cached_file_folder = "" + cached_file_content ="" + cached_file_header="" print("\n\n\nGot a DELETE for %s " % self.path) self.check_credentials() print("self.headers:\n %s" % self.headers) - cached_file = '%s/%s' % (CACHE_ROOT, self.path,) - print("Cached file name before escaping : %s" % cached_file) - cached_file = cached_file.replace('<','<').replace('>','>').replace('?','?').replace('*','*').replace('\\','*').replace(':',':').replace('|','|') - print("Cached file name after escaping (used for cache storage) : %s" % cached_file) - cached_file_content = "%s/.file" % (cached_file,) - cached_file_header = "%s/.header" % (cached_file,) + is_special = self._execute_content_generated_cases("DELETE") + if is_special: + cached_file_folder = self._get_cached_file_folder_name(TMP_ROOT) + cached_file_content = self._get_cached_content_file_name(cached_file_folder) + cached_file_header = self._get_cached_header_file_name(cached_file_folder) + else: + cached_file_folder = self._get_cached_file_folder_name(CACHE_ROOT) + cached_file_content = self._get_cached_content_file_name(cached_file_folder) + cached_file_header = self._get_cached_header_file_name(cached_file_folder) _file_available = os.path.exists(cached_file_content) if not _file_available: - if self.path.startswith("/dcae-deployments/"): - print "self.path start with /dcae-deployments/, generating response json..." - #jsondata = json.loads(self.data_string) - jsonGenerated = "{\"links\":{\"status\":\"http:\/\/" + PROXY_ADDRESS + "\/dcae-operationstatus\",\"test2\":\"test2\"}}" - print "jsonGenerated: " + jsonGenerated - - os.makedirs(cached_file, True) - 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) + if not HOST: + self.send_response(404) + return "404 Not found" + + print("Request for data currently not present in cache: %s" % (cached_file_folder,)) + + url = '%s%s' % (HOST, self.path) + print("url: %s" % (url,)) + response = requests.put(url, data=self.data_string, headers=self.headers, stream=True) + + if response.status_code == 200: + self._write_cache(cached_file_folder, cached_file_header, cached_file_content, response) else: - if not HOST: - self.send_response(404) - return "404 Not found" - - print("Request for data currently not present in cache: %s" % (cached_file,)) - - url = '%s%s' % (HOST, self.path) - print("url: %s" % (url,)) - response = requests.put(url, data=self.data_string, headers=self.headers, stream=True) - - if response.status_code == 200: - self._write_cache(cached_file, cached_file_header, cached_file_content, response) - else: - print('Error when requesting file :') - print('Requested url : %s' % (url,)) - print('Status code : %s' % (response.status_code,)) - print('Content : %s' % (response.content,)) - self.send_response(response.status_code) - return response.content + print('Error when requesting file :') + print('Requested url : %s' % (url,)) + print('Status code : %s' % (response.status_code,)) + print('Content : %s' % (response.content,)) + self.send_response(response.status_code) + return response.content else: - print("Request for data present in cache: %s" % (cached_file,)) + print("Request for data present in cache: %s" % (cached_file_folder,)) self._send_content(cached_file_header, cached_file_content) @@ -322,6 +372,7 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): # Main code that start the HTTP server httpd = SocketServer.ForkingTCPServer(('', PORT), Proxy) httpd.allow_reuse_address = True -print "Listening on port "+ str(PORT) + " and caching in " + CACHE_ROOT + "(Press Ctrl+C to stop HTTPD Caching script)" +print "Listening on port "+ str(PORT) + "(Press Ctrl+C/Ctrl+Z to stop HTTPD Caching script)" +print "Caching folder " + CACHE_ROOT + ", Tmp folder for generated files " + TMP_ROOT signal.signal(signal.SIGINT, signal_handler) httpd.serve_forever()
\ No newline at end of file |