def testEvaluateFromHost(self): """Tests Evaluate() which runs test from host.""" # Mock RunTestFromDut fail. command_mock = self.StartPatcher(cros_test_lib.RunCommandMock()) command_mock.AddCmdResult( partial_mock.InOrder(['rm', '-f', self.REMOTE_REPORT_FILE]), returncode=0) command_mock.AddCmdResult( partial_mock.InOrder([self.AUTOTEST_CLIENT, self.TEST_TARGET]), returncode=1) self.SkipMaySetupBoard() # Mock RunTestFromHost success. command_mock.AddCmdResult(self.TEST_THAT_COMMAND, returncode=0) # Mock 'find' and returns a result file for verify. score = 59.9 report_file_in_chroot, _ = self.WriteTestResult(self.evaluator, score) command_mock.AddCmdResult( ['find', '.', '-name', 'results-chart.json'], output=report_file_in_chroot) eval_score = self.evaluator.Evaluate(self.DUT, self.BUILD_LABEL) self.assertEqual(1, len(eval_score.values)) self.assertEqual(score, eval_score.values[0]) self.assertEqual(score, eval_score.mean) self.assertEqual(0.0, eval_score.variance) self.assertEqual(0.0, eval_score.std)
def testSanityCheckSyncToHeadWorks(self): """Tests SanityCheck() that good and bad commit do not exist. As good and bad commit do not exist, it calls SyncToHead(). """ git_mock = self.StartPatcher(GitMock(self.repo_dir)) sync_to_head_mock = self.PatchObject(builder_module.Builder, 'SyncToHead') # Mock git result for DoesCommitExistInRepo to return False. git_mock.AddRunGitResult(partial_mock.InOrder( ['rev-list', self.GOOD_COMMIT_SHA1]), returncode=128) # Mock git result for GetCommitTimestamp. git_mock.AddRunGitResult(partial_mock.InOrder( ['show', self.GOOD_COMMIT_SHA1]), output=str(self.GOOD_COMMIT_TIMESTAMP)) git_mock.AddRunGitResult(partial_mock.InOrder( ['show', self.BAD_COMMIT_SHA1]), output=str(self.BAD_COMMIT_TIMESTAMP)) self.assertTrue(self.bisector.SanityCheck()) # After SyncToHead, found both bad and good commit. sync_to_head_mock.assert_called()
def testPrepareBisect(self): """Tests PrepareBisect().""" # Pass SanityCheck(). git_mock = self.StartPatcher( git_bisector_unittest.GitMock(self.repo_dir)) git_mock.AddRunGitResult( partial_mock.InOrder(['rev-list', self.GOOD_COMMIT_SHA1])) git_mock.AddRunGitResult( partial_mock.InOrder(['rev-list', self.BAD_COMMIT_SHA1])) git_mock.AddRunGitResult(partial_mock.InOrder( ['show', self.GOOD_COMMIT_SHA1]), output=str(self.GOOD_COMMIT_TIMESTAMP)) git_mock.AddRunGitResult(partial_mock.InOrder( ['show', self.BAD_COMMIT_SHA1]), output=str(self.BAD_COMMIT_TIMESTAMP)) # Inject score for both side. git_mock.AddRunGitResult(['checkout', self.GOOD_COMMIT_SHA1]) git_mock.AddRunGitResult(['checkout', self.BAD_COMMIT_SHA1]) build_deploy_eval_mock = self.PatchObject( chrome_on_cros_bisector.ChromeOnCrosBisector, 'BuildDeployEval') build_deploy_eval_mock.side_effect = [ self.GOOD_COMMIT_SCORE, self.BAD_COMMIT_SCORE ] # Set auto_threshold. self.bisector.auto_threshold = True self.assertTrue(self.bisector.PrepareBisect())
def setUp(self): self.rc.SetDefaultCmdResult() self.rc.AddCmdResult(partial_mock.InOrder( ['dump_kernel_config', '/dev/loop9999p2']), output=SAMPLE_KERNEL_CONFIG) self.image = image_lib_unittest.LoopbackPartitionsMock( 'outfile', self.tempdir)
def testMissingKernB(self): """Test the path where KERN-B fails to dump config""" self.keyset.keys['keyA_kernel_data_key'] = keys.KeyPair( 'keyA_kernel_data_key', self.keytempdir.tempdir) self.rc.AddCmdResult(partial_mock.InOrder( ['dump_kernel_config', '/dev/loop9999p2']), output=SAMPLE_KERNEL_CONFIG) self.rc.AddCmdResult(partial_mock.In('/dev/loop9999p4'), returncode=1) imagefile.UpdateRootfsHash(self.image, self.image.GetPartitionDevName('KERN-A'), self.keyset, 'keyA_') self.assertEqual(5, self.rc.call_count) expected_kernel_cmdline = kernel_cmdline.CommandLine( 'console= loglevel=7 init=/sbin/init cros_secure oops=panic panic=-1 ' 'root=/dev/dm-0 rootwait ro dm_verity.error_behavior=3 ' 'dm_verity.max_bios=-1 dm_verity.dev_wait=1 dm="1 vroot none ro 1,0 ' '800 verity alg=sha1" noinitrd cros_debug vt.global_cursor_default=0 ' 'kern_guid=%U add_efi_memmap boot=local noresume noswap ' 'i915.modeset=1 tpm_tis.force=1 tpm_tis.interrupts=0 ' 'nmi_watchdog=panic,lapic disablevmx=off') expected_calls = [ mock.call('/dev/loop9999p2', expected_kernel_cmdline, self.keyset.keys['keyA_kernel_data_key']), ] self.assertEqual(expected_calls, self.ukc.call_args_list)
def testSanityCheckWrongTimeOrder(self): """Tests SanityCheck() that good and bad commit have wrong time order.""" git_mock = self.StartPatcher(GitMock(self.repo_dir)) # Mock git result for DoesCommitExistInRepo. git_mock.AddRunGitResult( partial_mock.InOrder(['rev-list', self.GOOD_COMMIT_SHA1])) git_mock.AddRunGitResult( partial_mock.InOrder(['rev-list', self.BAD_COMMIT_SHA1])) # Mock git result for GetCommitTimestamp, but swap timestamp. git_mock.AddRunGitResult(partial_mock.InOrder( ['show', self.GOOD_COMMIT_SHA1]), output=str(self.BAD_COMMIT_TIMESTAMP)) git_mock.AddRunGitResult(partial_mock.InOrder( ['show', self.BAD_COMMIT_SHA1]), output=str(self.GOOD_COMMIT_TIMESTAMP)) self.assertFalse(self.bisector.SanityCheck())
def testGetCommitTimestampNotFound(self): """Tests GetCommitTimeStamp when the commit is not found.""" git_mock = self.StartPatcher(GitMock(self.repo_dir)) nonexist_sha1 = '12345' # Mock git result for GetCommitTimestamp. git_mock.AddRunGitResult(partial_mock.InOrder(['show', nonexist_sha1]), output='commit does not exist') self.assertIsNone(self.bisector.GetCommitTimestamp(nonexist_sha1))
def testSanityCheck(self): """Tests SanityCheck(). It tests by mocking out git commands called by SanityCheck(). """ git_mock = self.StartPatcher(GitMock(self.repo_dir)) # Mock git result for DoesCommitExistInRepo. git_mock.AddRunGitResult( partial_mock.InOrder(['rev-list', self.GOOD_COMMIT_SHA1])) git_mock.AddRunGitResult( partial_mock.InOrder(['rev-list', self.BAD_COMMIT_SHA1])) # Mock git result for GetCommitTimestamp. git_mock.AddRunGitResult(partial_mock.InOrder( ['show', self.GOOD_COMMIT_SHA1]), output=str(self.GOOD_COMMIT_TIMESTAMP)) git_mock.AddRunGitResult(partial_mock.InOrder( ['show', self.BAD_COMMIT_SHA1]), output=str(self.BAD_COMMIT_TIMESTAMP)) self.assertTrue(self.bisector.SanityCheck())
def testGetCommitTimestamp(self): """Tests GetCommitTimeStamp by mocking git.RunGit.""" git_mock = self.StartPatcher(GitMock(self.repo_dir)) # Mock git result for GetCommitTimestamp. git_mock.AddRunGitResult(partial_mock.InOrder( ['show', self.GOOD_COMMIT_SHA1]), output=str(self.GOOD_COMMIT_TIMESTAMP)) self.assertEqual( self.GOOD_COMMIT_TIMESTAMP, self.bisector.GetCommitTimestamp(self.GOOD_COMMIT_SHA1))
def testSanityCheckGoodCommitNotExist(self): """Tests SanityCheck() that good commit does not exist.""" git_mock = self.StartPatcher(GitMock(self.repo_dir)) sync_to_head_mock = self.PatchObject(builder_module.Builder, 'SyncToHead') # Mock git result for DoesCommitExistInRepo to return False. git_mock.AddRunGitResult(partial_mock.InOrder( ['rev-list', self.GOOD_COMMIT_SHA1]), returncode=128) # Mock invalid result for GetCommitTimestamp to return None. git_mock.AddRunGitResult(partial_mock.InOrder( ['show', self.GOOD_COMMIT_SHA1]), output='invalid commit') git_mock.AddRunGitResult(partial_mock.InOrder( ['show', self.BAD_COMMIT_SHA1]), output='invalid commit') self.assertFalse(self.bisector.SanityCheck()) # SyncToHead() called because DoesCommitExistInRepo() returns False for # good commit. sync_to_head_mock.assert_called()
def setUp(self): self.rc.SetDefaultCmdResult() self.rc.AddCmdResult(partial_mock.InOrder( ['dump_kernel_config', '/dev/loop9999p2']), output=SAMPLE_KERNEL_CONFIG) self.keytempdir = osutils.TempDir() self.keyset = keys.Keyset(self.keytempdir.tempdir) self.image = image_lib_unittest.LoopbackPartitionsMock( 'outfile', self.tempdir) self.root_hash = CalculateRootfsHashMock('meh', SAMPLE_KERNEL_CONFIG) self.PatchObject(imagefile, 'CalculateRootfsHash', return_value=self.root_hash) self.rc.AddCmdResult(partial_mock.In('tune2fs'), output='Block count: 4480\n') self.ukc = self.PatchObject(imagefile, '_UpdateKernelConfig')
def SetUpChromeTest(self, test_exe, test_label, test_args=None): """Sets configurations necessary for running a chrome test. Args: test_exe: The name of the chrome test. test_label: The label of the chrome test. test_args: A list of arguments of the particular chrome test. """ self._tester.args = [test_exe] + test_args if test_args else [test_exe] self._tester.chrome_test = True self._tester.build_dir = self.TempFilePath('out_amd64-generic/Release') osutils.SafeMakedirs(self._tester.build_dir) isolate_map = self.TempFilePath('testing/buildbot/gn_isolate_map.pyl') # Add info about the specified chrome test to the isolate map. osutils.WriteFile(isolate_map, """{ "%s": { "label": "%s", "type": "console_test_launcher", } }""" % (test_exe, test_label), makedirs=True) self._tester.build = True self._tester.deploy = True self._tester.chrome_test_target = test_exe self._tester.chrome_test_deploy_target_dir = '/usr/local/chrome_test' # test_label looks like //crypto:crypto_unittests. # label_root extracts 'crypto' from the test_label in this instance. label_root = test_label.split(':')[0].lstrip('/') # A few files used by the chrome test. runtime_deps = [ './%s' % test_exe, 'gen.runtime/%s/%s/%s.runtime_deps' % (label_root, test_exe, test_exe), '../../third_party/chromite'] # Creates the test_exe to be an executable. osutils.Touch(os.path.join(self._tester.build_dir, runtime_deps[0]), mode=0o700) for dep in runtime_deps[1:]: osutils.Touch(os.path.join(self._tester.build_dir, dep), makedirs=True) # Mocks the output by providing necessary runtime files. self.rc.AddCmdResult( partial_mock.InOrder(['gn', 'desc', test_label]), output='\n'.join(runtime_deps))
def testNoKernBHash(self): """Test no KERN-B hash case.""" self.rc.AddCmdResult(partial_mock.InOrder( ['dump_kernel_config', '/dev/loop9999p2']), output=SAMPLE_KERNEL_CONFIG) recovery = keys.KeyPair('recovery', self.keytempdir.tempdir, version=3) self.keyset.keys['recovery'] = recovery imagefile.UpdateRecoveryKernelHash(self.image, self.keyset) self.rc.assertCommandCalled( ['sudo', '--', 'dump_kernel_config', '/dev/loop9999p2'], capture_output=True, print_cmd=False, check=True, encoding='utf-8') new_cmdline = SAMPLE_KERNEL_CONFIG.strip() self.loginfo.assert_called_once_with('New cmdline for kernel A is %s', new_cmdline) self.ukc.assert_called_once_with( '/dev/loop9999p2', kernel_cmdline.CommandLine(new_cmdline), recovery)
def testNoKernB(self): """Test the normal path""" kernel_key = keys.KeyPair('kernel_data_key', self.keytempdir.tempdir) self.keyset.keys['kernel_data_key'] = kernel_key self.rc.AddCmdResult(partial_mock.InOrder( ['dump_kernel_config', '/dev/loop9999p4']), returncode=1) imagefile.UpdateStatefulPartitionVblock(self.image, self.keyset) self.rc.assertCommandCalled( ['sudo', '--', 'dump_kernel_config', '/dev/loop9999p4'], print_cmd=False, capture_output=True, check=False, encoding='utf-8') self.rc.assertCommandCalled([ 'sudo', '--', 'vbutil_kernel', '--repack', mock.ANY, '--keyblock', kernel_key.keyblock, '--signprivate', kernel_key.private, '--oldblob', '/dev/loop9999p2', '--vblockonly' ]) self.rc.assertCommandCalled([ 'sudo', '--', 'cp', mock.ANY, '%s/dir-1/vmlinuz_hd.vblock' % self.tempdir ])
def setUp(self): self.rc.SetDefaultCmdResult() self.rc.AddCmdResult(partial_mock.In('/dev/loop9999p3'), returncode=1) self.rc.AddCmdResult(partial_mock.InOrder( ['dump_kernel_config', '/dev/loop9999p4']), output=SAMPLE_KERNEL_CONFIG)