def _HookRunCommand(rc): rc.AddCmdResult( partial_mock.ListRegex('cros_generate_update_payload'), side_effect=_SimUpdatePayload) rc.AddCmdResult(partial_mock.ListRegex( 'cros_generate_stateful_update_payload'), side_effect=_SimUpdateStatefulPayload)
def testNoLatestVersion(self): """We raise an exception when there is no recent latest version.""" self.sdk_mock.UnMockAttr('GetFullVersion') self.gs_mock.AddCmdResult(partial_mock.ListRegex('cat .*/LATEST-*'), output='', error=self.CAT_ERROR, returncode=1) self.gs_mock.AddCmdResult(partial_mock.ListRegex('ls .*%s' % self.VERSION), output='', error=self.LS_ERROR, returncode=1) self.assertRaises(cros_chrome_sdk.MissingSDK, self.sdk.GetFullVersion, self.VERSION)
def _SetupMissingVersions(self): """Version & Version-1 are missing, but Version-2 exists.""" def _RaiseGSNoSuchKey(*_args, **_kwargs): raise gs.GSNoSuchKey('file does not exist') self.sdk_mock.UnMockAttr('GetFullVersion') self.gs_mock.AddCmdResult(partial_mock.ListRegex('cat .*/LATEST-%s' % self.VERSION), side_effect=_RaiseGSNoSuchKey) self.gs_mock.AddCmdResult(partial_mock.ListRegex( 'cat .*/LATEST-%s' % self.RECENT_VERSION_MISSING), side_effect=_RaiseGSNoSuchKey) self.gs_mock.AddCmdResult(partial_mock.ListRegex( 'cat .*/LATEST-%s' % self.RECENT_VERSION_FOUND), output=self.FULL_VERSION_RECENT)
def _HookRunCommand(rc): rc.AddCmdResult(partial_mock.In('list-models'), output='reef\npyro\nelectro') rc.AddCmdResult(partial_mock.In('get'), output='key-123') rc.AddCmdResult(partial_mock.ListRegex('chromeos-firmwareupdate'), output=''' Model: reef BIOS image: BIOS version: Google_Reef.9042.87.1 BIOS (RW) version: Google_Reef.9042.110.0 EC version: reef_v1.1.5900-ab1ee51 EC (RW) version: reef_v1.1.5909-bd1f0c9 Model: pyro BIOS image: BIOS version: Google_Pyro.9042.87.1 BIOS (RW) version: Google_Pyro.9042.110.0 EC version: pyro_v1.1.5900-ab1ee51 EC (RW) version: pyro_v1.1.5909-bd1f0c9 Model: electro BIOS image: BIOS version: Google_Reef.9042.87.1 EC version: reef_v1.1.5900-ab1ee51 EC (RW) version: reef_v1.1.5909-bd1f0c9 ''')
def testVerifyBinpkgPresent(self): """Test case where binpkg is present.""" self.rc.AddCmdResult( partial_mock.ListRegex(r'emerge'), output='\n[binary] %s' % constants.CHROME_CP) commands.VerifyBinpkg(self._buildroot, self._board, constants.CHROME_CP, packages=())
def testBareVersion(self): """Codepath where crbug.com/230190 has been fixed.""" self.sdk_mock.UnMockAttr('GetFullVersion') self.gs_mock.AddCmdResult(partial_mock.ListRegex('ls .*/%s' % self.VERSION), output='results') self.assertEquals(self.VERSION, self.sdk.GetFullVersion(self.VERSION))
def testRename(self): """Branch rename.""" self._Prepare(extra_cmd_args=[ '--branch-name', self.RELEASE_BRANCH_NAME, '--rename-to', 'refs/heads/release-rename' ]) # Simulate source branch existing and destination branch not existing. self.rc_mock.AddCmdResult(partial_mock.ListRegex( 'git show-ref .*%s' % self.RELEASE_BRANCH_NAME), output='SomeSHA1Value') self.rc_mock.AddCmdResult( partial_mock.ListRegex('git show-ref .*release-rename'), returncode=1) self.RunStage() self._VerifyPush(self._run.options.rename_to, rename_from=self.norm_name)
def testListRegex(self): """Verify ListRegex match functionality.""" obj = partial_mock.ListRegex('.*monkey.*') self.assertTrue(obj.Match(['the', 'small monkeys', 'jumped'])) self.assertFalse(obj.Match(['the', 'jumped'])) self.assertFalse(obj.Match(None)) self.assertFalse(obj.Match(1))
def testFullVersionFromFullVersion(self): """Test that a fully specified version is allowed.""" self.sdk_mock.UnMockAttr('GetFullVersion') self.gs_mock.AddCmdResult(partial_mock.ListRegex('cat .*/LATEST-%s' % self.VERSION), output=self.FULL_VERSION) self.assertEqual(self.FULL_VERSION, self.sdk.GetFullVersion(self.FULL_VERSION))
def _SetCheckRebootResult(self, returncode=0, output='', error=''): """Sets the result object fields to mock a specific ssh command. The command is the one used to fetch the boot ID (cat /proc/sys/...) """ self.rsh_mock.AddCmdResult(partial_mock.ListRegex('/proc/sys/.*'), returncode=returncode, output=output, error=error)
def testFullVersionFromRecentLatest(self): """Test full version calculation when there is no matching LATEST- file.""" def _RaiseGSNoSuchKey(*_args, **_kwargs): raise gs.GSNoSuchKey('file does not exist') self.sdk_mock.UnMockAttr('GetFullVersion') self.gs_mock.AddCmdResult(partial_mock.ListRegex('cat .*/LATEST-%s' % self.VERSION), side_effect=_RaiseGSNoSuchKey) self.gs_mock.AddCmdResult(partial_mock.ListRegex( 'cat .*/LATEST-%s' % self.RECENT_VERSION_MISSING), side_effect=_RaiseGSNoSuchKey) self.gs_mock.AddCmdResult(partial_mock.ListRegex( 'cat .*/LATEST-%s' % self.RECENT_VERSION_FOUND), output=self.FULL_VERSION_RECENT) self.assertEquals(self.FULL_VERSION_RECENT, self.sdk.GetFullVersion(self.VERSION))
def testNonCanaryFullVersion(self): """Test full version calculation for a non canary version.""" self.sdk_mock.UnMockAttr('GetFullVersion') self.gs_mock.AddCmdResult(partial_mock.ListRegex( 'cat .*/LATEST-%s' % self.NON_CANARY_VERSION), output=self.FULL_VERSION_NON_CANARY) self.assertEqual(self.FULL_VERSION_NON_CANARY, self.sdk.GetFullVersion(self.NON_CANARY_VERSION))
def testVerifyBinpkgMissing(self): """Test case where binpkg is missing.""" self.rc.AddCmdResult( partial_mock.ListRegex(r'emerge'), output='\n[ebuild] %s' % constants.CHROME_CP) self.assertRaises( commands.MissingBinpkg, commands.VerifyBinpkg, self._buildroot, self._board, constants.CHROME_CP, packages=())
def _MockTarList(self, files): """Mock out tarball content list call. Args: files: A list of contents to return. """ self.rc_mock.AddCmdResult( partial_mock.ListRegex('tar -tf'), output='\n'.join(files))
def testFullVersionFromPlatformVersion(self): """Test full version calculation from the platform version.""" self.sdk_mock.UnMockAttr('GetFullVersion') self.gs_mock.AddCmdResult(partial_mock.ListRegex('cat .*/LATEST-%s' % self.VERSION), output=self.FULL_VERSION) self.assertEqual(self.FULL_VERSION, self.sdk.GetFullVersion(self.VERSION))
def testErrorCodePassthrough(self): """Test that error codes are passed through.""" self.SetupCommandMock() with cros_test_lib.LoggingCapturer(): self.rc_mock.AddCmdResult(partial_mock.ListRegex('-- true'), returncode=5) returncode = self.cmd_mock.inst.Run() self.assertEqual(returncode, 5)
def AssertSynced(self, args): """Assert repo_sync_manifest was run with at least the given args. Args: args: Expected args for repo_sync_manifest. """ self.cmd.rc_mock.assertCommandContains( [partial_mock.ListRegex('.*/repo_sync_manifest')] + args)
def testBadVersion(self): """We raise an exception for a bad version.""" self.sdk_mock.UnMockAttr('GetFullVersion') self.gs_mock.AddCmdResult(partial_mock.ListRegex('ls .*'), output='', error=self.LS_ERROR, returncode=1) self.assertRaises(cros_chrome_sdk.SDKError, self.sdk.GetFullVersion, self.VERSION)
def testDeletion(self): """Branch deletion.""" self._Prepare(extra_cmd_args=[ '--branch-name', self.RELEASE_BRANCH_NAME, '--delete-branch' ]) self.rc_mock.AddCmdResult( partial_mock.ListRegex('git show-ref .*release-test-branch.*'), output='SomeSHA1Value') self.RunStage() self._VerifyPush(self.norm_name, delete=True)
def MockVbutilKey(rc, sha1sum=MOCK_SHA1SUM): """Adds vbutil_key mocks to |rc|""" cmd_output = textwrap.dedent(""" Public Key file: firmware_data_key.vbpubk Algorithm: 7 RSA4096 SHA256 Key Version: 1 Key sha1sum: """ + sha1sum) rc.AddCmdResult(partial_mock.ListRegex('vbutil_key --unpack .*'), output=cmd_output)
def testFullVersion(self): """Test full version calculation.""" def RaiseException(*_args, **_kwargs): raise Exception('boom') self.sdk_mock.UnMockAttr('GetFullVersion') self.gs_mock.AddCmdResult(partial_mock.ListRegex('ls .*/%s' % self.VERSION), error=self.LS_ERROR, returncode=1) self.gs_mock.AddCmdResult(partial_mock.ListRegex('ls .*-%s' % self.VERSION), output=self.FAKE_LS) self.assertEquals(self.FULL_VERSION, self.sdk.GetFullVersion(self.VERSION)) # Test that we access the cache on the next call, rather than checking GS. self.gs_mock.AddCmdResult(partial_mock.ListRegex('ls .*/%s' % self.VERSION), side_effect=RaiseException) self.assertEquals(self.FULL_VERSION, self.sdk.GetFullVersion(self.VERSION))
def PathListRegexFor(self, pid, manifest=None): """Return the test project's path as a ListRegex. Args: pid: The test project ID (e.g. 'chromiumos-overlay'). manifest: The repo_manifest.Manifest to read from. Uses full_manifest if None. Returns: partial_mock.ListRegex for project path. """ return partial_mock.ListRegex('.*/%s' % self.PathFor(pid, manifest))
def testSELinux(self): """Tests behavior of IsSELinuxAvailable() and IsSELinuxEnforced().""" self.rsh_mock.AddCmdResult( partial_mock.ListRegex('which restorecon'), returncode=0) with remote_access.RemoteDeviceHandler(remote_access.TEST_IP) as device: self.rsh_mock.AddCmdResult( partial_mock.ListRegex('test -f'), returncode=0) self.rsh_mock.AddCmdResult( partial_mock.ListRegex('cat /sys/fs/selinux/enforce'), returncode=0, output='1') self.assertEqual(device.IsSELinuxAvailable(), True) self.assertEqual(device.IsSELinuxEnforced(), True) self.rsh_mock.AddCmdResult( partial_mock.ListRegex('test -f'), returncode=0) self.rsh_mock.AddCmdResult( partial_mock.ListRegex('cat /sys/fs/selinux/enforce'), returncode=0, output='0') self.assertEqual(device.IsSELinuxAvailable(), True) self.assertEqual(device.IsSELinuxEnforced(), False) self.rsh_mock.AddCmdResult( partial_mock.ListRegex('test -f'), returncode=1) self.assertEqual(device.IsSELinuxAvailable(), False) self.assertEqual(device.IsSELinuxEnforced(), False)
def testDryRun(self): """Verify we don't push to remote when --debug is set.""" # Simulate branch not existing. self.rc_mock.AddCmdResult(partial_mock.ListRegex( 'git show-ref .*%s' % self.RELEASE_BRANCH_NAME), returncode=1) self._Prepare(extra_cmd_args=[ '--branch-name', self.RELEASE_BRANCH_NAME, '--debug', '--version', self.DEFAULT_VERSION ]) self.RunStage() self.rc_mock.assertCommandContains(('push', ), expected=False)
def testNonCanaryNoLatestVersion(self): """We raise an exception when there is no matching latest non canary.""" self.sdk_mock.UnMockAttr('GetFullVersion') self.gs_mock.AddCmdResult(partial_mock.ListRegex( 'cat .*/LATEST-%s' % self.NON_CANARY_VERSION), output='', error=self.CAT_ERROR, returncode=1) # Set any other query to return a valid version, but we don't expect that # to occur for non canary versions. self.gs_mock.SetDefaultCmdResult(output=self.FULL_VERSION_NON_CANARY) self.assertRaises(cros_chrome_sdk.MissingSDK, self.sdk.GetFullVersion, self.NON_CANARY_VERSION)
def _DoCommand(self, inst, gsutil_cmd, **kwargs): result = self._results['_DoCommand'].LookupResult((gsutil_cmd, ), hook_args=( inst, gsutil_cmd, ), hook_kwargs=kwargs) rc_mock = cros_build_lib_unittest.RunCommandMock() rc_mock.AddCmdResult(partial_mock.ListRegex('gsutil'), result.returncode, result.output, result.error) with rc_mock: return self.backup['_DoCommand'](inst, gsutil_cmd, **kwargs)
def testNoFallbackVersion(self): """Test that all versions are checked before raising an exception.""" def _RaiseGSNoSuchKey(*_args, **_kwargs): raise gs.GSNoSuchKey('file does not exist') self.sdk_mock.UnMockAttr('GetFullVersion') self.gs_mock.AddCmdResult(partial_mock.ListRegex('cat .*/LATEST-*'), side_effect=_RaiseGSNoSuchKey) self.sdk.fallback_versions = 2000000 with cros_test_lib.LoggingCapturer() as logs: self.assertRaises(cros_chrome_sdk.MissingSDK, self.sdk.GetFullVersion, self.VERSION) self.AssertLogsContain(logs, 'LATEST-1.0.0') self.AssertLogsContain(logs, 'LATEST--1.0.0', inverted=True)
def testClang(self): """Verifies clang codepath.""" with cros_test_lib.LoggingCapturer() as logs: cmd_cls = cros_chrome_sdk.ChromeSDKCommand update_sh = os.path.join(self.chrome_src_dir, cmd_cls._CLANG_UPDATE_SH) osutils.Touch(update_sh, makedirs=True) self.rc_mock.AddCmdResult( partial_mock.ListRegex('.*-gcc -dumpversion'), output='4.7.3') self.SetupCommandMock(extra_args=['--clang', '--make']) self.cmd_mock.inst.Run() self.assertTrue( self.FindInPath(self.cmd_mock.env['PATH'], cmd_cls._CLANG_DIR)) self.AssertLogsContain(logs, '%s not found.' % cmd_cls._CLANG_DIR)
def testFullVersionCaching(self): """Test full version calculation and caching.""" def RaiseException(*_args, **_kwargs): raise Exception('boom') self.sdk_mock.UnMockAttr('GetFullVersion') self.gs_mock.AddCmdResult(partial_mock.ListRegex('cat .*/LATEST-%s' % self.VERSION), output=self.FULL_VERSION) self.assertEqual(self.FULL_VERSION, self.sdk.GetFullVersion(self.VERSION)) # Test that we access the cache on the next call, rather than checking GS. self.gs_mock.AddCmdResult(partial_mock.ListRegex('cat .*/LATEST-%s' % self.VERSION), side_effect=RaiseException) self.assertEqual(self.FULL_VERSION, self.sdk.GetFullVersion(self.VERSION)) # Test that we access GS again if the board is changed. self.sdk.board += '2' self.gs_mock.AddCmdResult(partial_mock.ListRegex('cat .*/LATEST-%s' % self.VERSION), output=self.FULL_VERSION + '2') self.assertEqual(self.FULL_VERSION + '2', self.sdk.GetFullVersion(self.VERSION))
def testChromeVersion(self): """We pick up the right LKGM version from the Chrome tree.""" gclient_root = os.path.join(self.tempdir, 'gclient_root') self.PatchObject(gclient, 'FindGclientCheckoutRoot', return_value=gclient_root) self.gs_mock.AddCmdResult(partial_mock.ListRegex('ls .*-%s' % self.PARTIAL_VERSION), output=self.FAKE_LS) lkgm_file = os.path.join(gclient_root, constants.PATH_TO_CHROME_LKGM) osutils.Touch(lkgm_file, makedirs=True) osutils.WriteFile(lkgm_file, self.PARTIAL_VERSION) self.sdk.UpdateDefaultVersion() self.assertEquals(self.sdk.GetDefaultVersion(), self.VERSION) # pylint: disable=E1101 self.assertTrue(gclient.FindGclientCheckoutRoot.called)