<s> import unittest <EOL> import json <EOL> import urllib <EOL> import urllib2 <EOL> import tempfile <EOL> import time <EOL> import os <EOL> import types <EOL> import shutil <EOL> from nose . tools import nottest <EOL> from nose . plugins . attrib import attr <EOL> from wagon . wagon import Wagon <EOL> from manager_rest import utils , config , storage_manager , archiving <EOL> from manager_rest . file_server import FileServer <EOL> from cloudify_rest_client import CloudifyClient <EOL> from cloudify_rest_client . client import HTTPClient <EOL> from cloudify_rest_client . executions import Execution <EOL> try : <EOL> from cloudify_rest_client . client import DEFAULT_API_VERSION as CLIENT_API_VERSION <EOL> except ImportError : <EOL> CLIENT_API_VERSION = '<STR_LIT>' <EOL> STORAGE_MANAGER_MODULE_NAME = '<STR_LIT>' <EOL> FILE_SERVER_PORT = <NUM_LIT> <EOL> FILE_SERVER_BLUEPRINTS_FOLDER = '<STR_LIT>' <EOL> FILE_SERVER_DEPLOYMENTS_FOLDER = '<STR_LIT>' <EOL> FILE_SERVER_UPLOADED_BLUEPRINTS_FOLDER = '<STR_LIT>' <EOL> FILE_SERVER_RESOURCES_URI = '<STR_LIT>' <EOL> LATEST_API_VERSION = <NUM_LIT> <EOL> def build_query_string ( query_params ) : <EOL> query_string = '<STR_LIT>' <EOL> if query_params and len ( query_params ) > <NUM_LIT:0> : <EOL> query_string += urllib . urlencode ( query_params , True ) + '<STR_LIT:&>' <EOL> return query_string <EOL> @ nottest <EOL> def test_config ( ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> def _test_config_decorator ( test_func ) : <EOL> test_func . test_config = kwargs <EOL> return test_func <EOL> return _test_config_decorator <EOL> @ nottest <EOL> def inject_test_config ( f ) : <EOL> """<STR_LIT>""" <EOL> def _wrapper ( test_obj , * args , ** kwargs ) : <EOL> test_func = getattr ( test_obj , test_obj . id ( ) . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] ) <EOL> if hasattr ( test_func , '<STR_LIT>' ) : <EOL> kwargs [ '<STR_LIT>' ] = test_func . test_config <EOL> return f ( test_obj , * args , ** kwargs ) <EOL> return _wrapper <EOL> class MockHTTPClient ( HTTPClient ) : <EOL> def __init__ ( self , app , headers = None ) : <EOL> super ( MockHTTPClient , self ) . __init__ ( host = '<STR_LIT:localhost>' , <EOL> headers = headers ) <EOL> self . app = app <EOL> def do_request ( self , <EOL> requests_method , <EOL> uri , <EOL> data = None , <EOL> params = None , <EOL> headers = None , <EOL> pagination = None , <EOL> sort = None , <EOL> expected_status_code = <NUM_LIT:200> , <EOL> stream = False ) : <EOL> if CLIENT_API_VERSION == '<STR_LIT>' : <EOL> uri = '<STR_LIT>' . format ( CLIENT_API_VERSION , uri ) <EOL> return super ( MockHTTPClient , self ) . do_request ( <EOL> requests_method = requests_method , <EOL> uri = uri , <EOL> data = data , <EOL> params = params , <EOL> headers = headers , <EOL> expected_status_code = expected_status_code , <EOL> stream = stream ) <EOL> def _do_request ( self , requests_method , request_url , body , params , headers , <EOL> expected_status_code , stream , verify ) : <EOL> if '<STR_LIT>' in requests_method . __name__ : <EOL> response = self . app . get ( request_url , <EOL> headers = headers , <EOL> data = body , <EOL> query_string = build_query_string ( params ) ) <EOL> elif '<STR_LIT>' in requests_method . __name__ : <EOL> if isinstance ( body , types . GeneratorType ) : <EOL> body = '<STR_LIT>' . join ( body ) <EOL> response = self . app . put ( request_url , <EOL> headers = headers , <EOL> data = body , <EOL> query_string = build_query_string ( params ) ) <EOL> elif '<STR_LIT>' in requests_method . __name__ : <EOL> response = self . app . post ( request_url , <EOL> headers = headers , <EOL> data = body , <EOL> query_string = build_query_string ( params ) ) <EOL> elif '<STR_LIT>' in requests_method . __name__ : <EOL> response = self . app . patch ( request_url , <EOL> headers = headers , <EOL> data = body , <EOL> query_string = build_query_string ( params ) ) <EOL> elif '<STR_LIT>' in requests_method . __name__ : <EOL> response = self . app . delete ( request_url , <EOL> headers = headers , <EOL> query_string = build_query_string ( params ) ) <EOL> else : <EOL> raise NotImplemented ( ) <EOL> if response . status_code != expected_status_code : <EOL> response . content = response . data <EOL> response . json = lambda : json . loads ( response . data ) <EOL> self . _raise_client_error ( response , request_url ) <EOL> return json . loads ( response . data ) <EOL> @ attr ( client_min_version = <NUM_LIT:1> , client_max_version = LATEST_API_VERSION ) <EOL> class BaseServerTestCase ( unittest . TestCase ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( BaseServerTestCase , self ) . __init__ ( * args , ** kwargs ) <EOL> def create_client ( self , headers = None ) : <EOL> client = CloudifyClient ( host = '<STR_LIT:localhost>' , <EOL> headers = headers ) <EOL> mock_http_client = MockHTTPClient ( self . app , <EOL> headers = headers ) <EOL> client . _client = mock_http_client <EOL> client . blueprints . api = mock_http_client <EOL> client . deployments . api = mock_http_client <EOL> client . deployments . outputs . api = mock_http_client <EOL> client . deployment_modifications . api = mock_http_client <EOL> client . executions . api = mock_http_client <EOL> client . nodes . api = mock_http_client <EOL> client . node_instances . api = mock_http_client <EOL> client . manager . api = mock_http_client <EOL> client . evaluate . api = mock_http_client <EOL> client . tokens . api = mock_http_client <EOL> client . events . api = mock_http_client <EOL> if CLIENT_API_VERSION != '<STR_LIT>' : <EOL> client . plugins . api = mock_http_client <EOL> client . snapshots . api = mock_http_client <EOL> if CLIENT_API_VERSION != '<STR_LIT>' : <EOL> client . maintenance_mode . api = mock_http_client <EOL> client . deployment_updates . api = mock_http_client <EOL> return client <EOL> def setUp ( self ) : <EOL> self . tmpdir = tempfile . mkdtemp ( prefix = '<STR_LIT>' ) <EOL> fd , self . rest_service_log = tempfile . mkstemp ( prefix = '<STR_LIT>' ) <EOL> os . close ( fd ) <EOL> fd , self . securest_log_file = tempfile . mkstemp ( prefix = '<STR_LIT>' ) <EOL> os . close ( fd ) <EOL> self . file_server = FileServer ( self . tmpdir ) <EOL> self . maintenance_mode_dir = tempfile . mkdtemp ( prefix = '<STR_LIT>' ) <EOL> self . addCleanup ( self . cleanup ) <EOL> self . file_server . start ( ) <EOL> storage_manager . storage_manager_module_name = STORAGE_MANAGER_MODULE_NAME <EOL> fd , self . tmp_conf_file = tempfile . mkstemp ( prefix = '<STR_LIT>' ) <EOL> os . close ( fd ) <EOL> with open ( self . tmp_conf_file , '<STR_LIT:w>' ) as f : <EOL> json . dump ( { '<STR_LIT>' : self . rest_service_log , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT>' } , <EOL> f ) <EOL> os . environ [ '<STR_LIT>' ] = self . tmp_conf_file <EOL> try : <EOL> from manager_rest import server <EOL> finally : <EOL> del ( os . environ [ '<STR_LIT>' ] ) <EOL> self . server_configuration = self . create_configuration ( ) <EOL> server . reset_state ( self . server_configuration ) <EOL> utils . copy_resources ( config . instance ( ) . file_server_root ) <EOL> server . setup_app ( ) <EOL> server . app . config [ '<STR_LIT>' ] = True <EOL> self . app = server . app . test_client ( ) <EOL> self . client = self . create_client ( ) <EOL> self . initialize_provider_context ( ) <EOL> def cleanup ( self ) : <EOL> self . quiet_delete ( self . rest_service_log ) <EOL> self . quiet_delete ( self . securest_log_file ) <EOL> self . quiet_delete_directory ( self . maintenance_mode_dir ) <EOL> if self . file_server : <EOL> self . file_server . stop ( ) <EOL> self . quiet_delete_directory ( self . tmpdir ) <EOL> def initialize_provider_context ( self , client = None ) : <EOL> if not client : <EOL> client = self . client <EOL> client . manager . create_context ( self . id ( ) , { '<STR_LIT>' : { } } ) <EOL> def create_configuration ( self ) : <EOL> from manager_rest . config import Config <EOL> test_config = Config ( ) <EOL> test_config . test_mode = True <EOL> test_config . file_server_root = self . tmpdir <EOL> test_config . file_server_base_uri = '<STR_LIT>' . format ( <EOL> FILE_SERVER_PORT ) <EOL> test_config . file_server_blueprints_folder = FILE_SERVER_BLUEPRINTS_FOLDER <EOL> test_config . file_server_deployments_folder = FILE_SERVER_DEPLOYMENTS_FOLDER <EOL> test_config . file_server_uploaded_blueprints_folder = FILE_SERVER_UPLOADED_BLUEPRINTS_FOLDER <EOL> test_config . file_server_resources_uri = FILE_SERVER_RESOURCES_URI <EOL> test_config . rest_service_log_level = '<STR_LIT>' <EOL> test_config . rest_service_log_path = self . rest_service_log <EOL> test_config . rest_service_log_file_size_MB = <NUM_LIT:100> , <EOL> test_config . rest_service_log_files_backup_count = <NUM_LIT:20> <EOL> test_config . security_audit_log_level = '<STR_LIT>' <EOL> test_config . security_audit_log_file = self . securest_log_file <EOL> test_config . security_audit_log_file_size_MB = <NUM_LIT:100> <EOL> test_config . security_audit_log_files_backup_count = <NUM_LIT:20> <EOL> test_config . _maintenance_folder = self . maintenance_mode_dir <EOL> return test_config <EOL> def _version_url ( self , url ) : <EOL> if CLIENT_API_VERSION not in url : <EOL> url = '<STR_LIT>' . format ( CLIENT_API_VERSION , url ) <EOL> return url <EOL> def post ( self , resource_path , data , query_params = None ) : <EOL> url = self . _version_url ( resource_path ) <EOL> result = self . app . post ( urllib . quote ( url ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( data ) , <EOL> query_string = build_query_string ( query_params ) ) <EOL> result . json = json . loads ( result . data ) <EOL> return result <EOL> def post_file ( self , resource_path , file_path , query_params = None ) : <EOL> url = self . _version_url ( resource_path ) <EOL> with open ( file_path ) as f : <EOL> result = self . app . post ( urllib . quote ( url ) , <EOL> data = f . read ( ) , <EOL> query_string = build_query_string ( <EOL> query_params ) ) <EOL> result . json = json . loads ( result . data ) <EOL> return result <EOL> def put_file ( self , resource_path , file_path , query_params = None ) : <EOL> url = self . _version_url ( resource_path ) <EOL> with open ( file_path ) as f : <EOL> result = self . app . put ( urllib . quote ( url ) , <EOL> data = f . read ( ) , <EOL> query_string = build_query_string ( <EOL> query_params ) ) <EOL> result . json = json . loads ( result . data ) <EOL> return result <EOL> def put ( self , resource_path , data = None , query_params = None ) : <EOL> url = self . _version_url ( resource_path ) <EOL> result = self . app . put ( urllib . quote ( url ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( data ) if data else None , <EOL> query_string = build_query_string ( query_params ) ) <EOL> result . json = json . loads ( result . data ) <EOL> return result <EOL> def patch ( self , resource_path , data ) : <EOL> url = self . _version_url ( resource_path ) <EOL> result = self . app . patch ( urllib . quote ( url ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( data ) ) <EOL> result . json = json . loads ( result . data ) <EOL> return result <EOL> def get ( self , resource_path , query_params = None , headers = None ) : <EOL> url = self . _version_url ( resource_path ) <EOL> result = self . app . get ( urllib . quote ( url ) , <EOL> headers = headers , <EOL> query_string = build_query_string ( query_params ) ) <EOL> result . json = json . loads ( result . data ) <EOL> return result <EOL> def head ( self , resource_path ) : <EOL> url = self . _version_url ( resource_path ) <EOL> result = self . app . head ( urllib . quote ( url ) ) <EOL> return result <EOL> def delete ( self , resource_path , query_params = None ) : <EOL> url = self . _version_url ( resource_path ) <EOL> result = self . app . delete ( urllib . quote ( url ) , <EOL> query_string = build_query_string ( query_params ) ) <EOL> result . json = json . loads ( result . data ) <EOL> return result <EOL> def _check_if_resource_on_fileserver ( self , <EOL> folder , <EOL> container_id , <EOL> resource_path ) : <EOL> url = '<STR_LIT>' . format ( <EOL> FILE_SERVER_PORT , folder , container_id , resource_path ) <EOL> try : <EOL> urllib2 . urlopen ( url ) <EOL> return True <EOL> except urllib2 . HTTPError : <EOL> return False <EOL> def check_if_resource_on_fileserver ( self , blueprint_id , resource_path ) : <EOL> return self . _check_if_resource_on_fileserver ( <EOL> FILE_SERVER_BLUEPRINTS_FOLDER , blueprint_id , resource_path ) <EOL> def check_if_deployment_resource_on_fileserver ( self , <EOL> deployment_id , <EOL> resource_path ) : <EOL> return self . _check_if_resource_on_fileserver ( <EOL> FILE_SERVER_DEPLOYMENTS_FOLDER , deployment_id , resource_path ) <EOL> def get_blueprint_path ( self , blueprint_dir_name ) : <EOL> return os . path . join ( os . path . dirname ( <EOL> os . path . abspath ( __file__ ) ) , blueprint_dir_name ) <EOL> def archive_mock_blueprint ( self , archive_func = archiving . make_targzfile , <EOL> blueprint_dir = '<STR_LIT>' ) : <EOL> archive_path = tempfile . mkstemp ( ) [ <NUM_LIT:1> ] <EOL> source_dir = os . path . join ( os . path . dirname ( <EOL> os . path . abspath ( __file__ ) ) , blueprint_dir ) <EOL> archive_func ( archive_path , source_dir ) <EOL> return archive_path <EOL> def get_mock_blueprint_path ( self ) : <EOL> return os . path . join ( os . path . dirname ( <EOL> os . path . abspath ( __file__ ) ) , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def put_blueprint_args ( self , blueprint_file_name = None , <EOL> blueprint_id = '<STR_LIT>' , <EOL> archive_func = archiving . make_targzfile , <EOL> blueprint_dir = '<STR_LIT>' ) : <EOL> resource_path = self . _version_url ( <EOL> '<STR_LIT>' . format ( CLIENT_API_VERSION , blueprint_id ) ) <EOL> result = [ <EOL> resource_path , <EOL> self . archive_mock_blueprint ( archive_func , blueprint_dir ) , <EOL> ] <EOL> if blueprint_file_name : <EOL> data = { '<STR_LIT>' : blueprint_file_name } <EOL> else : <EOL> data = { } <EOL> result . append ( data ) <EOL> return result <EOL> def put_deployment ( self , <EOL> deployment_id = '<STR_LIT>' , <EOL> blueprint_file_name = None , <EOL> blueprint_id = '<STR_LIT>' , <EOL> inputs = None ) : <EOL> blueprint_response = self . put_file ( <EOL> * self . put_blueprint_args ( blueprint_file_name , <EOL> blueprint_id ) ) . json <EOL> if '<STR_LIT>' in blueprint_response : <EOL> raise RuntimeError ( <EOL> '<STR_LIT>' . format ( blueprint_response [ '<STR_LIT>' ] , <EOL> blueprint_response [ '<STR_LIT:message>' ] ) ) <EOL> blueprint_id = blueprint_response [ '<STR_LIT:id>' ] <EOL> deployment = self . client . deployments . create ( blueprint_id , <EOL> deployment_id , <EOL> inputs = inputs ) <EOL> return blueprint_id , deployment . id , blueprint_response , deployment <EOL> def upload_plugin ( self , package_name , package_version ) : <EOL> temp_file_path = self . create_wheel ( package_name , package_version ) <EOL> response = self . post_file ( '<STR_LIT>' , temp_file_path ) <EOL> os . remove ( temp_file_path ) <EOL> return response <EOL> def create_wheel ( self , package_name , package_version ) : <EOL> module_src = '<STR_LIT>' . format ( package_name , package_version ) <EOL> wagon_client = Wagon ( module_src ) <EOL> return wagon_client . create ( <EOL> archive_destination_dir = tempfile . gettempdir ( ) , force = True ) <EOL> def wait_for_url ( self , url , timeout = <NUM_LIT:5> ) : <EOL> end = time . time ( ) + timeout <EOL> while end >= time . time ( ) : <EOL> try : <EOL> status = urllib . urlopen ( url ) . getcode ( ) <EOL> if status == <NUM_LIT:200> : <EOL> return <EOL> except IOError : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( url , timeout ) ) <EOL> @ staticmethod <EOL> def quiet_delete ( file_path ) : <EOL> try : <EOL> os . remove ( file_path ) <EOL> except : <EOL> pass <EOL> @ staticmethod <EOL> def quiet_delete_directory ( file_path ) : <EOL> shutil . rmtree ( file_path , ignore_errors = True ) <EOL> def wait_for_deployment_creation ( self , client , deployment_id ) : <EOL> env_creation_execution = None <EOL> deployment_executions = client . executions . list ( deployment_id ) <EOL> for execution in deployment_executions : <EOL> if execution . workflow_id == '<STR_LIT>' : <EOL> env_creation_execution = execution <EOL> break <EOL> if env_creation_execution : <EOL> self . wait_for_execution ( client , env_creation_execution ) <EOL> @ staticmethod <EOL> def wait_for_execution ( client , execution , timeout = <NUM_LIT> ) : <EOL> deadline = time . time ( ) + timeout <EOL> while True : <EOL> if time . time ( ) > deadline : <EOL> raise Exception ( <EOL> '<STR_LIT>' . <EOL> format ( execution . workflow_id , execution . deployment_id ) ) <EOL> execution = client . executions . get ( execution . id ) <EOL> if execution . status in Execution . END_STATES : <EOL> break <EOL> time . sleep ( <NUM_LIT:3> ) </s>
