def GetSkiaGoldSession(self, keys_dict, corpus=None, instance=DEFAULT_INSTANCE): """Gets a SkiaGoldSession for the given arguments. Lazily creates one if necessary. Args: keys_dict: A dictionary containing various comparison config data such as corpus and debug information like the hardware/software configuration the image was produced on. corpus: The corpus the session is for. If None, the corpus will be determined using available information. instance: The name of the Skia Gold instance to interact with. """ keys_string = json.dumps(keys_dict, sort_keys=True) if corpus is None: corpus = keys_dict.get('source_type', instance) # Use the string representation of the keys JSON as a proxy for a hash since # dicts themselves are not hashable. session = self._sessions.setdefault(instance, {}).setdefault( corpus, {}).setdefault(keys_string, None) if not session: working_dir = tempfile.mkdtemp(dir=self._working_dir) keys_file = tempfile.NamedTemporaryFile(suffix='.json', dir=working_dir, delete=False).name with open(keys_file, 'w') as f: json.dump(keys_dict, f) session = skia_gold_session.SkiaGoldSession( working_dir, self._gold_properties, keys_file, corpus, instance) self._sessions[instance][corpus][keys_string] = session return session
def test_commandWithUseLuciFalseNotLocal(self, cmd_mock): cmd_mock.return_value = (None, None) args = createSkiaGoldArgs(git_revision='a', local_pixel_tests=False) sgp = skia_gold_properties.SkiaGoldProperties(args) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, None, None, None) with self.assertRaises(RuntimeError): session.Authenticate(use_luci=False)
def test_commandWithLocalPixelTestsFalse(self, cmd_mock): cmd_mock.return_value = (None, None) args = createSkiaGoldArgs(git_revision='a', local_pixel_tests=False) sgp = skia_gold_properties.SkiaGoldProperties(args) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, None, None, None) session.Compare(None, None) self.assertNotIn('--dryrun', cmd_mock.call_args[0][0])
def test_commandWithUseLuciFalse(self, cmd_mock): cmd_mock.return_value = (None, None) args = createSkiaGoldArgs(git_revision='a', local_pixel_tests=True) sgp = skia_gold_properties.SkiaGoldProperties(args) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, None, None, None) session.Authenticate(use_luci=False) self.assertNotIn('--luci', cmd_mock.call_args[0][0])
def test_bypassSkiaGoldFunctionality(self, cmd_mock): cmd_mock.return_value = (None, None) args = createSkiaGoldArgs(git_revision='a', bypass_skia_gold_functionality=True) sgp = skia_gold_properties.SkiaGoldProperties(args) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, None, None, None) with self.assertRaises(RuntimeError): session.Diff(None, None)
def test_shortCircuitAlreadyInitialized(self, cmd_mock): cmd_mock.return_value = (None, None) args = createSkiaGoldArgs(git_revision='a') sgp = skia_gold_properties.SkiaGoldProperties(args) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, None, None, None) session._initialized = True rc, _ = session.Initialize() self.assertEqual(rc, 0) cmd_mock.assert_not_called()
def test_commandOutputReturned(self, cmd_mock): cmd_mock.return_value = (1, 'Something bad :(') args = createSkiaGoldArgs(git_revision='a', local_pixel_tests=False) sgp = skia_gold_properties.SkiaGoldProperties(args) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, None, None, None) rc, stdout = session.Diff(None, None) self.assertEqual(cmd_mock.call_count, 1) self.assertEqual(rc, 1) self.assertEqual(stdout, 'Something bad :(')
def test_bypassSkiaGoldFunctionality(self, cmd_mock): cmd_mock.return_value = (None, None) args = createSkiaGoldArgs(git_revision='a', bypass_skia_gold_functionality=True) sgp = skia_gold_properties.SkiaGoldProperties(args) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, None, None, None) rc, _ = session.Compare(None, None) self.assertEqual(rc, 0) cmd_mock.assert_not_called()
def test_commandCommonArgs(self, cmd_mock): cmd_mock.return_value = (None, None) args = createSkiaGoldArgs(git_revision='a') sgp = skia_gold_properties.SkiaGoldProperties(args) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, None, None, None) session.Authenticate() call_args = cmd_mock.call_args[0][0] self.assertIn('auth', call_args) assertArgWith(self, call_args, '--work-dir', self._working_dir)
def test_failureDoesNotSetShortCircuit(self, cmd_mock): cmd_mock.return_value = (1, None) args = createSkiaGoldArgs(git_revision='a') sgp = skia_gold_properties.SkiaGoldProperties(args) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, None, None, None) self.assertFalse(session._initialized) rc, _ = session.Initialize() self.assertEqual(rc, 1) self.assertFalse(session._initialized) cmd_mock.assert_called_once()
def test_successSetsShortCircuit(self, cmd_mock): cmd_mock.return_value = (0, None) args = createSkiaGoldArgs(git_revision='a') sgp = skia_gold_properties.SkiaGoldProperties(args) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, None, None, None) self.assertFalse(session._authenticated) rc, _ = session.Authenticate() self.assertEqual(rc, 0) self.assertTrue(session._authenticated) cmd_mock.assert_called_once()
def test_noLinkOnSuccess(self, cmd_mock): cmd_mock.return_value = (0, None) args = createSkiaGoldArgs(git_revision='a') sgp = skia_gold_properties.SkiaGoldProperties(args) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, 'keys_file', None, None) rc, _ = session.Compare('name', 'png_file') self.assertEqual(rc, 0) self.assertEqual(session._comparison_results['name'].triage_link, None) self.assertNotEqual( session._comparison_results['name'].triage_link_omission_reason, None)
def test_authFailure(self, auth_mock, init_mock, compare_mock, diff_mock): auth_mock.return_value = (1, 'Auth failed') session = skia_gold_session.SkiaGoldSession(self._working_dir, None, None, None, None) status, error = session.RunComparison(None, None, None) self.assertEqual( status, skia_gold_session.SkiaGoldSession.StatusCodes.AUTH_FAILURE) self.assertEqual(error, 'Auth failed') self.assertEqual(auth_mock.call_count, 1) self.assertEqual(init_mock.call_count, 0) self.assertEqual(compare_mock.call_count, 0) self.assertEqual(diff_mock.call_count, 0)
def test_commandTryjobArgsMissing(self, cmd_mock): cmd_mock.return_value = (None, None) args = createSkiaGoldArgs(git_revision='a') sgp = skia_gold_properties.SkiaGoldProperties(args) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, None, None, None) session.Initialize() call_args = cmd_mock.call_args[0][0] self.assertNotIn('--issue', call_args) self.assertNotIn('--patchset', call_args) self.assertNotIn('--jobid', call_args) self.assertNotIn('--crs', call_args) self.assertNotIn('--cis', call_args)
def test_commandTryjobArgs(self, cmd_mock): cmd_mock.return_value = (None, None) args = createSkiaGoldArgs(git_revision='a', gerrit_issue=1, gerrit_patchset=2, buildbucket_id=3) sgp = skia_gold_properties.SkiaGoldProperties(args) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, None, None, None) session.Initialize() call_args = cmd_mock.call_args[0][0] assertArgWith(self, call_args, '--issue', '1') assertArgWith(self, call_args, '--patchset', '2') assertArgWith(self, call_args, '--jobid', '3') assertArgWith(self, call_args, '--crs', 'gerrit') assertArgWith(self, call_args, '--cis', 'buildbucket')
def test_commandCommonArgs(self, cmd_mock): cmd_mock.return_value = (None, None) args = createSkiaGoldArgs(git_revision='a') sgp = skia_gold_properties.SkiaGoldProperties(args) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, 'keys_file', 'corpus', instance='instance') session.Compare('name', 'png_file') call_args = cmd_mock.call_args[0][0] self.assertIn('imgtest', call_args) self.assertIn('add', call_args) assertArgWith(self, call_args, '--test-name', 'name') assertArgWith(self, call_args, '--png-file', 'png_file') assertArgWith(self, call_args, '--work-dir', self._working_dir)
def test_comparisonSuccess(self, auth_mock, init_mock, compare_mock, diff_mock): auth_mock.return_value = (0, None) init_mock.return_value = (0, None) compare_mock.return_value = (0, None) keys_file = os.path.join(self._working_dir, 'keys.json') with open(os.path.join(self._working_dir, 'keys.json'), 'w') as f: json.dump({}, f) session = skia_gold_session.SkiaGoldSession(self._working_dir, None, keys_file, None, None) status, _ = session.RunComparison(None, None, None) self.assertEqual(status, skia_gold_session.SkiaGoldSession.StatusCodes.SUCCESS) self.assertEqual(auth_mock.call_count, 1) self.assertEqual(init_mock.call_count, 1) self.assertEqual(compare_mock.call_count, 1) self.assertEqual(diff_mock.call_count, 0)
def test_clLinkOnTrybot(self, cmd_mock): cmd_mock.return_value = (1, None) args = createSkiaGoldArgs(git_revision='a', gerrit_issue=1, gerrit_patchset=2, buildbucket_id=3) sgp = skia_gold_properties.SkiaGoldProperties(args) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, 'keys_file', None, None) rc, _ = session.Compare('name', 'png_file') self.assertEqual(rc, 1) self.assertNotEqual(session._comparison_results['name'].triage_link, None) self.assertIn('issue=1', session._comparison_results['name'].triage_link) self.assertEqual( session._comparison_results['name'].triage_link_omission_reason, None)
def test_compareFailureRemote(self, auth_mock, init_mock, compare_mock, diff_mock): auth_mock.return_value = (0, None) init_mock.return_value = (0, None) compare_mock.return_value = (1, 'Compare failed') args = createSkiaGoldArgs(local_pixel_tests=False) sgp = skia_gold_properties.SkiaGoldProperties(args) keys_file = os.path.join(self._working_dir, 'keys.json') with open(os.path.join(self._working_dir, 'keys.json'), 'w') as f: json.dump({}, f) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, keys_file, None, None) status, error = session.RunComparison(None, None, None) self.assertEqual( status, skia_gold_session.SkiaGoldSession.StatusCodes. COMPARISON_FAILURE_REMOTE) self.assertEqual(error, 'Compare failed') self.assertEqual(auth_mock.call_count, 1) self.assertEqual(init_mock.call_count, 1) self.assertEqual(compare_mock.call_count, 1) self.assertEqual(diff_mock.call_count, 0)
def test_commandCommonArgs(self, cmd_mock): cmd_mock.return_value = (None, None) args = createSkiaGoldArgs(git_revision='a', local_pixel_tests=False) sgp = skia_gold_properties.SkiaGoldProperties(args) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, None, 'corpus', instance='instance') session.Diff('name', 'png_file') call_args = cmd_mock.call_args[0][0] self.assertIn('diff', call_args) assertArgWith(self, call_args, '--corpus', 'corpus') assertArgWith(self, call_args, '--instance', 'instance') assertArgWith(self, call_args, '--input', 'png_file') assertArgWith(self, call_args, '--test', 'name') assertArgWith(self, call_args, '--work-dir', self._working_dir) i = call_args.index('--out-dir') # The output directory should not be a subdirectory of the working # directory. self.assertNotIn(self._working_dir, call_args[i + 1])
def test_commandCommonArgs(self, cmd_mock): cmd_mock.return_value = (None, None) args = createSkiaGoldArgs(git_revision='a') sgp = skia_gold_properties.SkiaGoldProperties(args) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, 'keys_file', 'corpus', instance='instance') session.Initialize() call_args = cmd_mock.call_args[0][0] self.assertIn('imgtest', call_args) self.assertIn('init', call_args) self.assertIn('--passfail', call_args) assertArgWith(self, call_args, '--instance', 'instance') assertArgWith(self, call_args, '--corpus', 'corpus') assertArgWith(self, call_args, '--keys-file', 'keys_file') assertArgWith(self, call_args, '--work-dir', self._working_dir) assertArgWith(self, call_args, '--failure-file', session._triage_link_file) assertArgWith(self, call_args, '--commit', 'a')
def test_validOmissionOnIoError(self, cmd_mock): cmd_mock.return_value = (1, None) args = createSkiaGoldArgs(git_revision='a') sgp = skia_gold_properties.SkiaGoldProperties(args) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, 'keys_file', None, None) def DeleteTriageLinkFile(_): os.remove(session._triage_link_file) return (1, None) cmd_mock.side_effect = DeleteTriageLinkFile rc, _ = session.Compare('name', 'png_file') self.assertEqual(rc, 1) self.assertEqual(session._comparison_results['name'].triage_link, None) self.assertNotEqual( session._comparison_results['name'].triage_link_omission_reason, None) self.assertIn( 'Failed to read', session._comparison_results['name'].triage_link_omission_reason)
def test_individualLinkOnCi(self, cmd_mock): args = createSkiaGoldArgs(git_revision='a') sgp = skia_gold_properties.SkiaGoldProperties(args) session = skia_gold_session.SkiaGoldSession(self._working_dir, sgp, 'keys_file', None, None) def WriteTriageLinkFile(_): with open(session._triage_link_file, 'w') as f: f.write('foobar') return (1, None) cmd_mock.side_effect = WriteTriageLinkFile rc, _ = session.Compare('name', 'png_file') self.assertEqual(rc, 1) self.assertNotEqual(session._comparison_results['name'].triage_link, None) self.assertEqual(session._comparison_results['name'].triage_link, 'foobar') self.assertEqual( session._comparison_results['name'].triage_link_omission_reason, None)