def test_build_images(self, os_chdir): builds = [0] tags = [0] def command(key, *args, **kwargs): if key == 'images': return '' elif key == 'build': builds[0] += 1 return True elif key == 'tag': tags[0] += 1 return True with mock.patch.object(MockDockerManager, 'command', side_effect=command): dmm = MockDockerManager() dmm.build_images() from apps.core import nvgpu if nvgpu.is_supported(): expected = 5 else: expected = 3 assert builds[0] == expected assert tags[0] == expected assert len(os_chdir.mock_calls) == 2 * expected
def test_get_hypervisor(self, xhyve_instance, hyperv_instance, *_): def reset(): xhyve_instance.called = False hyperv_instance.called = False dmm = MockDockerManager() with mock.patch('golem.docker.manager.is_windows', return_value=True): assert dmm._select_hypervisor() assert hyperv_instance.called assert not xhyve_instance.called reset() with mock.patch('golem.docker.manager.is_windows', return_value=False): with mock.patch('golem.docker.manager.is_osx', return_value=True): assert dmm._select_hypervisor() assert not hyperv_instance.called assert xhyve_instance.called reset() with mock.patch('golem.docker.manager.is_windows', return_value=False): with mock.patch('golem.docker.manager.is_osx', return_value=False): assert not dmm._select_hypervisor() assert not hyperv_instance.called assert not xhyve_instance.called
def test_command(self): dmm = MockDockerManager() with mock.patch.dict( 'golem.docker.commands.docker.DockerCommandHandler.commands', dict(test=[sys.executable, '--version'])): assert dmm.command('test').startswith('Python') assert not dmm.command('deadbeef')
def test_save_and_resume(self): dmm = MockDockerManager() dmm.hypervisor = MockHypervisor(dmm) dmm.hypervisor.command = mock.Mock() dmm.hypervisor._set_env_from_output = mock.Mock() dmm.hypervisor._set_env() callback = mock.Mock() dmm._save_and_resume(callback) assert callback.called
def test_build_config_ok(self): manager = MockDockerManager() cpu_to_check = 4 mem_to_check = 4096 config = ConfigMock(cpu_to_check, mem_to_check * 1024) manager.build_config(config) self.assertEqual(manager.get_config(), { 'cpu_count': cpu_to_check, 'memory_size': mem_to_check })
def test_check_environment_linux(self, *_): dmm = MockDockerManager() dmm.pull_images = mock.Mock() dmm.build_images = mock.Mock() with mock.patch.object(dmm, 'command'): assert not dmm.check_environment() assert dmm.pull_images.called assert not dmm.build_images.called assert not dmm.hypervisor assert dmm._env_checked
def test_constrain_not_called(self): dmm = MockDockerManager() dmm._diff_constraints = mock.Mock(return_value=dict()) dmm.hypervisor = MockHypervisor(dmm) dmm.constrain() assert not dmm.hypervisor.constrain.called
def test_check_environment_unsupported(self, *_): dmm = MockDockerManager() dmm.command = lambda *a, **kw: raise_exception('Docker not available') dmm.pull_images = mock.Mock() dmm.build_images = mock.Mock() with self.assertRaises(EnvironmentError): dmm.check_environment() assert not dmm.pull_images.called assert not dmm.build_images.called assert dmm._env_checked
def test_pull_images(self): pulls = [0] def command(key, *args, **kwargs): if key == 'images': return '' elif key == 'pull': pulls[0] += 1 return True with mock.patch.object(MockDockerManager, 'command', side_effect=command): dmm = MockDockerManager() dmm.pull_images() from apps.core import nvgpu if nvgpu.is_supported(): expected = 5 else: expected = 3 assert pulls[0] == expected
def test_diff_constraints(self): dmm = MockDockerManager() diff = dmm._diff_constraints assert diff(DEFAULTS, dict()) == dict() assert diff(dict(), DEFAULTS) == DEFAULTS old = DEFAULTS new = dict(cpu_count=DEFAULTS['cpu_count']) expected = dict() assert diff(old, new) == expected old = DEFAULTS new = dict(cpu_count=DEFAULTS['cpu_count'] + 1, unknown_key='value') expected = dict(cpu_count=DEFAULTS['cpu_count'] + 1) assert diff(old, new) == expected
def test_check_environment_none(self, *_): dmm = MockDockerManager() dmm.pull_images = mock.Mock() dmm.build_images = mock.Mock() with self.assertRaises(EnvironmentError): dmm.check_environment() assert not dmm.hypervisor assert not dmm.pull_images.called assert not dmm.build_images.called assert dmm._env_checked
def test_check_environment_windows_no_hyperv(self, *_): dmm = MockDockerManager() dmm.pull_images = mock.Mock() dmm.build_images = mock.Mock() with mock.patch('golem.docker.manager.VirtualBoxHypervisor.instance', mock.Mock(vm_running=mock.Mock(return_value=False))): # pylint: disable=no-member with mock.patch.object(dmm, 'command'): dmm.check_environment() assert dmm.hypervisor assert dmm.hypervisor.setup.called assert dmm.pull_images.called assert not dmm.build_images.called
def test_constrain_called(self): diff = dict(cpu_count=0) dmm = MockDockerManager() dmm._diff_constraints = mock.Mock(return_value=diff) dmm.hypervisor = MockHypervisor(dmm) dmm.hypervisor._set_env = mock.Mock() dmm.constrain() assert dmm.hypervisor.constrain.called _, kwargs = dmm.hypervisor.constrain.call_args_list.pop() assert len(kwargs) == len(diff) assert kwargs['cpu_count'] == DEFAULTS['cpu_count'] assert kwargs['memory_size'] == DEFAULTS['memory_size']
def test_check_environment_osx(self, *_): dmm = MockDockerManager() hypervisor = mock.Mock( start_vm=mock.Mock(), stop_vm=mock.Mock(), vm_running=mock.Mock(return_value=False), ) dmm.pull_images = mock.Mock() dmm.build_images = mock.Mock() with mock.patch('golem.docker.manager.XhyveHypervisor.instance', hypervisor): # pylint: disable=no-member with mock.patch.object(dmm, 'command'): dmm.check_environment() assert not dmm.hypervisor.create.called assert dmm.pull_images.called assert not dmm.build_images.called assert not dmm.hypervisor.start_vm.called assert not dmm.hypervisor._set_env.called
def test_update_config(self): status_switch = [True] def status_cb(): if status_switch[0]: status_switch[0] = False return True else: return status_switch[0] def done_cb(_): pass config = ConfigMock(0, 768) dmm = MockDockerManager() dmm.pull_images = mock.Mock() dmm.build_images = mock.Mock() hypervisor = mock.Mock() hypervisor.constraints.return_value = DEFAULTS dmm._select_hypervisor = mock.Mock(return_value=hypervisor) with mock.patch.object(dmm, 'command'): dmm.build_config(config) dmm.check_environment() dmm.update_config(status_cb, done_cb, in_background=False, work_dir=None) dmm.update_config(status_cb, done_cb, in_background=True, work_dir=None)
def test_config_defaults(self): manager = MockDockerManager() self.assertDictEqual(manager.get_config(), DEFAULTS)
def test_build_config_odd_memory_size(self): manager = MockDockerManager() manager.hypervisor = MockHypervisor(manager) config = ConfigMock(None, 1025 * 1024) manager.build_config(config) self.assertEqual(manager.get_config()['memory_size'], 1024)
def test_build_config_empty(self): manager = MockDockerManager() config = ConfigMock(None, None) manager.build_config(config) self.assertEqual(manager.get_config(), DEFAULTS)
def test_build_config_below_min_memory_size(self): manager = MockDockerManager() config = ConfigMock(None, 0) manager.build_config(config) self.assertEqual(manager.get_config()['memory_size'], MIN_CONSTRAINTS['memory_size'])
def test_recover_vm_connectivity(self): callback = mock.Mock() dmm = MockDockerManager() dmm._save_and_resume = mock.Mock() dmm.check_environment = mock.Mock() def reset(): callback.called = False dmm.check_environment.called = False dmm._save_and_resume.called = False dmm._env_checked = True dmm.recover_vm_connectivity(callback, in_background=False) assert not dmm._save_and_resume.called reset() dmm._env_checked = False dmm.recover_vm_connectivity(callback, in_background=False) assert not dmm._save_and_resume.called reset() dmm._env_checked = True dmm.hypervisor = MockHypervisor(dmm) dmm.recover_vm_connectivity(callback, in_background=False) assert dmm._save_and_resume.called reset() dmm.recover_vm_connectivity(callback, in_background=True)
def test_build_config_below_min_cpu_count(self): manager = MockDockerManager() config = ConfigMock(0, None) manager.build_config(config) self.assertEqual(manager.get_config()['cpu_count'], MIN_CONSTRAINTS['cpu_count'])
def test_build_config_default(self): manager = MockDockerManager() config = ConfigMock(DEFAULTS['cpu_count'], DEFAULTS['memory_size']) manager.build_config(config) self.assertEqual(manager.get_config(), DEFAULTS)