def test_valid_blocks_xml_matches_schema(self):
        self.cs.set_config_details(TEST_CONFIG)
        xml = ConfigurationXmlConverter.blocks_to_xml(self.cs.get_block_details(), MACROS)

        try:
            ConfigurationSchemaChecker.check_xml_data_matches_schema(os.path.join(self.schema_dir, "blocks.xsd"), xml)
        except:
            self.fail()
    def test_meta_xml_does_not_match_schema_raises(self):
        self.cs.set_config_details(TEST_CONFIG)
        xml = ConfigurationXmlConverter.meta_to_xml(self.cs.get_config_meta())

        # Keep it valid XML but don't match schema
        xml = xml.replace("synoptic>", "snyoptic>")

        self.assertRaises(ConfigurationInvalidUnderSchema, ConfigurationSchemaChecker.check_xml_data_matches_schema,
                          os.path.join(self.schema_dir, "meta.xsd"), xml)
    def test_iocs_xml_does_not_match_schema_raises(self):
        self.cs.set_config_details(TEST_CONFIG)
        xml = ConfigurationXmlConverter.iocs_to_xml(self.cs.get_ioc_details())

        # Keep it valid XML but don't match schema
        xml = xml.replace("<macro ", "<nacho ")

        self.assertRaises(ConfigurationInvalidUnderSchema, ConfigurationSchemaChecker.check_xml_data_matches_schema,
                          os.path.join(self.schema_dir, "iocs.xsd"), xml)
    def test_groups_xml_does_not_match_schema_raises(self):
        self.cs.set_config_details(TEST_CONFIG)
        xml = ConfigurationXmlConverter.groups_to_xml(self.cs.get_group_details(), MACROS)

        # Keep it valid XML but don't match schema
        xml = xml.replace("<block ", "<notblock ")

        self.assertRaises(ConfigurationInvalidUnderSchema, ConfigurationSchemaChecker.check_xml_data_matches_schema,
                          os.path.join(self.schema_dir, "groups.xsd"), xml)
    def test_valid_components_xml_matches_schema(self):
        conf = Configuration(MACROS)
        conf.components["COMP1"] = "COMP1"
        conf.components["COMP2"] = "COMP2"

        xml = ConfigurationXmlConverter.components_to_xml(conf.components)

        try:
            ConfigurationSchemaChecker.check_xml_data_matches_schema(os.path.join(self.schema_dir, "components.xsd"), xml)
        except:
            self.fail()
    def test_components_xml_does_not_match_schema_raises(self):
        conf = Configuration(MACROS)
        conf.components["COMP1"] = "COMP1"
        conf.components["COMP2"] = "COMP2"

        xml = ConfigurationXmlConverter.components_to_xml(conf.components)

        # Keep it valid XML but don't match schema
        xml = xml.replace("<component ", "<domponent ")

        self.assertRaises(ConfigurationInvalidUnderSchema, ConfigurationSchemaChecker.check_xml_data_matches_schema,
                          os.path.join(self.schema_dir, "meta.xsd"), xml)
    def save_config(self, configuration, is_component):
        """Saves the current configuration with the specified name.

        Args:
            configuration (Configuration): The actual configuration to save
            is_component (bool): Is it a component?
        """
        if is_component:
            path = os.path.abspath(FILEPATH_MANAGER.get_component_path(configuration.get_name()))
        else:
            path = os.path.abspath(FILEPATH_MANAGER.get_config_path(configuration.get_name()))

        if not os.path.isdir(path):
            # Create the directory
            os.makedirs(path)

        blocks_xml = ConfigurationXmlConverter.blocks_to_xml(configuration.blocks, configuration.macros)
        groups_xml = ConfigurationXmlConverter.groups_to_xml(configuration.groups)
        iocs_xml = ConfigurationXmlConverter.iocs_to_xml(configuration.iocs)
        meta_xml = ConfigurationXmlConverter.meta_to_xml(configuration.meta)
        try:
            components_xml = ConfigurationXmlConverter.components_to_xml(configuration.components)
        except:
            # Is a component, so no components
            components_xml = ConfigurationXmlConverter.components_to_xml(dict())

        # Save blocks
        with open(path + "/" + FILENAME_BLOCKS, "w") as f:
            f.write(blocks_xml)

        # Save groups
        with open(path + "/" + FILENAME_GROUPS, "w") as f:
            f.write(groups_xml)

        # Save IOCs
        with open(path + "/" + FILENAME_IOCS, "w") as f:
            f.write(iocs_xml)

        # Save components
        with open(path + "/" + FILENAME_COMPONENTS, "w") as f:
            f.write(components_xml)

        # Save meta
        with open(path + "/" + FILENAME_META, "w") as f:
            f.write(meta_xml)
    def load_config(self, name, macros, is_component):
        """Loads the configuration from the specified folder.

        Args:
            name (string): The name of the configuration
            macros (dict): The BlockServer macros
            is_component (bool): Is it a component?
        """
        configuration = Configuration(macros)

        if is_component:
            path = os.path.abspath(FILEPATH_MANAGER.get_component_path(name))
        else:
            path = os.path.abspath(FILEPATH_MANAGER.get_config_path(name))

        if not os.path.isdir(path):
            raise IOError("Configuration could not be found: " + name)

        # Create empty containers
        blocks = OrderedDict()
        groups = OrderedDict()
        components = OrderedDict()
        iocs = OrderedDict()

        # Make sure NONE group exists
        groups[GRP_NONE.lower()] = Group(GRP_NONE)

        config_files_missing = list()

        # Open the block file first
        blocks_path = os.path.join(path, FILENAME_BLOCKS)
        if os.path.isfile(blocks_path):
            root = ElementTree.parse(blocks_path).getroot()

            # Check against the schema - raises if incorrect
            self._check_againgst_schema(ElementTree.tostring(root, encoding="utf8"), FILENAME_BLOCKS)

            ConfigurationXmlConverter.blocks_from_xml(root, blocks, groups)
        else:
            config_files_missing.append(FILENAME_BLOCKS)

        # Import the groups
        groups_path = os.path.join(path, FILENAME_GROUPS)
        if os.path.isfile(groups_path):
            root = ElementTree.parse(groups_path).getroot()

            # Check against the schema - raises if incorrect
            self._check_againgst_schema(ElementTree.tostring(root, encoding="utf8"), FILENAME_GROUPS)

            ConfigurationXmlConverter.groups_from_xml(root, groups, blocks)
        else:
            config_files_missing.append(FILENAME_GROUPS)

        # Import the IOCs
        iocs_path = os.path.join(path, FILENAME_IOCS)
        if os.path.isfile(iocs_path):
            root = ElementTree.parse(iocs_path).getroot()

            # There was a historic bug where the simlevel was saved as 'None' rather than "none".
            # Correct that here
            correct_xml = ElementTree.tostring(root, encoding="utf8").replace('simlevel="None"', 'simlevel="none"')

            # Check against the schema - raises if incorrect
            self._check_againgst_schema(correct_xml, FILENAME_IOCS)

            ConfigurationXmlConverter.ioc_from_xml(root, iocs)
        else:
            config_files_missing.append(FILENAME_IOCS)

        # Import the components
        component_path = os.path.join(path, FILENAME_COMPONENTS)
        if os.path.isfile(component_path):
            root = ElementTree.parse(component_path).getroot()

            # Check against the schema - raises if incorrect
            self._check_againgst_schema(ElementTree.tostring(root, encoding="utf8"), FILENAME_COMPONENTS)

            ConfigurationXmlConverter.components_from_xml(root, components)
        elif not is_component:
            # It should be missing for a component
            config_files_missing.append(FILENAME_COMPONENTS)

        # Import the metadata
        meta = MetaData(name)
        meta_path = os.path.join(path, FILENAME_META)
        if os.path.isfile(meta_path):
            root = ElementTree.parse(meta_path).getroot()

            # Check against the schema - raises if incorrect
            self._check_againgst_schema(ElementTree.tostring(root, encoding="utf8"), FILENAME_META)

            ConfigurationXmlConverter.meta_from_xml(root, meta)
        else:
            config_files_missing.append(FILENAME_META)

        if len(config_files_missing) > 0:
            raise ConfigurationIncompleteException(
                "Files missing in " + name + " (%s)" % ",".join(list(config_files_missing))
            )

        # Set properties in the config
        configuration.blocks = blocks
        configuration.groups = groups
        configuration.iocs = iocs
        configuration.components = components
        configuration.meta = meta
        return configuration
    def test_cannot_find_schema_raises(self):
        self.cs.set_config_details(TEST_CONFIG)
        xml = ConfigurationXmlConverter.blocks_to_xml(self.cs.get_block_details(), MACROS)

        self.assertRaises(IOError, ConfigurationSchemaChecker.check_xml_data_matches_schema,
                          os.path.join(self.schema_dir, "does_not_exist.xsd"), xml)
 def save_config(self, configuration, root_path, config_name):
     self.blocks_xml = ConfigurationXmlConverter.blocks_to_xml(configuration.blocks, configuration.macros)
     self.groups_xml = ConfigurationXmlConverter.groups_to_xml(configuration.groups)
     self.iocs_xml = ConfigurationXmlConverter.iocs_to_xml(configuration.iocs)
     self.components_xml = ConfigurationXmlConverter.components_to_xml(configuration.components)