Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1acbdec6c1 |
No files matched your search
@@ -61,6 +61,35 @@ def content_binds(name, meta, env):
|
||||
return out
|
||||
|
||||
|
||||
def outside(path, mounted):
|
||||
for source, target in sorted(mounted, key=lambda m: len(m[1]), reverse=True):
|
||||
if path == target or path.startswith(target + "/"):
|
||||
return source + path[len(target):]
|
||||
return path
|
||||
|
||||
|
||||
def layout_args(meta, env, mounted):
|
||||
out = []
|
||||
writable = tuple(env[k] + "/" for k in ("SNAP_DATA", "SNAP_COMMON"))
|
||||
layout = [(p, e) for p, e in (meta.get("layout") or {}).items() if isinstance(e, dict)]
|
||||
for path, entry in sorted(layout, key=lambda item: ("symlink" in item[1], item[0])):
|
||||
dest = os.path.normpath(expand(path, env))
|
||||
if "symlink" in entry:
|
||||
out += ["--symlink", os.path.normpath(expand(entry["symlink"], env)), dest]
|
||||
elif entry.get("type") == "tmpfs":
|
||||
out += ["--tmpfs", dest]
|
||||
elif "bind" in entry or "bind-file" in entry:
|
||||
source = os.path.normpath(expand(entry.get("bind") or entry.get("bind-file"), env))
|
||||
if source.startswith(writable) and not os.path.exists(source):
|
||||
if "bind" in entry:
|
||||
os.makedirs(source, exist_ok=True)
|
||||
else:
|
||||
os.makedirs(os.path.dirname(source), exist_ok=True)
|
||||
open(source, "a").close()
|
||||
out += ["--bind", outside(source, mounted), dest]
|
||||
return out
|
||||
|
||||
|
||||
def game_gpu(env, snap):
|
||||
try:
|
||||
game_env = runpy.run_path(os.path.join(BIN_DIR, "droiddeck-game-env"))
|
||||
@@ -133,9 +162,10 @@ def main(argv):
|
||||
bwrap += ["--bind", "/", "/var/lib/snapd/hostfs"]
|
||||
bwrap += ["--bind", "/dev/null", "/etc/ld.so.preload"]
|
||||
binds = content_binds(name, meta, env)
|
||||
for _, source, target in binds:
|
||||
if os.path.isdir(source):
|
||||
bwrap += ["--bind", source, target]
|
||||
mounted = [(source, target) for _, source, target in binds if os.path.isdir(source)]
|
||||
for source, target in mounted:
|
||||
bwrap += ["--bind", source, target]
|
||||
bwrap += layout_args(meta, env, mounted)
|
||||
content = {k for k, v in (meta.get("plugs") or {}).items() if isinstance(v, dict) and v.get("interface") == "content"}
|
||||
plugs = (set(meta.get("plugs") or {}) | set(spec.get("plugs") or [])) - content | {plug for plug, _, _ in binds}
|
||||
env["DROIDDECK_SNAP_PLUGS"] = " ".join(sorted(plugs))
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
import json
|
||||
import os
|
||||
import runpy
|
||||
import shutil
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin"
|
||||
WEBKIT = "/usr/lib/aarch64-linux-gnu/webkit2gtk-4.1"
|
||||
|
||||
|
||||
class SnapLayoutTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
try:
|
||||
self.snap_run = runpy.run_path(str(BIN / "droiddeck-snap-run"))
|
||||
except ImportError:
|
||||
self.skipTest("PyYAML is not installed")
|
||||
self.tmp = Path(tempfile.mkdtemp())
|
||||
self.addCleanup(shutil.rmtree, self.tmp, True)
|
||||
self.snaps = self.tmp / "snap"
|
||||
self.snap_run["main"].__globals__["SNAP_DIR"] = str(self.snaps)
|
||||
provider = self.snaps / "gnome-core26/36"
|
||||
(provider / "meta").mkdir(parents=True)
|
||||
(provider / "meta/snap.yaml").write_text(json.dumps(
|
||||
{"slots": {"gnome-core26": {"interface": "content", "read": ["/"]}}}))
|
||||
os.symlink("36", self.snaps / "gnome-core26/current")
|
||||
self.snap = str(self.snaps / "app/12")
|
||||
self.env = {"SNAP": self.snap, "SNAP_DATA": str(self.tmp / "data/12"),
|
||||
"SNAP_COMMON": str(self.tmp / "data/common")}
|
||||
|
||||
def args(self, meta):
|
||||
binds = self.snap_run["content_binds"]("app", meta, self.env)
|
||||
mounted = [(source, target) for _, source, target in binds]
|
||||
return self.snap_run["layout_args"](meta, self.env, mounted)
|
||||
|
||||
def test_a_layout_into_a_content_snap_binds_the_provider_directory(self):
|
||||
meta = {
|
||||
"plugs": {"gnome-core26": {"interface": "content", "target": "$SNAP/gnome-platform",
|
||||
"default-provider": "gnome-core26"}},
|
||||
"layout": {WEBKIT: {"bind": "$SNAP/gnome-platform" + WEBKIT}},
|
||||
}
|
||||
self.assertEqual(["--bind", str(self.snaps / "gnome-core26/36") + WEBKIT, WEBKIT], self.args(meta))
|
||||
|
||||
def test_every_layout_kind_is_mounted_and_links_come_last(self):
|
||||
meta = {"layout": {
|
||||
"/etc/app.conf": {"bind-file": "$SNAP_DATA/etc/app.conf"},
|
||||
"/usr/share/app": {"symlink": "$SNAP/usr/share/app"},
|
||||
"/var/cache/app": {"type": "tmpfs"},
|
||||
"/var/lib/app": {"bind": "$SNAP_COMMON/lib"},
|
||||
}}
|
||||
data, common = self.env["SNAP_DATA"], self.env["SNAP_COMMON"]
|
||||
self.assertEqual([
|
||||
"--bind", data + "/etc/app.conf", "/etc/app.conf",
|
||||
"--tmpfs", "/var/cache/app",
|
||||
"--bind", common + "/lib", "/var/lib/app",
|
||||
"--symlink", self.snap + "/usr/share/app", "/usr/share/app",
|
||||
], self.args(meta))
|
||||
self.assertTrue(os.path.isfile(data + "/etc/app.conf"))
|
||||
self.assertTrue(os.path.isdir(common + "/lib"))
|
||||
|
||||
def test_a_snap_without_a_layout_gets_no_extra_mounts(self):
|
||||
self.assertEqual([], self.args({"plugs": {"home": None}}))
|
||||
self.assertEqual([], self.args({"layout": {"/opt/x": "not a mapping"}}))
|
||||
|
||||
def test_a_read_only_source_that_is_missing_is_not_created(self):
|
||||
self.assertEqual(["--bind", self.snap + "/opt/x", "/opt/x"],
|
||||
self.args({"layout": {"/opt/x": {"bind": "$SNAP/opt/x"}}}))
|
||||
self.assertFalse(os.path.exists(self.snap))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user