aboutsummaryrefslogtreecommitdiff
path: root/lava_dispatcher/test/test_connections.py
blob: 2df1854fe87abf9f48c7a0a49fb8edf7d9f388ba (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# Copyright (C) 2014 Linaro Limited
#
# Author: Neil Williams <neil.williams@linaro.org>
#
# This file is part of LAVA Dispatcher.
#
# LAVA Dispatcher is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LAVA Dispatcher 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 General Public License
# along
# with this program; if not, see <http://www.gnu.org/licenses>.


import os
import yaml
import logging
import unittest
from lava_dispatcher.action import JobError
from lava_common.timeout import Timeout
from lava_dispatcher.actions.boot.ssh import SchrootAction
from lava_dispatcher.test.test_basic import Factory, StdoutTestCase
from lava_dispatcher.test.utils import infrastructure_error
from lava_dispatcher.utils.filesystem import check_ssh_identity_file
from lava_dispatcher.protocols.multinode import MultinodeProtocol


class ConnectionFactory(Factory):  # pylint: disable=too-few-public-methods
    """
    Not Model based, this is not a Django factory.
    Factory objects are dispatcher based classes, independent
    of any database objects.
    """

    def create_ssh_job(self, filename):
        return self.create_job('ssh-host-01.jinja2', filename)

    def create_bbb_job(self, filename):
        return self.create_job('bbb-02.jinja2', filename)


class TestConnection(StdoutTestCase):  # pylint: disable=too-many-public-methods

    def setUp(self):
        super().setUp()
        factory = ConnectionFactory()
        self.job = factory.create_ssh_job('sample_jobs/ssh-deploy.yaml')
        self.guest_job = factory.create_bbb_job('sample_jobs/bbb-ssh-guest.yaml')
        logging.getLogger('dispatcher').addHandler(logging.NullHandler())

    @unittest.skipIf(infrastructure_error('schroot'), "schroot not installed")
    def test_ssh_job(self):
        self.assertIsNotNone(self.job)
        self.job.validate()
        self.assertEqual([], self.job.pipeline.errors)
        # Check Pipeline
        description_ref = self.pipeline_reference('ssh-deploy.yaml')
        self.assertEqual(description_ref, self.job.pipeline.describe(False))

    @unittest.skipIf(infrastructure_error('schroot'), "schroot not installed")
    def test_ssh_authorize(self):
        overlay = [action for action in self.job.pipeline.actions if action.name == 'scp-overlay'][0]
        prepare = [action for action in overlay.internal_pipeline.actions if action.name == 'lava-overlay'][0]
        authorize = [action for action in prepare.internal_pipeline.actions if action.name == 'ssh-authorize'][0]
        self.assertFalse(authorize.active)
        self.job.validate()
        # only secondary connections set 'active' which then copies the identity file into the overlay.
        self.assertFalse(authorize.active)

    def test_ssh_identity(self):
        params = {
            'tftp': 'None',
            'usb': 'None',
            'ssh': {
                'host': '172.16.200.165', 'options': [
                    '-o', 'Compression=yes', '-o', 'UserKnownHostsFile=/dev/null',
                    '-o', 'PasswordAuthentication=no', '-o', 'StrictHostKeyChecking=no',
                    '-o', 'LogLevel=FATAL', '-l', 'root ', '-p', 22],
                'identity_file': 'dynamic_vm_keys/lava'
            }
        }
        check = check_ssh_identity_file(params)
        self.assertIsNone(check[0])
        self.assertIsNotNone(check[1])
        self.assertEqual(os.path.basename(check[1]), 'lava')

    @unittest.skipIf(infrastructure_error('schroot'), "schroot not installed")
    def test_ssh_params(self):
        self.assertTrue(any('ssh' in item for item in self.job.device['actions']['deploy']['methods']))
        params = self.job.device['actions']['deploy']['methods']
        identity = os.path.realpath(os.path.join(__file__, '../../', params['ssh']['identity_file']))
        self.assertTrue(os.path.exists(identity))
        test_command = [
            'ssh', '-o', 'Compression=yes',
            '-o', 'PasswordAuthentication=no',
            '-o', 'LogLevel=FATAL',
            '-o', 'UserKnownHostsFile=/dev/null',
            '-o', 'StrictHostKeyChecking=no',
            '-i', identity,
            '-p', '8022'
        ]
        self.job.validate()
        login = [action for action in self.job.pipeline.actions if action.name == 'login-ssh'][0]
        self.assertIn('ssh-connection', [action.name for action in login.internal_pipeline.actions])
        primary = [action for action in login.internal_pipeline.actions if action.name == 'ssh-connection'][0]
        prepare = [action for action in login.internal_pipeline.actions if action.name == 'prepare-ssh'][0]
        self.assertTrue(prepare.primary)
        self.assertEqual(identity, primary.identity_file)
        self.assertEqual(primary.host, params['ssh']['host'])
        self.assertEqual(int(primary.ssh_port[1]), params['ssh']['port'])
        self.assertEqual(set(test_command), set(primary.command))
        # idempotency check
        self.job.validate()
        self.assertEqual(identity, primary.identity_file)
        self.assertEqual(test_command, primary.command)
        bad_port = {
            'host': 'localhost',
            'port': 'bob',
            'options': [
                '-o', 'Compression=yes', '-o', 'UserKnownHostsFile=/dev/null',
                '-o', 'PasswordAuthentication=no', '-o', 'StrictHostKeyChecking=no',
                '-o', 'LogLevel=FATAL'
            ],
            'identity_file': 'dynamic_vm_keys/lava'}
        self.job.device['actions']['deploy']['methods']['ssh'] = bad_port
        with self.assertRaises(JobError):
            self.job.validate()

    @unittest.skipIf(infrastructure_error('schroot'), "schroot not installed")
    def test_scp_command(self):
        self.job.validate()
        login = [action for action in self.guest_job.pipeline.actions if action.name == 'login-ssh'][0]
        scp = [action for action in login.internal_pipeline.actions if action.name == 'scp-deploy'][0]
        self.assertIsNotNone(scp)
        # FIXME: schroot needs to make use of scp
        self.assertNotIn('ssh', scp.scp)
        self.assertFalse(scp.primary)

    @unittest.skipIf(infrastructure_error('schroot'), "schroot not installed")
    def test_tar_command(self):
        self.job.validate()
        login = [item for item in self.job.pipeline.actions if item.name == 'login-ssh'][0]
        tar_flags = login.get_namespace_data(action='scp-overlay', label='scp-overlay', key='tar_flags')
        self.assertIsNotNone(tar_flags)
        self.assertEqual('--warning no-timestamp', tar_flags)

    @unittest.skipIf(infrastructure_error('schroot'), "schroot not installed")
    def test_schroot_params(self):
        self.assertIn('schroot-login', [action.name for action in self.job.pipeline.actions])
        schroot = [action for action in self.job.pipeline.actions if action.name == "schroot-login"][0]
        self.job.validate()
        schroot.run_command(['schroot', '-i', '-c', schroot.parameters['schroot']])
        if any("Chroot not found" in chk for chk in schroot.errors) or not schroot.valid:
            # schroot binary found but no matching chroot configured - skip test
            self.skipTest("no schroot support for %s" % schroot.parameters['schroot'])
        bad_chroot = 'unobtainium'
        schroot.run_command(['schroot', '-i', '-c', bad_chroot])
        if not any("Chroot not found" in chk for chk in schroot.errors) or schroot.valid:
            self.fail("Failed to catch a missing schroot name")

        self.assertIsInstance(schroot, SchrootAction)
        self.assertEqual(schroot.parameters['schroot'], 'unstable')
        boot_act = [boot['boot'] for boot in self.job.parameters['actions']
                    if 'boot' in boot and 'schroot' in boot['boot']][0]
        self.assertEqual(boot_act['schroot'], schroot.parameters['schroot'])

    def test_primary_ssh(self):
        factory = ConnectionFactory()
        job = factory.create_ssh_job('sample_jobs/primary-ssh.yaml')
        job.validate()
        overlay = [action for action in job.pipeline.actions if action.name == 'scp-overlay'][0]
        self.assertIsNotNone(overlay.parameters['deployment_data'])
        tar_flags = overlay.parameters['deployment_data']['tar_flags'] if 'tar_flags' in overlay.parameters['deployment_data'].keys() else ''
        self.assertIsNotNone(tar_flags)

    def test_guest_ssh(self):  # pylint: disable=too-many-locals,too-many-statements
        self.assertIsNotNone(self.guest_job)
        description_ref = self.pipeline_reference('bbb-ssh-guest.yaml', job=self.guest_job)
        self.assertEqual(description_ref, self.guest_job.pipeline.describe(False))
        self.guest_job.validate()
        multinode = [protocol for protocol in self.guest_job.protocols if protocol.name == MultinodeProtocol.name][0]
        self.assertEqual(int(multinode.system_timeout.duration), 900)
        self.assertEqual([], self.guest_job.pipeline.errors)
        self.assertEqual(len([item for item in self.guest_job.pipeline.actions if item.name == 'scp-overlay']), 1)
        scp_overlay = [item for item in self.guest_job.pipeline.actions if item.name == 'scp-overlay'][0]
        prepare = [item for item in scp_overlay.internal_pipeline.actions if item.name == 'prepare-scp-overlay'][0]
        self.assertEqual(prepare.host_keys, ['ipv4'])
        self.assertEqual(prepare.get_namespace_data(action=prepare.name, label=prepare.name, key='overlay'), prepare.host_keys)
        params = prepare.parameters['protocols'][MultinodeProtocol.name]
        for call_dict in [call for call in params if 'action' in call and call['action'] == prepare.name]:
            del call_dict['yaml_line']
            if 'message' in call_dict:
                del call_dict['message']['yaml_line']
            if 'timeout' in call_dict:
                del call_dict['timeout']['yaml_line']
            self.assertEqual(
                call_dict, {
                    'action': 'prepare-scp-overlay',
                    'message': {'ipaddr': '$ipaddr'},
                    'messageID': 'ipv4', 'request': 'lava-wait',
                    'timeout': {'minutes': 5}
                },
            )
        login = [action for action in self.guest_job.pipeline.actions if action.name == 'login-ssh'][0]
        scp = [action for action in login.internal_pipeline.actions if action.name == 'scp-deploy'][0]
        self.assertFalse(scp.primary)
        ssh = [action for action in login.internal_pipeline.actions if action.name == 'prepare-ssh'][0]
        self.assertFalse(ssh.primary)
        self.assertIsNotNone(scp.scp)
        self.assertFalse(scp.primary)
        autologin = [action for action in login.internal_pipeline.actions if action.name == 'auto-login-action'][0]
        self.assertIsNone(autologin.params)
        self.assertIn('host_key', login.parameters['parameters'])
        self.assertIn('hostID', login.parameters['parameters'])
        self.assertIn(  # ipv4
            login.parameters['parameters']['hostID'],
            prepare.host_keys)
        prepare.set_namespace_data(
            action=MultinodeProtocol.name, label=MultinodeProtocol.name,
            key='ipv4', value={'ipaddr': '172.16.200.165'})
        self.assertEqual(prepare.get_namespace_data(
            action=prepare.name, label=prepare.name, key='overlay'), prepare.host_keys)
        self.assertIn(
            login.parameters['parameters']['host_key'],
            prepare.get_namespace_data(
                action=MultinodeProtocol.name, label=MultinodeProtocol.name, key=login.parameters['parameters']['hostID']))
        host_data = prepare.get_namespace_data(
            action=MultinodeProtocol.name, label=MultinodeProtocol.name, key=login.parameters['parameters']['hostID'])
        self.assertEqual(
            host_data[login.parameters['parameters']['host_key']],
            '172.16.200.165'
        )
        data = scp_overlay.get_namespace_data(action=MultinodeProtocol.name, label=MultinodeProtocol.name, key='ipv4')
        if 'protocols' in scp_overlay.parameters:
            for params in scp_overlay.parameters['protocols'][MultinodeProtocol.name]:
                (replacement_key, placeholder) = [
                    (key, value)for key, value in params['message'].items() if key != 'yaml_line'][0]
                self.assertEqual(data[replacement_key], '172.16.200.165')
                self.assertEqual(placeholder, '$ipaddr')
        environment = scp_overlay.get_namespace_data(action=prepare.name, label='environment', key='env_dict')
        self.assertIsNotNone(environment)
        self.assertIn('LANG', environment.keys())
        self.assertIn('C', environment.values())
        overlay = [item for item in scp_overlay.internal_pipeline.actions if item.name == 'lava-overlay']
        self.assertIn('action', overlay[0].parameters['protocols'][MultinodeProtocol.name][0])
        self.assertIn('message', overlay[0].parameters['protocols'][MultinodeProtocol.name][0])
        self.assertIn('timeout', overlay[0].parameters['protocols'][MultinodeProtocol.name][0])
        msg_dict = overlay[0].parameters['protocols'][MultinodeProtocol.name][0]['message']
        for key, value in msg_dict.items():
            if key == 'yaml_line':
                continue
            self.assertTrue(value.startswith('$'))
            self.assertFalse(key.startswith('$'))
        self.assertIn('request', overlay[0].parameters['protocols'][MultinodeProtocol.name][0])
        multinode = [item for item in overlay[0].internal_pipeline.actions if item.name == 'lava-multinode-overlay']
        self.assertEqual(len(multinode), 1)
        # Check Pipeline
        description_ref = self.pipeline_reference('ssh-guest.yaml', job=self.guest_job)
        self.assertEqual(description_ref, self.guest_job.pipeline.describe(False))


class TestConsoleConnections(StdoutTestCase):

    def setUp(self):
        super().setUp()
        factory = ConnectionFactory()
        self.job = factory.create_ssh_job('sample_jobs/ssh-deploy.yaml')
        self.guest_job = factory.create_bbb_job('sample_jobs/bbb-ssh-guest.yaml')
        logging.getLogger('dispatcher').addHandler(logging.NullHandler())

    def test_device_commands(self):
        device_data = """
commands:
    # deprecated
    # connect: telnet localhost 7019
    connections:
      uart1:
        connect: telnet localhost 7019
        tags:
        - primary
      uart0:
        connect: telnet localhost 7020
        """
        data = yaml.load(device_data)
        self.assertIn('commands', data)
        self.assertIn('connections', data['commands'])
        self.assertNotIn('connect', data['commands'])
        for hardware, value in data['commands']['connections'].items():
            if 'connect' not in value:
                self.fail("Misconfigured device configuration")
            if 'tags' in value and 'primary' in value['tags']:
                self.assertEqual('uart1', hardware)
            else:
                self.assertEqual('uart0', hardware)

    def test_connection_tags(self):
        factory = ConnectionFactory()
        job = factory.create_bbb_job('sample_jobs/uboot-ramdisk.yaml')
        job.validate()
        self.assertIsNotNone(job.device['commands']['connections'].items())
        for hardware, value in job.device['commands']['connections'].items():
            self.assertEqual('uart0', hardware)
            self.assertIn('connect', value)
            self.assertIn('tags', value)
            self.assertEqual(['primary', 'telnet'], value['tags'])
        boot = [action for action in job.pipeline.actions if action.name == 'uboot-action'][0]
        connect = [action for action in boot.internal_pipeline.actions if action.name == 'connect-device'][0]
        for hardware, value in connect.tag_dict.items():
            self.assertEqual('uart0', hardware)
            self.assertNotIn('connect', value)
            self.assertNotIn('tags', value)
            self.assertEqual(['primary', 'telnet'], value)
        self.assertIn(connect.hardware, connect.tag_dict)
        self.assertEqual(['primary', 'telnet'], connect.tag_dict[connect.hardware])


class TestTimeouts(StdoutTestCase):
    """
    Test action and connection timeout parsing.
    """

    def setUp(self):
        super().setUp()
        self.factory = ConnectionFactory()

    def test_action_timeout(self):
        factory = ConnectionFactory()
        job = factory.create_bbb_job('sample_jobs/uboot-ramdisk.yaml')
        job.validate()
        deploy = [action for action in job.pipeline.actions if action.name == 'tftp-deploy'][0]
        test_action = [action for action in job.pipeline.actions if action.name == 'lava-test-retry'][0]
        test_shell = [action for action in test_action.internal_pipeline.actions if action.name == 'lava-test-shell'][0]
        self.assertEqual(test_shell.connection_timeout.duration, 240)  # job specifies 4 minutes
        self.assertEqual(test_shell.timeout.duration, 300)  # job (test action block) specifies 5 minutes
        self.assertEqual(deploy.timeout.duration, 120)  # job specifies 2 minutes
        self.assertEqual(deploy.connection_timeout.duration, Timeout.default_duration())
        self.assertNotEqual(deploy.connection_timeout.duration, test_shell.connection_timeout)
        self.assertEqual(test_action.timeout.duration, 300)
        uboot = [action for action in job.pipeline.actions if action.name == 'uboot-action'][0]
        retry = [action for action in uboot.internal_pipeline.actions if action.name == 'uboot-retry'][0]
        auto = [action for action in retry.internal_pipeline.actions if action.name == 'auto-login-action'][0]
        self.assertEqual(auto.timeout.duration / 60, 9)  # 9 minutes in the job def

    def test_job_connection_timeout(self):
        """
        Test connection timeout specified in the submission YAML
        """
        y_file = os.path.join(os.path.dirname(__file__), './sample_jobs/uboot-ramdisk.yaml')
        with open(y_file, 'r') as uboot_ramdisk:
            data = yaml.load(uboot_ramdisk)
        data['timeouts']['connection'] = {'seconds': 20}
        job = self.factory.create_custom_job('bbb-01.jinja2', data)
        for action in job.pipeline.actions:
            if action.internal_pipeline:
                for check_action in action.internal_pipeline.actions:
                    if check_action.connection_timeout and check_action.name not in ['uboot-retry', 'lava-test-shell']:
                        # lava-test-shell and uboot-retry have overrides in this sample job
                        # lava-test-shell from the job, uboot-retry from the device
                        self.assertEqual(check_action.connection_timeout.duration, 20)
        deploy = [action for action in job.pipeline.actions if action.name == 'tftp-deploy'][0]
        self.assertIsNotNone(deploy)
        test_action = [action for action in job.pipeline.actions if action.name == 'lava-test-retry'][0]
        test_shell = [action for action in test_action.internal_pipeline.actions if action.name == 'lava-test-shell'][0]
        self.assertEqual(test_shell.timeout.duration, 300)
        uboot = [action for action in job.pipeline.actions if action.name == 'uboot-action'][0]
        retry = [action for action in uboot.internal_pipeline.actions if action.name == 'uboot-retry'][0]
        auto = [action for action in retry.internal_pipeline.actions if action.name == 'auto-login-action'][0]
        self.assertEqual(auto.connection_timeout.duration / 60, 12)

    def test_action_connection_timeout(self):
        """
        Test connection timeout specified for a particular action
        """
        y_file = os.path.join(os.path.dirname(__file__), './sample_jobs/uboot-ramdisk.yaml')
        with open(y_file, 'r') as uboot_ramdisk:
            data = yaml.load(uboot_ramdisk)
        connection_timeout = Timeout.parse(data['timeouts']['connections']['lava-test-shell'])
        data['timeouts']['actions']['uboot-retry'] = {}
        data['timeouts']['actions']['uboot-retry']['seconds'] = 90
        data['timeouts']['connections']['uboot-retry'] = {}
        data['timeouts']['connections']['uboot-retry']['seconds'] = 45
        self.assertEqual(connection_timeout, 240)
        job = self.factory.create_custom_job('bbb-01.jinja2', data)
        boot = [action for action in job.pipeline.actions if action.name == 'uboot-action'][0]
        retry = [action for action in boot.internal_pipeline.actions if action.name == 'uboot-retry'][0]
        self.assertEqual(retry.timeout.duration, 90)  # Set by the job global action timeout
        self.assertEqual(retry.connection_timeout.duration, 45)