aboutsummaryrefslogtreecommitdiff
path: root/lava/device/tests/test_commands.py
blob: 91b204fdc53df82f55896b15788329bf391f5bfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Copyright (C) 2013 Linaro Limited
#
# Author: Milo Casagrande <milo.casagrande@linaro.org>
#
# This file is part of lava-tool.
#
# lava-tool is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation
#
# lava-tool is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with lava-tool.  If not, see <http://www.gnu.org/licenses/>.

"""
lava.device.commands unit tests.
"""

import os

from mock import (
    MagicMock,
    call,
    patch,
)

from lava.device.commands import (
    add,
    config,
    remove,
)
from lava.helper.tests.helper_test import HelperTest
from lava.tool.errors import CommandError


class AddCommandTest(HelperTest):

    def test_register_argument(self):
        # Make sure that the parser add_argument is called and we have the
        # correct argument.
        add_command = add(self.parser, self.args)
        add_command.register_arguments(self.parser)
        name, args, kwargs = self.parser.method_calls[0]
        self.assertIn("--non-interactive", args)

        name, args, kwargs = self.parser.method_calls[1]
        self.assertIn("DEVICE", args)

    @patch("lava.device.commands.edit_file", create=True)
    @patch("lava.device.Device.__str__")
    @patch("lava.device.Device.update")
    @patch("lava.device.commands.get_device_file")
    @patch("lava.device.commands.get_devices_path")
    def test_add_invoke_0(self, mocked_get_devices_path,
                          mocked_get_device_file, mocked_update, mocked_str,
                          mocked_edit_file):
        # Tests invocation of the add command. Verifies that the conf file is
        # written to disk.
        mocked_get_devices_path.return_value = self.temp_dir
        mocked_get_device_file.return_value = None
        mocked_str.return_value = ""

        add_command = add(self.parser, self.args)
        add_command.invoke()

        expected_path = os.path.join(self.temp_dir,
                                     ".".join([self.device, "conf"]))
        self.assertTrue(os.path.isfile(expected_path))

    @patch("lava.device.commands.edit_file", create=True)
    @patch("lava.device.commands.get_known_device")
    @patch("lava.device.commands.get_devices_path")
    @patch("lava.device.commands.sys.exit")
    @patch("lava.device.commands.get_device_file")
    def test_add_invoke_1(self, mocked_get_device_file, mocked_sys_exit,
                          mocked_get_devices_path, mocked_get_known_device,
                          mocked_edit_file):
        mocked_get_devices_path.return_value = self.temp_dir
        mocked_get_device_file.return_value = self.temp_file.name

        add_command = add(self.parser, self.args)
        add_command.invoke()

        self.assertTrue(mocked_sys_exit.called)


class RemoveCommandTests(HelperTest):

    def test_register_argument(self):
        # Make sure that the parser add_argument is called and we have the
        # correct argument.
        command = remove(self.parser, self.args)
        command.register_arguments(self.parser)
        name, args, kwargs = self.parser.method_calls[0]
        self.assertIn("--non-interactive", args)

        name, args, kwargs = self.parser.method_calls[1]
        self.assertIn("DEVICE", args)

    @patch("lava.device.commands.edit_file", create=True)
    @patch("lava.device.Device.__str__", return_value="")
    @patch("lava.device.Device.update")
    @patch("lava.device.commands.get_device_file")
    @patch("lava.device.commands.get_devices_path")
    def test_remove_invoke(self, get_devices_path_mock, get_device_file_mock,
                           mocked_update, mocked_str, mocked_edit_file):
        # Tests invocation of the remove command. Verifies that the conf file
        # has been correctly removed.
        # First we add a new conf file, then we remove it.
        get_device_file_mock.return_value = None
        get_devices_path_mock.return_value = self.temp_dir

        add_command = add(self.parser, self.args)
        add_command.invoke()

        expected_path = os.path.join(self.temp_dir,
                                     ".".join([self.device, "conf"]))

        # Set new values for the mocked function.
        get_device_file_mock.return_value = expected_path

        remove_command = remove(self.parser, self.args)
        remove_command.invoke()

        self.assertFalse(os.path.isfile(expected_path))

    @patch("lava.device.commands.get_device_file",
           new=MagicMock(return_value="/root"))
    def test_remove_invoke_raises(self):
        # Tests invocation of the remove command, with a non existent device
        # configuration file.
        remove_command = remove(self.parser, self.args)
        self.assertRaises(CommandError, remove_command.invoke)


class ConfigCommanTests(HelperTest):

    def test_register_argument(self):
        # Make sure that the parser add_argument is called and we have the
        # correct argument.
        command = config(self.parser, self.args)
        command.register_arguments(self.parser)
        name, args, kwargs = self.parser.method_calls[0]
        self.assertIn("--non-interactive", args)

        name, args, kwargs = self.parser.method_calls[1]
        self.assertIn("DEVICE", args)

    @patch("lava.device.commands.can_edit_file", create=True)
    @patch("lava.device.commands.edit_file", create=True)
    @patch("lava.device.commands.get_device_file")
    def test_config_invoke_0(self, mocked_get_device_file, mocked_edit_file,
                             mocked_can_edit_file):
        command = config(self.parser, self.args)

        mocked_can_edit_file.return_value = True
        mocked_get_device_file.return_value = self.temp_file.name
        command.invoke()

        self.assertTrue(mocked_edit_file.called)
        self.assertEqual([call(self.temp_file.name)],
                         mocked_edit_file.call_args_list)

    @patch("lava.device.commands.get_device_file",
           new=MagicMock(return_value=None))
    def test_config_invoke_raises_0(self):
        # Tests invocation of the config command, with a non existent device
        # configuration file.
        config_command = config(self.parser, self.args)
        self.assertRaises(CommandError, config_command.invoke)

    @patch("lava.device.commands.get_device_file",
           new=MagicMock(return_value="/etc/password"))
    def test_config_invoke_raises_1(self):
        # Tests invocation of the config command, with a non writable file.
        # Hopefully tests are not run as root.
        config_command = config(self.parser, self.args)
        self.assertRaises(CommandError, config_command.invoke)