def test_make_wrapper_script0001(self, mock_platform): args = ["arg1", "arg2", "arg3"] heap_lim_k = 1024 * 1024 * 1024 # 1GiB stack_lim_k = 8192 dash = find_executable("dash") assert dash is not None vmdef = PythonVMDef("python2.7") vmdef.set_platform(mock_platform) wrapper_filename, envlog_filename = vmdef.make_wrapper_script( args, heap_lim_k, stack_lim_k) expect = [ '#!%s' % dash, 'ENVLOG=`env`', 'ulimit -d %s || exit $?' % heap_lim_k, 'ulimit -s %s || exit $?' % stack_lim_k, 'arg1 arg2 arg3', 'echo "${ENVLOG}" > %s' % envlog_filename, 'exit $?' ] with open(wrapper_filename) as fh: got = fh.read().splitlines() util.del_envlog_tempfile(envlog_filename, mock_platform) assert expect == got
def test_run_exec_popen0002(self, monkeypatch): """Check that writing stderr to a file works. Used for instrumentation""" config = Config() platform = MockPlatform(None, config) vm_def = PythonVMDef('/dummy/bin/python') vm_def.set_platform(platform) args = [sys.executable, "-c", "import sys; sys.stdout.write('STDOUT'); sys.stderr.write('STDERR')"] with NamedTemporaryFile(delete=False, prefix="kruntest") as fh: filename = fh.name out, err, rv = vm_def._run_exec_popen(args, fh) assert err == "" # not here due to redirection assert out == "STDOUT" # behaviour should be unchanged assert rv == 0 # stderr should be in this file with open(filename) as fh: assert fh.read() == "STDERR" fh.close() os.unlink(filename)
def test_run_exec_popen0002(self, monkeypatch): """Check that writing stderr to a file works. Used for instrumentation""" config = Config() platform = MockPlatform(None, config) vm_def = PythonVMDef('/dummy/bin/python') vm_def.set_platform(platform) args = [ sys.executable, "-c", "import sys; sys.stdout.write('STDOUT'); sys.stderr.write('STDERR')" ] with NamedTemporaryFile(delete=False, prefix="kruntest") as fh: filename = fh.name out, err, rv, timed_out = vm_def._run_exec_popen(args, fh) assert err == "" # not here due to redirection assert out == "STDOUT" # behaviour should be unchanged assert rv == 0 assert timed_out == False # stderr should be in this file with open(filename) as fh: assert fh.read() == "STDERR" fh.close() os.unlink(filename)
def test_wrapper_args0001(self, platform): vm_def = PythonVMDef('/dummy/bin/python') vm_def.set_platform(platform) wrapper_filename = "abcdefg.dash" got = vm_def._wrapper_args(wrapper_filename) expect = ['/usr/bin/sudo', '-u', 'root', '/usr/bin/nice', '-n', '-20', '/usr/bin/sudo', '-u', 'krun', '/bin/dash', wrapper_filename] assert got == expect
def test_wrapper_args0001(self, platform): vm_def = PythonVMDef('/dummy/bin/python') vm_def.set_platform(platform) wrapper_filename = "abcdefg.dash" got = vm_def._wrapper_args(wrapper_filename) expect = [ '/usr/bin/sudo', '-u', 'root', '/usr/bin/nice', '-n', '-20', '/usr/bin/sudo', '-u', 'krun', DASH, wrapper_filename ] assert got == expect
def test_wrapper_args0002(self, platform): # Pinning isn't supported on OpenBSD, so it should make no difference platform.config.ENABLE_PINNING = False vm_def = PythonVMDef('/dummy/bin/python') vm_def.set_platform(platform) wrapper_filename = "/tmp/abcdefg.dash" got = vm_def._wrapper_args(wrapper_filename) expect = ['/usr/local/bin/sudo', '-u', 'root', '/usr/bin/nice', '-n', '-20', '/usr/local/bin/sudo', '-u', 'krun', '/usr/local/bin/dash', wrapper_filename] assert got == expect
def test_wrapper_args0002(self, platform): platform.config.ENABLE_PINNING = False vm_def = PythonVMDef('/dummy/bin/python') vm_def.set_platform(platform) wrapper_filename = "abcdefg.dash" got = vm_def._wrapper_args(wrapper_filename) expect = [ '/usr/bin/sudo', '-u', 'root', '/usr/bin/nice', '-n', '-20', '/usr/bin/sudo', '-u', 'krun', '/bin/dash', wrapper_filename ] assert got == expect
def test_wrapper_args0002(self, platform): # Pinning isn't supported on OpenBSD, so it should make no difference platform.config.ENABLE_PINNING = False vm_def = PythonVMDef('/dummy/bin/python') vm_def.set_platform(platform) wrapper_filename = "/tmp/abcdefg.dash" got = vm_def._wrapper_args(wrapper_filename) expect = [ '/usr/local/bin/sudo', '-u', 'root', '/usr/bin/nice', '-n', '-20', '/usr/local/bin/sudo', '-u', 'krun', '/usr/local/bin/dash', wrapper_filename ] assert got == expect
def test_run_exec_popen0001(self, monkeypatch): """Check normal operation of _run_exec_popen()""" config = Config() platform = MockPlatform(None, config) vm_def = PythonVMDef('/dummy/bin/python') vm_def.set_platform(platform) args = [sys.executable, "-c", "import sys; sys.stdout.write('STDOUT'); sys.stderr.write('STDERR')"] out, err, rv = vm_def._run_exec_popen(args) assert err == "STDERR" assert out == "STDOUT" assert rv == 0
def test_sync_disks0002(self, monkeypatch): """We throw away the results from sanity checks, so there's no need to sync disks (and wait).""" stdout = json.dumps({ "wallclock_times": [123.4], "core_cycle_counts": [[1], [2], [3], [4]], "aperf_counts": [[5], [6], [7], [8]], "mperf_counts": [[9], [10], [11], [12]], }) config = Config() platform = MockPlatform(None, config) ep = EntryPoint("test") vm_def = PythonVMDef('/dummy/bin/python') sync_called = [False] def fake_sync_disks(): sync_called[0] = True monkeypatch.setattr(platform, "sync_disks", fake_sync_disks) def fake_run_exec_popen(args, stderr_file=None): return stdout, "", 0, False # stdout, stderr, exit_code, timed_out monkeypatch.setattr(vm_def, "_run_exec_popen", fake_run_exec_popen) util.spawn_sanity_check(platform, ep, vm_def, "test") assert sync_called == [False]
def test_run_exec_popen0001(self, monkeypatch): """Check normal operation of _run_exec_popen()""" config = Config() platform = MockPlatform(None, config) vm_def = PythonVMDef('/dummy/bin/python') vm_def.set_platform(platform) args = [ sys.executable, "-c", "import sys; sys.stdout.write('STDOUT'); sys.stderr.write('STDERR')" ] out, err, rv = vm_def._run_exec_popen(args) assert err == "STDERR" assert out == "STDOUT" assert rv == 0
def _sanity_check_user_change(self): from krun.vm_defs import PythonVMDef from krun import EntryPoint ep = EntryPoint("check_user_change.py") vd = PythonVMDef(sys.executable) # run under the VM that runs *this* util.spawn_sanity_check(self, ep, vd, "UNIX user change", force_dir=PLATFORM_SANITY_CHECK_DIR)
def test_env_ctor0001(self): vm = PythonVMDef("python2.7", env={"MYENV": "xyz"}) assert len(vm.common_env_changes) == 1 ec = vm.common_env_changes[0] assert ec.var == "MYENV" assert ec.val == "xyz"
def test_make_wrapper_script0001(self, mock_platform): args = ["arg1", "arg2", "arg3"] heap_lim_k = 1024 * 1024 * 1024 # 1GiB stack_lim_k = 8192 dash = find_executable("dash") assert dash is not None vmdef = PythonVMDef("python2.7") vmdef.set_platform(mock_platform) wrapper_filename, envlog_filename = vmdef.make_wrapper_script( args, heap_lim_k, stack_lim_k) expect = [ '#!%s' % dash, 'ENVLOG=`env`', 'ulimit -d %s || exit $?' % heap_lim_k, 'ulimit -s %s || exit $?' % stack_lim_k, 'arg1 arg2 arg3', 'echo "${ENVLOG}" > %s' % envlog_filename, 'exit $?' ] with open(wrapper_filename) as fh: got = fh.read().splitlines() util.del_envlog_tempfile(envlog_filename, mock_platform) assert expect == got
def test_sync_disks0002(self, monkeypatch): """We throw away the results from sanity checks, so there's no need to sync disks (and wait).""" platform = MockPlatform(None) ep = EntryPoint("test") vm_def = PythonVMDef('/dummy/bin//python') sync_called = [False] def fake_sync_disks(): sync_called[0] = True monkeypatch.setattr(platform, "sync_disks", fake_sync_disks) def fake_run_exec_popen(args): return "[1]", "", 0 # stdout, stderr, exit_code monkeypatch.setattr(vm_def, "_run_exec_popen", fake_run_exec_popen) util.spawn_sanity_check(platform, ep, vm_def, "test") assert sync_called == [False]
def test_sync_disks0001(self, monkeypatch): """Check disk sync method is called""" platform = MockPlatform(None) ep = EntryPoint("test") vm_def = PythonVMDef('/dummy/bin//python') vm_def.set_platform(platform) sync_called = [False] def fake_sync_disks(): sync_called[0] = True monkeypatch.setattr(platform, "sync_disks", fake_sync_disks) def fake_run_exec_popen(args): return "[1]", "", 0 # stdout, stderr, exit_code monkeypatch.setattr(vm_def, "_run_exec_popen", fake_run_exec_popen) vm_def.run_exec(ep, "test", 1, 1, 1, 1) assert sync_called == [True]
def test_sync_disks0001(self, monkeypatch): """Check disk sync method is called""" config = Config() platform = MockPlatform(None, config) ep = EntryPoint("test") vm_def = PythonVMDef('/dummy/bin/python') vm_def.set_platform(platform) sync_called = [False] def fake_sync_disks(): sync_called[0] = True monkeypatch.setattr(platform, "sync_disks", fake_sync_disks) def fake_run_exec_popen(args, stderr_file=None): return "[1]", "", 0, False # stdout, stderr, exit_code, timed_out monkeypatch.setattr(vm_def, "_run_exec_popen", fake_run_exec_popen) vm_def.run_exec(ep, 1, 1, 1, 1, "test:dummyvm:default", 0) assert sync_called == [True]