File: conftest.py

package info (click to toggle)
lava 2026.01-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 30,796 kB
  • sloc: python: 82,790; javascript: 16,658; sh: 1,364; makefile: 335
file content (84 lines) | stat: -rw-r--r-- 2,503 bytes parent folder | download | duplicates (2)
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
# Copyright (C) 2018 Linaro Limited
#
# Author: Remi Duraffort <remi.duraffort@linaro.org>
#
# SPDX-License-Identifier: GPL-2.0-or-later

import os

import pytest
import requests

import lava_dispatcher.job
import lava_dispatcher.utils.filesystem

os.environ["LANGUAGE"] = "C.UTF-8"


# @pytest.fixture
# def job_config(tmp_path):
#     return {"dispatcher_download_path": tmp_path}


@pytest.fixture(autouse=True)
def tempdir(monkeypatch, tmp_path):
    monkeypatch.setattr(
        lava_dispatcher.utils.filesystem,
        "dispatcher_download_dir",
        lambda _: str(tmp_path),
    )
    monkeypatch.setattr(
        lava_dispatcher.utils.filesystem, "tftpd_dir", lambda: str(tmp_path)
    )


class LavaTestOverwriteInConftest(NotImplementedError):
    ...


@pytest.fixture(autouse=True)
def no_network(mocker, request):
    def get(url, allow_redirects, stream, headers, timeout):
        assert allow_redirects is True  # nosec - unit test support
        assert stream is True  # nosec - unit test support
        res = requests.Response()
        res.status_code = requests.codes["OK"]
        res.close = lambda: None
        res.raw = LavaTestOverwriteInConftest()
        return res

    def head(url, allow_redirects, headers, timeout):
        assert allow_redirects is True  # nosec - unit test support
        print(url)
        res = requests.Response()
        res.status_code = requests.codes["OK"]
        res.raw = LavaTestOverwriteInConftest()
        res.close = lambda: None
        return res

    # List of tests that should have access to the network
    # When pytest is mandatory, we can use pytest marks
    # See https://stackoverflow.com/a/38763328
    skip_tests = {
        "test_bad_download_decompression",
        "test_download_decompression",
        "test_invalid_multinode",
    }
    if not skip_tests & set(request.keywords.keys()):
        mocker.patch("requests.head", head)
        mocker.patch("requests.get", get)
        mocker.patch(
            "lava_dispatcher.actions.deploy.download.requests_retry",
            lambda retries=15: requests,
        )

    # Fake lava_dispatcher.utils.network to always return the same results
    def gateways():
        return "eth0"

    def ifaddresses(iface):
        assert iface == "eth0"  # nosec - unit test support
        return "192.168.0.2"

    mocker.patch("lava_dispatcher.utils.network.get_default_iface", gateways)
    mocker.patch("lava_dispatcher.utils.network.get_iface_addr", ifaddresses)