def test_process_call_request_exceptions(self): request = remote.encode_call_request("test_server_channel", "raiser_function", ["MYVAR"], [], {"MYVAR": "VALUE1"}, {}, []) test_connector.create_file("/tmp/sremote_test/request.data", request) server = ServerChannel() return_content = server.process_call_request( "/tmp/sremote_test/request.data", "/tmp/sremote_test/response.data") file_response = open("/tmp/sremote_test/response.data") response_content = file_response.read() status, reponse_obj = remote.decode_call_response(response_content) self.assertFalse(status) self.assertEqual(reponse_obj["sremote_type"], "ExceptionRemoteExecError") request = remote.encode_call_request("test_server_channel", "non_exist", ["MYVAR"], [], {"MYVAR": "VALUE1"}, {}, []) test_connector.create_file("/tmp/sremote_test/request.data", request) server = ServerChannel() return_content = server.process_call_request( "/tmp/sremote_test/request.data", "/tmp/sremote_test/response.data") file_response = open("/tmp/sremote_test/response.data") response_content = file_response.read() status, reponse_obj = remote.decode_call_response(response_content) self.assertFalse(status) self.assertEqual(reponse_obj["sremote_type"], "ExceptionRemoteExecError")
def test_place_and_execute(self): self._configure_remote_environment() serialized_method_call_request = \ remote.encode_call_request("os", "listdir", args=["/tmp/sremote_test/"]) response, output, location, response_location = \ self._connector.place_and_execute(serialized_method_call_request) code, function_return = remote.decode_call_response(response) self.assertIn("interpreter.sh", function_return)
def test_process_call_request_exceptions(self): request = remote.encode_call_request("test_server_channel", "raiser_function", ["MYVAR"], [], {"MYVAR": "VALUE1"}, {}, []) test_connector.create_file("/tmp/sremote_test/request.data", request) server = ServerChannel() return_content=server.process_call_request( "/tmp/sremote_test/request.data", "/tmp/sremote_test/response.data") file_response = open("/tmp/sremote_test/response.data") response_content = file_response.read() status, reponse_obj = remote.decode_call_response(response_content) self.assertFalse(status) self.assertEqual(reponse_obj["sremote_type"], "ExceptionRemoteExecError") request = remote.encode_call_request("test_server_channel", "non_exist", ["MYVAR"], [], {"MYVAR": "VALUE1"}, {}, []) test_connector.create_file("/tmp/sremote_test/request.data", request) server = ServerChannel() return_content=server.process_call_request( "/tmp/sremote_test/request.data", "/tmp/sremote_test/response.data") file_response = open("/tmp/sremote_test/response.data") response_content = file_response.read() status, reponse_obj = remote.decode_call_response(response_content) self.assertFalse(status) self.assertEqual(reponse_obj["sremote_type"], "ExceptionRemoteExecError")
def test_effectice_pwd(self): self._configure_remote_environment() #- not using /tmp/ folder, because in OsX it maps to /private/tmp/ self._connector.set_pwd_at_home_dir("tmp/testpwd") serialized_method_call_request = \ remote.encode_call_request("os", "getcwd") response, output, location, response_location = \ self._connector.place_and_execute(serialized_method_call_request) code, function_return = remote.decode_call_response(response) self.assertEqual(self._connector.get_home_dir()+"/tmp/testpwd", function_return)
def test_remote_environemt(self): self._configure_remote_environment() serialized_method_call_request = \ remote.encode_call_request("os", "getenv", args=["MYVAR", None], remote_env_variables = {"MYVAR":"MYVALUE"} ) response, output, location, response_location = \ self._connector.place_and_execute(serialized_method_call_request) code, function_return = remote.decode_call_response(response) self.assertEqual(function_return, "MYVALUE") serialized_method_call_request = \ remote.encode_call_request("os", "getenv", args=["PATH", None], conditional_remote_env_variables = {"PATH":"MYVALUE"} ) response, output, location, response_location = \ self._connector.place_and_execute(serialized_method_call_request) code, function_return = remote.decode_call_response(response) self.assertNotEqual(function_return, "MYVALUE") serialized_method_call_request = \ remote.encode_call_request("os", "getenv", args=["PATH", None], remote_path_addons = ["/impossible_dir"] ) response, output, location, response_location = \ self._connector.place_and_execute(serialized_method_call_request) code, function_return = remote.decode_call_response(response) self.assertIn("/impossible_dir", function_return.split(":"))
def test_process_call_request(self): request = remote.encode_call_request("os", "getenv", ["MYVAR"], [], {"MYVAR": "VALUE1"}, {}, []) test_connector.create_file("/tmp/sremote_test/request.data", request) server = ServerChannel() return_content = server.process_call_request( "/tmp/sremote_test/request.data", "/tmp/sremote_test/response.data") file_response = open("/tmp/sremote_test/response.data") response_content = file_response.read() self.assertEqual(return_content, response_content) status, reponse_obj = remote.decode_call_response(response_content) self.assertEqual(reponse_obj, "VALUE1")
def test_encode_call_request(self): self.assertEqual(json.loads(remote.encode_call_request( module_name="test_tools", command_name="env_check", args = ["MYVAR", "HOME", "PATH"], required_extra_modules = ["os"], remote_env_variables = {"MYVAR":"MYVAL"}, conditional_remote_env_variables = {"HOME": "HOMEMODIFIED"}, remote_path_addons = ["/mypath"])), json.loads("""{"module": "test_tools", "command": "env_check", "args": ["MYVAR", "HOME", "PATH"], "modules_check": ["os"], "enviroment_variables": {"MYVAR":"MYVAL"}, "conditional_enviroment_variables": {"HOME":"HOMEMODIFIED"}, "path_addons" : ["/mypath"] }"""))
def test_encode_call_request(self): self.assertEqual( json.loads( remote.encode_call_request( module_name="test_tools", command_name="env_check", args=["MYVAR", "HOME", "PATH"], required_extra_modules=["os"], remote_env_variables={"MYVAR": "MYVAL"}, conditional_remote_env_variables={"HOME": "HOMEMODIFIED"}, remote_path_addons=["/mypath"])), json.loads("""{"module": "test_tools", "command": "env_check", "args": ["MYVAR", "HOME", "PATH"], "modules_check": ["os"], "enviroment_variables": {"MYVAR":"MYVAL"}, "conditional_enviroment_variables": {"HOME":"HOMEMODIFIED"}, "path_addons" : ["/mypath"] }"""))
def test_process_call_request(self): request = remote.encode_call_request("os", "getenv", ["MYVAR"], [], {"MYVAR": "VALUE1"}, {}, []) test_connector.create_file("/tmp/sremote_test/request.data", request) server = ServerChannel() return_content=server.process_call_request( "/tmp/sremote_test/request.data", "/tmp/sremote_test/response.data") file_response = open("/tmp/sremote_test/response.data") response_content = file_response.read() self.assertEqual(return_content, response_content) status, reponse_obj = remote.decode_call_response(response_content) self.assertEqual(reponse_obj, "VALUE1")
def do_remote_call(self, module_name, method_name, args=[], keep_env=False): """Uses _comms_client to send a request to execute module_name.method_name(*args) in the remote host. It asumes that module_name is installed in the sremote environemnt of the remote host. Args: module_name: string with the name of the module which method will be executed. method_name: string with name of the method to be executed. args: list or dict containing the argument values. Dict is desired for calling methods with default values. keep_env: loads user enviroment before calling the method. Returns: If successes: response object and a string with the std_out of the execution. Raises an exception otherwise. Rises Exceptions: ExceptionRemoteNotSetup: Remote end point does not have sremote or the version of remote-local sites don't match. ExceptionRemoteModulesError: Remote end-point does not have module_name or required_registered_modules or versions don't match. """ if (not isinstance(args, types.ListType) and not isinstance(args, types.DictType)): raise Exception("Wrong type for args, expected list or dict, found " + str(type(args))) # Encodes and sends the call request to the remote host and calls the # interpreter to execute. Then the response is retrieved in a # serialized format.. request_obj= \ remote.encode_call_request(module_name, method_name, args, required_extra_modules = self._registered_remote_modules, remote_env_variables= self._remote_env_variables, conditional_remote_env_variables= self._conditional_remote_env_variables, remote_path_addons= self._remote_path_addons) response_encoded, std_out, request_location, response_location = \ self._comms_client.place_and_execute( request_obj, keep_env=keep_env) success, response = remote.decode_call_response(response_encoded) if (success): self._comms_client.delete_file(request_location) self._comms_client.delete_file(response_location) return response, std_out else: if response!=None: if (type(response) is dict): if response.has_key("sremote_type"): raise remote.ExceptionRemoteExecError(response[ "message"]) raise remote.ExceptionRemoteNotSetup( "Error executing " +module_name+"."+ method_name + "\n Output:" +str(std_out))
def do_remote_call(self, module_name, method_name, args=[], keep_env=False): """Uses _comms_client to send a request to execute module_name.method_name(*args) in the remote host. It asumes that module_name is installed in the sremote environemnt of the remote host. Args: module_name: string with the name of the module which method will be executed. method_name: string with name of the method to be executed. args: list or dict containing the argument values. Dict is desired for calling methods with default values. keep_env: loads user enviroment before calling the method. Returns: If successes: response object and a string with the std_out of the execution. Raises an exception otherwise. Rises Exceptions: ExceptionRemoteNotSetup: Remote end point does not have sremote or the version of remote-local sites don't match. ExceptionRemoteModulesError: Remote end-point does not have module_name or required_registered_modules or versions don't match. """ if (not isinstance(args, types.ListType) and not isinstance(args, types.DictType)): raise Exception( "Wrong type for args, expected list or dict, found " + str(type(args))) # Encodes and sends the call request to the remote host and calls the # interpreter to execute. Then the response is retrieved in a # serialized format.. request_obj= \ remote.encode_call_request(module_name, method_name, args, required_extra_modules = self._registered_remote_modules, remote_env_variables= self._remote_env_variables, conditional_remote_env_variables= self._conditional_remote_env_variables, remote_path_addons= self._remote_path_addons) response_encoded, std_out, request_location, response_location = \ self._comms_client.place_and_execute( request_obj, keep_env=keep_env) success, response = remote.decode_call_response(response_encoded) if (success): self._comms_client.delete_file(request_location) self._comms_client.delete_file(response_location) return response, std_out else: if response != None: if (type(response) is dict): if response.has_key("sremote_type"): raise remote.ExceptionRemoteExecError( response["message"]) raise remote.ExceptionRemoteNotSetup("Error executing " + module_name + "." + method_name + "\n Output:" + str(std_out))