def test_update_cmdb_reuse(self): data = [{'a': 1, 'used': 1}] var = {'a': 1} result = cmdb.update_cmdb(data, var, var, False) self.assertTrue(result, cmdb) self.assertEqual(data, [{'a': 1, 'used': 1}]) self.assertEqual(var, {'a': 1, 'used': 1})
def test_update_cmdb_reuse2(self): data = [{'a': 1, 'b': 1, 'c': 1, 'used': 1}] cmdb_result = [{'a': 1, 'b': 2, 'c': 1, 'used': 1}] var = {'a': 1, 'b': 2} pref = {'a': 1} result = cmdb.update_cmdb(data, var, pref, False) self.assertTrue(result, cmdb_result) self.assertEqual(data, cmdb_result) self.assertEqual(var, {'a': 1, 'b': 2, 'c': 1, 'used': 1})
def find_match(self, hw_items): '''Finds an hardware profile matching the hardware items in the state If a profiles matches, its count is decremented. Returns the name of the matching profile. ''' idx = 0 times = '*' name = None valid_roles = [] for name, times in self._data: LOG.info('testing %s' % name) if times == '*' or int(times) > 0: valid_roles.append(name) specs = self._load_specs(name) var = {} var2 = {} if matcher.match_all(hw_items, specs, var, var2): LOG.info('Specs %s matches' % name) forced = (var2 != {}) if var2 == {}: var2 = var if times != '*': self._data[idx] = (name, int(times) - 1) LOG.info('Decrementing %s to %d' % (name, int(times) - 1)) dbase = cmdb.load_cmdb(self._cfg_dir, name) if dbase: if cmdb.update_cmdb(dbase, var, var2, forced): cmdb.save_cmdb(self._cfg_dir, name, dbase) else: idx += 1 continue return name, var idx += 1 else: if not valid_roles: raise StateError('No more role available in %s' % (self._state_filename,)) else: raise StateError( 'Unable to match requirements on the following available ' 'roles in %s: %s' % (self._cfg_dir, ', '.join(valid_roles)))