def test_GIVEN_xml_with_two_ioc_WHEN_get_ioc_names_THEN_single_ioc_name(self):
        ioc_names = ["TEST_NAME", "TEST_NAME_2"]
        xml = create_xml_with_iocs(ioc_names)

        result = AddToBaseIOCs._get_ioc_names(xml)

        assert_that(result, is_(ioc_names), "result")
    def test_GIVEN_xml_already_containing_two_ioc_to_add_WHEN_checking_prerequisites_THEN_error(self):
        ioc_to_add = "TO_ADD"
        xml = create_xml_with_iocs([ioc_to_add, "ANOTHER_IOC", ioc_to_add])

        adder = AddToBaseIOCs(ioc_to_add, "", "")

        self._assert_prerequiste_fails(adder, xml, ALREADY_CONTAINS.format(FILE_TO_CHECK_STR, ioc_to_add))
Esempio n. 3
0
    def test_GIVEN_more_than_one_IOC_in_config_WHEN_its_name_is_changed_THEN_IOC_suffix_digits_are_preserved(self):
        # Given:
        number_of_repeated_iocs = 3

        ioc_to_change = "CHANGEME"
        new_ioc_name = "CHANGED"

        ioc_names = ["{}_{:02d}".format(ioc_to_change, i) for i in range(1, number_of_repeated_iocs + 1)]
        new_ioc_names = ["{}_{:02d}".format(new_ioc_name, i) for i in range(1, number_of_repeated_iocs + 1)]

        xml_contents = create_xml_with_iocs(ioc_names).toxml()

        self.file_access.open_file = Mock(return_value=xml_contents)
        self.file_access.write_file = Mock()
        self.file_access.get_config_files = Mock(return_value=[("file1.xml", self.file_access.open_xml_file(None))])

        # When:
        self.macro_changer.change_ioc_name(ioc_to_change, new_ioc_name)

        # Then:
        written_xml = ET.fromstring(self.file_access.write_file_contents)
        tree = ET.ElementTree(written_xml)
        iocs = tree.findall(".//ioc", {"ns": NAMESPACE})

        for repeated_ioc_index, ioc in enumerate(iocs):
            assert_that(ioc.get("name"), is_(new_ioc_names[repeated_ioc_index]))
    def test_GIVEN_xml_containing_ioc_to_add_after_and_no_ioc_to_add_WHEN_checking_prerequisites_THEN_passes(self):
        ioc_to_add = "TO_ADD"
        after_ioc = "AFTER_THIS"
        xml = create_xml_with_iocs(["ANOTHER_IOC", after_ioc, "SECOND_IOC"])

        adder = AddToBaseIOCs(ioc_to_add, after_ioc, "")
        result = adder._check_prerequistes_for_file(xml, self.logger)

        assert_that(result, is_(True), "result")
    def test_GIVEN_xml_containing_no_ioc_to_add_after_WHEN_add_ioc_THEN_error(self):
        ioc_to_add = "TO_ADD"
        after_ioc = "AFTER_THIS"
        xml = create_xml_with_iocs(["ANOTHER_IOC", "SECOND_IOC"])

        adder = AddToBaseIOCs(ioc_to_add, after_ioc, "")
        result = adder._add_ioc(xml, self.logger)

        assert_that(result, is_(xml), "xml changed despite error")
        assert_that(self.logger.log_err, has_item("Could not find {0} ioc in file so no {1} ioc added.".format(
            after_ioc, ioc_to_add)))
    def test_GIVEN_xml_containing_ioc_to_add_after_WHEN_add_ioc_THEN_ioc_added(self):
        ioc_to_add = "TO_ADD"
        after_ioc = "AFTER_THIS"
        xml = create_xml_with_iocs([after_ioc, "ANOTHER_IOC"])

        adder = AddToBaseIOCs(ioc_to_add, after_ioc, TEST_XML_TO_ADD)

        result = adder._add_ioc(xml, self.logger)

        iocs = result.getElementsByTagName("ioc")

        assert_that(len(iocs), is_(3), "ioc added")

        assert_that(iocs[1].getAttribute("name"), is_(ioc_to_add), "correctly named ioc added")
        [assert_that(iocs[1].hasAttribute(attr), "correct xml added") for attr in ["restart", "autostart", "simlevel"]]
Esempio n. 7
0
    def test_GIVEN_multiple_different_IOCs_in_configuration_WHEN_ones_name_is_changed_THEN_only_that_ioc_changes(self):
        # Given:
        number_of_repeated_iocs = 3

        ioc_to_change = "CHANGEME"
        new_ioc_name = "CHANGED"

        all_ioc_names = ["CHANGEME", "GALIL", "DONTCHANGE"]

        ioc_names = []
        new_ioc_names = []

        for ioc in all_ioc_names:
            for repeat_suffix in range(1, number_of_repeated_iocs + 1):
                ioc_names.append("{}_{:02d}".format(ioc, repeat_suffix))

                if ioc == ioc_to_change:
                    new_ioc_names.append("{}_{:02d}".format(new_ioc_name, repeat_suffix))
                else:
                    new_ioc_names.append("{}_{:02d}".format(ioc, repeat_suffix))

        xml_contents = create_xml_with_iocs(ioc_names).toxml()

        self.file_access.open_file = Mock(return_value=xml_contents)
        self.file_access.write_file = Mock()
        self.file_access.get_config_files = Mock(return_value=[("file1.xml", self.file_access.open_xml_file(None))])

        # When:
        self.macro_changer.change_ioc_name(ioc_to_change, new_ioc_name)

        # Then:
        written_xml = ET.fromstring(self.file_access.write_file_contents)
        tree = ET.ElementTree(written_xml)
        iocs = tree.findall(".//ioc", {"ns": NAMESPACE})

        for i, ioc in enumerate(iocs):
            assert_that(ioc.get("name"), is_(new_ioc_names[i]))
    def test_GIVEN_xml_containing_two_ioc_to_add_after_WHEN_checking_prerequisites_THEN_error(self):
        after_ioc = "AFTER_THIS"
        xml = create_xml_with_iocs([after_ioc, "ANOTHER_IOC", after_ioc])

        adder = AddToBaseIOCs("", after_ioc, "")
        self._assert_prerequiste_fails(adder, xml, ADD_AFTER_MISSING.format(FILE_TO_CHECK_STR, 2, after_ioc))
    def test_GIVEN_xml_with_no_iocs_WHEN_get_ioc_names_THEN_empty_list(self):
        xml = create_xml_with_iocs(list())

        result = AddToBaseIOCs._get_ioc_names(xml)

        assert_that(result, is_(list()), "result")
Esempio n. 10
0
def generate_many_iocs(configs):
    for config, iocs in configs.items():
        yield config, create_xml_with_iocs(iocs)