Add Honeycomb persistence tests
[csit.git] / resources / libraries / python / honeycomb / HcPersistence.py
1 # Copyright (c) 2016 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 """Implementation of keywords for managing Honeycomb persistence files."""
15
16 from robot.api import logger
17
18 from resources.libraries.python.constants import Constants as Const
19 from resources.libraries.python.honeycomb.HoneycombUtil import HoneycombError
20 from resources.libraries.python.ssh import SSH
21 from resources.libraries.python.topology import NodeType
22
23
24 class HcPersistence(object):
25     """Implements keywords for managing Honeycomb persistence files.
26
27     The keywords implemented in this class make possible to:
28     - find and replace strings in config.json persistence file
29     """
30
31     def __init__(self):
32         pass
33
34     @staticmethod
35     def clear_persisted_honeycomb_config(*nodes):
36         """Remove configuration data persisted from last Honeycomb session.
37         Default configuration will be used instead.
38
39         :param nodes: List of DUTs to execute on.
40         :type nodes: list
41         :raises HoneycombError: If persisted configuration could not be removed.
42         """
43         cmd = "rm {0}/etc/opendaylight/honeycomb/*".format(
44             Const.REMOTE_HC_DIR)
45         for node in nodes:
46             if node['type'] == NodeType.DUT:
47                 ssh = SSH()
48                 ssh.connect(node)
49                 (ret_code, _, stderr) = ssh.exec_command_sudo(cmd)
50                 if ret_code != 0:
51                     if "No such file or directory" not in stderr:
52                         raise HoneycombError('Could not clear persisted '
53                                              'configuration on node {0}, {1}'
54                                              .format(node['host'], stderr))
55                     else:
56                         logger.info("Persistence data was not present on node"
57                                     " {0}".format(node['host']))
58                 else:
59                     logger.info("Persistence files removed on node {0}"
60                                 .format(node['host']))
61
62     @staticmethod
63     def modify_persistence_files(node, find, replace):
64         """Searches contents of persistence file config.json for the provided
65          string, and replaces all occurrences with another string.
66
67         :param node: Honeycomb node.
68         :param find: Text to find in file.
69         :param replace: String to replace anything found with.
70         :type node: dict
71         :type find: string
72         :type replace: string
73         :raises HoneycombError: If persistent configuration couldn't be
74         modified.
75         """
76
77         argument = "\"s/{0}/{1}/g\"".format(find, replace)
78         path = "{0}/etc/opendaylight/honeycomb/config.json".format(
79             Const.REMOTE_HC_DIR)
80         command = "sed -i {0} {1}".format(argument, path)
81
82         ssh = SSH()
83         ssh.connect(node)
84         (ret_code, _, stderr) = ssh.exec_command_sudo(command)
85         if ret_code != 0:
86             raise HoneycombError("Failed to modify persistence file on node"
87                                  " {0}, {1}".format(node, stderr))

©2016 FD.io a Linux Foundation Collaborative Project. All Rights Reserved.
Linux Foundation is a registered trademark of The Linux Foundation. Linux is a registered trademark of Linus Torvalds.
Please see our privacy policy and terms of use.