def testInit(self): """Construct an empty or seeded AutomountMap.""" self.assertEqual(automount.AutomountMap, type(automount.AutomountMap()), msg='failed to create an empty AutomountMap') amap = automount.AutomountMap([self._good_entry]) self.assertEqual(self._good_entry, amap.PopItem(), msg='failed to seed AutomountMap with list') self.assertRaises(TypeError, automount.AutomountMap, ['string'])
def testUpdateNoMaster(self): """An update skips updating the master map, and approprate sub maps.""" source_entry1 = automount.AutomountMapEntry() source_entry2 = automount.AutomountMapEntry() source_entry1.key = '/home' source_entry2.key = '/auto' source_entry1.location = 'ou=auto.home,ou=automounts' source_entry2.location = 'ou=auto.auto,ou=automounts' source_master = automount.AutomountMap([source_entry1, source_entry2]) local_entry1 = automount.AutomountMapEntry() local_entry2 = automount.AutomountMapEntry() local_entry1.key = '/home' local_entry2.key = '/auto' local_entry1.location = '/etc/auto.home' local_entry2.location = '/etc/auto.null' local_master = automount.AutomountMap([local_entry1, local_entry2]) source_mock = self.mox.CreateMock(source.Source) # return the source master map source_mock.GetAutomountMasterMap().AndReturn(source_master) # the auto.home cache cache_home = self.mox.CreateMock(caches.Cache) # GetMapLocation() is called, and set to the master map map_entry cache_home.GetMapLocation().AndReturn('/etc/auto.home') # the auto.auto cache cache_auto = self.mox.CreateMock(caches.Cache) # GetMapLocation() is called, and set to the master map map_entry cache_auto.GetMapLocation().AndReturn('/etc/auto.auto') # the auto.master cache, which should not be written to cache_master = self.mox.CreateMock(caches.Cache) cache_master.GetMap().AndReturn(local_master) self.mox.StubOutWithMock(cache_factory, 'Create') cache_factory.Create(mox.IgnoreArg(), mox.IgnoreArg(), automount_mountpoint=None).AndReturn(cache_master) cache_factory.Create(mox.IgnoreArg(), mox.IgnoreArg(), automount_mountpoint='/home').AndReturn(cache_home) cache_factory.Create(mox.IgnoreArg(), mox.IgnoreArg(), automount_mountpoint='/auto').AndReturn(cache_auto) skip = map_updater.AutomountUpdater.OPT_LOCAL_MASTER updater = map_updater.AutomountUpdater( config.MAP_AUTOMOUNT, self.workdir, {skip: 'yes'}) self.mox.StubOutClassWithMocks(map_updater, 'MapUpdater') updater_home = map_updater.MapUpdater(config.MAP_AUTOMOUNT, self.workdir, {'local_automount_master': 'yes'}, automount_mountpoint='/home') updater_home.UpdateCacheFromSource(cache_home, source_mock, True, False, 'ou=auto.home,ou=automounts').AndReturn(0) self.mox.ReplayAll() updater.UpdateFromSource(source_mock)
def testUpdateCatchesMissingMaster(self): """Gracefully handle a missing local master maps.""" # use an empty master map from the source, to avoid mocking out already # tested code master_map = automount.AutomountMap() source_mock = self.mox.CreateMockAnything() source_mock.GetAutomountMasterMap().AndReturn(master_map) cache_mock = self.mox.CreateMock(caches.Cache) # raise error on GetMap() cache_mock.GetMap().AndRaise(error.CacheNotFound) skip = map_updater.AutomountUpdater.OPT_LOCAL_MASTER cache_options = {skip: 'yes'} self.mox.StubOutWithMock(cache_factory, 'Create') cache_factory.Create( cache_options, 'automount', automount_mountpoint=None).AndReturn(cache_mock) self.mox.ReplayAll() updater = map_updater.AutomountUpdater( config.MAP_AUTOMOUNT, self.workdir, cache_options) return_value = updater.UpdateFromSource(source_mock) self.assertEqual(return_value, 1)
def testUpdate(self): """An update gets a master map and updates each entry.""" map_entry1 = automount.AutomountMapEntry() map_entry2 = automount.AutomountMapEntry() map_entry1.key = '/home' map_entry2.key = '/auto' map_entry1.location = 'ou=auto.home,ou=automounts' map_entry2.location = 'ou=auto.auto,ou=automounts' master_map = automount.AutomountMap([map_entry1, map_entry2]) source_mock = self.mox.CreateMock(source.Source) # return the master map source_mock.GetAutomountMasterMap().AndReturn(master_map) # the auto.home cache cache_home = self.mox.CreateMock(caches.Cache) # GetMapLocation() is called, and set to the master map map_entry cache_home.GetMapLocation().AndReturn('/etc/auto.home') # the auto.auto cache cache_auto = self.mox.CreateMock(caches.Cache) # GetMapLocation() is called, and set to the master map map_entry cache_auto.GetMapLocation().AndReturn('/etc/auto.auto') # the auto.master cache cache_master = self.mox.CreateMock(caches.Cache) self.mox.StubOutWithMock(cache_factory, 'Create') cache_factory.Create(mox.IgnoreArg(), 'automount', automount_mountpoint='/home').AndReturn(cache_home) cache_factory.Create(mox.IgnoreArg(), 'automount', automount_mountpoint='/auto').AndReturn(cache_auto) cache_factory.Create(mox.IgnoreArg(), 'automount', automount_mountpoint=None).AndReturn(cache_master) updater = map_updater.AutomountUpdater( config.MAP_AUTOMOUNT, self.workdir, {}) self.mox.StubOutClassWithMocks(map_updater, 'MapUpdater') updater_home = map_updater.MapUpdater(config.MAP_AUTOMOUNT, self.workdir, {}, automount_mountpoint='/home') updater_home.UpdateCacheFromSource(cache_home, source_mock, True, False, 'ou=auto.home,ou=automounts').AndReturn(0) updater_auto = map_updater.MapUpdater(config.MAP_AUTOMOUNT, self.workdir, {}, automount_mountpoint='/auto') updater_auto.UpdateCacheFromSource(cache_auto, source_mock, True, False, 'ou=auto.auto,ou=automounts').AndReturn(0) updater_master = map_updater.MapUpdater(config.MAP_AUTOMOUNT, self.workdir, {}) updater_master.FullUpdateFromMap(cache_master, master_map).AndReturn(0) self.mox.ReplayAll() updater.UpdateFromSource(source_mock) self.assertEqual(map_entry1.location, '/etc/auto.home') self.assertEqual(map_entry2.location, '/etc/auto.auto')
def testAdd(self): """Add throws an error for objects it can't verify.""" amap = automount.AutomountMap() entry = self._good_entry self.assertTrue(amap.Add(entry), msg='failed to append new entry.') self.assertEqual(1, len(amap), msg='unexpected size for Map.') ret_entry = amap.PopItem() self.assertEqual(ret_entry, entry, msg='failed to pop correct entry.') pentry = passwd.PasswdMapEntry() pentry.name = 'foo' pentry.uid = 10 pentry.gid = 10 self.assertRaises(TypeError, amap.Add, pentry)
def testGetAutomountMapMetadata(self): # need to stub out GetSingleMapMetadata (tested above) and then # stub out cache_factory.Create to return a cache mock that spits # out an iterable map for the function to use. # stub out GetSingleMapMetadata class DummyStatus(command.Status): def GetSingleMapMetadata(self, unused_map_name, unused_conf, automount_mountpoint=None, epoch=False): return { 'map': 'map_name', 'last-modify-timestamp': 'foo', 'last-update-timestamp': 'bar' } # the master map to loop over master_map = automount.AutomountMap() master_map.Add( automount.AutomountMapEntry({ 'key': '/home', 'location': '/etc/auto.home' })) master_map.Add( automount.AutomountMapEntry({ 'key': '/auto', 'location': '/etc/auto.auto' })) # mock out a cache to return the master map cache_mock = self.mox.CreateMock(caches.Cache) cache_mock.GetMap().AndReturn(master_map) self.mox.StubOutWithMock(cache_factory, 'Create') cache_factory.Create(self.conf.options[config.MAP_AUTOMOUNT].cache, config.MAP_AUTOMOUNT, automount_mountpoint=None).AndReturn(cache_mock) self.mox.ReplayAll() c = DummyStatus() value_list = c.GetAutomountMapMetadata(self.conf) self.assertEqual(9, len(value_list))
def testUpdateAutomounts(self): self.mox.StubOutClassWithMocks(lock, 'PidFile') lock_mock = lock.PidFile(filename=None) lock_mock.Lock(force=False).AndReturn(True) lock_mock.Locked().AndReturn(True) lock_mock.Unlock() self.conf.maps = [config.MAP_AUTOMOUNT] self.conf.cache = 'dummy' modify_stamp = 1 map_entry = automount.AutomountMapEntry() map_entry.key = '/home' map_entry.location = 'foo' automount_map = automount.AutomountMap([map_entry]) automount_map.SetModifyTimestamp(modify_stamp) source_mock = self.mox.CreateMock(source.Source) source_mock.GetAutomountMasterMap().AndReturn(automount_map) source_mock.GetMap(config.MAP_AUTOMOUNT, location='foo').AndReturn(automount_map) self.mox.StubOutWithMock(source_factory, 'Create') source_factory.Create(self.conf.options[ config.MAP_PASSWORD].source).AndReturn(source_mock) cache_mock = self.mox.CreateMock(caches.Cache) cache_mock.GetMapLocation().AndReturn('home') cache_mock.WriteMap(map_data=automount_map).AndReturn(0) cache_mock.WriteMap(map_data=automount_map).AndReturn(0) self.mox.StubOutWithMock(cache_factory, 'Create') cache_factory.Create( self.conf.options[config.MAP_AUTOMOUNT].cache, config.MAP_AUTOMOUNT, automount_mountpoint='/home').AndReturn(cache_mock) cache_factory.Create(self.conf.options[config.MAP_AUTOMOUNT].cache, config.MAP_AUTOMOUNT, automount_mountpoint=None).AndReturn(cache_mock) self.mox.ReplayAll() c = command.Update() self.assertEquals( 0, c.UpdateMaps(self.conf, incremental=True, force_write=False))
def __init__(self, conf, map_name, automount_mountpoint=None): """Initialise the Cache object. Args: conf: A dictionary of key/value pairs map_name: A string representation of the map type automount_mountpoint: A string containing the automount mountpoint, used only by automount maps. Raises: UnsupportedMap: for map types we don't know about """ super(Cache, self).__init__() # Set up a logger for our children self.log = logging.getLogger(self.__class__.__name__) # Store config info self.conf = conf self.output_dir = conf.get('dir', '.') self.automount_mountpoint = automount_mountpoint self.map_name = map_name # Setup the map we may be asked to load our cache into. if map_name == config.MAP_PASSWORD: self.data = passwd.PasswdMap() elif map_name == config.MAP_SSHKEY: self.data = sshkey.SshkeyMap() elif map_name == config.MAP_GROUP: self.data = group.GroupMap() elif map_name == config.MAP_SHADOW: self.data = shadow.ShadowMap() elif map_name == config.MAP_NETGROUP: self.data = netgroup.NetgroupMap() elif map_name == config.MAP_AUTOMOUNT: self.data = automount.AutomountMap() else: raise error.UnsupportedMap('Cache does not support %s' % map_name)
def CreateMap(self): """Return a AutomountMap instance.""" return automount.AutomountMap()