feat(jobspec): Unify soak jobspecs
[csit.git] / resources / libraries / python / PapiHistory.py
1 # Copyright (c) 2023 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """DUT PAPI command history setup library."""
15
16 from robot.api import logger
17
18 from resources.libraries.python.topology import NodeType, DICT__nodes
19
20 __all__ = [u"DICT__DUTS_PAPI_HISTORY", u"PapiHistory"]
21
22
23 DICT__DUTS_PAPI_HISTORY = dict()
24
25
26 class PapiHistory:
27     """Contains methods to set up DUT PAPI command history.
28     """
29
30     @staticmethod
31     def reset_papi_history(node):
32         """Reset PAPI command history for DUT node.
33
34         :param node: DUT node to reset PAPI command history for.
35         :type node: dict
36         """
37         DICT__DUTS_PAPI_HISTORY[node[u"host"]] = list()
38
39     @staticmethod
40     def reset_papi_history_on_all_duts(nodes):
41         """Reset PAPI command history for all DUT nodes.
42
43         :param nodes: Nodes to reset PAPI command history for.
44         :type nodes: dict
45         """
46         for node in nodes.values():
47             if node[u"type"] == NodeType.DUT:
48                 PapiHistory.reset_papi_history(node)
49
50     @staticmethod
51     def add_to_papi_history(node, csit_papi_command, **kwargs):
52         """Add command to PAPI command history on DUT node.
53
54         Repr strings are used for argument values.
55
56         The argument name 'csit_papi_command' must be unique enough as it cannot
57         be repeated in kwargs.
58
59         Examples of PAPI history items:
60
61         Request without parameters:
62             show_threads()
63
64         Request with parameters:
65             ipsec_select_backend(index=1,protocol=1)
66
67         Dump:
68             sw_interface_rx_placement_dump(sw_if_index=4)
69
70         VPP Stats:
71             vpp-stats(path=['^/if', '/err/ip4-input', '/sys/node/ip4-input'])
72
73         :param node: DUT node to add command to PAPI command history for.
74         :param csit_papi_command: Command to be added to PAPI command history.
75         :param kwargs: Optional key-value arguments.
76         :type node: dict
77         :type csit_papi_command: str
78         :type kwargs: dict
79         """
80         args = list()
81         for key, val in kwargs.items():
82             args.append(f"{key}={val!r}")
83         item = f"{csit_papi_command}({u','.join(args)})"
84         DICT__DUTS_PAPI_HISTORY[node[u"host"]].append(item)
85
86     @staticmethod
87     def show_papi_history(node):
88         """Show PAPI command history for DUT node.
89
90         :param node: DUT node to show PAPI command history for.
91         :type node: dict
92         """
93         history_list = DICT__DUTS_PAPI_HISTORY[node[u"host"]]
94         if not history_list:
95             history_list = (u"No PAPI command executed", )
96         history = u'\n'.join(history_list)
97         logger.info(f"{node[u'host']} PAPI command history:\n{history}\n")
98
99     @staticmethod
100     def show_papi_history_on_all_duts(nodes):
101         """Show PAPI command history for all DUT nodes.
102
103         :param nodes: Nodes to show PAPI command history for.
104         :type nodes: dict
105         """
106         for node in nodes.values():
107             if node[u"type"] == NodeType.DUT:
108                 PapiHistory.show_papi_history(node)
109
110
111 # This module can be imported outside usual Robot test context,
112 # e.g. in pylint or by tools generating docs from docstrings.
113 # For the tools to work, we need to avoid processing
114 # when DICT__nodes value is not usable.
115 if DICT__nodes:
116     PapiHistory.reset_papi_history_on_all_duts(DICT__nodes)