You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
260 lines
8.7 KiB
260 lines
8.7 KiB
1 year ago
|
import os
|
||
|
import pathlib
|
||
|
import shutil
|
||
|
import signal
|
||
1 year ago
|
import stat
|
||
1 year ago
|
import subprocess
|
||
|
import tempfile
|
||
|
import time
|
||
|
import pytest
|
||
1 year ago
|
|
||
|
from openpilot.common.params import Params
|
||
1 year ago
|
from openpilot.selfdrive.manager.process import ManagerProcess
|
||
1 year ago
|
from openpilot.selfdrive.test.helpers import processes_context
|
||
1 year ago
|
|
||
|
|
||
1 year ago
|
def get_consistent_flag(path: str) -> bool:
|
||
|
consistent_file = pathlib.Path(os.path.join(path, ".overlay_consistent"))
|
||
|
return consistent_file.is_file()
|
||
1 year ago
|
|
||
|
|
||
|
def run(args, **kwargs):
|
||
1 year ago
|
return subprocess.check_output(args, **kwargs)
|
||
1 year ago
|
|
||
|
|
||
1 year ago
|
def update_release(directory, name, version, agnos_version, release_notes):
|
||
1 year ago
|
with open(directory / "RELEASES.md", "w") as f:
|
||
|
f.write(release_notes)
|
||
|
|
||
|
(directory / "common").mkdir(exist_ok=True)
|
||
|
|
||
|
with open(directory / "common" / "version.h", "w") as f:
|
||
|
f.write(f'#define COMMA_VERSION "{version}"')
|
||
|
|
||
1 year ago
|
launch_env = directory / "launch_env.sh"
|
||
|
with open(launch_env, "w") as f:
|
||
1 year ago
|
f.write(f'export AGNOS_VERSION="{agnos_version}"')
|
||
|
|
||
1 year ago
|
st = os.stat(launch_env)
|
||
|
os.chmod(launch_env, st.st_mode | stat.S_IEXEC)
|
||
|
|
||
1 year ago
|
test_symlink = directory / "test_symlink"
|
||
|
if not os.path.exists(str(test_symlink)):
|
||
|
os.symlink("common/version.h", test_symlink)
|
||
|
|
||
1 year ago
|
|
||
1 year ago
|
def get_version(path: str) -> str:
|
||
|
with open(os.path.join(path, "common", "version.h")) as f:
|
||
|
return f.read().split('"')[1]
|
||
|
|
||
|
|
||
1 year ago
|
@pytest.mark.slow # TODO: can we test overlayfs in GHA?
|
||
11 months ago
|
class TestBaseUpdate:
|
||
1 year ago
|
@classmethod
|
||
11 months ago
|
def setup_class(cls):
|
||
1 year ago
|
if "Base" in cls.__name__:
|
||
11 months ago
|
pytest.skip()
|
||
1 year ago
|
|
||
11 months ago
|
def setup_method(self):
|
||
1 year ago
|
self.tmpdir = tempfile.mkdtemp()
|
||
|
|
||
|
run(["sudo", "mount", "-t", "tmpfs", "tmpfs", self.tmpdir]) # overlayfs doesn't work inside of docker unless this is a tmpfs
|
||
|
|
||
|
self.mock_update_path = pathlib.Path(self.tmpdir)
|
||
|
|
||
|
self.params = Params()
|
||
|
|
||
|
self.basedir = self.mock_update_path / "openpilot"
|
||
|
self.basedir.mkdir()
|
||
|
|
||
|
self.staging_root = self.mock_update_path / "safe_staging"
|
||
|
self.staging_root.mkdir()
|
||
|
|
||
|
self.remote_dir = self.mock_update_path / "remote"
|
||
|
self.remote_dir.mkdir()
|
||
|
|
||
|
os.environ["UPDATER_STAGING_ROOT"] = str(self.staging_root)
|
||
|
os.environ["UPDATER_LOCK_FILE"] = str(self.mock_update_path / "safe_staging_overlay.lock")
|
||
|
|
||
|
self.MOCK_RELEASES = {
|
||
1 year ago
|
"release3": ("0.1.2", "1.2", "0.1.2 release notes"),
|
||
|
"master": ("0.1.3", "1.2", "0.1.3 release notes"),
|
||
1 year ago
|
}
|
||
|
|
||
11 months ago
|
@pytest.fixture(autouse=True)
|
||
|
def mock_basedir(self, mocker):
|
||
|
mocker.patch("openpilot.common.basedir.BASEDIR", self.basedir)
|
||
|
|
||
1 year ago
|
def set_target_branch(self, branch):
|
||
|
self.params.put("UpdaterTargetBranch", branch)
|
||
|
|
||
|
def setup_basedir_release(self, release):
|
||
|
self.params = Params()
|
||
|
self.set_target_branch(release)
|
||
|
|
||
|
def update_remote_release(self, release):
|
||
1 year ago
|
raise NotImplementedError("")
|
||
1 year ago
|
|
||
|
def setup_remote_release(self, release):
|
||
1 year ago
|
raise NotImplementedError("")
|
||
|
|
||
|
def additional_context(self):
|
||
|
raise NotImplementedError("")
|
||
1 year ago
|
|
||
11 months ago
|
def teardown_method(self):
|
||
1 year ago
|
try:
|
||
|
run(["sudo", "umount", "-l", str(self.staging_root / "merged")])
|
||
|
run(["sudo", "umount", "-l", self.tmpdir])
|
||
|
shutil.rmtree(self.tmpdir)
|
||
|
except Exception:
|
||
|
print("cleanup failed...")
|
||
1 year ago
|
|
||
1 year ago
|
def wait_for_condition(self, condition, timeout=12):
|
||
|
start = time.monotonic()
|
||
|
while True:
|
||
|
waited = time.monotonic() - start
|
||
|
if condition():
|
||
|
print(f"waited {waited}s for condition ")
|
||
|
return waited
|
||
1 year ago
|
|
||
1 year ago
|
if waited > timeout:
|
||
|
raise TimeoutError("timed out waiting for condition")
|
||
1 year ago
|
|
||
1 year ago
|
time.sleep(1)
|
||
1 year ago
|
|
||
1 year ago
|
def _test_finalized_update(self, branch, version, agnos_version, release_notes):
|
||
11 months ago
|
assert get_version(str(self.staging_root / "finalized")) == version
|
||
|
assert get_consistent_flag(str(self.staging_root / "finalized"))
|
||
|
assert os.access(str(self.staging_root / "finalized" / "launch_env.sh"), os.X_OK)
|
||
1 year ago
|
|
||
1 year ago
|
with open(self.staging_root / "finalized" / "test_symlink") as f:
|
||
11 months ago
|
assert version in f.read()
|
||
1 year ago
|
|
||
11 months ago
|
class ParamsBaseUpdateTest(TestBaseUpdate):
|
||
1 year ago
|
def _test_finalized_update(self, branch, version, agnos_version, release_notes):
|
||
11 months ago
|
assert self.params.get("UpdaterNewDescription", encoding="utf-8").startswith(f"{version} / {branch}")
|
||
|
assert self.params.get("UpdaterNewReleaseNotes", encoding="utf-8") == f"<p>{release_notes}</p>\n"
|
||
1 year ago
|
super()._test_finalized_update(branch, version, agnos_version, release_notes)
|
||
1 year ago
|
|
||
1 year ago
|
def send_check_for_updates_signal(self, updated: ManagerProcess):
|
||
|
updated.signal(signal.SIGUSR1.value)
|
||
1 year ago
|
|
||
1 year ago
|
def send_download_signal(self, updated: ManagerProcess):
|
||
|
updated.signal(signal.SIGHUP.value)
|
||
|
|
||
|
def _test_params(self, branch, fetch_available, update_available):
|
||
11 months ago
|
assert self.params.get("UpdaterTargetBranch", encoding="utf-8") == branch
|
||
|
assert self.params.get_bool("UpdaterFetchAvailable") == fetch_available
|
||
|
assert self.params.get_bool("UpdateAvailable") == update_available
|
||
1 year ago
|
|
||
1 year ago
|
def wait_for_idle(self):
|
||
|
self.wait_for_condition(lambda: self.params.get("UpdaterState", encoding="utf-8") == "idle")
|
||
|
|
||
1 year ago
|
def wait_for_failed(self):
|
||
|
self.wait_for_condition(lambda: self.params.get("UpdateFailedCount", encoding="utf-8") is not None and \
|
||
|
int(self.params.get("UpdateFailedCount", encoding="utf-8")) > 0)
|
||
|
|
||
1 year ago
|
def wait_for_fetch_available(self):
|
||
|
self.wait_for_condition(lambda: self.params.get_bool("UpdaterFetchAvailable"))
|
||
|
|
||
|
def wait_for_update_available(self):
|
||
|
self.wait_for_condition(lambda: self.params.get_bool("UpdateAvailable"))
|
||
|
|
||
1 year ago
|
def test_no_update(self):
|
||
|
# Start on release3, ensure we don't fetch any updates
|
||
|
self.setup_remote_release("release3")
|
||
|
self.setup_basedir_release("release3")
|
||
|
|
||
1 year ago
|
with self.additional_context(), processes_context(["updated"]) as [updated]:
|
||
1 year ago
|
self._test_params("release3", False, False)
|
||
1 year ago
|
self.wait_for_idle()
|
||
1 year ago
|
self._test_params("release3", False, False)
|
||
|
|
||
|
self.send_check_for_updates_signal(updated)
|
||
|
|
||
|
self.wait_for_idle()
|
||
|
|
||
|
self._test_params("release3", False, False)
|
||
|
|
||
1 year ago
|
def test_new_release(self):
|
||
|
# Start on release3, simulate a release3 commit, ensure we fetch that update properly
|
||
|
self.setup_remote_release("release3")
|
||
|
self.setup_basedir_release("release3")
|
||
|
|
||
1 year ago
|
with self.additional_context(), processes_context(["updated"]) as [updated]:
|
||
1 year ago
|
self._test_params("release3", False, False)
|
||
1 year ago
|
self.wait_for_idle()
|
||
1 year ago
|
self._test_params("release3", False, False)
|
||
|
|
||
1 year ago
|
self.MOCK_RELEASES["release3"] = ("0.1.3", "1.2", "0.1.3 release notes")
|
||
1 year ago
|
self.update_remote_release("release3")
|
||
|
|
||
|
self.send_check_for_updates_signal(updated)
|
||
|
|
||
1 year ago
|
self.wait_for_fetch_available()
|
||
1 year ago
|
|
||
|
self._test_params("release3", True, False)
|
||
|
|
||
|
self.send_download_signal(updated)
|
||
|
|
||
1 year ago
|
self.wait_for_update_available()
|
||
1 year ago
|
|
||
|
self._test_params("release3", False, True)
|
||
1 year ago
|
self._test_finalized_update("release3", *self.MOCK_RELEASES["release3"])
|
||
1 year ago
|
|
||
|
def test_switch_branches(self):
|
||
|
# Start on release3, request to switch to master manually, ensure we switched
|
||
|
self.setup_remote_release("release3")
|
||
|
self.setup_remote_release("master")
|
||
|
self.setup_basedir_release("release3")
|
||
|
|
||
1 year ago
|
with self.additional_context(), processes_context(["updated"]) as [updated]:
|
||
1 year ago
|
self._test_params("release3", False, False)
|
||
|
self.wait_for_idle()
|
||
|
self._test_params("release3", False, False)
|
||
|
|
||
|
self.set_target_branch("master")
|
||
|
self.send_check_for_updates_signal(updated)
|
||
|
|
||
1 year ago
|
self.wait_for_fetch_available()
|
||
1 year ago
|
|
||
|
self._test_params("master", True, False)
|
||
|
|
||
|
self.send_download_signal(updated)
|
||
|
|
||
1 year ago
|
self.wait_for_update_available()
|
||
1 year ago
|
|
||
|
self._test_params("master", False, True)
|
||
1 year ago
|
self._test_finalized_update("master", *self.MOCK_RELEASES["master"])
|
||
1 year ago
|
|
||
11 months ago
|
def test_agnos_update(self, mocker):
|
||
1 year ago
|
# Start on release3, push an update with an agnos change
|
||
|
self.setup_remote_release("release3")
|
||
|
self.setup_basedir_release("release3")
|
||
|
|
||
11 months ago
|
with self.additional_context(), processes_context(["updated"]) as [updated]:
|
||
|
mocker.patch("openpilot.system.hardware.AGNOS", "True")
|
||
|
mocker.patch("openpilot.system.hardware.tici.hardware.Tici.get_os_version", "1.2")
|
||
|
mocker.patch("openpilot.system.hardware.tici.agnos.get_target_slot_number")
|
||
|
mocker.patch("openpilot.system.hardware.tici.agnos.flash_agnos_update")
|
||
1 year ago
|
|
||
|
self._test_params("release3", False, False)
|
||
1 year ago
|
self.wait_for_idle()
|
||
1 year ago
|
self._test_params("release3", False, False)
|
||
|
|
||
|
self.MOCK_RELEASES["release3"] = ("0.1.3", "1.3", "0.1.3 release notes")
|
||
|
self.update_remote_release("release3")
|
||
|
|
||
|
self.send_check_for_updates_signal(updated)
|
||
|
|
||
1 year ago
|
self.wait_for_fetch_available()
|
||
1 year ago
|
|
||
|
self._test_params("release3", True, False)
|
||
|
|
||
|
self.send_download_signal(updated)
|
||
|
|
||
1 year ago
|
self.wait_for_update_available()
|
||
1 year ago
|
|
||
|
self._test_params("release3", False, True)
|
||
1 year ago
|
self._test_finalized_update("release3", *self.MOCK_RELEASES["release3"])
|