def update_sensor(config_file, config, sensor): """ Updates the sensor section of specified config_file after checking to ensure sensor exists that the configuration is valid. :param config_file: Name of configuration file to update. :type config_file: str :param config: Complete current configuration. :type config: dict :returns: Complete configuration contents after being updated. :rtype: dict """ if 'name' not in sensor: raise Exception('Missing sensor name') name = sensor['name'] match = None index = 0 for s in config['sensors']: if s['name'] == name: match = s break index += 1 if match is None: raise Exception("Sensor '%s' not found in configuration" % name) # Merges existing content, preferring new sensor config for duplicates # TODO: Handle case when items are removed rather than updated or added. config['sensors'][index].update(sensor) check.check_sensor(config['sensors'][index]) _update_config(config_file, config) log.msg("Configuration updated for sensor '%s'" % name) return config
def test_check_sensor_passes_when_attributes_are_valid(self): # Arrange sensors = json.loads('{"sensors": [{"url": "ws://127.0.0.1:80/ws", ' '"name": "bogus"}]}') sensor = sensors['sensors'][0] # Act & Assert check.check_sensor(sensor)
def add_sensor(config_file, config, sensor): """ Adds the sensor section of specified config_file after checking to ensure sensor exists that the configuration is valid. :param config_file: Name of configuration file to update. :type config_file: str :param config: Complete current configuration. :type config: dict :returns: Complete configuration contents after being updated. :rtype: dict """ if 'name' not in sensor: raise Exception('Missing sensor name') name = sensor['name'] match = None for s in config['sensors']: if s['name'] == name: match = s break if match is not None: raise Exception("Sensor '%s' already found in configuration" % name) check.check_sensor(sensor) # Merges existing content, preferring new sensor config for duplicates config['sensors'].append(sensor) _update_config(config_file, config) log.msg("Configuration added for sensor '%s'" % name) return config
def test_check_sensor_raises_exception_when_not_dictionary(self): # Arrange sensor = json.loads('{"bogus": "data"}') # Act with self.assertRaises(Exception) as context: check.check_sensor(sensor) # Assert self.assertEqual(context.exception.message, "Unknown attribute 'bogus' in sensor configuration")
def test_check_sensor_raises_exception_when_attribute_is_invalid(self): # Arrange sensors = json.loads('{"sensors": {"bogus": "data"}}') sensor = sensors['sensors'] # Act with self.assertRaises(Exception) as context: check.check_sensor(sensor) # Assert self.assertEqual(context.exception.message, "Unknown attribute 'bogus' in sensor configuration")