def test_create_ioerror(chown): chown.side_effect = IOError('Disk is borked') with temporary_dir() as d: ds = DirectorySandbox(d) with pytest.raises(DirectorySandbox.CreationError): ds.create()
def test_create_no_user(*args): with temporary_dir() as d: ds = DirectorySandbox(d) ds.create() assert os.path.exists(ds.root) for mocked in args: mocked.assert_not_called()
def test_create_ioerror(chown): chown.side_effect = IOError('Disk is borked') with temporary_dir() as d: real_path = os.path.join(d, 'sandbox') ds = DirectorySandbox(real_path) with pytest.raises(DirectorySandbox.CreationError): ds.create()
def test_user_does_not_exist(getpwnam): getpwnam.side_effect = KeyError('johndoe') with temporary_dir() as d: ds = DirectorySandbox(d, 'cletus') with pytest.raises(DirectorySandbox.CreationError): ds.create() getpwnam.assert_called_with('cletus')
def test_create_no_user(*args): with temporary_dir() as d: real_path = os.path.join(d, 'sandbox') ds = DirectorySandbox(real_path) ds.create() assert os.path.exists(real_path) for mocked in args: mocked.assert_not_called()
def test_destroy_ioerror(): with temporary_dir() as d: ds = DirectorySandbox(d) ds.create() with mock.patch('shutil.rmtree') as shutil_rmtree: shutil_rmtree.side_effect = IOError('What even are you doing?') with pytest.raises(DirectorySandbox.DeletionError): ds.destroy()
def test_user_does_not_exist(getpwnam): getpwnam.side_effect = KeyError("johndoe") with temporary_dir() as d: real_path = os.path.join(d, "sandbox") ds = DirectorySandbox(real_path, "cletus") with pytest.raises(DirectorySandbox.CreationError): ds.create() getpwnam.assert_called_with("cletus")
def test_destroy_ioerror(): with temporary_dir() as d: real_path = os.path.join(d, "sandbox") ds = DirectorySandbox(real_path) ds.create() with mock.patch("shutil.rmtree") as shutil_rmtree: shutil_rmtree.side_effect = IOError("What even are you doing?") with pytest.raises(DirectorySandbox.DeletionError): ds.destroy()
def test_directory_sandbox(): with temporary_dir() as d: ds1 = DirectorySandbox(os.path.join(d, 'task1')) ds2 = DirectorySandbox(os.path.join(d, 'task2')) ds1.create() ds2.create() assert os.path.exists(ds1.root) assert os.path.exists(ds2.root) ds1.destroy() assert not os.path.exists(ds1.root) assert os.path.exists(ds2.root) ds2.destroy() assert not os.path.exists(ds2.root)
def test_create(chmod, chown, getpwnam, getgrgid): getgrgid.return_value.gr_name = 'foo' getpwnam.return_value.pw_gid = 123 getpwnam.return_value.pw_uid = 456 with temporary_dir() as d: real_path = os.path.join(d, 'sandbox') ds = DirectorySandbox(real_path, 'cletus') ds.create() assert os.path.exists(real_path) getpwnam.assert_called_with('cletus') getgrgid.assert_called_with(123) chown.assert_called_with(real_path, 456, 123) chmod.assert_called_with(real_path, 0700)
def yield_runner(self, runner_class, portmap=None, clock=time, preserve_env=False, **bindings): with contextlib.nested(temporary_dir(), temporary_dir()) as (td1, td2): sandbox = DirectorySandbox(td1) checkpoint_root = td2 if not portmap: portmap = {} task_runner = runner_class( runner_pex=self.PEX_PATH, task_id='hello_world', task=TASK.bind(**bindings).task(), role=getpass.getuser(), portmap=portmap, clock=clock, sandbox=sandbox, checkpoint_root=checkpoint_root, preserve_env=preserve_env, ) yield task_runner
def test_sandbox_root_path(): with temporary_dir() as d: mesos_dir = os.path.join(d, 'task1') ds = DirectorySandbox(mesos_dir) assert os.path.join(mesos_dir, DirectorySandbox.SANDBOX_NAME) == ds.root
def test_create(chmod, chown, getpwnam, getgrgid): getgrgid.return_value.gr_name = 'foo' getpwnam.return_value.pw_gid = 123 getpwnam.return_value.pw_uid = 456 with temporary_dir() as mesos_dir: ds = DirectorySandbox(mesos_dir, 'cletus') ds.create() assert os.path.exists(ds.root) getpwnam.assert_called_with('cletus') getgrgid.assert_called_with(123) chown.assert_any_call(mesos_dir, 456, 123) chown.assert_any_call(ds.root, 456, 123) chmod.assert_called_with(ds.root, 0700)
def yield_runner(self, runner_class, **bindings): with contextlib.nested(temporary_dir(), temporary_dir()) as (td1, td2): sandbox = DirectorySandbox(td1) checkpoint_root = td2 task_runner = runner_class( runner_pex=os.path.join('dist', 'thermos_runner.pex'), task_id='hello_world', task=TASK.bind(**bindings).task(), role=getpass.getuser(), portmap={}, sandbox=sandbox, checkpoint_root=checkpoint_root, ) yield task_runner
def test_resource_manager(): with temporary_dir() as td: assigned_task = make_assigned_task( make_job('some-role', 'some-env', 'some-job', 'http', portmap={'http': 80})) sandbox = os.path.join(td, 'sandbox') root = os.path.join(td, 'thermos') write_header(root, sandbox, assigned_task.taskId) mock_disk_collector_provider = mock.create_autospec( DiskCollectorProvider, spec_set=True) mock_disk_collector = mock_disk_collector_provider.provides.return_value mock_disk_collector.sample.return_value = None value_mock = mock.PropertyMock(return_value=4197) type(mock_disk_collector).value = value_mock completed_event = threading.Event() completed_mock = mock.PropertyMock(return_value=completed_event) type(mock_disk_collector).completed_event = completed_mock rmp = ResourceManagerProvider( root, disk_collector_provider=mock_disk_collector_provider) rm = rmp.from_assigned_task(assigned_task, DirectorySandbox(sandbox)) assert rm.status is None try: rm.start() while rm.status is None: rm._kill_event.wait(timeout=1) result = rm.status assert result is not None assert result.reason.startswith('Disk limit exceeded') assert result.status == mesos_pb2.TASK_FAILED assert value_mock.call_count == 1 assert completed_mock.call_count == 1 assert mock_disk_collector.sample.call_count == 1 finally: rm._resource_monitor.kill()
def from_assigned_task(self, assigned_task, **kwargs): return DirectorySandbox(safe_mkdtemp(), **kwargs)