From b2833ccfdefc71e0bed5aa6ff3cd5cad8362ada3 Mon Sep 17 00:00:00 2001 From: Ivan Devat Date: Tue, 16 Jan 2018 14:55:14 +0100 Subject: [PATCH] add some shortcuts for tests --- pcs/test/tools/command_env/config_http.py | 117 ++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/pcs/test/tools/command_env/config_http.py b/pcs/test/tools/command_env/config_http.py index 53925ba76..329da0989 100644 --- a/pcs/test/tools/command_env/config_http.py +++ b/pcs/test/tools/command_env/config_http.py @@ -4,12 +4,32 @@ print_function, ) +import json +import pprint + from pcs.test.tools.command_env.config_http_corosync import CorosyncShortcuts from pcs.test.tools.command_env.mock_node_communicator import( AddRequestCall, create_communication, StartLoopCall, ) +from pcs.test.tools.command_env.mock_node_communicator import ( + place_multinode_call +) + +def _mutual_exclusive(param_names, **kwargs): + entered = { + key: value for key, value in kwargs.items() + if key in param_names and value is not None + } + if len(entered) != 1: + raise AssertionError( + "Exactly one of '{0}' must be specified, \nwas specified:\n{1}" + .format( + "', '".join(param_names), + pprint.pformat(entered) if entered else " nothing", + ) + ) class HttpConfig(object): @@ -52,3 +72,100 @@ def start_loop(self, response_list, name, before=None, instead=None): before=before, instead=instead ) + + def put_file( + self, communication_list, name="http.common.put_file", + results=None, files=None, **kwargs + ): + """ + Example: + config.http.put_file( + communication_list=[dict(label="node")], + files={ + "pacemaker_remote authkey": { + "type": "pcmk_remote_authkey", + "data": base64.b64encode(pcmk_authkey_content), + "rewrite_existing": True + } + }, + results={ + "pacemaker_remote authkey": { + "code": "written", + "message": "", + } + } + ) + """ + + _mutual_exclusive(["output", "results"], results=results, **kwargs) + _mutual_exclusive(["files", "param_list"], files=files, **kwargs) + + if results: + kwargs["output"]=json.dumps({"files": results}) + + if files: + kwargs["param_list"] = [("data_json", json.dumps(files))] + + + self.place_multinode_call( + name, + communication_list=communication_list, + action="remote/put_file", + **kwargs + ) + + def manage_services( + self, communication_list, name="http.common.manage_services", + results=None, action_map=None, **kwargs + ): + """ + Example: + config.http.manage_services( + communication_list=[dict(label=label)], + action_map={ + "pacemaker_remote enable": { + "type": "service_command", + "service": "pacemaker_remote", + "command": "enable", + }, + "pacemaker_remote start": { + "type": "service_command", + "service": "pacemaker_remote", + "command": "start", + }, + }, + results={ + "pacemaker_remote enable": { + "code": "success", + "message": "", + }, + "pacemaker_remote start": { + "code": "success", + "message": "", + } + } + ) + """ + _mutual_exclusive(["output", "results"], results=results, **kwargs) + _mutual_exclusive( + ["action_map", "param_list"], + action_map=action_map, + **kwargs + ) + + if results: + kwargs["output"]=json.dumps({"actions": results}) + + if action_map: + kwargs["param_list"] = [("data_json", json.dumps(action_map))] + + + self.place_multinode_call( + name, + communication_list=communication_list, + action="remote/manage_services", + **kwargs + ) + + def place_multinode_call(self, *args, **kwargs): + place_multinode_call(self.__calls, *args, **kwargs)