-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconftest.py
125 lines (99 loc) · 4.46 KB
/
conftest.py
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
#!/usr/bin/python3
# Copyright 2019 Northern.tech AS
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import os.path
import pytest
import subprocess
import urllib
from .helpers import *
def do_setup_test_container(request, setup_test_container_props, mender_version):
# This should be parametrized in the mother project.
image = setup_test_container_props.image_name
if setup_test_container_props.append_mender_version:
image = "%s:%s" % (image, mender_version)
cmd = "docker run --rm --network host --privileged -tid %s" % image
logging.debug("setup_test_container: %s", cmd)
output = subprocess.check_output(cmd, shell=True)
global docker_container_id
docker_container_id = output.decode("utf-8").split("\n")[0]
setup_test_container_props.container_id = docker_container_id
def finalizer():
cmd = "docker stop {}".format(docker_container_id)
logging.debug("setup_test_container: %s", cmd)
subprocess.check_output(cmd, shell=True)
request.addfinalizer(finalizer)
ready = wait_for_container_boot(docker_container_id)
assert ready, "Image did not boot. Aborting"
return setup_test_container_props
@pytest.fixture(scope="class")
def setup_test_container(request, setup_test_container_props, mender_version):
return do_setup_test_container(request, setup_test_container_props, mender_version)
@pytest.fixture(scope="function")
def setup_test_container_f(request, setup_test_container_props, mender_version):
return do_setup_test_container(request, setup_test_container_props, mender_version)
@pytest.fixture(scope="class")
def setup_tester_ssh_connection(setup_test_container):
yield new_tester_ssh_connection(setup_test_container)
@pytest.fixture(scope="function")
def setup_tester_ssh_connection_f(setup_test_container_f):
yield new_tester_ssh_connection(setup_test_container_f)
# Requires the user to implement mender_deb_version fixture
@pytest.fixture(scope="class")
def setup_mender_configured(
setup_test_container, setup_tester_ssh_connection, mender_deb_version
):
if (
setup_tester_ssh_connection.run(
"test -x /usr/bin/mender-update", warn=True
).exited
== 0
or setup_tester_ssh_connection.run("test -x /usr/bin/mender", warn=True).exited
== 0
):
# If mender is already present, do nothing.
return
if version_is_minimum(mender_deb_version, "4.0.0"):
pkgs_to_install = ["mender-auth", "mender-update"]
url = "https://downloads.mender.io/repos/debian/pool/main/m/mender-client4/"
else:
pkgs_to_install = ["mender-client"]
url = "https://downloads.mender.io/repos/debian/pool/main/m/mender-client/"
for pkg in pkgs_to_install:
pkg_url = url + f"{pkg}_{mender_deb_version}-1%2Bdebian%2Bbullseye_armhf.deb"
filename = urllib.parse.unquote(os.path.basename(pkg_url))
# Install deb package and missing dependencies
setup_tester_ssh_connection.run(f"wget {pkg_url}")
result = setup_tester_ssh_connection.sudo(
f"DEBIAN_FRONTEND=noninteractive dpkg --install {filename}", warn=True
)
if result.exited != 0:
setup_tester_ssh_connection.sudo("apt-get update")
setup_tester_ssh_connection.sudo(
"apt-get --fix-broken --assume-yes install"
)
# Verify that the packages were installed
for pkg in pkgs_to_install:
setup_tester_ssh_connection.run(f"dpkg --status {pkg}")
output = setup_tester_ssh_connection.run("uname -m").stdout.strip()
if output == "x86_64":
device_type = "generic-x86_64"
elif output.startswith("arm"):
device_type = "generic-armv6"
else:
raise KeyError(f"{output} is not a recognized machine type")
setup_tester_ssh_connection.sudo("mkdir -p /var/lib/mender")
setup_tester_ssh_connection.run(
f"echo device_type={device_type} | sudo tee /var/lib/mender/device_type"
)