Revision da961187

b/lib/utils.py
1785 1785
      # we need to re-raise the exception
1786 1786
      raise
1787 1787

  
1788
def IsNormAbsPath(path):
1789
  """Check whether a path is absolute and also "normal".
1790

  
1791
  This avoids things like /dir/../../other/path to be valid.
1792

  
1793
  """
1794
  return os.path.normpath(path) == path and os.path.isabs(path)
1788 1795

  
1789 1796
def TailFile(fname, lines=20):
1790 1797
  """Return the last lines from a file.
b/test/ganeti.utils_unittest.py
44 44
     ParseUnit, AddAuthorizedKey, RemoveAuthorizedKey, \
45 45
     ShellQuote, ShellQuoteArgs, TcpPing, ListVisibleFiles, \
46 46
     SetEtcHostsEntry, RemoveEtcHostsEntry, FirstFree, OwnIpAddress, \
47
     TailFile, ForceDictType
47
     TailFile, ForceDictType, IsNormAbsPath
48 48

  
49 49
from ganeti.errors import LockError, UnitParseError, GenericError, \
50 50
     ProgrammerError
......
969 969
    self.assertRaises(errors.TypeEnforcementError, self._fdt, {'d': '4 L'})
970 970

  
971 971

  
972
class TestIsAbsNormPath(unittest.TestCase):
973
  """Testing case for IsProcessAlive"""
974

  
975
  def _pathTestHelper(self, path, result):
976
    if result:
977
      self.assert_(IsNormAbsPath(path),
978
          "Path %s should be absolute and normal" % path)
979
    else:
980
      self.assert_(not IsNormAbsPath(path),
981
          "Path %s should not be absolute and normal" % path)
982

  
983
  def testBase(self):
984
    self._pathTestHelper('/etc', True)
985
    self._pathTestHelper('/srv', True)
986
    self._pathTestHelper('etc', False)
987
    self._pathTestHelper('/etc/../root', False)
988
    self._pathTestHelper('/etc/', False)
989

  
972 990
if __name__ == '__main__':
973 991
  unittest.main()

Also available in: Unified diff