async def test_get_s2(self): s1 = MockScript() sp1 = SingleScriptProvider(s1) s2 = MockScript() sp2 = SingleScriptProvider(s2) switched = SwitchedScriptProvider(sp1=sp1, sp2=sp2) async with switched: self.assertIs(await switched.get(type="sp2"), s2)
async def test_switch_flag_changed(self): s1 = MockScript() sp1 = SingleScriptProvider(s1) s2 = MockScript() sp2 = SingleScriptProvider(s2) switched = SwitchedScriptProvider(sp1=sp1, sp2=sp2, _switch="test") async with switched: self.assertIs(await switched.get(test="sp2"), s2)
async def test_fail_unknown_type(self): s1 = MockScript() sp1 = SingleScriptProvider(s1) s2 = MockScript() sp2 = SingleScriptProvider(s2) switched = SwitchedScriptProvider(sp1=sp1, sp2=sp2) async with switched: self.assertIsNone(await switched.get(type="sp_unknown"))
async def test_non_owned(self): script = MockScript() single = SingleScriptProvider(script) async with script: async with single: pass self.assertTrue(script.acquired)
async def test_pass_config_down(self): ms = MockScript({"test": str(id(self))}) async with ms: ls = LocalScript(ms) async with ls: self.assertEqual(str(id(self)), await wait_for(ls.get_config("test"), 5))
async def test_remote_clip_map(self): c_client, c_server = pipe_bidi() rs = MockScript({}) rss = RemoteScriptServer(rs, c_server) rsc = RemoteScript(c_client) async with timeout_context(rss, 10), timeout_context(rsc, 10): clips = await wait_for(rsc.retrieve_clips(), 10) self.assertEqual(len(clips), 1) self.assertEqual(list(clips), ["test"])
async def test_cd(self): ms = MockScript() async with ms: ls = LocalScript(ms) async with ls: with retain_cd_after() as cd: self.assertEqual( cd, await wait_for(ls.get_config("subprocess.cd"), 5)) d = tempfile.mkdtemp() await wait_for(ls.set_config("subprocess.cd", str(d)), 5) self.assertEqual(os.getcwd(), d)
async def test_ro_pid(self): ms = MockScript() async with ms: ls = LocalScript(ms) async with ls: self.assertEqual( os.getpid(), await wait_for(ls.get_config("subprocess.pid"), 5)) await wait_for(ls.set_config("subprocess.pid", 123), 5) self.assertEqual( os.getpid(), await wait_for(ls.get_config("subprocess.pid"), 5))
async def test_get_script(self): c_client, c_server = pipe_bidi() ms = MockScript({"id": id(self)}) sp = SingleScriptProvider(ms) rsps = RemoteScriptProviderServer(sp, c_server) rspc = RemoteScriptProvider(c_client) async with timeout_context(rsps, 10), timeout_context(rspc, 10): rsc = await rspc.get() async with timeout_context(rsc, 10): self.assertEqual(await wait_for(rsc.get_config("id"), 10), id(self))
async def test_remote_script_run(self): c_client, c_server = pipe_bidi() rs = MockScript({}) rss = RemoteScriptServer(rs, c_server) rsc = RemoteScript(c_client) async with timeout_context(rss, 10), timeout_context(rsc, 10): await wait_for(rsc.run("test-code-string"), 10) self.assertEqual( await wait_for(rsc.get_config("last-command"), 10), "test-code-string") await wait_for(rsc.run(b"test-code-binary"), 10) self.assertEqual( await wait_for(rsc.get_config("last-command"), 10), b"test-code-binary")
async def test_remote_script_config(self): c_client, c_server = pipe_bidi() rs = MockScript({ "test-int": 1, "test-bytes": b"abc", "test-null": None, "test-string": "foo", "test-float": 3.1415926 }) unset = object() rss = RemoteScriptServer(rs, c_server) rsc = RemoteScript(c_client) async with timeout_context(rss, 10), timeout_context(rsc, 10): self.assertEqual(await wait_for(rsc.list_config(), 10), [ "test-int", "test-bytes", "test-null", "test-string", "test-float" ]) self.assertEqual(await wait_for(rsc.get_config("test-int"), 10), 1) self.assertEqual(await wait_for(rsc.get_config("test-bytes"), 10), b"abc") self.assertIsNone(await wait_for(rsc.get_config("test-null"), 10)) self.assertEqual(await wait_for(rsc.get_config("test-float"), 10), 3.1415926) self.assertEqual(await wait_for(rsc.get_config("test-string"), 10), "foo") with self.assertRaises(KeyError): await wait_for(rsc.get_config("test-unset"), 10) self.assertIs( await wait_for(rsc.get_config("test-unset", unset), 10), unset) for val in [2, b"def", None, 6.2831852, "bar"]: await wait_for(rsc.set_config("test-set", val), 10) self.assertEqual(await wait_for(rsc.list_config(), 10), [ "test-int", "test-bytes", "test-null", "test-string", "test-float", "test-set" ]) self.assertEqual( await wait_for(rsc.get_config("test-set"), 10), val)
async def test_env(self): ms = MockScript() async with ms: ls = LocalScript(ms) async with ls: raw_env = os.environ.keys() env = ["subprocess.env." + k for k in raw_env] values = [ f for f in (await wait_for(ls.list_config(), 5)) if f.startswith("subprocess.env.") ] self.assertEqual(env, values) os.environ["test"] = "abc" self.assertEqual( "abc", await wait_for(ls.get_config("subprocess.env.test"), 5)) await wait_for(ls.set_config("subprocess.env.test", "def"), 5) self.assertEqual(os.environ["test"], "def") await wait_for(ls.set_config("subprocess.env.test", None), 5) self.assertNotIn("test", os.environ)
async def test_always_same(self): script = MockScript() single = SingleScriptProvider(script) async with single: self.assertIs(script, await single.get()) self.assertIs(script, await single.get())
async def test_owner(self): script = MockScript() single = SingleScriptProvider(script) async with single: self.assertTrue(script.acquired) self.assertFalse(script.acquired)
import asyncio import os import sys from asyncio import ProactorEventLoop, wait_for from aiounittest import AsyncTestCase from yuuno2.providers.remote.client import RemoteScript from yuuno2.providers.single import SingleScriptProvider from yuuno2.tests.mocks import MockScript from yuuno2server.subprocesses import SubprocessScript MOCK_PROVIDER = lambda: SingleScriptProvider( MockScript({"test": f"{os.getppid()}"})) if os.name == 'posix': def pid_exists(pid): """Check whether pid exists in the current process table.""" import errno if pid < 0: return False try: os.kill(pid, 0) except OSError as e: return e.errno == errno.EPERM else: return True else: def pid_exists(pid):