From 498d2203145ffe91413a607991f339b9f8201d28 Mon Sep 17 00:00:00 2001 From: Dan Bungert Date: Fri, 27 May 2022 12:54:10 -0600 Subject: [PATCH] test/api: add MachineConfig class Make it easier to edit machine configs on the fly for test. --- subiquity/tests/api/test_api.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/subiquity/tests/api/test_api.py b/subiquity/tests/api/test_api.py index 4ef627b5..a47dc411 100755 --- a/subiquity/tests/api/test_api.py +++ b/subiquity/tests/api/test_api.py @@ -135,6 +135,27 @@ class Server(Client): class TestAPI(unittest.IsolatedAsyncioTestCase, SubiTestCase): + class _MachineConfig(os.PathLike): + def __init__(self, outer, path): + self.outer = outer + self.orig_path = path + self.path = None + + def __fspath__(self): + return self.path or self.orig_path + + @contextlib.contextmanager + def edit(self): + with open(self.orig_path, 'r') as fp: + data = json.load(fp) + yield data + self.path = self.outer.tmp_path('machine-config.json') + with open(self.path, 'w') as fp: + json.dump(data, fp) + + def machineConfig(self, path): + return self._MachineConfig(self, path) + def assertDictSubset(self, expected, actual): """All keys in dictionary expected, and matching values, must match keys and values in actual. Actual may contain additional keys and