test_kvm.py 33.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Copyright (C) 2014 Linaro Limited
#
# Author: Neil Williams <neil.williams@linaro.org>
#         Remi Duraffort <remi.duraffort@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 glob
Rémi Duraffort's avatar
Rémi Duraffort committed
24
import sys
25
import time
26
import unittest
27
import pexpect
28

29
from lava_common.compat import yaml_safe_dump, yaml_safe_load
30
from lava_common.constants import SYS_CLASS_KVM
31
from lava_common.exceptions import JobError, InfrastructureError
Rémi Duraffort's avatar
Rémi Duraffort committed
32
from lava_dispatcher.utils.filesystem import mkdtemp
33
from lava_dispatcher.action import Pipeline, Action
34
from tests.lava_dispatcher.test_basic import Factory, StdoutTestCase
Rémi Duraffort's avatar
Rémi Duraffort committed
35
36
37
38
from lava_dispatcher.actions.deploy import DeployAction
from lava_dispatcher.actions.boot.qemu import BootAction
from lava_dispatcher.device import NewDevice
from lava_dispatcher.parser import JobParser
39
from tests.lava_dispatcher.test_messages import FakeConnection
Rémi Duraffort's avatar
Rémi Duraffort committed
40
from lava_dispatcher.utils.messages import LinuxKernelMessages
41
from tests.lava_dispatcher.test_defs import allow_missing_path, check_missing_path
42
from tests.utils import DummyLogger, infrastructure_error
43
from lava_dispatcher.connections.serial import QemuSession
44

45

46
47
class TestKVMSimulation(StdoutTestCase):
    def test_kvm_simulation(self):
48
49
50
51
52
53
54
        """
        Build a pipeline which simulates a KVM LAVA job
        without using the formal objects (to avoid validating
        data known to be broken). The details are entirely
        arbitrary.
        """
        factory = Factory()
55
        job = factory.create_kvm_job("sample_jobs/kvm.yaml")
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
        pipe = Pipeline()
        action = Action()
        action.name = "deploy_linaro_image"
        action.description = "deploy action using preset subactions in an internal pipe"
        action.summary = "deploy_linaro_image"
        action.job = job
        # deliberately unlikely location
        # a successful validation would need to use the cwd
        action.parameters = {"image": "file:///none/images/bad-kvm-debian-wheezy.img"}
        pipe.add_action(action)
        self.assertEqual(action.level, "1")
        deploy_pipe = Pipeline(action)
        action = Action()
        action.name = "downloader"
        action.description = "download image wrapper, including an internal retry pipe"
        action.summary = "downloader"
        action.job = job
        deploy_pipe.add_action(action)
        self.assertEqual(action.level, "1.1")
        # a formal RetryAction would contain a pre-built pipeline which can be inserted directly
        retry_pipe = Pipeline(action)
        action = Action()
        action.name = "wget"
        action.description = "do the download with retries"
        action.summary = "wget"
        action.job = job
        retry_pipe.add_action(action)
        self.assertEqual(action.level, "1.1.1")
        action = Action()
        action.name = "checksum"
        action.description = "checksum the downloaded file"
        action.summary = "md5sum"
        action.job = job
        deploy_pipe.add_action(action)
        self.assertEqual(action.level, "1.2")
        action = Action()
        action.name = "overlay"
        action.description = "apply lava overlay"
        action.summary = "overlay"
        action.job = job
        deploy_pipe.add_action(action)
        self.assertEqual(action.level, "1.3")
        action = Action()
        action.name = "boot"
        action.description = "boot image"
        action.summary = "qemu"
        action.job = job
        # cmd_line built from device configuration
        action.parameters = {
105
106
107
108
109
110
            "cmd_line": [
                "qemu-system-x86_64",
                "-machine accel=kvm:tcg",
                "-hda" "%s" % "tbd",
                "-nographic",
                "-net",
111
112
                "nic,model=virtio",
                "-net user",
113
114
115
116
117
118
119
120
121
122
123
124
125
126
            ]
        }
        pipe.add_action(action)
        self.assertEqual(action.level, "2")

        action = Action()
        action.name = "simulated"
        action.description = "lava test shell"
        action.summary = "simulated"
        action.job = job
        # a formal lava test shell action would include an internal pipe
        # which would handle the run.sh
        pipe.add_action(action)
        self.assertEqual(action.level, "3")
Neil Williams's avatar
Neil Williams committed
127
        # just a fake action
128
        action = Action()
Neil Williams's avatar
Neil Williams committed
129
130
131
        action.name = "fake"
        action.description = "faking results"
        action.summary = "fake action"
132
133
134
135
136
137
        action.job = job
        pipe.add_action(action)
        self.assertEqual(action.level, "4")
        self.assertEqual(len(pipe.describe()), 4)


138
class TestKVMBasicDeploy(StdoutTestCase):
139
    def setUp(self):
Rémi Duraffort's avatar
Rémi Duraffort committed
140
        super().setUp()
141
        self.factory = Factory()
142
143
144
145
146
147
148
        job_ctx = {
            "arch": "amd64",
            "no_kvm": True,
        }  # override to allow unit tests on all types of systems
        self.job = self.factory.create_job(
            "qemu01.jinja2", "sample_jobs/kvm.yaml", job_ctx
        )
149
150
151
152
153
154
155
156

    def test_deploy_job(self):
        self.assertEqual(self.job.pipeline.job, self.job)
        for action in self.job.pipeline.actions:
            if isinstance(action, DeployAction):
                self.assertEqual(action.job, self.job)

    def test_pipeline(self):
157
        description_ref = self.pipeline_reference("kvm.yaml", job=self.job)
158
159
160
161
162
163
164
        deploy = [
            action
            for action in self.job.pipeline.actions
            if action.name == "deployimages"
        ][0]
        overlay = [
            action
165
            for action in deploy.pipeline.actions
166
167
168
169
            if action.name == "lava-overlay"
        ][0]
        self.assertIn(
            "persistent-nfs-overlay",
170
            [action.name for action in overlay.pipeline.actions],
171
        )
172
173
174
175
        self.assertEqual(description_ref, self.job.pipeline.describe(False))

    def test_validate(self):
        try:
176
177
178
            allow_missing_path(
                self.job.pipeline.validate_actions, self, "qemu-system-x86_64"
            )
179
180
181
182
183
        except JobError as exc:
            self.fail(exc)
        for action in self.job.pipeline.actions:
            self.assertEqual([], action.errors)

184
    def test_available_architectures(self):
185
186
187
188
        job_ctx = {"arch": "unknown", "no_kvm": True}
        job = self.factory.create_job("qemu01.jinja2", "sample_jobs/kvm.yaml", job_ctx)
        self.assertIsNotNone(job.device["available_architectures"])
        self.assertEqual(job.parameters["context"]["arch"], "unknown")
189
190
        self.assertRaises(JobError, job.pipeline.validate_actions)

191
192
193
194
195
    def test_overlay(self):
        overlay = None
        for action in self.job.pipeline.actions:
            self.assertIsNotNone(action.name)
            if isinstance(action, DeployAction):
196
                overlay = action.pipeline.actions[2]
197
198
199
        self.assertIsNotNone(overlay)
        # these tests require that lava-dispatcher itself is installed, not just running tests from a git clone
        self.assertTrue(os.path.exists(overlay.lava_test_dir))
200
201
202
203
204
205
206
207
208
209
210
211
212
213
        self.assertIsNot(overlay.lava_test_dir, "/")
        self.assertNotIn("lava_multi_node_test_dir", dir(overlay))
        self.assertNotIn("lava_multi_node_cache_file", dir(overlay))
        self.assertNotIn("lava_lmp_test_dir", dir(overlay))
        self.assertNotIn("lava_lmp_cache_file", dir(overlay))
        self.assertIsNotNone(
            overlay.parameters["deployment_data"]["lava_test_results_dir"]
        )
        self.assertIsNotNone(overlay.parameters["deployment_data"]["lava_test_sh_cmd"])
        self.assertEqual(overlay.parameters["deployment_data"]["distro"], "debian")
        self.assertIsNotNone(
            overlay.parameters["deployment_data"]["lava_test_results_part_attr"]
        )
        self.assertIsNotNone(glob.glob(os.path.join(overlay.lava_test_dir, "lava-*")))
214
215
216
217
218

    def test_boot(self):
        for action in self.job.pipeline.actions:
            if isinstance(action, BootAction):
                # get the action & populate it
219
220
221
222
223
                self.assertEqual(action.parameters["method"], "qemu")
                self.assertEqual(
                    action.parameters["prompts"], ["linaro-test", "root@debian:~#"]
                )
                params = action.parameters.get("auto_login")
224

225
226
227
228
                if "login_prompt" in params:
                    self.assertEqual(params["login_prompt"], "login:")
                if "username" in params:
                    self.assertEqual(params["username"], "root")
229
230
231

    def test_testdefinitions(self):
        for action in self.job.pipeline.actions:
232
            if action.name == "test":
233
                # get the action & populate it
234
                self.assertEqual(len(action.parameters["definitions"]), 2)
235
236


237
class TestKVMPortable(StdoutTestCase):
238
239
240
    def setUp(self):
        super().setUp()
        factory = Factory()
241
        self.job = factory.create_kvm_job("sample_jobs/kvm-noos.yaml")
242
243
244
245
246
247
248
249

    def test_deploy_job(self):
        self.assertEqual(self.job.pipeline.job, self.job)
        for action in self.job.pipeline.actions:
            if isinstance(action, DeployAction):
                self.assertEqual(action.job, self.job)

    def test_pipeline(self):
250
        description_ref = self.pipeline_reference("kvm-noos.yaml", job=self.job)
251
252
253
254
        self.assertEqual(description_ref, self.job.pipeline.describe(False))

    def test_validate(self):
        try:
255
256
257
            allow_missing_path(
                self.job.pipeline.validate_actions, self, "qemu-system-x86_64"
            )
258
259
260
261
262
263
        except JobError as exc:
            self.fail(exc)
        for action in self.job.pipeline.actions:
            self.assertEqual([], action.errors)


264
class TestKVMQcow2Deploy(StdoutTestCase):
265
    def setUp(self):
Rémi Duraffort's avatar
Rémi Duraffort committed
266
        super().setUp()
267
        factory = Factory()
268
        self.job = factory.create_kvm_job("sample_jobs/kvm-qcow2.yaml")
269
270
271
272
273
274
275
276

    def test_deploy_job(self):
        self.assertEqual(self.job.pipeline.job, self.job)
        for action in self.job.pipeline.actions:
            if isinstance(action, DeployAction):
                self.assertEqual(action.job, self.job)

    def test_pipeline(self):
277
        description_ref = self.pipeline_reference("kvm-qcow2.yaml", job=self.job)
278
279
280
281
        self.assertEqual(description_ref, self.job.pipeline.describe(False))

    def test_validate(self):
        try:
282
283
284
            allow_missing_path(
                self.job.pipeline.validate_actions, self, "qemu-system-x86_64"
            )
285
286
287
288
289
290
        except JobError as exc:
            self.fail(exc)
        for action in self.job.pipeline.actions:
            self.assertEqual([], action.errors)


291
class TestKVMDownloadLocalDeploy(StdoutTestCase):
292
    def setUp(self):
Rémi Duraffort's avatar
Rémi Duraffort committed
293
        super().setUp()
294
        factory = Factory()
295
        self.job = factory.create_kvm_job("sample_jobs/kvm-local.yaml")
296
297
298
299
300
301
302
303

    def test_deploy_job(self):
        self.assertEqual(self.job.pipeline.job, self.job)
        for action in self.job.pipeline.actions:
            if isinstance(action, DeployAction):
                self.assertEqual(action.job, self.job)

    def test_pipeline(self):
304
        description_ref = self.pipeline_reference("kvm-local.yaml", job=self.job)
305
306
307
        self.assertEqual(description_ref, self.job.pipeline.describe(False))


308
309
310
311
312
313
314
315
316
317
318
class TestKVMDeployOverlays(StdoutTestCase):
    def setUp(self):
        super().setUp()
        factory = Factory()
        self.job = factory.create_kvm_job("sample_jobs/kvm-overlays.yaml")

    def test_pipeline(self):
        description_ref = self.pipeline_reference("kvm-overlays.yaml", job=self.job)
        self.assertEqual(description_ref, self.job.pipeline.describe(False))


319
320
321
322
323
def prepare_test_connection(failure=False):
    if failure:
        logfile = os.path.join(os.path.dirname(__file__), "kernel-login-error.txt")
    else:
        logfile = os.path.join(os.path.dirname(__file__), "kernel-1.txt")
324
325
    if not os.path.exists(logfile):
        raise OSError("Missing test support file.")
326
    child = pexpect.spawn("cat", [logfile])
327
328
329
330
    message_list = LinuxKernelMessages.get_kernel_prompts()
    return FakeConnection(child, message_list)


331
class TestKVMInlineTestDeploy(StdoutTestCase):
332
    def setUp(self):
Rémi Duraffort's avatar
Rémi Duraffort committed
333
        super().setUp()
334
        self.factory = Factory()
335
        self.job = self.factory.create_kvm_job("sample_jobs/kvm-inline.yaml")
336
337
338
339
340
341
342
343
344
345
346
347

    def test_deploy_job(self):
        self.assertEqual(self.job.pipeline.job, self.job)
        for action in self.job.pipeline.actions:
            if isinstance(action, DeployAction):
                self.assertEqual(action.job, self.job)

    def test_validate(self):
        try:
            self.job.pipeline.validate_actions()
        except JobError as exc:
            self.fail(exc)
348
349
        except InfrastructureError:
            pass
350
351
352
        for action in self.job.pipeline.actions:
            self.assertEqual([], action.errors)

353
    def test_extra_options(self):
354
        (rendered, _) = self.factory.create_device("kvm01.jinja2")
355
        device = NewDevice(yaml_safe_load(rendered))
356
357
358
        kvm_yaml = os.path.join(
            os.path.dirname(__file__), "sample_jobs/kvm-inline.yaml"
        )
359
        with open(kvm_yaml) as sample_job_data:
360
            job_data = yaml_safe_load(sample_job_data)
361
362
        device["actions"]["boot"]["methods"]["qemu"]["parameters"][
            "extra"
363
        ] = yaml_safe_load(
364
            """
365
366
367
368
369
370
                  - -smp
                  - 1
                  - -global
                  - virtio-blk-device.scsi=off
                  - -device virtio-scsi-device,id=scsi
                  - --append "console=ttyAMA0 root=/dev/vda rw"
371
372
373
374
375
                  """
        )
        self.assertIsInstance(
            device["actions"]["boot"]["methods"]["qemu"]["parameters"]["extra"][1], int
        )
376
        parser = JobParser()
377
        job = parser.parse(yaml_safe_dump(job_data), device, 4212, None, "")
378
        job.logger = DummyLogger()
379
        job.validate()
380
381
382
383
384
385
386
        boot_image = [
            action
            for action in job.pipeline.actions
            if action.name == "boot-image-retry"
        ][0]
        boot_qemu = [
            action
387
            for action in boot_image.pipeline.actions
388
389
390
391
            if action.name == "boot-qemu-image"
        ][0]
        qemu = [
            action
392
            for action in boot_qemu.pipeline.actions
393
394
            if action.name == "execute-qemu"
        ][0]
395
        self.assertIsInstance(qemu.sub_command, list)
396
        [self.assertIsInstance(item, str) for item in qemu.sub_command]
397
398
        self.assertIn("virtio-blk-device.scsi=off", qemu.sub_command)
        self.assertIn("1", qemu.sub_command)
399
400
        self.assertNotIn(1, qemu.sub_command)

401
    def test_pipeline(self):
402
        description_ref = self.pipeline_reference("kvm-inline.yaml", job=self.job)
403
404
        self.assertEqual(description_ref, self.job.pipeline.describe(False))

Neil Williams's avatar
Neil Williams committed
405
        self.assertEqual(len(self.job.pipeline.describe()), 4)
406
        inline_repo = None
407
408
        for action in self.job.pipeline.actions:
            if isinstance(action, DeployAction):
409
                self.assertIsNotNone(action.pipeline.actions[1])
410
                overlay = action.pipeline.actions[1]
411
412
413
414
                self.assertIsNotNone(overlay.pipeline.actions[1])
                testdef = overlay.pipeline.actions[2]
                self.assertIsNotNone(testdef.pipeline.actions[0])
                inline_repo = testdef.pipeline.actions[0]
415
                break
416
417
418
419
        # Test the InlineRepoAction directly
        self.assertIsNotNone(inline_repo)
        location = mkdtemp()
        # other actions have not been run, so fake up
420
421
422
423
424
425
426
427
428
429
430
431
        inline_repo.set_namespace_data(
            action="test", label="results", key="lava_test_results_dir", value=location
        )
        inline_repo.set_namespace_data(
            action="test", label="test-definition", key="overlay_dir", value=location
        )
        inline_repo.set_namespace_data(
            action="test", label="shared", key="location", value=location
        )
        inline_repo.set_namespace_data(
            action="test", label="test-definiton", key="overlay_dir", value=location
        )
432

Rémi Duraffort's avatar
Rémi Duraffort committed
433
        inline_repo.run(None, None)
434
435
436
        yaml_file = os.path.join(
            location, "0/tests/0_smoke-tests-inline/inline/smoke-tests-basic.yaml"
        )
437
        self.assertTrue(os.path.exists(yaml_file))
438
        with open(yaml_file, "r") as f_in:
439
            testdef = yaml_safe_load(f_in)
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
        expected_testdef = {
            "metadata": {
                "description": "Basic system test command for Linaro Ubuntu images",
                "devices": [
                    "panda",
                    "panda-es",
                    "arndale",
                    "vexpress-a9",
                    "vexpress-tc2",
                ],
                "format": "Lava-Test Test Definition 1.0",
                "name": "smoke-tests-basic",
                "os": ["ubuntu"],
                "scope": ["functional"],
            },
            "run": {
                "steps": [
                    "lava-test-case linux-INLINE-pwd --shell pwd",
                    "lava-test-case linux-INLINE-uname --shell uname -a",
                    "lava-test-case linux-INLINE-vmstat --shell vmstat",
                    "lava-test-case linux-INLINE-ifconfig --shell ifconfig -a",
                    "lava-test-case linux-INLINE-lscpu --shell lscpu",
                    "lava-test-case linux-INLINE-lsusb --shell lsusb",
                    "lava-test-case linux-INLINE-lsb_release --shell lsb_release -a",
464
                ]
465
466
            },
        }
467
        self.assertEqual(set(testdef), set(expected_testdef))
468

469

470
471
472
473
class TestKvmConnection(StdoutTestCase):
    def setUp(self):
        super().setUp()
        factory = Factory()
474
        self.job = factory.create_kvm_job("sample_jobs/qemu-reboot.yaml")
475
476
477
478
479
        self.job.logger = DummyLogger()
        self.max_end_time = time.time() + 30

    def test_kvm_connection(self):
        self.job.validate()
480
        description_ref = self.pipeline_reference("qemu-reboot.yaml", job=self.job)
481
        self.assertEqual(description_ref, self.job.pipeline.describe(False))
482
483
484
485
486
487
488
        bootaction = [
            action
            for action in self.job.pipeline.actions
            if action.name == "boot-image-retry"
        ][0]
        bootqemu = [
            action
489
            for action in bootaction.pipeline.actions
490
491
492
493
            if action.name == "boot-qemu-image"
        ][0]
        call_qemu = [
            action
494
            for action in bootqemu.pipeline.actions
495
496
            if action.name == "execute-qemu"
        ][0]
497
        self.assertEqual(call_qemu.session_class, QemuSession)
498
499
500
501
502
503
504
        bootaction = [
            action
            for action in self.job.pipeline.actions
            if action.name == "boot-image-retry"
        ][1]
        bootqemu = [
            action
505
            for action in bootaction.pipeline.actions
506
507
508
509
            if action.name == "boot-qemu-image"
        ][0]
        call_qemu = [
            action
510
            for action in bootqemu.pipeline.actions
511
512
            if action.name == "execute-qemu"
        ][0]
513
514
515
        self.assertEqual(call_qemu.session_class, QemuSession)


516
class TestAutoLogin(StdoutTestCase):
517
    def setUp(self):
Rémi Duraffort's avatar
Rémi Duraffort committed
518
        super().setUp()
519
        factory = Factory()
520
        self.job = factory.create_kvm_job("sample_jobs/kvm-inline.yaml")
521
        self.job.logger = DummyLogger()
522
        self.max_end_time = time.time() + 30
523

524
525
    def test_autologin_prompt_patterns(self):
        self.assertEqual(len(self.job.pipeline.describe()), 4)
526
        self.job.validate()
527

528
529
530
531
532
533
534
        bootaction = [
            action
            for action in self.job.pipeline.actions
            if action.name == "boot-image-retry"
        ][0]
        autologinaction = [
            action
535
            for action in bootaction.pipeline.actions
536
537
538
539
540
541
542
543
544
            if action.name == "auto-login-action"
        ][0]

        autologinaction.parameters.update(
            {
                "auto_login": {"login_prompt": "login:", "username": "root"},
                "prompts": ["root@debian:~#"],
            }
        )
545
546

        # initialise the first Connection object, a command line shell
547
        shell_connection = prepare_test_connection()
548
        autologinaction.set_namespace_data(
549
550
551
552
553
            action="deploy-device-env",
            label="environment",
            key="line_separator",
            value="testsep",
        )
554

555
        # Test the AutoLoginAction directly
556
        conn = autologinaction.run(shell_connection, max_end_time=self.max_end_time)
557
        self.assertEqual(shell_connection.raw_connection.linesep, "testsep")
558

559
560
561
562
        self.assertIn("root@debian:~#", conn.prompt_str)
        conn.prompt_str = "root@stretch:"
        self.assertNotIn("root@debian:~#", conn.prompt_str)
        self.assertIn("root@stretch:", conn.prompt_str)
563
564
565
566

    def test_autologin_void_login_prompt(self):
        self.assertEqual(len(self.job.pipeline.describe()), 4)

567
568
569
570
571
572
573
        bootaction = [
            action
            for action in self.job.pipeline.actions
            if action.name == "boot-image-retry"
        ][0]
        autologinaction = [
            action
574
            for action in bootaction.pipeline.actions
575
576
577
578
579
580
581
582
583
            if action.name == "auto-login-action"
        ][0]

        autologinaction.parameters.update(
            {
                "auto_login": {"login_prompt": "", "username": "root"},
                "prompts": ["root@debian:~#"],
            }
        )
584

585
586
        with self.assertRaises((JobError, InfrastructureError)) as check:
            self.job.validate()
587
            check_missing_path(self, check, "qemu-system-x86_64")
588
589
590
591

    def test_missing_autologin_void_prompts_list(self):
        self.assertEqual(len(self.job.pipeline.describe()), 4)

592
593
594
595
596
597
598
        bootaction = [
            action
            for action in self.job.pipeline.actions
            if action.name == "boot-image-retry"
        ][0]
        autologinaction = [
            action
599
            for action in bootaction.pipeline.actions
600
601
            if action.name == "auto-login-action"
        ][0]
602

603
604
        autologinaction.parameters.update({"prompts": []})
        autologinaction.parameters.update({"method": "qemu"})
605

606
607
        with self.assertRaises((JobError, InfrastructureError)) as check:
            self.job.validate()
608
            check_missing_path(self, check, "qemu-system-x86_64")
609
610
611
612

    def test_missing_autologin_void_prompts_list_item(self):
        self.assertEqual(len(self.job.pipeline.describe()), 4)

613
614
615
616
617
618
619
        bootaction = [
            action
            for action in self.job.pipeline.actions
            if action.name == "boot-image-retry"
        ][0]
        autologinaction = [
            action
620
            for action in bootaction.pipeline.actions
621
622
            if action.name == "auto-login-action"
        ][0]
623

624
        autologinaction.parameters.update({"prompts": [""]})
625

626
627
        with self.assertRaises((JobError, InfrastructureError)) as check:
            self.job.validate()
628
            check_missing_path(self, check, "qemu-system-x86_64")
629
630
631
632

    def test_missing_autologin_void_prompts_list_item2(self):
        self.assertEqual(len(self.job.pipeline.describe()), 4)

633
634
635
636
637
638
639
        bootaction = [
            action
            for action in self.job.pipeline.actions
            if action.name == "boot-image-retry"
        ][0]
        autologinaction = [
            action
640
            for action in bootaction.pipeline.actions
641
642
            if action.name == "auto-login-action"
        ][0]
643

644
        autologinaction.parameters.update({"prompts": ["root@debian:~#", ""]})
645

646
647
        with self.assertRaises((JobError, InfrastructureError)) as check:
            self.job.validate()
648
            check_missing_path(self, check, "qemu-system-x86_64")
649
650
651
652

    def test_missing_autologin_prompts_list(self):
        self.assertEqual(len(self.job.pipeline.describe()), 4)

653
654
655
656
657
658
659
        bootaction = [
            action
            for action in self.job.pipeline.actions
            if action.name == "boot-image-retry"
        ][0]
        autologinaction = [
            action
660
            for action in bootaction.pipeline.actions
661
662
663
664
665
            if action.name == "auto-login-action"
        ][0]

        autologinaction.parameters.update({"prompts": ["root@debian:~#"]})
        autologinaction.parameters.update({"method": "qemu"})
666
        autologinaction.validate()
667
668

        # initialise the first Connection object, a command line shell
669
        shell_connection = prepare_test_connection()
670
671

        # Test the AutoLoginAction directly
672
        conn = autologinaction.run(shell_connection, max_end_time=self.max_end_time)
673

674
        self.assertIn("root@debian:~#", conn.prompt_str)
675
676
677
678

    def test_missing_autologin_void_prompts_str(self):
        self.assertEqual(len(self.job.pipeline.describe()), 4)

679
680
681
682
683
684
685
        bootaction = [
            action
            for action in self.job.pipeline.actions
            if action.name == "boot-image-retry"
        ][0]
        autologinaction = [
            action
686
            for action in bootaction.pipeline.actions
687
688
            if action.name == "auto-login-action"
        ][0]
689

690
        autologinaction.parameters.update({"prompts": ""})
691

692
693
        with self.assertRaises((JobError, InfrastructureError)) as check:
            self.job.validate()
694
            check_missing_path(self, check, "qemu-system-x86_64")
695
696
697
698

    def test_missing_autologin_prompts_str(self):
        self.assertEqual(len(self.job.pipeline.describe()), 4)

699
700
701
702
703
704
705
        bootaction = [
            action
            for action in self.job.pipeline.actions
            if action.name == "boot-image-retry"
        ][0]
        autologinaction = [
            action
706
            for action in bootaction.pipeline.actions
707
708
709
710
711
            if action.name == "auto-login-action"
        ][0]

        autologinaction.parameters.update({"prompts": ["root@debian:~#"]})
        autologinaction.parameters.update({"method": "qemu"})
712
        autologinaction.validate()
713
714

        # initialise the first Connection object, a command line shell
715
        shell_connection = prepare_test_connection()
716
717

        # Test the AutoLoginAction directly
718
        conn = autologinaction.run(shell_connection, max_end_time=self.max_end_time)
719

720
        self.assertIn("root@debian:~#", conn.prompt_str)
721

722
723
724
    def test_autologin_login_incorrect(self):
        self.assertEqual(len(self.job.pipeline.describe()), 4)

725
726
727
728
729
730
731
        bootaction = [
            action
            for action in self.job.pipeline.actions
            if action.name == "boot-image-retry"
        ][0]
        autologinaction = [
            action
732
            for action in bootaction.pipeline.actions
733
734
735
736
737
738
739
740
            if action.name == "auto-login-action"
        ][0]

        autologinaction.parameters.update({"prompts": ["root@debian:~#"]})
        autologinaction.parameters.update(
            {"auto_login": {"login_prompt": "debian login:", "username": "root"}}
        )
        autologinaction.parameters.update({"method": "qemu"})
741
        autologinaction.validate()
742
743

        # initialise the first Connection object, a command line shell
744
        shell_connection = prepare_test_connection(True)
745
746

        # Test the AutoLoginAction directly
747
748
749
750
751
752
753
754
755
756
        try:
            conn = autologinaction.run(shell_connection, max_end_time=self.max_end_time)
        except JobError as exc:
            self.assertEqual(str(exc), "Login incorrect")
        else:
            self.assertFalse("Should raise a JobError")
        self.assertIn("root@debian:~#", shell_connection.prompt_str)
        self.assertIn("Login incorrect", shell_connection.prompt_str)
        self.assertIn("Login timed out", shell_connection.prompt_str)
        self.assertIn("Login incorrect", autologinaction.errors)
757

758

759
class TestKvmGuest(StdoutTestCase):
760
    def setUp(self):
Rémi Duraffort's avatar
Rémi Duraffort committed
761
        super().setUp()
762
        factory = Factory()
763
        self.job = factory.create_kvm_job("sample_jobs/kvm-local.yaml")
764
765

    def test_guest_size(self):
766
767
768
769
770
771
772
773
774
775
        self.assertIn(
            "guest",
            self.job.device["actions"]["deploy"]["methods"]["image"]["parameters"],
        )
        self.assertEqual(
            512,
            self.job.device["actions"]["deploy"]["methods"]["image"]["parameters"][
                "guest"
            ]["size"],
        )
776
777


778
class TestKvmUefi(StdoutTestCase):
779
    def setUp(self):
Rémi Duraffort's avatar
Rémi Duraffort committed
780
        super().setUp()
781
        factory = Factory()
782
        self.job = factory.create_kvm_job("sample_jobs/kvm-uefi.yaml")
783

784
785
786
    @unittest.skipIf(
        infrastructure_error("qemu-system-x86_64"), "qemu-system-x86_64 not installed"
    )
787
    def test_uefi_path(self):
788
789
790
791
792
793
794
        deploy = [
            action
            for action in self.job.pipeline.actions
            if action.name == "deployimages"
        ][0]
        downloaders = [
            action
795
            for action in deploy.pipeline.actions
796
797
            if action.name == "download-retry"
        ]
798
799
800
        self.assertEqual(len(downloaders), 2)
        uefi_download = downloaders[0]
        image_download = downloaders[1]
801
802
803
804
        self.assertEqual(image_download.key, "disk1")
        uefi_dir = uefi_download.get_namespace_data(
            action="deployimages", label="image", key="uefi_dir"
        )
805
        self.assertIsNotNone(uefi_dir)
806
807
808
809
810
811
812
813
814
815
816
        self.assertTrue(
            os.path.exists(uefi_dir)
        )  # no download has taken place, but the directory needs to exist
        self.assertFalse(uefi_dir.endswith("bios-256k.bin"))
        boot = [
            action
            for action in self.job.pipeline.actions
            if action.name == "boot-image-retry"
        ][0]
        qemu = [
            action
817
            for action in boot.pipeline.actions
818
819
820
            if action.name == "boot-qemu-image"
        ][0]
        execute = [
821
            action for action in qemu.pipeline.actions if action.name == "execute-qemu"
822
        ][0]
823
        self.job.validate()
Rémi Duraffort's avatar
Rémi Duraffort committed
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
        execute.run(None, 1)
        self.assertEqual(["-drive format=raw,file={disk1}"], execute.commands)
        self.assertEqual(
            [
                "/usr/bin/qemu-system-x86_64",
                "-nographic",
                "-net nic,model=virtio,macaddr=52:54:00:12:34:59 -net user",
                "-m 256",
                "-monitor none",
                "-drive format=raw,file=%s"
                % execute.get_namespace_data(
                    action="download-action", label="disk1", key="file"
                ),
                "-L",
                execute.get_namespace_data(
                    action="deployimages", label="image", key="uefi_dir"
                ),
                "-monitor",
                "none",
            ],
            execute.sub_command,
        )
846
847


848
849
class TestQemuNFS(StdoutTestCase):
    def setUp(self):
Rémi Duraffort's avatar
Rémi Duraffort committed
850
        super().setUp()
851
        factory = Factory()
852
        self.job = factory.create_job("kvm02.jinja2", "sample_jobs/qemu-nfs.yaml")
853
854
        self.job.logger = DummyLogger()

Rémi Duraffort's avatar
Rémi Duraffort committed
855
    @unittest.skipIf(sys.version_info.minor < 6, "unreliable on python3.5 and before")
856
857
858
    @unittest.skipIf(
        infrastructure_error("qemu-system-aarch64"), "qemu-system-arm not installed"
    )
859
    @unittest.skipIf(not os.path.exists(SYS_CLASS_KVM), "Cannot use --enable-kvm")
860
861
    def test_qemu_nfs(self):
        self.assertIsNotNone(self.job)
862
        description_ref = self.pipeline_reference("qemu-nfs.yaml", job=self.job)
863
864
        self.assertEqual(description_ref, self.job.pipeline.describe(False))

865
866
867
868
869
870
871
        boot = [
            action
            for action in self.job.pipeline.actions
            if action.name == "boot-image-retry"
        ][0]
        qemu = [
            action
872
            for action in boot.pipeline.actions
873
874
875
            if action.name == "boot-qemu-image"
        ][0]
        execute = [
876
            action for action in qemu.pipeline.actions if action.name == "execute-qemu"
877
        ][0]
878
        self.job.validate()
Rémi Duraffort's avatar
Rémi Duraffort committed
879
880
        execute.run(None, 1)
        self.assertEqual(["-initrd {initrd}", "-kernel {kernel}"], execute.commands)
881
        self.assertEqual(
Rémi Duraffort's avatar
Rémi Duraffort committed
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
            [
                "/usr/bin/qemu-system-x86_64",
                "-cpu host",
                "-enable-kvm",
                "-nographic",
                "-net nic,model=virtio,macaddr=52:54:00:12:34:59 -net tap",
                "-m 256",
                "-monitor none",
                "-smp",
                "1",
                "-initrd %s"
                % execute.get_namespace_data(
                    action="download-action", label="initrd", key="file"
                ),
                "-kernel %s"
                % execute.get_namespace_data(
                    action="download-action", label="kernel", key="file"
                ),
                "--append",
                '"console=ttyAMA0 root=/dev/nfs nfsroot=192.168.0.2:{NFSROOTFS},tcp,hard,intr rw ip=dhcp"',
            ],
            execute.sub_command,
904
905
906
907
908
        )

        args = execute.methods["qemu-nfs"]["parameters"]["append"]["nfsrootargs"]
        self.assertIn("{NFS_SERVER_IP}", args)
        self.assertIn("{NFSROOTFS}", args)
909
910


911
class TestMonitor(StdoutTestCase):
912
    def setUp(self):
Rémi Duraffort's avatar
Rémi Duraffort committed
913
        super().setUp()
914
        factory = Factory()
915
        self.job = factory.create_kvm_job("sample_jobs/qemu-monitor.yaml")
916
917
918
919
920
921

    def test_qemu_monitor(self):
        self.assertIsNotNone(self.job)
        self.assertIsNotNone(self.job.pipeline)
        self.assertIsNotNone(self.job.pipeline.actions)
        self.job.validate()