def setUp(self):
     self.file_access = FileAccessStub()
     self.logger = LoggingStub()
Пример #2
0
 def setUp(self):
     self.file_access = FileAccessStub()
     self.file_access.existing_files = {GLOBALS_FILENAME: GLOBALS_FILENAME}
     self.logger = LoggingStub()
     self.macro_changer = ChangeMacroInGlobals(self.file_access,
                                               self.logger)
Пример #3
0
 def setUp(self):
     self.file_access = FileAccessStub()
     self.logger = LoggingStub()
     self.macro_changer = ChangeMacrosInXML(self.file_access, self.logger)
Пример #4
0
class TestAddMacro(unittest.TestCase):
    def setUp(self):
        self.file_access = FileAccessStub()
        self.logger = LoggingStub()
        self.macro_changer = ChangeMacrosInXML(self.file_access, self.logger)

    def test_GIVEN_one_ioc_THEN_add_macro(self):
        # Given:
        xml = IOC_FILE_XML.format(iocs=create_galil_ioc(1, {"GALILADDR": "0", "MTRCTRL": "0"}))
        ioc_name = "GALIL"
        macro_to_add = Macro("TEST", "1")
        pattern = "^(0|1)$"
        description = "Test description"
        default = "0"

        self.file_access.open_file = Mock(return_value=xml)
        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.add_macro(ioc_name, macro_to_add, pattern, description, default)

        # Then:
        written_xml = ET.fromstring(self.file_access.write_file_contents)
        result_galiladdr = written_xml.findall(".//ns:macros/*[@name='GALILADDR']", {"ns": NAMESPACE})
        result_mtrctrl = written_xml.findall(".//ns:macros/*[@name='MTRCTRL']", {"ns": NAMESPACE})
        result_test = written_xml.findall(".//ns:macros/*[@name='TEST']", {"ns": NAMESPACE})

        assert_that(result_galiladdr, has_length(1), "changed macro count")
        assert_that(result_galiladdr[0].get("name"), is_("GALILADDR"))
        assert_that(result_galiladdr[0].get("value"), is_("0"))

        assert_that(result_mtrctrl, has_length(1), "changed macro count")
        assert_that(result_mtrctrl[0].get("name"), is_("MTRCTRL"))
        assert_that(result_mtrctrl[0].get("value"), is_("0"))

        assert_that(result_test, has_length(1), "changed macro count")
        assert_that(result_test[0].get("name"), is_(macro_to_add.name))
        assert_that(result_test[0].get("value"), is_(macro_to_add.value))

    def test_GIVEN_one_ioc_that_already_has_macro_THEN_dont_add_macro(self):
        # Given:
        xml = IOC_FILE_XML.format(iocs=create_galil_ioc(1, {"GALILADDR": "0", "MTRCTRL": "0", "TEST": "0"}))
        ioc_name = "GALIL"
        macro_to_add = Macro("TEST", "1")
        pattern = "^(0|1)$"
        description = "Test description"
        default = "0"

        self.file_access.open_file = Mock(return_value=xml)
        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.add_macro(ioc_name, macro_to_add, pattern, description, default)

        # Then:
        written_xml = ET.fromstring(self.file_access.write_file_contents)
        result_galiladdr = written_xml.findall(".//ns:macros/*[@name='GALILADDR']", {"ns": NAMESPACE})
        result_mtrctrl = written_xml.findall(".//ns:macros/*[@name='MTRCTRL']", {"ns": NAMESPACE})
        result_test = written_xml.findall(".//ns:macros/*[@name='TEST']", {"ns": NAMESPACE})

        assert_that(result_galiladdr, has_length(1), "changed macro count")
        assert_that(result_galiladdr[0].get("name"), is_("GALILADDR"))
        assert_that(result_galiladdr[0].get("value"), is_("0"))

        assert_that(result_mtrctrl, has_length(1), "changed macro count")
        assert_that(result_mtrctrl[0].get("name"), is_("MTRCTRL"))
        assert_that(result_mtrctrl[0].get("value"), is_("0"))

        assert_that(result_test, has_length(1), "changed macro count")
        assert_that(result_test[0].get("name"), is_("TEST"))
        assert_that(result_test[0].get("value"), is_("0"))
Пример #5
0
class TestChangeIOCName(unittest.TestCase):

    def setUp(self):
        self.file_access = FileAccessStub()
        self.logger = LoggingStub()
        self.macro_changer = ChangeMacrosInXML(self.file_access, self.logger)

    def create_synoptic_file_with_multiple_IOCs(self, iocs):
        """
        Mocks out a synoptic file with multiple IOCs in it.
        Args:
            iocs: List of strings with the IOC names in it

        Returns:
            formatted_synoptic_file: A mock XML document containing a sample synoptic

        """

        synoptics = "".join([SYOPTIC_XML.format(ioc) for ioc in iocs])
        formatted_synoptic_file = SYNOPTIC_FILE_XML.format(synoptics=synoptics)

        return formatted_synoptic_file

    def test_GIVEN_an_ioc_name_WHEN_IOC_change_asked_THEN_ioc_name_is_changed(self):
        # Given:
        ioc_suffix_digit = 1
        xml = IOC_FILE_XML.format(iocs=create_galil_ioc(ioc_suffix_digit, {"GALILADDRXX": ""}))

        self.file_access.open_file = Mock(return_value=xml)
        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("GALIL", "CHANGED")

        # Then:
        written_xml = ET.fromstring(self.file_access.write_file_contents)
        tree = ET.ElementTree(written_xml)

        iocs = tree.findall(".//ns:ioc", {"ns": NAMESPACE})

        assert_that(iocs[0].get("name"), is_("CHANGED_{:02}".format(ioc_suffix_digit)))

    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_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_synoptic_xml_file_WHEN_IOC_name_changed_THEN_only_the_ioc_synoptics_are_changed(self):
        # Given:
        ioc_to_change = "CHANGEME"
        new_ioc_name = "CHANGED"
        unchanged_ioc = "DONTCHANGE"

        suffix = "_01"

        synoptic_file = self.create_synoptic_file_with_multiple_IOCs([ioc_to_change+suffix, unchanged_ioc+suffix])

        self.file_access.open_file = Mock(return_value=synoptic_file)
        self.file_access.is_dir = Mock(return_value=True)
        self.file_access.listdir = Mock(return_value=["file1.xml"])

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

        # Then:
        output_file = self.file_access.write_file_contents

        assert_that((ioc_to_change in output_file), is_(False))
        assert_that((new_ioc_name in output_file), is_(True))
        assert_that((unchanged_ioc in output_file), is_(True))
Пример #6
0
class TestMacroChangesWithMultipleInputs(unittest.TestCase):

    def setUp(self):
        self.file_access = FileAccessStub()
        self.logger = LoggingStub()
        self.macro_changer = ChangeMacrosInXML(self.file_access, self.logger)

    def test_that_GIVEN_xml_with_single_macro_WHEN_calling_change_macros_THEN_the_single_macro_is_updated(self):
        # Given:
        xml = IOC_FILE_XML.format(iocs=create_galil_ioc(1, {"GALILADDRXX": ""}))
        ioc_name = "GALIL"
        macro_to_change = [
            (Macro("GALILADDRXX"), Macro("GALILADDR"))
        ]

        self.file_access.open_file = Mock(return_value=xml)
        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_macros(ioc_name, macro_to_change)

        # Then:
        written_xml = ET.fromstring(self.file_access.write_file_contents)
        result = written_xml.findall(".//ns:macros/*[@name='GALILADDR']", {"ns": NAMESPACE})

        assert_that(result, has_length(1), "changed macro count")
        assert_that(result[0].get("name"), is_("GALILADDR"))

    def test_that_GIVEN_xml_with_multiple_macros_THEN_only_value_of_named_macro_is_changed(self):
        # Given:
        xml = IOC_FILE_XML.format(iocs=create_galil_ioc(1, {"GALILADDR": "0", "MTRCTRL": "0"}))
        ioc_name = "GALIL"
        macro_to_change = [
            (Macro("GALILADDR"), Macro("GALILADDR", "1"))
        ]

        self.file_access.open_file = Mock(return_value=xml)
        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_macros(ioc_name, macro_to_change)

        # Then:
        written_xml = ET.fromstring(self.file_access.write_file_contents)
        result_galiladdr = written_xml.findall(".//ns:macros/*[@name='GALILADDR']", {"ns": NAMESPACE})
        result_mtrctrl = written_xml.findall(".//ns:macros/*[@name='MTRCTRL']", {"ns": NAMESPACE})

        assert_that(result_galiladdr, has_length(1), "changed macro count")
        assert_that(result_galiladdr[0].get("name"), is_("GALILADDR"))
        assert_that(result_galiladdr[0].get("value"), is_("1"))

        assert_that(result_mtrctrl, has_length(1), "changed macro count")
        assert_that(result_mtrctrl[0].get("name"), is_("MTRCTRL"))
        assert_that(result_mtrctrl[0].get("value"), is_("0"))


    def test_that_GIVEN_xml_with_multiple_old_ioc_macros_THEN_all_macros_are_updated(self):
        # Given:
        xml = IOC_FILE_XML.format(iocs=create_galil_ioc(1, {"GALILADDRXX": "", "MTRCTRLXX": ""}))
        ioc_name = "GALIL"
        macros_to_change = [
            (Macro("GALILADDRXX", ""), Macro("GALILADDR", "1")),
            (Macro("MTRCTRLXX", ""), Macro("MTRCTRL", "1"))
        ]
        self.file_access.write_file_contents = xml

        self.file_access.open_file = Mock(return_value=self.file_access.write_file_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_macros(ioc_name, macros_to_change)

        # Then:
        written_xml = ET.fromstring(self.file_access.write_file_contents)
        result_galiladdr = written_xml.findall(".//ns:macros/*[@name='GALILADDR']", {"ns": NAMESPACE})
        result_mtrctrl = written_xml.findall(".//ns:macros/*[@name='MTRCTRL']", {"ns": NAMESPACE})

        assert_that(result_mtrctrl, has_length(1), "changed macro count")
        assert_that(result_mtrctrl[0].get("name"), is_("MTRCTRL"))
        assert_that(result_mtrctrl[0].get("value"), is_("1"))

        assert_that(result_galiladdr, has_length(1), "changed macro count")
        assert_that(result_galiladdr[0].get("name"), is_("GALILADDR"))
        assert_that(result_galiladdr[0].get("value"), is_("1"))