pmb.chroot.init: verify chroot channel (MR 1912)
Refuse to work with a chroot that was created for another release channel.
This commit is contained in:
parent
ce433ae012
commit
3ff0b18a08
|
@ -53,6 +53,7 @@ def init(args, suffix="native"):
|
||||||
pmb.chroot.mount(args, suffix)
|
pmb.chroot.mount(args, suffix)
|
||||||
setup_qemu_emulation(args, suffix)
|
setup_qemu_emulation(args, suffix)
|
||||||
if os.path.islink(chroot + "/bin/sh"):
|
if os.path.islink(chroot + "/bin/sh"):
|
||||||
|
pmb.config.workdir.chroot_check_channel(args, suffix)
|
||||||
copy_resolv_conf(args, suffix)
|
copy_resolv_conf(args, suffix)
|
||||||
pmb.chroot.apk.update_repository_list(args, suffix)
|
pmb.chroot.apk.update_repository_list(args, suffix)
|
||||||
return
|
return
|
||||||
|
@ -74,7 +75,7 @@ def init(args, suffix="native"):
|
||||||
copy_resolv_conf(args, suffix)
|
copy_resolv_conf(args, suffix)
|
||||||
pmb.chroot.apk.update_repository_list(args, suffix)
|
pmb.chroot.apk.update_repository_list(args, suffix)
|
||||||
|
|
||||||
pmb.config.workdir.chroot_save_date(args, suffix)
|
pmb.config.workdir.chroot_save_init(args, suffix)
|
||||||
|
|
||||||
# Install alpine-base
|
# Install alpine-base
|
||||||
pmb.helpers.repo.update(args, arch)
|
pmb.helpers.repo.update(args, arch)
|
||||||
|
|
|
@ -9,21 +9,26 @@ import os
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import pmb.config
|
import pmb.config
|
||||||
|
import pmb.config.pmaports
|
||||||
|
|
||||||
|
|
||||||
def chroot_save_date(args, suffix):
|
def chroot_save_init(args, suffix):
|
||||||
""" Save the chroot initialization date in $WORK/workdir.cfg. """
|
""" Save the chroot initialization data in $WORK/workdir.cfg. """
|
||||||
# Read existing cfg
|
# Read existing cfg
|
||||||
cfg = configparser.ConfigParser()
|
cfg = configparser.ConfigParser()
|
||||||
path = args.work + "/workdir.cfg"
|
path = args.work + "/workdir.cfg"
|
||||||
if os.path.isfile(path):
|
if os.path.isfile(path):
|
||||||
cfg.read(path)
|
cfg.read(path)
|
||||||
|
|
||||||
# Set current date for chroot suffix
|
# Create sections
|
||||||
key = "chroot-init-dates"
|
for key in ["chroot-init-dates", "chroot-channels"]:
|
||||||
if key not in cfg:
|
if key not in cfg:
|
||||||
cfg[key] = {}
|
cfg[key] = {}
|
||||||
cfg[key][suffix] = str(int(time.time()))
|
|
||||||
|
# Update sections
|
||||||
|
channel = pmb.config.pmaports.read_config(args)["channel"]
|
||||||
|
cfg["chroot-channels"][suffix] = channel
|
||||||
|
cfg["chroot-init-dates"][suffix] = str(int(time.time()))
|
||||||
|
|
||||||
# Write back
|
# Write back
|
||||||
with open(path, "w") as handle:
|
with open(path, "w") as handle:
|
||||||
|
@ -54,6 +59,28 @@ def chroots_outdated(args):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def chroot_check_channel(args, suffix):
|
||||||
|
path = args.work + "/workdir.cfg"
|
||||||
|
msg_again = "Run 'pmbootstrap zap' to delete your chroots and try again."
|
||||||
|
msg_unknown = ("Could not figure out on which release channel the"
|
||||||
|
f" '{suffix}' chroot is.")
|
||||||
|
if not os.path.exists(path):
|
||||||
|
raise RuntimeError(f"{msg_unknown} {msg_again}")
|
||||||
|
|
||||||
|
cfg = configparser.ConfigParser()
|
||||||
|
cfg.read(path)
|
||||||
|
key = "chroot-channels"
|
||||||
|
if key not in cfg or suffix not in cfg[key]:
|
||||||
|
raise RuntimeError(f"{msg_unknown} {msg_again}")
|
||||||
|
|
||||||
|
channel = pmb.config.pmaports.read_config(args)["channel"]
|
||||||
|
channel_cfg = cfg[key][suffix]
|
||||||
|
if channel != channel_cfg:
|
||||||
|
raise RuntimeError(f"Chroot '{suffix}' was created for the"
|
||||||
|
f" '{channel_cfg}' channel, but you are on the"
|
||||||
|
f" '{channel}' channel now. {msg_again}")
|
||||||
|
|
||||||
|
|
||||||
def clean(args):
|
def clean(args):
|
||||||
""" Remove obsolete data data from workdir.cfg.
|
""" Remove obsolete data data from workdir.cfg.
|
||||||
:returns: None if workdir does not exist,
|
:returns: None if workdir does not exist,
|
||||||
|
@ -69,9 +96,10 @@ def clean(args):
|
||||||
cfg.read(path)
|
cfg.read(path)
|
||||||
|
|
||||||
# Remove entries for deleted chroots
|
# Remove entries for deleted chroots
|
||||||
key = "chroot-init-dates"
|
|
||||||
changed = False
|
changed = False
|
||||||
if key in cfg:
|
for key in ["chroot-init-dates", "chroot-channels"]:
|
||||||
|
if key not in cfg:
|
||||||
|
continue
|
||||||
for suffix in cfg[key]:
|
for suffix in cfg[key]:
|
||||||
path_suffix = args.work + "/chroot_" + suffix
|
path_suffix = args.work + "/chroot_" + suffix
|
||||||
if os.path.exists(path_suffix):
|
if os.path.exists(path_suffix):
|
||||||
|
|
|
@ -8,6 +8,7 @@ import time
|
||||||
|
|
||||||
import pmb_test # noqa
|
import pmb_test # noqa
|
||||||
import pmb.config
|
import pmb.config
|
||||||
|
import pmb.config.pmaports
|
||||||
import pmb.config.workdir
|
import pmb.config.workdir
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,24 +23,36 @@ def args(request):
|
||||||
return args
|
return args
|
||||||
|
|
||||||
|
|
||||||
def test_chroot_save_date(args, tmpdir, monkeypatch):
|
def test_chroot_save_init(args, tmpdir, monkeypatch):
|
||||||
# Override time.time()
|
# Override time.time()
|
||||||
def fake_time():
|
def fake_time():
|
||||||
return 1234567890.1234
|
return 1234567890.1234
|
||||||
monkeypatch.setattr(time, "time", fake_time)
|
monkeypatch.setattr(time, "time", fake_time)
|
||||||
|
|
||||||
|
# Pretend channel=stable in pmaports.cfg
|
||||||
|
def read_config(args):
|
||||||
|
return {"channel": "stable"}
|
||||||
|
monkeypatch.setattr(pmb.config.pmaports, "read_config", read_config)
|
||||||
|
|
||||||
args.work = str(tmpdir)
|
args.work = str(tmpdir)
|
||||||
func = pmb.config.workdir.chroot_save_date
|
func = pmb.config.workdir.chroot_save_init
|
||||||
func(args, "native")
|
func(args, "native")
|
||||||
|
|
||||||
expected = "[chroot-init-dates]\nnative = 1234567890\n\n"
|
expected = ("[chroot-init-dates]\n"
|
||||||
|
"native = 1234567890\n\n"
|
||||||
|
"[chroot-channels]\n"
|
||||||
|
"native = stable\n\n")
|
||||||
with open(args.work + "/workdir.cfg", "r") as handle:
|
with open(args.work + "/workdir.cfg", "r") as handle:
|
||||||
assert handle.read() == expected
|
assert handle.read() == expected
|
||||||
|
|
||||||
# Write again (different code path)
|
# Write again (different code path)
|
||||||
func(args, "buildroot_armhf")
|
func(args, "buildroot_armhf")
|
||||||
expected = ("[chroot-init-dates]\nnative = 1234567890\n"
|
expected = ("[chroot-init-dates]\n"
|
||||||
"buildroot_armhf = 1234567890\n\n")
|
"native = 1234567890\n"
|
||||||
|
"buildroot_armhf = 1234567890\n\n"
|
||||||
|
"[chroot-channels]\n"
|
||||||
|
"native = stable\n"
|
||||||
|
"buildroot_armhf = stable\n\n")
|
||||||
with open(args.work + "/workdir.cfg", "r") as handle:
|
with open(args.work + "/workdir.cfg", "r") as handle:
|
||||||
assert handle.read() == expected
|
assert handle.read() == expected
|
||||||
|
|
||||||
|
@ -74,6 +87,41 @@ def test_chroots_outdated(args, tmpdir, monkeypatch):
|
||||||
assert func(args) is False
|
assert func(args) is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_chroot_check_channel(args, tmpdir, monkeypatch):
|
||||||
|
func = pmb.config.workdir.chroot_check_channel
|
||||||
|
args.work = str(tmpdir)
|
||||||
|
channel = "edge"
|
||||||
|
|
||||||
|
# Pretend to have a certain channel in pmaports.cfg
|
||||||
|
def read_config(args):
|
||||||
|
return {"channel": channel}
|
||||||
|
monkeypatch.setattr(pmb.config.pmaports, "read_config", read_config)
|
||||||
|
|
||||||
|
# workdir.cfg does not exist
|
||||||
|
with pytest.raises(RuntimeError) as e:
|
||||||
|
func(args, "native")
|
||||||
|
assert "Could not figure out on which release channel" in str(e.value)
|
||||||
|
|
||||||
|
# Write workdir.cfg
|
||||||
|
with open(f"{args.work}/workdir.cfg", "w") as handle:
|
||||||
|
handle.write("[chroot-channels]\nnative = stable\n\n")
|
||||||
|
|
||||||
|
# workdir.cfg: no entry for buildroot_armhf chroot
|
||||||
|
with pytest.raises(RuntimeError) as e:
|
||||||
|
func(args, "buildroot_armhf")
|
||||||
|
assert "Could not figure out on which release channel" in str(e.value)
|
||||||
|
|
||||||
|
# Chroot was created for wrong channel
|
||||||
|
with pytest.raises(RuntimeError) as e:
|
||||||
|
func(args, "native")
|
||||||
|
exp = "created for the 'stable' channel, but you are on the 'edge'"
|
||||||
|
assert exp in str(e.value)
|
||||||
|
|
||||||
|
# Check runs through without raising an exception
|
||||||
|
channel = "stable"
|
||||||
|
func(args, "native")
|
||||||
|
|
||||||
|
|
||||||
def test_clean(args, tmpdir):
|
def test_clean(args, tmpdir):
|
||||||
args.work = str(tmpdir)
|
args.work = str(tmpdir)
|
||||||
|
|
||||||
|
|
|
@ -3,9 +3,11 @@
|
||||||
import glob
|
import glob
|
||||||
import os
|
import os
|
||||||
import pytest
|
import pytest
|
||||||
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import pmb_test # noqa
|
import pmb_test # noqa
|
||||||
|
import pmb_test.const
|
||||||
import pmb.build.newapkbuild
|
import pmb.build.newapkbuild
|
||||||
import pmb.config
|
import pmb.config
|
||||||
import pmb.config.init
|
import pmb.config.init
|
||||||
|
@ -15,7 +17,8 @@ import pmb.helpers.logging
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def args(tmpdir, request):
|
def args(tmpdir, request):
|
||||||
import pmb.parse
|
import pmb.parse
|
||||||
sys.argv = ["pmbootstrap.py", "init"]
|
cfg = f"{pmb_test.const.testdata}/channels.cfg"
|
||||||
|
sys.argv = ["pmbootstrap.py", "--config-channels", cfg, "init"]
|
||||||
args = pmb.parse.arguments()
|
args = pmb.parse.arguments()
|
||||||
args.log = args.work + "/log_testsuite.txt"
|
args.log = args.work + "/log_testsuite.txt"
|
||||||
pmb.helpers.logging.init(args)
|
pmb.helpers.logging.init(args)
|
||||||
|
@ -24,6 +27,8 @@ def args(tmpdir, request):
|
||||||
|
|
||||||
|
|
||||||
def test_newapkbuild(args, monkeypatch, tmpdir):
|
def test_newapkbuild(args, monkeypatch, tmpdir):
|
||||||
|
testdata = pmb_test.const.testdata
|
||||||
|
|
||||||
# Fake functions
|
# Fake functions
|
||||||
def confirm_true(*nargs):
|
def confirm_true(*nargs):
|
||||||
return True
|
return True
|
||||||
|
@ -35,11 +40,12 @@ def test_newapkbuild(args, monkeypatch, tmpdir):
|
||||||
monkeypatch.setattr(pmb.helpers.cli, "confirm", confirm_false)
|
monkeypatch.setattr(pmb.helpers.cli, "confirm", confirm_false)
|
||||||
pmb.build.init(args)
|
pmb.build.init(args)
|
||||||
args.aports = tmpdir = str(tmpdir)
|
args.aports = tmpdir = str(tmpdir)
|
||||||
|
shutil.copy(f"{testdata}/pmaports.cfg", args.aports)
|
||||||
func = pmb.build.newapkbuild
|
func = pmb.build.newapkbuild
|
||||||
|
|
||||||
# Show the help
|
# Show the help
|
||||||
func(args, "main", ["-h"])
|
func(args, "main", ["-h"])
|
||||||
assert glob.glob(tmpdir + "/*") == []
|
assert glob.glob(f"{tmpdir}/*") == [f"{tmpdir}/pmaports.cfg"]
|
||||||
|
|
||||||
# Test package
|
# Test package
|
||||||
pkgname = "testpackage"
|
pkgname = "testpackage"
|
||||||
|
|
Loading…
Reference in New Issue