def test_le_var(self): specs = [('disk', '$disk', 'size', '$size=le(20)')] lines = [('disk', 'vda', 'size', '20')] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {})) self.assertEqual(arr['disk'], 'vda') self.assertEqual(arr['size'], '20')
def test_backtracklong(self): specs = [ ('disk', 'logical', 'count', '8'), ('disk', '$disk1', 'size', '1000'), ('disk', '$disk1', 'vendor', 'Hitachi'), ('disk', '$disk1', 'model', 'HUA722010CLA330'), ('disk', '$disk1', 'rev', 'R001'), ('disk', '$disk1', 'optimal_io_size', '0'), ('disk', '$disk1', 'physical_block_size', '512'), ('disk', '$disk1', 'rotational', '1'), ('disk', '$disk1', 'Write Cache Enable', '1'), ('disk', '$disk1', 'Read Cache Disable', '0'), ('disk', '$disk2', 'size', '1000'), ('disk', '$disk2', 'vendor', 'Seagate'), ('disk', '$disk2', 'model', 'ST31000528AS'), ('disk', '$disk2', 'rev', 'R001'), ('disk', '$disk2', 'optimal_io_size', '0'), ('disk', '$disk2', 'physical_block_size', '512'), ('disk', '$disk2', 'rotational', '1'), ('disk', '$disk2', 'Write Cache Enable', '1'), ('disk', '$disk2', 'Read Cache Disable', '0'), ('disk', '$disk3', 'size', '1000'), ('disk', '$disk3', 'optimal_io_size', '0'), ('disk', '$disk3', 'physical_block_size', '512'), ('disk', '$disk3', 'rotational', '1'), ('disk', '$disk3', 'Write Cache Enable', '1'), ('disk', '$disk3', 'Read Cache Disable', '0'), ('disk', '$disk4', 'size', '1000'), ('disk', '$disk4', 'optimal_io_size', '0'), ('disk', '$disk4', 'physical_block_size', '512'), ('disk', '$disk4', 'rotational', '1'), ('disk', '$disk4', 'Write Cache Enable', '1'), ('disk', '$disk4', 'Read Cache Disable', '0'), ('disk', '$disk5', 'size', '1000'), ('disk', '$disk5', 'optimal_io_size', '0'), ('disk', '$disk5', 'physical_block_size', '512'), ('disk', '$disk5', 'rotational', '1'), ('disk', '$disk5', 'Write Cache Enable', '1'), ('disk', '$disk5', 'Read Cache Disable', '0'), ('disk', '$disk6', 'size', '1000'), ('disk', '$disk6', 'optimal_io_size', '0'), ('disk', '$disk6', 'physical_block_size', '512'), ('disk', '$disk6', 'rotational', '1'), ('disk', '$disk6', 'Write Cache Enable', '1'), ('disk', '$disk6', 'Read Cache Disable', '0'), ('disk', '$disk7', 'size', '1000'), ('disk', '$disk7', 'optimal_io_size', '0'), ('disk', '$disk7', 'physical_block_size', '512'), ('disk', '$disk7', 'rotational', '1'), ('disk', '$disk7', 'Write Cache Enable', '1'), ('disk', '$disk7', 'Read Cache Disable', '0'), ('disk', '$disk8', 'size', '1000'), ('disk', '$disk8', 'optimal_io_size', '0'), ('disk', '$disk8', 'physical_block_size', '512'), ('disk', '$disk8', 'rotational', '1'), ('disk', '$disk8', 'Write Cache Enable', '1'), ('disk', '$disk8', 'Read Cache Disable', '0'), ] arr = {} self.assertTrue(matcher.match_all(X8_HW, specs, arr, {}))
def test_multiple_vars(self): specs = [ ('disk', 'vda', 'size', '8'), ('disk', 'vdb', 'size', '16'), ] specs2 = [ ('disk', 'vda', 'size', '8'), ('disk', 'vdb', 'size', '8'), ] lines = [ ('disk', 'vda', 'size', '8'), ('disk', 'vdb', 'size', '8'), ] arr = {} self.assertFalse(matcher.match_all(lines, specs, arr, {})) self.assertTrue(matcher.match_all(lines, specs2, arr, {}), lines)
def test_network(self): specs = [('network', '$eth', 'ipv4', 'network(192.168.2.0/24)')] lines = [('network', 'eth0', 'ipv4', '192.168.2.2')] arr = {} if matcher._HAS_IPADDR: self.assertTrue(matcher.match_all(lines, specs, arr, {})) self.assertEqual(arr['eth'], 'eth0')
def test_regexp(self): specs = [('network', '$eth', 'serial', 'regexp(^28:d2:)')] lines = [ ('network', 'eth0', 'serial', '28:d2:44:1b:0a:8b'), ] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {}))
def test_not(self): specs = [('network', '$eth', 'serial', '$mac=not(regexp(^28:d2:))')] lines = [('network', 'eth0', 'serial', '20:d2:44:1b:0a:8b')] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {})) self.assertEqual(arr['eth'], 'eth0') self.assertEqual(arr['mac'], '20:d2:44:1b:0a:8b')
def test_lt(self): specs = [('disk', '$disk', 'size', 'lt(30)')] lines = [ ('disk', 'vda', 'size', '20'), ] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {})) self.assertEqual(arr['disk'], 'vda')
def test_ge(self): specs = [('disk', '$disk', 'size', 'ge(10.1)')] lines = [ ('disk', 'vda', 'size', '10.5'), ] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {})) self.assertEqual(arr['disk'], 'vda')
def test_in(self): specs = [('disk', '$disk', 'size', 'in(10, 20, 30)')] lines = [ ('disk', 'vda', 'size', '20'), ] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {})) self.assertEqual(arr['disk'], 'vda')
def test_in2(self): specs = [('disk', '$disk=in("vda", "vdb")', 'size', '20')] lines = [ ('disk', 'vda', 'size', '20'), ] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {})) self.assertEqual(arr['disk'], 'vda')
def test_2vars(self): specs = [ ('disk', '$disk', 'size', '$size'), ] lines = [ ('disk', 'vda', 'size', '8'), ] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {})) self.assertEqual(arr, {'size': '8', 'disk': 'vda', })
def test_2vars(self): specs = [ ('disk', '$disk', 'size', '$size'), ] lines = [ ('disk', 'vda', 'size', '8'), ] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {})) self.assertEqual(arr, { 'size': '8', 'disk': 'vda', })
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)))
def test_vars(self): lines = [ ('system', 'product', 'serial', 'CZJ31402CD'), ('disk', '1I:1:1', 'size', '1000GB'), ('disk', '1I:1:1', 'type', 'SATA'), ('disk', '1I:1:1', 'control', 'hpa'), ('disk', '1I:1:2', 'size', '1000GB'), ('disk', '1I:1:2', 'type', 'SATA'), ('disk', '1I:1:2', 'control', 'hpa'), ('disk', '1I:1:3', 'size', '1000GB'), ('disk', '1I:1:3', 'type', 'SATA'), ('disk', '1I:1:3', 'control', 'hpa'), ('disk', '1I:1:4', 'size', '1000GB'), ('disk', '1I:1:4', 'type', 'SATA'), ('disk', '1I:1:4', 'control', 'hpa'), ('disk', '2I:1:5', 'size', '1000GB'), ('disk', '2I:1:5', 'type', 'SATA'), ('disk', '2I:1:5', 'control', 'hpa'), ('disk', '2I:1:6', 'size', '1000GB'), ('disk', '2I:1:6', 'type', 'SATA'), ('disk', '2I:1:6', 'control', 'hpa'), ('disk', '2I:1:7', 'size', '100GB'), ('disk', '2I:1:7', 'type', 'SSDSATA'), ('disk', '2I:1:7', 'control', 'hpa'), ('disk', '2I:1:8', 'size', '100GB'), ('disk', '2I:1:8', 'type', 'SSDSATA'), ('disk', '2I:1:8', 'control', 'hpa'), ] specs = [('system', 'product', 'serial', 'CZJ31402CD'), ('disk', '$disk1', 'size', '100GB'), ('disk', '$disk2', 'size', '100GB'), ('disk', '$disk3', 'size', '1000GB'), ('disk', '$disk4', 'size', '1000GB'), ('disk', '$disk5', 'size', '1000GB'), ('disk', '$disk6', 'size', '1000GB'), ('disk', '$disk7', 'size', '1000GB'), ('disk', '$disk8', 'size', '1000GB')] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {})) self.assertEqual( arr, { 'disk1': '2I:1:7', 'disk2': '2I:1:8', 'disk3': '1I:1:1', 'disk4': '1I:1:2', 'disk5': '1I:1:3', 'disk6': '1I:1:4', 'disk7': '2I:1:5', 'disk8': '2I:1:6', })
def test_backtrack(self): specs = [ ('disk', '$disk', 'size', '8'), ('disk', '$disk', 'type', 'b'), ] lines = [ ('disk', 'vda', 'size', '8'), ('disk', 'vda', 'type', 'a'), ('disk', 'vdb', 'size', '8'), ('disk', 'vdb', 'type', 'b'), ] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {})) self.assertEqual(arr['disk'], 'vdb', arr)
def test_backtrack3(self): specs = [ ('disk', '$disk', 'size', '8'), ('disk', '$disk', 'type', 'c'), ('disk', '$disk2', 'size', '8'), ] lines = [ ('disk', 'vda', 'size', '8'), ('disk', 'vda', 'type', 'a'), ('disk', 'vdb', 'size', '8'), ('disk', 'vdb', 'type', 'b'), ] arr = {} self.assertFalse(matcher.match_all(lines, specs, arr, {}))
def test_vars(self): lines = [ ('system', 'product', 'serial', 'CZJ31402CD'), ('disk', '1I:1:1', 'size', '1000GB'), ('disk', '1I:1:1', 'type', 'SATA'), ('disk', '1I:1:1', 'control', 'hpa'), ('disk', '1I:1:2', 'size', '1000GB'), ('disk', '1I:1:2', 'type', 'SATA'), ('disk', '1I:1:2', 'control', 'hpa'), ('disk', '1I:1:3', 'size', '1000GB'), ('disk', '1I:1:3', 'type', 'SATA'), ('disk', '1I:1:3', 'control', 'hpa'), ('disk', '1I:1:4', 'size', '1000GB'), ('disk', '1I:1:4', 'type', 'SATA'), ('disk', '1I:1:4', 'control', 'hpa'), ('disk', '2I:1:5', 'size', '1000GB'), ('disk', '2I:1:5', 'type', 'SATA'), ('disk', '2I:1:5', 'control', 'hpa'), ('disk', '2I:1:6', 'size', '1000GB'), ('disk', '2I:1:6', 'type', 'SATA'), ('disk', '2I:1:6', 'control', 'hpa'), ('disk', '2I:1:7', 'size', '100GB'), ('disk', '2I:1:7', 'type', 'SSDSATA'), ('disk', '2I:1:7', 'control', 'hpa'), ('disk', '2I:1:8', 'size', '100GB'), ('disk', '2I:1:8', 'type', 'SSDSATA'), ('disk', '2I:1:8', 'control', 'hpa'), ] specs = [('system', 'product', 'serial', 'CZJ31402CD'), ('disk', '$disk1', 'size', '100GB'), ('disk', '$disk2', 'size', '100GB'), ('disk', '$disk3', 'size', '1000GB'), ('disk', '$disk4', 'size', '1000GB'), ('disk', '$disk5', 'size', '1000GB'), ('disk', '$disk6', 'size', '1000GB'), ('disk', '$disk7', 'size', '1000GB'), ('disk', '$disk8', 'size', '1000GB')] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {})) self.assertEqual(arr, {'disk1': '2I:1:7', 'disk2': '2I:1:8', 'disk3': '1I:1:1', 'disk4': '1I:1:2', 'disk5': '1I:1:3', 'disk6': '1I:1:4', 'disk7': '2I:1:5', 'disk8': '2I:1:6', } )
def test_order(self): specs = [ ('disk', '$disk1', 'size', '100'), ('disk', '$disk1', 'slot', '$slot1'), ('disk', '$disk2', 'size', '1000'), ('disk', '$disk2', 'slot', '$slot2'), ] lines = [ ('disk', '1I:1:1', 'size', '1000'), ('disk', '1I:1:1', 'control', 'hpa'), ('disk', '1I:1:1', 'slot', '2'), ('disk', '2I:1:8', 'size', '100'), ('disk', '2I:1:8', 'control', 'hpa'), ('disk', '2I:1:8', 'slot', '2'), ] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {}))
def test_already_bound(self): lines = [ ('disk', '1I:1:2', 'size', '100GB'), ('disk', '1I:1:1', 'size', '1000GB'), ('disk', '1I:1:1', 'control', 'hpa'), ('disk', '1I:1:2', 'control', 'hpa'), ] specs = [ ('disk', '$disk1', 'size', '100GB'), ('disk', '$disk1', 'control', 'hpa'), ('disk', '$disk2', 'size', '1000GB'), ] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {})) self.assertEqual(arr, {'disk1': '1I:1:2', 'disk2': '1I:1:1', })
def test_already_bound(self): lines = [ ('disk', '1I:1:2', 'size', '100GB'), ('disk', '1I:1:1', 'size', '1000GB'), ('disk', '1I:1:1', 'control', 'hpa'), ('disk', '1I:1:2', 'control', 'hpa'), ] specs = [ ('disk', '$disk1', 'size', '100GB'), ('disk', '$disk1', 'control', 'hpa'), ('disk', '$disk2', 'size', '1000GB'), ] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {})) self.assertEqual(arr, { 'disk1': '1I:1:2', 'disk2': '1I:1:1', })
def test_rangeint(self): specs = [('disk', '$disk', 'size', 'range(20, 25)')] lines = [('disk', 'vda', 'size', '20')] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {})) self.assertEqual(arr['disk'], 'vda')
def test_rangefloat(self): specs = [('ipmi', '+12V', 'value', 'range(11.9, 12.2)')] lines = [('ipmi', '+12V', 'value', '12.14')] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {}))
def test_or(self): specs = [('disk', '$disk', 'size', 'or(lt(20), gt(30))')] lines = [('disk', 'vda', 'size', '40')] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {})) self.assertEqual(arr['disk'], 'vda')
def test_regexp(self): specs = [('network', '$eth', 'serial', 'regexp(^28:d2:)')] lines = [('network', 'eth0', 'serial', '28:d2:44:1b:0a:8b')] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {}))
def test_in2(self): specs = [('disk', '$disk=in("vda", "vdb")', 'size', '20')] lines = [('disk', 'vda', 'size', '20')] arr = {} self.assertTrue(matcher.match_all(lines, specs, arr, {})) self.assertEqual(arr['disk'], 'vda')