def test_load_yaml_config_raises_error_if_unsafe_yaml(): """Test error raised if unsafe YAML.""" with open(YAML_PATH, "w") as fp: fp.write("hello: !!python/object/apply:os.system") with pytest.raises(OpenPeerPowerError): config_util.load_yaml_config_file(YAML_PATH)
def test_load_yaml_config_raises_error_if_malformed_yaml(): """Test error raised if invalid YAML.""" with open(YAML_PATH, "w") as fp: fp.write(":") with pytest.raises(OpenPeerPowerError): config_util.load_yaml_config_file(YAML_PATH)
def test_load_yaml_config_raises_error_if_not_dict(): """Test error raised when YAML file is not a dict.""" with open(YAML_PATH, "w") as fp: fp.write("5") with pytest.raises(OpenPeerPowerError): config_util.load_yaml_config_file(YAML_PATH)
def test_no_recursive_secrets(caplog): """Test that loading of secrets from the secrets file fails correctly.""" files = { YAML_CONFIG_FILE: "key: !secret a", yaml.SECRET_YAML: "a: 1\nb: !secret a" } with patch_yaml_files(files), pytest.raises(OpenPeerPowerError) as e: load_yaml_config_file(YAML_CONFIG_FILE) assert e.value.args == ("Secrets not supported in this YAML file", )
def autosetup_ihc_products(opp: OpenPeerPower, config, ihc_controller, controller_id): """Auto setup of IHC products from the IHC project file.""" project_xml = ihc_controller.get_project() if not project_xml: _LOGGER.error("Unable to read project from IHC controller") return False project = ElementTree.fromstring(project_xml) # If an auto setup file exist in the configuration it will override yaml_path = opp.config.path(AUTO_SETUP_YAML) if not os.path.isfile(yaml_path): yaml_path = os.path.join(os.path.dirname(__file__), AUTO_SETUP_YAML) yaml = load_yaml_config_file(yaml_path) try: auto_setup_conf = AUTO_SETUP_SCHEMA(yaml) except vol.Invalid as exception: _LOGGER.error("Invalid IHC auto setup data: %s", exception) return False groups = project.findall(".//group") for platform in PLATFORMS: platform_setup = auto_setup_conf[platform] discovery_info = get_discovery_info(platform_setup, groups, controller_id) if discovery_info: discovery.load_platform(opp, platform, DOMAIN, discovery_info, config) return True
def __init__(self, opp, app_name, topic, sandbox, cert_file): """Initialize APNS application.""" self.opp = opp self.app_name = app_name self.sandbox = sandbox self.certificate = cert_file self.yaml_path = opp.config.path(f"{app_name}_{APNS_DEVICES}") self.devices = {} self.device_states = {} self.topic = topic with suppress(FileNotFoundError): self.devices = { str(key): ApnsDevice( str(key), value.get("name"), value.get("tracking_device_id"), value.get("disabled", False), ) for (key, value) in load_yaml_config_file(self.yaml_path).items() } tracking_ids = [ device.full_tracking_device_id for (key, device) in self.devices.items() if device.tracking_device_id is not None ] track_state_change(opp, tracking_ids, self.device_state_changed_listener)
def test_load_yaml_config_preserves_key_order(): """Test removal of library.""" with open(YAML_PATH, "w") as fp: fp.write("hello: 2\n") fp.write("world: 1\n") assert [("hello", 2), ("world", 1) ] == list(config_util.load_yaml_config_file(YAML_PATH).items())
def test_duplicate_key(caplog): """Test duplicate dict keys.""" files = {YAML_CONFIG_FILE: "key: thing1\nkey: thing2"} with patch_yaml_files(files): load_yaml_config_file(YAML_CONFIG_FILE) assert "contains duplicate key" in caplog.text
def test_representing_yaml_loaded_data(): """Test we can represent YAML loaded data.""" files = {YAML_CONFIG_FILE: 'key: [1, "2", 3]'} with patch_yaml_files(files): data = load_yaml_config_file(YAML_CONFIG_FILE) assert yaml.dump(data) == "key:\n- 1\n- '2'\n- 3\n"
def test_unhashable_key(): """Test an unhashable key.""" files = {YAML_CONFIG_FILE: "message:\n {{ states.state }}"} with pytest.raises(OpenPeerPowerError), patch_yaml_files(files): load_yaml_config_file(YAML_CONFIG_FILE)
def load_yaml(fname, string, secrets=None): """Write a string to file and return the parsed yaml.""" FILES[fname] = string with patch_yaml_files(FILES): return load_yaml_config_file(fname, secrets)
def test_load_yaml_config_converts_empty_files_to_dict(): """Test that loading an empty file returns an empty dict.""" create_file(YAML_PATH) assert isinstance(config_util.load_yaml_config_file(YAML_PATH), dict)