diff options
author | Mike Frysinger <vapier@chromium.org> | 2015-05-12 00:30:53 -0400 |
---|---|---|
committer | ChromeOS Commit Bot <chromeos-commit-bot@chromium.org> | 2015-05-13 08:36:08 +0000 |
commit | c99cacbd0c7177fa0fc1df4957e47a1bd0270236 (patch) | |
tree | caad6d3704380ae73609288d7e3d45a0e5e49a13 /lib | |
parent | 231348146015739254fd2978dd172975555e9bdc (diff) | |
download | chromite-c99cacbd0c7177fa0fc1df4957e47a1bd0270236.tar.gz |
remote_access: delay creation of remote temp paths
For some scenarios, we never actually need or use the remote tempdir.
That means the time we spend creating it (two connections) is purely
overhead. Lets turn these two paths into properties which will auto
create them on the device on demand.
BUG=brillo:985
TEST=`cros_debug` delays creation of paths until they're used
TEST=`cros_debug` (w/other speedups) does not run `mkdir` or `mktemp` at all
Change-Id: I6a91c070e29e9b2b86554f8322e5b90db4eb5f43
Reviewed-on: https://chromium-review.googlesource.com/270400
Trybot-Ready: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Yiming Chen <yimingc@chromium.org>
Commit-Queue: Mike Frysinger <vapier@chromium.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/remote_access.py | 36 | ||||
-rw-r--r-- | lib/remote_access_unittest.py | 17 |
2 files changed, 38 insertions, 15 deletions
diff --git a/lib/remote_access.py b/lib/remote_access.py index 509626300..35d9e37fe 100644 --- a/lib/remote_access.py +++ b/lib/remote_access.py @@ -591,8 +591,8 @@ class RemoteDevice(object): self.private_key = private_key self.debug_level = debug_level # The temporary work directories on the device. - self.base_dir = base_dir - self.work_dir = None + self._base_dir = base_dir + self._work_dir = None # Use GetAgent() instead of accessing this directly for deferred connect. self._agent = None self.cleanup_cmds = [] @@ -625,23 +625,31 @@ class RemoteDevice(object): return self._agent def _Connect(self): - """Sets up the SSH connection and device work directories.""" + """Sets up the SSH connection and internal state.""" self._agent = RemoteAccess(self.hostname, self.tempdir.tempdir, port=self.port, username=self.username, private_key=self.private_key) - self._SetupRemoteWorkDir() - - def _SetupRemoteWorkDir(self): - """Setup working directory on the remote device.""" - if self.base_dir: - self.BaseRunCommand(['mkdir', '-p', self.base_dir]) - self.work_dir = self.BaseRunCommand( - ['mktemp', '-d', '--tmpdir=%s' % self.base_dir], + + @property + def work_dir(self): + """The work directory to create on the device. + + This property exists so we can create the remote paths on demand. For + some use cases, it'll never be needed, so skipping creation is faster. + """ + if self._base_dir is None: + return None + + if self._work_dir is None: + self.BaseRunCommand(['mkdir', '-p', self._base_dir]) + self._work_dir = self.BaseRunCommand( + ['mktemp', '-d', '--tmpdir=%s' % self._base_dir], capture_output=True).output.strip() - logging.debug( - 'The temporary working directory on the device is %s', self.work_dir) + logging.debug('The temporary working directory on the device is %s', + self._work_dir) + self.RegisterCleanupCmd(['rm', '-rf', self._work_dir]) - self.RegisterCleanupCmd(['rm', '-rf', self.work_dir]) + return self._work_dir # Since this object is instantiated once per device, we can safely cache the # result of the rsync test. We assume the remote side doesn't go and delete diff --git a/lib/remote_access_unittest.py b/lib/remote_access_unittest.py index 7f575ce2c..d02a52174 100644 --- a/lib/remote_access_unittest.py +++ b/lib/remote_access_unittest.py @@ -21,7 +21,7 @@ from chromite.lib import partial_mock from chromite.lib import remote_access -# pylint: disable=W0212 +# pylint: disable=protected-access class TestNormalizePort(cros_test_lib.TestCase): @@ -270,6 +270,21 @@ class RemoteDeviceTest(cros_test_lib.MockTestCase): self.assertEqual(expected_output, device.BaseRunCommand(['echo', 'foo']).output) + def testDelayedRemoteDirs(self): + """Tests the delayed creation of base_dir/work_dir.""" + with remote_access.RemoteDeviceHandler('1.1.1.1', base_dir='/f') as device: + # Make sure we didn't talk to the remote yet. + self.assertEqual(self.rsh_mock.call_count, 0) + + # The work dir will get automatically created when we use it. + self.rsh_mock.AddCmdResult(partial_mock.In('mkdir')) + self.rsh_mock.AddCmdResult(partial_mock.In('mktemp')) + _ = device.work_dir + self.assertEqual(self.rsh_mock.call_count, 2) + + # Add a mock for the clean up logic. + self.rsh_mock.AddCmdResult(partial_mock.In('rm')) + class USBDeviceTestCase(mdns_unittest.mDnsTestCase): """Base class for USB device related tests.""" |