def testRunCommandShortCmdline(self):
   """Verify short command lines execute env settings directly."""
   with remote_access.RemoteDeviceHandler(remote_access.TEST_IP) as device:
     self.PatchObject(remote_access.RemoteDevice, 'CopyToWorkDir',
                      side_effect=Exception('should not be copying files'))
     self.rsh_mock.AddCmdResult(partial_mock.In('runit'))
     device.run(['runit'], extra_env={'VAR': 'val'})
  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)
Esempio n. 3
0
  def testNoDeviceBaseDir(self):
    """Tests base_dir=None."""
    command = ['echo', 'foo']
    expected_output = 'foo'
    self.rsh_mock.AddCmdResult(command, output=expected_output)

    with remote_access.RemoteDeviceHandler('1.1.1.1', base_dir=None) as device:
      self.assertEqual(expected_output,
                       device.BaseRunCommand(['echo', 'foo']).output)
 def testRunCommandLongCmdline(self):
   """Verify long command lines execute env settings via script."""
   with remote_access.RemoteDeviceHandler(remote_access.TEST_IP) as device:
     self._SetupRemoteTempDir()
     m = self.PatchObject(remote_access.RemoteDevice, 'CopyToWorkDir')
     self.rsh_mock.AddCmdResult(partial_mock.In('runit'))
     device.run(['runit'], extra_env={'VAR': 'v' * 1024 * 1024})
     # We'll assume that the test passed when it tries to copy a file to the
     # remote side (the shell script to run indirectly).
     self.assertEqual(m.call_count, 1)
  def testCommands(self):
    """Tests simple run() and BaseRunCommand() usage."""
    command = ['echo', 'foo']
    expected_output = 'foo'
    self.rsh_mock.AddCmdResult(command, output=expected_output)
    self._SetupRemoteTempDir()

    with remote_access.RemoteDeviceHandler(remote_access.TEST_IP) as device:
      self.assertEqual(expected_output, device.run(['echo', 'foo']).stdout)
      self.assertEqual(expected_output,
                       device.BaseRunCommand(['echo', 'foo']).output)
  def testCommandsExtraEnv(self):
    """Tests simple RunCommand() usage with extra_env arg."""
    self._SetupRemoteTempDir()

    with remote_access.RemoteDeviceHandler(remote_access.TEST_IP) as device:
      # RemoteSh accepts cmd as either string or list, so try both.
      self.rsh_mock.AddCmdResult(['VAR=val', 'echo', 'foo'], stdout='foo')
      self.assertEqual(
          'foo',
          device.run(['echo', 'foo'], extra_env={'VAR': 'val'}).stdout)

      self.rsh_mock.AddCmdResult('VAR=val echo foo', stdout='foo')
      self.assertEqual(
          'foo',
          device.run('echo foo', extra_env={'VAR': 'val'}, shell=True).stdout)
Esempio n. 7
0
  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('mktemp'))
      _ = device.work_dir
      self.assertEqual(self.rsh_mock.call_count, 1)

      # Add a mock for the clean up logic.
      self.rsh_mock.AddCmdResult(partial_mock.In('rm'))

    self.assertEqual(self.rsh_mock.call_count, 2)