Skip to content

Commit

Permalink
Merge pull request #105 from 3c2b2ff5/master
Browse files Browse the repository at this point in the history
formated project with yapf
  • Loading branch information
jaqx0r authored Nov 6, 2019
2 parents 8917cc6 + ddf0770 commit 1c5bfd9
Show file tree
Hide file tree
Showing 60 changed files with 11,902 additions and 11,687 deletions.
428 changes: 212 additions & 216 deletions examples/authorized-keys-command.py

Large diffs are not rendered by default.

399 changes: 202 additions & 197 deletions nss_cache/app.py

Large diffs are not rendered by default.

182 changes: 92 additions & 90 deletions nss_cache/app_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,95 +27,97 @@


class TestNssCacheApp(unittest.TestCase):
"""Unit tests for NssCacheApp class."""

def setUp(self):
dev_null = io.StringIO()
self.stdout = sys.stdout
sys.stdout = dev_null

def tearDown(self):
sys.stdout = self.stdout

def testRun(self):
return_code = app.NssCacheApp().Run([], {})
self.assertEqual(os.EX_USAGE, return_code)

def testParseGlobalOptions(self):
a = app.NssCacheApp()
(options, args) = a.parser.parse_args(['-d', '-v', 'command'])
self.assertNotEqual(None, options.debug)
self.assertNotEqual(None, options.verbose)
self.assertEqual(['command'], args)

def testParseCommandLineDebug(self):
a = app.NssCacheApp()
(options, args) = a.parser.parse_args(['-d'])
self.assertNotEqual(None, options.debug)
(options, args) = a.parser.parse_args(['--debug'])
self.assertNotEqual(None, options.debug)
a.Run(['-d'], {})
self.assertEqual(logging.DEBUG, a.log.getEffectiveLevel())

def testParseCommandLineVerbose(self):
a = app.NssCacheApp()
(options, args) = a.parser.parse_args(['-v'])
self.assertNotEqual(None, options.verbose)
self.assertEqual([], args)
(options, args) = a.parser.parse_args(['--verbose'])
self.assertNotEqual(None, options.verbose)
self.assertEqual([], args)
a.Run(['-v'], {})
self.assertEqual(logging.INFO, a.log.getEffectiveLevel())

def testParseCommandLineVerboseDebug(self):
a = app.NssCacheApp()
a.Run(['-v', '-d'], {})
self.assertEqual(logging.DEBUG, a.log.getEffectiveLevel())

def testParseCommandLineConfigFile(self):
a = app.NssCacheApp()
(options, args) = a.parser.parse_args(['-c', 'file'])
self.assertNotEqual(None, options.config_file)
self.assertEqual([], args)
(options, args) = a.parser.parse_args(['--config-file', 'file'])
self.assertNotEqual(None, options.config_file)
self.assertEqual([], args)

def testBadOptionsCauseNoExit(self):
a = app.NssCacheApp()
stderr_buffer = io.StringIO()
old_stderr = sys.stderr
sys.stderr = stderr_buffer
self.assertEqual(2, a.Run(['--invalid'], {}))
sys.stderr = old_stderr

def testHelpOptionPrintsGlobalHelp(self):
stdout_buffer = io.StringIO()
a = app.NssCacheApp()
old_stdout = sys.stdout
sys.stdout = stdout_buffer
self.assertEqual(0, a.Run(['--help'], {}))
sys.stdout = old_stdout
self.assertNotEqual(0, stdout_buffer.tell())
(prelude, usage, commands, options) = stdout_buffer.getvalue().split('\n\n')
self.assertTrue(prelude.startswith('nsscache synchronises'))
expected_str = 'Usage: nsscache [global options] command [command options]'
self.assertEqual(expected_str, usage)
self.assertTrue(commands.startswith('commands:'))
self.assertTrue(options.startswith('Options:'))
self.assertTrue(options.find('show this help message and exit') >= 0)

def testHelpCommandOutput(self):
# trap stdout into a StringIO
stdout_buffer = io.StringIO()
a = app.NssCacheApp()
old_stdout = sys.stdout
sys.stdout = stdout_buffer
self.assertEqual(0, a.Run(['help'], {}))
sys.stdout = old_stdout
self.assertNotEqual(0, stdout_buffer.tell())
self.assertTrue(stdout_buffer.getvalue().find('nsscache synchronises') >= 0)
"""Unit tests for NssCacheApp class."""

def setUp(self):
dev_null = io.StringIO()
self.stdout = sys.stdout
sys.stdout = dev_null

def tearDown(self):
sys.stdout = self.stdout

def testRun(self):
return_code = app.NssCacheApp().Run([], {})
self.assertEqual(os.EX_USAGE, return_code)

def testParseGlobalOptions(self):
a = app.NssCacheApp()
(options, args) = a.parser.parse_args(['-d', '-v', 'command'])
self.assertNotEqual(None, options.debug)
self.assertNotEqual(None, options.verbose)
self.assertEqual(['command'], args)

def testParseCommandLineDebug(self):
a = app.NssCacheApp()
(options, args) = a.parser.parse_args(['-d'])
self.assertNotEqual(None, options.debug)
(options, args) = a.parser.parse_args(['--debug'])
self.assertNotEqual(None, options.debug)
a.Run(['-d'], {})
self.assertEqual(logging.DEBUG, a.log.getEffectiveLevel())

def testParseCommandLineVerbose(self):
a = app.NssCacheApp()
(options, args) = a.parser.parse_args(['-v'])
self.assertNotEqual(None, options.verbose)
self.assertEqual([], args)
(options, args) = a.parser.parse_args(['--verbose'])
self.assertNotEqual(None, options.verbose)
self.assertEqual([], args)
a.Run(['-v'], {})
self.assertEqual(logging.INFO, a.log.getEffectiveLevel())

def testParseCommandLineVerboseDebug(self):
a = app.NssCacheApp()
a.Run(['-v', '-d'], {})
self.assertEqual(logging.DEBUG, a.log.getEffectiveLevel())

def testParseCommandLineConfigFile(self):
a = app.NssCacheApp()
(options, args) = a.parser.parse_args(['-c', 'file'])
self.assertNotEqual(None, options.config_file)
self.assertEqual([], args)
(options, args) = a.parser.parse_args(['--config-file', 'file'])
self.assertNotEqual(None, options.config_file)
self.assertEqual([], args)

def testBadOptionsCauseNoExit(self):
a = app.NssCacheApp()
stderr_buffer = io.StringIO()
old_stderr = sys.stderr
sys.stderr = stderr_buffer
self.assertEqual(2, a.Run(['--invalid'], {}))
sys.stderr = old_stderr

def testHelpOptionPrintsGlobalHelp(self):
stdout_buffer = io.StringIO()
a = app.NssCacheApp()
old_stdout = sys.stdout
sys.stdout = stdout_buffer
self.assertEqual(0, a.Run(['--help'], {}))
sys.stdout = old_stdout
self.assertNotEqual(0, stdout_buffer.tell())
(prelude, usage, commands,
options) = stdout_buffer.getvalue().split('\n\n')
self.assertTrue(prelude.startswith('nsscache synchronises'))
expected_str = 'Usage: nsscache [global options] command [command options]'
self.assertEqual(expected_str, usage)
self.assertTrue(commands.startswith('commands:'))
self.assertTrue(options.startswith('Options:'))
self.assertTrue(options.find('show this help message and exit') >= 0)

def testHelpCommandOutput(self):
# trap stdout into a StringIO
stdout_buffer = io.StringIO()
a = app.NssCacheApp()
old_stdout = sys.stdout
sys.stdout = stdout_buffer
self.assertEqual(0, a.Run(['help'], {}))
sys.stdout = old_stdout
self.assertNotEqual(0, stdout_buffer.tell())
self.assertTrue(
stdout_buffer.getvalue().find('nsscache synchronises') >= 0)


# TODO(jaq): app.Run() invocation of command_callable is tested by inspection
Expand Down Expand Up @@ -168,4 +170,4 @@ def testHelpCommandOutput(self):
# a.log.verbose('logged at level verbose')

if __name__ == '__main__':
unittest.main()
unittest.main()
89 changes: 45 additions & 44 deletions nss_cache/caches/cache_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""Package level factory implementation for cache implementations.
We use a factory instead of relying on the __init__.py module to register cache
implementations at import time. This is much more reliable.
We use a factory instead of relying on the __init__.py module to
register cache implementations at import time. This is much more
reliable.
"""

__author__ = 'springer@google.com (Matthew Springer)'
Expand All @@ -30,54 +31,54 @@


def RegisterImplementation(cache_name, map_name, cache):
"""Register a Cache implementation with the CacheFactory.
"""Register a Cache implementation with the CacheFactory.
Child modules are expected to call this method in the file-level scope
so that the CacheFactory is aware of them.
Child modules are expected to call this method in the file-level scope
so that the CacheFactory is aware of them.
Args:
cache_name: (string) The name of the NSS backend.
map_name: (string) The name of the map handled by this Cache.
cache: A class type that is a subclass of Cache.
Args:
cache_name: (string) The name of the NSS backend.
map_name: (string) The name of the map handled by this Cache.
cache: A class type that is a subclass of Cache.
Returns: Nothing
"""
global _cache_implementations
if cache_name not in _cache_implementations:
logging.info('Registering [%s] cache for [%s].', cache_name, map_name)
_cache_implementations[cache_name] = {}
_cache_implementations[cache_name][map_name] = cache
Returns: Nothing
"""
global _cache_implementations
if cache_name not in _cache_implementations:
logging.info('Registering [%s] cache for [%s].', cache_name, map_name)
_cache_implementations[cache_name] = {}
_cache_implementations[cache_name][map_name] = cache


def Create(conf, map_name, automount_mountpoint=None):
"""Cache creation factory method.
Args:
conf: a dictionary of configuration key/value pairs, including one
required attribute 'name'
map_name: a string identifying the map name to handle
automount_mountpoint: A string containing the automount mountpoint, used only
by automount maps.
Returns:
an instance of a Cache
Raises:
RuntimeError: problem instantiating the requested cache
"""
global _cache_implementations
if not _cache_implementations:
raise RuntimeError('no cache implementations exist')
cache_name = conf['name']

if cache_name not in _cache_implementations:
raise RuntimeError('cache not implemented: %r' % (cache_name,))
if map_name not in _cache_implementations[cache_name]:
raise RuntimeError(
'map %r not supported by cache %r' % (map_name, cache_name))

return _cache_implementations[cache_name][map_name](
conf, map_name, automount_mountpoint=automount_mountpoint)
"""Cache creation factory method.
Args:
conf: a dictionary of configuration key/value pairs, including one
required attribute 'name'
map_name: a string identifying the map name to handle
automount_mountpoint: A string containing the automount mountpoint, used only
by automount maps.
Returns:
an instance of a Cache
Raises:
RuntimeError: problem instantiating the requested cache
"""
global _cache_implementations
if not _cache_implementations:
raise RuntimeError('no cache implementations exist')
cache_name = conf['name']

if cache_name not in _cache_implementations:
raise RuntimeError('cache not implemented: %r' % (cache_name,))
if map_name not in _cache_implementations[cache_name]:
raise RuntimeError('map %r not supported by cache %r' %
(map_name, cache_name))

return _cache_implementations[cache_name][map_name](
conf, map_name, automount_mountpoint=automount_mountpoint)


files.RegisterAllImplementations(RegisterImplementation)
Expand Down
40 changes: 20 additions & 20 deletions nss_cache/caches/cache_factory_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""Unit tests for out cache factory"""
"""Unit tests for out cache factory."""

__author__ = 'springer@google.com (Matthew Springer)'

Expand All @@ -25,29 +25,29 @@

class TestCacheFactory(unittest.TestCase):

def testRegister(self):
def testRegister(self):

class DummyCache(caches.Cache):
pass
class DummyCache(caches.Cache):
pass

old_cache_implementations = cache_factory._cache_implementations
cache_factory._cache_implementations = {}
cache_factory.RegisterImplementation('dummy', 'dummy', DummyCache)
self.assertEqual(1, len(cache_factory._cache_implementations))
self.assertEqual(1, len(cache_factory._cache_implementations['dummy']))
self.assertEqual(DummyCache,
cache_factory._cache_implementations['dummy']['dummy'])
cache_factory._cache_implementations = old_cache_implementations
old_cache_implementations = cache_factory._cache_implementations
cache_factory._cache_implementations = {}
cache_factory.RegisterImplementation('dummy', 'dummy', DummyCache)
self.assertEqual(1, len(cache_factory._cache_implementations))
self.assertEqual(1, len(cache_factory._cache_implementations['dummy']))
self.assertEqual(DummyCache,
cache_factory._cache_implementations['dummy']['dummy'])
cache_factory._cache_implementations = old_cache_implementations

def testCreateWithNoImplementations(self):
old_cache_implementations = cache_factory._cache_implementations
cache_factory._cache_implementations = {}
self.assertRaises(RuntimeError, cache_factory.Create, {}, 'map_name')
cache_factory._cache_implementations = old_cache_implementations
def testCreateWithNoImplementations(self):
old_cache_implementations = cache_factory._cache_implementations
cache_factory._cache_implementations = {}
self.assertRaises(RuntimeError, cache_factory.Create, {}, 'map_name')
cache_factory._cache_implementations = old_cache_implementations

def testThatRegularImplementationsArePresent(self):
self.assertEqual(len(cache_factory._cache_implementations), 2)
def testThatRegularImplementationsArePresent(self):
self.assertEqual(len(cache_factory._cache_implementations), 2)


if __name__ == '__main__':
unittest.main()
unittest.main()
Loading

0 comments on commit 1c5bfd9

Please sign in to comment.