def test_stores_options_in_datastore(self): self.instantiate() options = MM() id = MM() self.host.parse(self.plugin_id, self.run_id, options) self.mock_datastore.write.assert_called_once_with( self.plugin_id, self.run_id, options)
def test_list_jobs(self): self.instance() # build hosts host1 = MM() host1.jobs.return_value = ['host1-job1', 'host1-job2'] host2 = MM() host2.jobs.return_value = ['host2-job2', 'host2-job2'] self.rc.hosts = [host1, host2] # list hosts ret = self.rc.list_jobs() # list repeated twice (once for each Plugin) # with all host1 first and host2 second jobs = [ 'host1-job1', 'host1-job2', 'host1-job1', 'host1-job2', 'host2-job2', 'host2-job2', 'host2-job2', 'host2-job2' ] # check calls to host.jobs self.assertEqual(ret, jobs) # check for host1 for host in [host1, host2]: call_arg_list = host.jobs.call_args_list call_type1, call_type2 = call_arg_list args, vals = call_type1 self.assertEqual(args, (self.plugins[0].domain, )) args, vals = call_type2 self.assertEqual(args, (self.plugins[1].domain, ))
def test_stores_runstate(self): self.instantiate() context = MM() id = MM() self.host.run(self.plugin_id, self.run_id, context) self.mock_datastore.isrunning.assert_called_once_with( self.plugin_id, self.run_id, self.mock_pattern_parser.run())
def plugins(): plugins = {} plugins['rcv0'] = RunCreateView( [TextField('param0', 'Parameter 0', default='Some default value 0')]) plugins['rcv1'] = RunCreateView( [TextField('param1', 'Parameter 1', default='Some default value 1')]) plugins['view_keys1'] = ("id", "name", "some-other-data"), plugins['runner0'] = MM() plugins['runner1'] = MM() # create Plugin objects plugins['rt0'] = Plugin( 'swansea.ac.uk/1', 'My First Type', plugins['rcv0'], runner=plugins['runner0']) plugins['rt1'] = Plugin( 'swansea.ac.uk/2', 'My Second Type', plugins['rcv1'], view_keys=plugins['view_keys1'], runner=plugins['runner1']) plugins['plugins'] = [plugins['rt0'], plugins['rt1']] return plugins
def instantiate(self): # instantiate Host self.mock_pattern_parser = MM() self.mock_datastore = MM() self.host = Host(self.mock_pattern_parser, self.mock_connection, self.mock_datastore) self.context = MM()
def instantiate(self, opt_map=None): self.opt_map = MM() if opt_map is not None: self.opt_map = opt_map self.defaults = MM() self.pattern_parser = PatternParser(self.opt_map, self.submit_cmd, self.defaults)
def test_action_created_with_pattern_parser_and_label(self): self.instance() action_function = MM() action_label = MM() action = self.plugin.add_action(action_label, action_function) self.assertEqual(action.label, action_label) self.assertEqual(action.function, action_function)
def distro(self, isobin_exists, filelist_in_iso, input_text): mock_isobin_exists = MM(return_value=isobin_exists) mock_iso_list = MM(return_value=filelist_in_iso) mock_os_walk = MM(return_value=[('/', [], ['grub.cfg'])]) @patch('scripts.iso.isolinux_bin_exist', mock_isobin_exists) @patch('os.walk', mock_os_walk) @patch('scripts._7zip.list_iso', mock_iso_list) @patch('builtins.open', mock_open(read_data=input_text)) def test_when_isolinux_bin_is_available(): return (distro.distro('{iso-cfg-dir}', 'ProDOS2.iso')) return test_when_isolinux_bin_is_available()
def setUp(self): self.runner = MM() self.create_view = MM() self.data = {'a': 'a', 'b': 'b'} # set up some mock hosts self.host1 = MM() self.host1.jobs.return_value = ['host1-job1', 'host1-job2'] self.host2 = MM() self.host2.jobs.return_value = ['host2-job1', 'host2-job2'] self.hosts = [self.host1, self.host2] self.domain = 'rynner.swansea.ac.uk' self.name = 'Test Plugin'
def test_run_passes_connection(self): self.instantiate() options = MM() self.host.run(self.plugin_id, self.run_id, options) self.mock_pattern_parser.run.assert_called_once_with( self.mock_connection, options, f'rynner/{self.plugin_id}/{self.run_id}')
def test_gets_jobs_from_list_jobs(self): from unittest.mock import call, Mock self.plugin = MM() i = self.instance() i.update_jobs() self.assertEqual(self.plugin.mock_calls.count(call.list_jobs()), 2)
def test_uploads_correct_format(self): self.instantiate() self.host.run = MM() run = self.create_run(host=self.host, script='my_script', uploads=[('local', 'remote')]) self.connection.put_file.assert_called_once_with('local', 'remote')
def test_upload_incorrect_formats(self): self.instantiate() self.host.run = MM() with self.assertRaises(InvalidContextOption): run = self.create_run(host=self.host, script='my_script', uploads=['throw an error'])
def setUp(self): self.cluster_host = 'example.cluster.com' self.cluster_user = '******' self.context = MM() self.patcher = patch('rynner.host.fabric.Connection') self.FabricMock = self.patcher.start() self.connection = Connection( host=self.cluster_host, user=self.cluster_user)
def instantiate(self, opt_map=None): host_pattern = [ ('#FAKE --walltime={}', 'walltime'), ('#FAKE --num-nodes={}', 'num_nodes'), ] defaults = MM() self.pattern_parser = PatternParser(host_pattern, 'submit_cmd', defaults) self.datastore = Datastore(self.connection) self.host = Host(self.pattern_parser, self.connection, self.datastore)
def test_run_action(self): i = self.instance() i.update_jobs() for index, job in enumerate(self.jobs): action = MM() # move across diagonal of indexing, to ensure the # item data always returned correctly model_index = i.index(index, index) i.run_action(action, [model_index]) action.run.assert_called_once_with([job])
def test_with_function(self): def parsing_function(a, k): return {'function-return': [a.copy(), k]} opt_map = [ (parsing_function, ('cpus', 'memory')), ] mock_mem, mock_cpu, mock_template = (MM(), MM(), MM()) input = {'memory': mock_mem, 'cpus': mock_cpu, 'script': 'script'} output = [{ 'function-return': [{ 'memory': mock_mem, 'cpus': mock_cpu }, ('cpus', 'memory')] }] context = self.assert_parse(opt_map, input, output)
def test_run_empty_runner(self): self.runner = MM() self.instance() qtest_helpers.button_callback(method=self.plugin.create, button=self.ok_button) self.runner.assert_called_once_with(self.plugin.run_manager, { 'key': 'My Default', 'another_key': 'My Other Default' })
def test_slots_raise_exception_with_invalid_model_index(self): i = self.instance() action = MM() model_index = i.index(-1, -1) slots = [('stop_run', [model_index]), ('run_action', action, [model_index])] for slot in slots: attr = getattr(i, slot[0]) with self.assertRaises(InvalidModelIndex) as exception: attr(*slot[1:])
def test_throw_exception_on_invalid_option(self): self.instantiate() self.host.run = MM() with self.assertRaises(InvalidContextOption): run = self.create_run( # script, uploads and downloads are "special" and are handled differently host=self.host, script='this is my script', walltime='10:0:00', num_nodes=10, invalid=5, )
def test_instantiate_run_with_walltime(self): self.instantiate() self.host.run = MM() id = self.create_run(host=self.host, walltime='10:0:00', num_nodes=10, script='this is my script') context = { 'options': ['#FAKE --walltime=10:0:00', '#FAKE --num-nodes=10'], 'script': 'this is my script' } self.host.run.assert_called_once_with(id, context)
def test_instantiate_run_with_walltime(self): self.instantiate() self.host.run = MM() id = self.create_run(host=self.host, walltime='10:0:00', num_nodes=10, script=Template('this is my {var}').format( {'var': 'script'})) context = { 'options': ['#FAKE --walltime=10:0:00', '#FAKE --num-nodes=10'], 'script': 'this is my script' } self.host.run.assert_called_once_with('my-plugin-id', id, context)
def test_distro_detection(self): def os_path_exists(f): if f.endswith('multibootusb.log'): return False return True os_path_exists_mock = MM() log_mock = MM() @patch('os.path.exists', os_path_exists) @patch('scripts.distro.log', log_mock) def _(): fn = distro.detect_iso_from_file_list assert fn(['BOOT.wim', 'Sources']) == 'Windows' assert fn(['BOOT.wim', 'Sause']) is None assert fn(['config.isoclient', 'foo']) == 'opensuse' assert fn(['bar', 'dban', 'foo']) == 'slitaz' assert fn(['memtest.img']) == 'memtest' assert fn(['mt86.png', 'isolinux']) == 'raw_iso' assert fn(['menu.lst']) == 'grub4dos' assert fn(['bootwiz.cfg', 'bootmenu_logo.png']) == \ 'grub4dos_iso' _()
def test_opening_ts_file_should_init_project(self): self.create_settings() self.create_ts_file() tmp_init = Project.OpenedProject.__init__ Project.OpenedProject.__init__ = MM() self.open_and_focus_tsfile() yield 10 self.assertTrue(Project.OpenedProject.__init__) # reset mocked method Project.OpenedProject.__init__ = tmp_init
def runner(run_manager, data): datastore = MM() defaults = [] host_pattern = [('#FAKE num_nodes={}', 'nodes'), ('#FAKE memory={}', 'memory')] pattern_parser = PatternParser(host_pattern, 'submit_cmd', defaults) rid = run_manager.new(nodes=10, memory=10000, host=Host(pattern_parser, connection, datastore), script='my_command') job_id.append(rid)
def test_opening_ts_file_should_trigger_dialog_if_no_project_settings( self): """ Dialog is only shown when no .sublimets or *.sublime-project is found or if these files do not specify any root files""" return # mock show method tmp_show = Project.ProjectError.show Project.ProjectError.show = MM() self.create_ts_file() self.open_and_focus_tsfile() yield 10 # pause 10 ms self.assertTrue(Project.ProjectError.show.called) # reset mocked method Project.ProjectError.show = tmp_show self.close_view() self.rm_file()
def test_throws_error_without_host(self): run_create_view = RunCreateView([ TextField('Some Parameter', 'param', default='Some default value') ]) connection = MM() connection.run_command.return_value = (0, "std out", "std err") # job_id is a hack to get the run id out of the runner function job_id = [] def runner(run_manager, data): datastore = MM() defaults = [] host_pattern = [('#FAKE num_nodes={}', 'nodes'), ('#FAKE memory={}', 'memory')] pattern_parser = PatternParser(host_pattern, 'submit_cmd', defaults) rid = run_manager.new(nodes=10, memory=10000, host=Host(pattern_parser, connection, datastore), script='my_command') job_id.append(rid) rt = Plugin(self.domain, self.type_name, run_create_view, runner) button = qtest_helpers.find_QPushButton(rt.create_view, 'ok') qtest_helpers.button_callback(method=rt.create, button=button) connection.put_file_content.assert_called_once_with( '#FAKE num_nodes=10\n#FAKE memory=10000\nmy_command\n', f'{job_id[0]}/jobcard') connection.run_command.assert_called_once_with('submit_cmd', pwd=f'{job_id[0]}')
def test_jobs_returns_jobs_from_datastore(self): self.instantiate() plugin = MM() self.assertFalse(self.mock_datastore.jobs.called) self.host.update(self.plugin_id) self.mock_datastore.read_multiple.assert_called_once()
def test_returns_value_of_pattern_parser(self): self.instantiate() ret = self.host.type(MM()) assert ret == self.mock_pattern_parser.type()
def test_type_handled_by_pattern_parser(self): self.instantiate() string = MM() self.host.type(string) self.mock_pattern_parser.type.assert_called_once_with(string)