| """Tests for devicemap.yaml config file.""" |
| |
| import unittest |
| |
| from catatester.devicemap import devicemap_lib |
| |
| |
| class DeviceMapConfigTest(unittest.TestCase): |
| """Tests for devicemap config file.""" |
| |
| def assertBotConfigMatchesSwarmingSchema(self, bot_config): |
| self.assertIn('dimensions', bot_config) |
| self.assertIn('state', bot_config) |
| for value in bot_config['dimensions'].values(): |
| self.assertTrue(hasattr(value, '__iter__')) |
| |
| def assertDimensionsAllowed(self, config, configs): |
| """Checks that all the given dimensions are in allowed_dimensions.""" |
| allowed_dimensions = configs.GetAllowedDimensions(config.type) |
| allowed_dimensions.add('extra_state') |
| dimensions = set(config.dimensions) |
| if not dimensions.issubset(allowed_dimensions): |
| raise devicemap_lib.DeviceMapError( |
| 'Dimensions not currently allowed: %s' % ', '.join( |
| dimensions - allowed_dimensions)) |
| |
| def testAllowedDimensionsAreDefined(self): |
| configs = devicemap_lib.Configs() |
| self.assertTrue(configs.GetAllowedDimensions('Testbed')) |
| self.assertTrue(configs.GetAllowedDimensions('Device')) |
| |
| def testEveryConfigIsValid(self): |
| configs = devicemap_lib.Configs() |
| for host in configs.hosts: |
| for bot_num in range(1, configs.GetNumBotConfigs(host) + 1): |
| try: |
| config = configs.GetBotConfig(host, bot_num) |
| self.assertIn('dimensions', config.as_dict) |
| self.assertDimensionsAllowed(config, configs) |
| except devicemap_lib.DeviceMapError as e: |
| raise AssertionError('config failure for %s--%s:\n%s\n%s' % ( |
| host, bot_num, config, e)) |
| |
| def testNoDuplicateDevices(self): |
| configs = devicemap_lib.Configs() |
| device_ips = set() |
| device_serials = set() |
| for host in configs.hosts: |
| for bot_num in range(1, configs.GetNumBotConfigs(host) + 1): |
| config = configs.GetBotConfig(host, bot_num) |
| if config.type == 'Device': |
| dimensions = config.dimensions |
| if 'device_ip' in dimensions: |
| ip = dimensions['device_ip'] |
| self.assertNotIn(ip, device_ips, 'duplicate ip found: %s' % ip) |
| device_ips.add(ip) |
| if 'serial' in dimensions: |
| serial = dimensions['serial'] |
| device_serials.add(serial) |
| self.assertNotIn(ip, device_serials, |
| 'duplicate serial found: %s' % serial) |
| |
| def testMultizoneHostConfig(self): |
| """Tests the configuration of multizone hosts.""" |
| configs = devicemap_lib.Configs() |
| for host in configs.hosts: |
| if 'multizone' not in host: |
| continue |
| for bot_num in range(1, configs.GetNumBotConfigs(host) + 1): |
| config = configs.GetBotConfig(host, bot_num) |
| self.assertIn('audio_card_id', config.state, |
| 'No "audio_card_id" in %r:%r.' % (host, bot_num)) |
| self.assertRegexpMatches( |
| config.state['audio_card_id'], r'hw:\d,\d', |
| '"audio_card_id" in %r:%r is invalid.' % (host, bot_num)) |
| self.assertIn('audio_card_ports', config.state, |
| 'No "audio_card_ports" in %r:%r.' % (host, bot_num)) |
| self.assertIsInstance( |
| config.state['audio_card_ports'], list, |
| '"audio_card_ports" in %r:%r is not a list.' % (host, bot_num)) |
| self.assertIn('devices', config.state, |
| 'No "devices" in %r:%r.' % (host, bot_num)) |
| self.assertIsInstance( |
| config.state['devices'], list, |
| '"devices" in %r:%r is not a list.' % (host, bot_num)) |
| |
| |
| if __name__ == '__main__': |
| unittest.main() |