def test_init_parser(self): results = "results" log = os.path.join(results, '.parse.log') # do some additional setup self.god.stub_function(tko_utils, 'redirect_parser_debugging') self.god.stub_function(tko_db, 'db') self.god.stub_function(status_lib, 'parser') # set up recording file_obj = self.god.create_mock_class(file, "file") server_job.open.expect_call(log, 'w', 0).and_return(file_obj) tko_utils.redirect_parser_debugging.expect_call(file_obj) db = self.god.create_mock_class(tko_db.db_sql, "db_sql") tko_db.db.expect_call(autocommit=True).and_return(db) parser = self.god.create_mock_class(parser_mod.parser, "parser_class") status_lib.parser.expect_call(1).and_return(parser) job_model = self.god.create_mock_class_obj(parser_mod0.job, "pjob") parser.make_job.expect_call(results).and_return(job_model) parser.start.expect_call(job_model) db.find_job.expect_call(mock.is_string_comparator()) db.insert_job.expect_call(mock.is_string_comparator(), job_model) # run method self.job.init_parser(results) # check self.god.check_playback()
def _setup_post_record_init(self, cont, resultdir, my_harness): # now some specific stubs self.god.stub_function(self.job, 'config_get') self.god.stub_function(self.job, 'config_set') self.god.stub_function(self.job, 'record') # other setup results = os.path.join(self.autodir, 'results') download = os.path.join(self.autodir, 'tests', 'download') pkgdir = os.path.join(self.autodir, 'packages') utils.drop_caches.expect_call() job_sysinfo = sysinfo.sysinfo.expect_new(resultdir) if not cont: os.path.exists.expect_call(download).and_return(False) os.mkdir.expect_call(download) shutil.copyfile.expect_call(mock.is_string_comparator(), os.path.join(resultdir, 'control')) job.local_host.LocalHost.expect_new(hostname='localhost') job_sysinfo.log_per_reboot_data.expect_call() if not cont: self.job.record.expect_call('START', None, None) my_harness.run_start.expect_call()
def _setup_post_record_init(self, cont, resultdir, my_harness): # now some specific stubs self.god.stub_function(self.job, "config_get") self.god.stub_function(self.job, "config_set") self.god.stub_function(self.job, "record") # other setup results = os.path.join(self.autodir, "results") download = os.path.join(self.autodir, "tests", "download") pkgdir = os.path.join(self.autodir, "packages") utils.drop_caches.expect_call() job_sysinfo = sysinfo.sysinfo.expect_new(resultdir) if not cont: os.path.exists.expect_call(download).and_return(False) os.mkdir.expect_call(download) shutil.copyfile.expect_call(mock.is_string_comparator(), os.path.join(resultdir, "control")) self.config = config.config.expect_new(self.job) self.job.config_get.expect_call("boottool.executable").and_return(None) bootloader = boottool.boottool.expect_new(None) job.local_host.LocalHost.expect_new(hostname="localhost", bootloader=bootloader) job_sysinfo.log_per_reboot_data.expect_call() if not cont: self.job.record.expect_call("START", None, None) my_harness.run_start.expect_call() self.god.stub_function(utils, "read_one_line") utils.read_one_line.expect_call("/proc/cmdline").and_return( "blah more-blah root=lala IDENT=81234567 blah-again console=tty1" ) self.job.config_set.expect_call("boot.default_args", "more-blah console=tty1")
def test_client_logger_process_line_log_copy_collection_failure(self): collector = autotest.log_collector.expect_new(self.host, '', '') logger = autotest.client_logger(self.host, '', '') collector.collect_client_job_results.expect_call().and_raises( Exception('log copy failure')) logging.exception.expect_call(mock.is_string_comparator()) logger._process_line('AUTOTEST_TEST_COMPLETE:/autotest/fifo1')
def test_client_logger_process_line_log_copy_collection_failure(self): collector = autotest_remote.log_collector.expect_new(self.host, '', '') logger = autotest_remote.client_logger(self.host, '', '') collector.collect_client_job_results.expect_call().and_raises( Exception('log copy failure')) logging.exception.expect_call(mock.is_string_comparator()) logger._process_line('AUTOTEST_TEST_COMPLETE:/autotest/fifo1')
def _setup_post_record_init(self, cont, resultdir, my_harness): # now some specific stubs self.god.stub_function(self.job, 'config_get') self.god.stub_function(self.job, 'config_set') self.god.stub_function(self.job, 'record') # other setup results = os.path.join(self.autodir, 'results') download = os.path.join(self.autodir, 'tests', 'download') pkgdir = os.path.join(self.autodir, 'packages') utils.drop_caches.expect_call() job_sysinfo = sysinfo.sysinfo.expect_new(resultdir) if not cont: os.path.exists.expect_call(download).and_return(False) os.mkdir.expect_call(download) shutil.copyfile.expect_call(mock.is_string_comparator(), os.path.join(resultdir, 'control')) self.config = config.config.expect_new(self.job) self.job.config_get.expect_call('boottool.executable').and_return(None) bootloader = boottool.boottool.expect_new(None) job.local_host.LocalHost.expect_new(hostname='localhost', bootloader=bootloader) job_sysinfo.log_per_reboot_data.expect_call() if not cont: self.job.record.expect_call('START', None, None) my_harness.run_start.expect_call() self.god.stub_function(utils, 'read_one_line') utils.read_one_line.expect_call('/proc/cmdline').and_return( 'blah more-blah root=lala IDENT=81234567 blah-again console=tty1') self.job.config_set.expect_call('boot.default_args', 'more-blah console=tty1')
def _setup_post_record_init(self, cont, resultdir, my_harness): # now some specific stubs self.god.stub_function(self.job, 'config_get') self.god.stub_function(self.job, 'config_set') self.god.stub_function(self.job, 'record') self.god.stub_function(self.job, '_increment_group_level') self.god.stub_function(self.job, '_decrement_group_level') self.god.stub_function(self.job, 'get_state') self.god.stub_function(self.job, 'set_state') # other setup results = os.path.join(self.autodir, 'results') download = os.path.join(self.autodir, 'tests', 'download') pkgdir = os.path.join(self.autodir, 'packages') utils.drop_caches.expect_call() self.job.get_state.expect_call("__run_test_cleanup", default=True).and_return(True) job_sysinfo = sysinfo.sysinfo.expect_new(resultdir) self.job.get_state.expect_call("__sysinfo", None).and_return(None) self.job.get_state.expect_call("__last_boot_tag", default=None).and_return(None) self.job.get_state.expect_call("__job_tag", default=None).and_return('1337-gps') if not cont: os.path.exists.expect_call(pkgdir).and_return(False) os.mkdir.expect_call(pkgdir) os.path.exists.expect_call(results).and_return(False) os.mkdir.expect_call(results) os.path.exists.expect_call(download).and_return(False) os.mkdir.expect_call(download) os.makedirs.expect_call(os.path.join(resultdir, 'analysis')) shutil.copyfile.expect_call(mock.is_string_comparator(), os.path.join(resultdir, 'control')) self.config = config.config.expect_new(self.job) job.local_host.LocalHost.expect_new(hostname='localhost') self.job.config_get.expect_call( 'boottool.executable').and_return(None) bootloader = boottool.boottool.expect_new(None) job_sysinfo.log_per_reboot_data.expect_call() if not cont: self.job.record.expect_call('START', None, None) self.job._increment_group_level.expect_call() my_harness.run_start.expect_call() self.job.get_state.expect_call('__monitor_disk', default=0.0).and_return(0.0) self.god.stub_function(utils, 'read_one_line') utils.read_one_line.expect_call('/proc/cmdline').and_return( 'blah more-blah root=lala IDENT=81234567 blah-again console=tty1') self.job.config_set.expect_call('boot.default_args', 'more-blah console=tty1')
def test_fill_server_control_namespace(self): class MockAutotest(object): job = None class MockHosts(object): job = None # Verify that the job attributes are injected in the expected place. self.god.stub_with(autotest, 'Autotest', MockAutotest) self.god.stub_with(hosts, 'Host', MockHosts) self.job._fill_server_control_namespace({}) self.assertEqual(hosts.Host.job, self.job) self.assertEqual(autotest.Autotest.job, self.job) test_ns = {} self.job._fill_server_control_namespace(test_ns) # Verify that a few of the expected module exports were loaded. self.assertEqual(test_ns['sys'], sys) self.assert_('git' in test_ns) self.assert_('parallel_simple' in test_ns) self.assert_('sh_escape' in test_ns) self.assert_('barrier' in test_ns) self.assert_('format_error' in test_ns) self.assert_('AutoservRebootError' in test_ns) # This should not exist, client.common_lib.errors does not export it. self.assert_('format_exception' not in test_ns) # Replacing something that exists with something else is an error. orig_test_ns = {'hosts': 'not the autotest_lib.server.hosts module'} test_ns = orig_test_ns.copy() self.assertRaises(error.AutoservError, self.job._fill_server_control_namespace, test_ns) # Replacing something that exists with something else is an error. test_ns = orig_test_ns.copy() self.assertRaises(error.AutoservError, self.job._fill_server_control_namespace, test_ns) # Replacing something without protection should succeed. test_ns = orig_test_ns.copy() self.job._fill_server_control_namespace(test_ns, protect=False) self.assertEqual(test_ns['hosts'], hosts) # Replacing something with itself should issue a warning. test_ns = {'hosts': hosts} self.god.stub_function(warnings, 'showwarning') warnings.showwarning.expect_call( mock.is_instance_comparator(UserWarning), UserWarning, mock.is_string_comparator(), mock.is_instance_comparator(int)) self.job._fill_server_control_namespace(test_ns) self.god.check_playback() self.assertEqual(test_ns['hosts'], hosts)
def test_client_logger_process_line_package_install_fifo_failure(self): collector = autotest_remote.log_collector.expect_new(self.host, '', '') logger = autotest_remote.client_logger(self.host, '', '') self.god.stub_function(logger, '_send_tarball') c = autotest_remote.global_config.global_config c.get_config_value.expect_call('PACKAGES', 'serve_packages_from_autoserv', type=bool).and_return(True) logger._send_tarball.expect_call('pkgname.tar.bz2', '/autotest/dest/') self.host.run.expect_call('echo B > /autotest/fifo3').and_raises( Exception('fifo failure')) logging.exception.expect_call(mock.is_string_comparator()) logger._process_line('AUTOTEST_FETCH_PACKAGE:pkgname.tar.bz2:' '/autotest/dest/:/autotest/fifo3')
def test_client_logger_process_line_package_install_fifo_failure(self): collector = autotest.log_collector.expect_new(self.host, '', '') logger = autotest.client_logger(self.host, '', '') self.god.stub_function(logger, '_send_tarball') c = autotest.global_config.global_config c.get_config_value.expect_call('PACKAGES', 'serve_packages_from_autoserv', type=bool).and_return(True) logger._send_tarball.expect_call('pkgname.tar.bz2', '/autotest/dest/') self.host.run.expect_call('echo B > /autotest/fifo3').and_raises( Exception('fifo failure')) logging.exception.expect_call(mock.is_string_comparator()) logger._process_line('AUTOTEST_FETCH_PACKAGE:pkgname.tar.bz2:' '/autotest/dest/:/autotest/fifo3')
def test_wrap_control_file_client(self): self._test_wrap_control_file_helper() control_base64 = 'control_base64' control_segment = '|control_segment|' self.god.stub_function(base64, 'encodestring') base64.encodestring.expect_call(self.control).and_return(control_base64) control_file.apply_string_arguments.expect_call( control_file.CLIENT_SEGMENT, control_base64=control_base64, control_comment=mock.is_string_comparator()).and_return( control_segment) result = control_file.wrap_control_file(control_file=self.control, is_server=False, skip_verify=False, verify_params=self.verify_params) self.assertEqual(result, self.verify_segment + control_segment) self.god.check_playback()
def construct_server_job(self): # XXX(gps): These expect_call's must be kept in perfect sync # call for call with what base_server_job does. This is too # painful to maintain. # setup recording for constructor file_obj = self.god.create_mock_class(file, "file") server_job.open.expect_call(self.control).and_return(file_obj) file_obj.read.expect_call().and_return('') file_obj.close.expect_call() os.path.exists.expect_call( mock.is_string_comparator()).and_return(False) os.mkdir.expect_call(mock.is_string_comparator()) os.path.exists.expect_call( mock.is_string_comparator()).and_return(False) os.mkdir.expect_call(mock.is_string_comparator()) self.god.mock_up(sysinfo, 'sysinfo') sysinfo.sysinfo.expect_call(mock.is_string_comparator()) os.access.expect_call(mock.is_string_comparator(), os.W_OK).and_return(False) os.makedirs.expect_call(mock.is_string_comparator(), 0700) os.access.expect_call(mock.is_string_comparator(), os.W_OK).and_return(True) os.path.isdir.expect_call(mock.is_string_comparator()).and_return(False) self.god.stub_function(tempfile, 'gettempdir') tempfile.gettempdir.expect_call().and_return('/tmp/server_job_unittest') os.makedirs.expect_call(mock.is_string_comparator(), 0700).and_raises(os.error) os.chmod.expect_call(mock.is_string_comparator(), stat.S_IRWXU) cls = server_job.base_server_job compare = mock.is_instance_comparator(cls) os.path.isdir.expect_call('results').and_return(True) os.path.exists.expect_call('results/keyval').and_return(False) server_job.get_site_job_data.expect_call(compare).and_return({}) utils.write_keyval.expect_call(mock.is_string_comparator(), mock.is_instance_comparator(dict)) self.job = server_job.base_server_job(self.control, self.args, self.resultdir, self.label, self.user, self.machines) self.god.check_playback() # use this stub alot self.god.stub_function(self.job, "_execute_code")
def construct_job(self, cont): # will construct class instance using __new__ self.job = job.base_job.__new__(job.base_job) # now some specific stubs self.god.stub_function(self.job, '_load_state') self.god.stub_function(self.job, '_init_group_level') self.god.stub_function(self.job, 'config_get') self.god.stub_function(self.job, 'config_set') self.god.stub_function(self.job, 'record') self.god.stub_function(self.job, '_increment_group_level') self.god.stub_function(self.job, '_decrement_group_level') self.god.stub_function(self.job, 'get_state') self.god.stub_function(self.job, 'set_state') # other setup tmpdir = os.path.join(self.autodir, 'tmp') results = os.path.join(self.autodir, 'results') download = os.path.join(self.autodir, 'tests', 'download') resultdir = os.path.join(self.autodir, 'results', self.jobtag) pkgdir = os.path.join(self.autodir, 'packages') # record os.path.exists.expect_call(resultdir).and_return(False) os.makedirs.expect_call(resultdir) if not cont: job.base_job._cleanup_results_dir.expect_call() utils.drop_caches.expect_call() self.job._load_state.expect_call() self.job.get_state.expect_call("__run_test_cleanup", default=True).and_return(True) job_sysinfo = sysinfo.sysinfo.expect_new(resultdir) self.job.get_state.expect_call("__sysinfo", None).and_return(None) self.job.get_state.expect_call("__last_boot_tag", default=None).and_return(None) self.job.get_state.expect_call("__job_tag", default=None).and_return('1337-gps') if not cont: os.path.exists.expect_call(tmpdir).and_return(False) os.mkdir.expect_call(tmpdir) os.path.exists.expect_call(pkgdir).and_return(False) os.mkdir.expect_call(pkgdir) os.path.exists.expect_call(results).and_return(False) os.mkdir.expect_call(results) os.path.exists.expect_call(download).and_return(False) os.mkdir.expect_call(download) os.makedirs.expect_call(os.path.join(resultdir, 'analysis')) shutil.copyfile.expect_call(mock.is_string_comparator(), os.path.join(resultdir, 'control')) self.job._init_group_level.expect_call() self.config = config.config.expect_new(self.job) my_harness = self.god.create_mock_class(harness.harness, 'my_harness') harness.select.expect_call(None, self.job).and_return(my_harness) self.job.config_get.expect_call( 'boottool.executable').and_return(None) bootloader = boottool.boottool.expect_new(None) job_sysinfo.log_per_reboot_data.expect_call() if not cont: self.job.record.expect_call('START', None, None) self.job._increment_group_level.expect_call() my_harness.run_start.expect_call() self.job.get_state.expect_call('__monitor_disk', default=0.0).and_return(0.0) self.god.stub_function(utils, 'read_one_line') utils.read_one_line.expect_call('/proc/cmdline').and_return( 'blah more-blah root=lala IDENT=81234567 blah-again console=tty1') self.job.config_set.expect_call('boot.default_args', 'more-blah console=tty1') # finish constructor options = Dummy() options.tag = self.jobtag options.cont = cont options.harness = None options.log = False options.verbose = False self.job.__init__(self.control, options, extra_copy_cmdline=['more-blah']) # check self.god.check_playback()