2012-08-08 13:43:34 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-12-11 10:23:37 +00:00
|
|
|
'''
|
2012-08-08 13:43:34 +00:00
|
|
|
tests.unit.utils.path_join_test
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-12-11 10:23:37 +00:00
|
|
|
:codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)`
|
|
|
|
:copyright: © 2012 by the SaltStack Team, see AUTHORS for more details.
|
2012-08-08 13:43:34 +00:00
|
|
|
:license: Apache 2.0, see LICENSE for more details.
|
2012-12-11 10:23:37 +00:00
|
|
|
'''
|
2012-08-08 13:43:34 +00:00
|
|
|
|
2012-12-11 10:23:37 +00:00
|
|
|
# Import python libs
|
2012-08-08 13:43:34 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import posixpath
|
|
|
|
import ntpath
|
|
|
|
import platform
|
|
|
|
import tempfile
|
|
|
|
|
2012-12-11 10:23:37 +00:00
|
|
|
# Import salt libs
|
2013-06-24 22:53:59 +00:00
|
|
|
try:
|
|
|
|
import integration
|
|
|
|
except ImportError:
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
sys.path.insert(
|
|
|
|
0, os.path.abspath(
|
|
|
|
os.path.join(
|
|
|
|
os.path.dirname(__file__), '../../'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
import integration
|
2012-08-08 13:43:34 +00:00
|
|
|
from salt.utils import path_join
|
|
|
|
|
2013-06-24 22:53:59 +00:00
|
|
|
# Import Salt Testing libs
|
|
|
|
from salttesting import TestCase
|
|
|
|
|
2012-09-07 17:04:14 +00:00
|
|
|
|
2012-08-08 13:43:34 +00:00
|
|
|
class PathJoinTestCase(TestCase):
|
|
|
|
|
|
|
|
PLATFORM_FUNC = platform.system
|
|
|
|
BUILTIN_MODULES = sys.builtin_module_names
|
|
|
|
|
|
|
|
NIX_PATHS = (
|
|
|
|
(('/', 'key'), '/key'),
|
2012-09-07 17:04:14 +00:00
|
|
|
(('/etc/salt', '/etc/salt/pki'), '/etc/salt/etc/salt/pki'),
|
2012-08-08 13:43:34 +00:00
|
|
|
(('/usr/local', '/etc/salt/pki'), '/usr/local/etc/salt/pki')
|
2012-09-07 17:04:14 +00:00
|
|
|
|
2012-08-08 13:43:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
WIN_PATHS = (
|
|
|
|
(('c:', 'temp', 'foo'), 'c:\\temp\\foo'),
|
|
|
|
(('c:', r'\temp', r'\foo'), 'c:\\temp\\foo'),
|
|
|
|
(('c:\\', r'\temp', r'\foo'), 'c:\\temp\\foo'),
|
|
|
|
((r'c:\\', r'\temp', r'\foo'), 'c:\\temp\\foo'),
|
|
|
|
(('c:', r'\temp', r'\foo', 'bar'), 'c:\\temp\\foo\\bar'),
|
|
|
|
(('c:', r'\temp', r'\foo\bar'), 'c:\\temp\\foo\\bar'),
|
|
|
|
(('c', r'\temp', r'\foo\bar'), 'c:\\temp\\foo\\bar')
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_nix_paths(self):
|
|
|
|
if platform.system().lower() == "windows":
|
|
|
|
self.skipTest(
|
|
|
|
"Windows platform found. not running *nix path_join tests"
|
|
|
|
)
|
|
|
|
for idx, (parts, expected) in enumerate(self.NIX_PATHS):
|
|
|
|
path = path_join(*parts)
|
|
|
|
self.assertEqual(
|
|
|
|
'{0}: {1}'.format(idx, path),
|
|
|
|
'{0}: {1}'.format(idx, expected)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_windows_paths(self):
|
|
|
|
if platform.system().lower() != "windows":
|
|
|
|
self.skipTest(
|
2012-09-07 17:04:14 +00:00
|
|
|
'Non windows platform found. not running non patched os.path '
|
|
|
|
'path_join tests'
|
2012-08-08 13:43:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for idx, (parts, expected) in enumerate(self.WIN_PATHS):
|
|
|
|
path = path_join(*parts)
|
|
|
|
self.assertEqual(
|
|
|
|
'{0}: {1}'.format(idx, path),
|
|
|
|
'{0}: {1}'.format(idx, expected)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_windows_paths_patched_path_module(self):
|
|
|
|
if platform.system().lower() == "windows":
|
|
|
|
self.skipTest(
|
2012-09-07 17:04:14 +00:00
|
|
|
'Windows platform found. not running patched os.path '
|
|
|
|
'path_join tests'
|
2012-08-08 13:43:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.__patch_path()
|
|
|
|
|
|
|
|
for idx, (parts, expected) in enumerate(self.WIN_PATHS):
|
|
|
|
path = path_join(*parts)
|
|
|
|
self.assertEqual(
|
|
|
|
'{0}: {1}'.format(idx, path),
|
|
|
|
'{0}: {1}'.format(idx, expected)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.__unpatch_path()
|
|
|
|
|
|
|
|
def __patch_path(self):
|
|
|
|
import imp
|
|
|
|
modules = list(self.BUILTIN_MODULES[:])
|
|
|
|
modules.pop(modules.index('posix'))
|
|
|
|
modules.append('nt')
|
|
|
|
|
|
|
|
code = """'''Salt unittest loaded NT module'''"""
|
|
|
|
module = imp.new_module('nt')
|
|
|
|
exec code in module.__dict__
|
|
|
|
sys.modules['nt'] = module
|
|
|
|
|
|
|
|
sys.builtin_module_names = modules
|
|
|
|
platform.system = lambda: "windows"
|
|
|
|
|
|
|
|
for module in (ntpath, os, os.path, tempfile):
|
|
|
|
reload(module)
|
|
|
|
|
|
|
|
def __unpatch_path(self):
|
|
|
|
del sys.modules['nt']
|
|
|
|
sys.builtin_module_names = self.BUILTIN_MODULES[:]
|
|
|
|
platform.system = self.PLATFORM_FUNC
|
|
|
|
|
|
|
|
for module in (posixpath, os, os.path, tempfile, platform):
|
|
|
|
reload(module)
|
|
|
|
|
2013-06-24 22:53:59 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(PathJoinTestCase, needs_daemon=False)
|