def perform(self, file_access, logger):
     try:
         hostname = socket.gethostname()
         # IMAT want it blank as they are not using ISOBUS
         if hostname == "NDXIMAT":
             ioc_name = "ILM200"
             change_macros_in_xml = ChangeMacrosInXML(file_access, logger)
             change_macros_in_xml.add_macro(ioc_name, Macro("USE_ISOBUS", "No"), "^(Yes|No)$", "Whether to use ISOBUS for communications (default: Yes)", "Yes")
         return 0
     except Exception as e:
         logger.error("Unable to perform upgrade, caught error: {}".format(e))
         return 1
예제 #2
0
 def perform(self, file_access, logger):
     try:
         hostname = socket.gethostname()
         ioc_name = "DFKPS"
         if hostname == "NDXEMU":
             change_macros_in_xml = ChangeMacrosInXML(file_access, logger)
             change_macros_in_xml.add_macro(
                 ioc_name, Macro("DISABLE_AUTOONOFF", "0"), "^(0|1)$",
                 "Disable automatic PSU on/off feature", "1")
             change_macros_in_xml.change_macros(
                 ioc_name, [(Macro("DISABLE_AUTOONOFF"),
                             Macro("DISABLE_AUTOONOFF", "0"))])
         return 0
     except Exception as e:
         logger.error(
             "Unable to perform upgrade, caught error: {}".format(e))
         return 1
예제 #3
0
 def setUp(self):
     self.file_access = FileAccessStub()
     self.logger = LoggingStub()
     self.macro_changer = ChangeMacrosInXML(self.file_access, self.logger)
예제 #4
0
class TestTagGenerator(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_no_requested_iocs_WHEN_filtering_THEN_no_iocs_returned(self):
        # Given:
        ioc_to_change = "CHANGE_ME"
        configs = {"CONFIG_1": ["DONT_CHANGE", "ANOTHER_ONE"]}

        # When:
        self.macro_changer._ioc_file_generator = partial(generate_many_iocs, configs)
        result = []
        for config, xml in self.macro_changer._ioc_file_generator():
            result.extend(list(self.macro_changer.ioc_tag_generator(config, xml, ioc_to_change)))

        # Then:
        assert_that(len(list(result)), is_(0), "no results")

    def test_that_GIVEN_two_xml_with_no_requested_iocs_WHEN_filtering_THEN_no_iocs_returned(self):
        # Given:
        ioc_to_change = "CHANGE_ME"
        configs = {"CONFIG_1": ["DONT_CHANGE", "ANOTHER_ONE"], "CONFIG_2": ["OTHER_IOC"]}

        # When:
        self.macro_changer._ioc_file_generator = partial(generate_many_iocs, configs)
        result = []
        for config, xml in self.macro_changer._ioc_file_generator():
            result.extend(list(self.macro_changer.ioc_tag_generator(config, xml, ioc_to_change)))

        # Then:
        assert_that(len(list(result)), is_(0), "no results")

    def test_that_GIVEN_xml_with_requested_iocs_WHEN_filtering_THEN_expected_ioc_returned(self):
        # Given:
        ioc_to_change = "CHANGE_ME"
        config_name = "CONFIG_1"
        configs = {config_name: [ioc_to_change, "ANOTHER_ONE"]}

        # When:
        self.macro_changer._ioc_file_generator = partial(generate_many_iocs, configs)
        result = []
        for config, xml in self.macro_changer._ioc_file_generator():
            result.extend(list(self.macro_changer.ioc_tag_generator(config, xml, ioc_to_change)))

        # Then:
        assert_that(len(result), is_(1))
        assert_that(result[0].getAttribute("name"), is_(ioc_to_change))

    def test_that_GIVEN_one_xml_with_requested_iocs_and_one_without_WHEN_filtering_THEN_only_expected_ioc_returned(self):
        # Given:
        ioc_to_change = "CHANGE_ME"
        good_config = "CONFIG_1"
        configs = {good_config: [ioc_to_change, "ANOTHER_ONE"], "CONFIG_2": ["DONT_CHANGE", "ANOTHER_ONE"]}

        # When:
        self.macro_changer._ioc_file_generator = partial(generate_many_iocs, configs)
        result = []
        for config, xml in self.macro_changer._ioc_file_generator():
            result.extend(list(self.macro_changer.ioc_tag_generator(config, xml, ioc_to_change)))

        # Then:
        assert_that(len(result), is_(1))
        assert_that(result[0].getAttribute("name"), is_(ioc_to_change))

    def test_that_GIVEN_xml_with_numbered_ioc_WHEN_filtering_THEN_expected_ioc_returned(self):
        # Given
        root_ioc_name = "CHANGE_ME"
        ioc_name = root_ioc_name + "_03"
        config_name = "CONFIG_1"
        configs = {config_name: [ioc_name, "ANOTHER_ONE"]}

        # When:
        self.macro_changer._ioc_file_generator = partial(generate_many_iocs, configs)
        result = []
        for config, xml in self.macro_changer._ioc_file_generator():
            result.extend(list(self.macro_changer.ioc_tag_generator(config, xml, root_ioc_name)))

        # Then:
        assert_that(len(result), is_(1))
        assert_that(result[0].getAttribute("name"), is_(ioc_name))

    def test_that_GIVEN_xml_with_ioc_containing_requested_WHEN_filtering_THEN_nothing_returned(self):
        # Given:
        root_ioc_name = "CHANGE_ME"
        ioc_name = "PRE-{}-POST".format(root_ioc_name)
        config_name = "CONFIG_1"
        configs = {config_name: [ioc_name, "ANOTHER_ONE"]}

        # When:
        self.macro_changer._ioc_file_generator = partial(generate_many_iocs, configs)
        result = []
        for config, xml in self.macro_changer._ioc_file_generator():
            result.extend(list(self.macro_changer.ioc_tag_generator(config, xml, root_ioc_name)))

        # Then:
        assert_that(len(result), is_(0))
예제 #5
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"))
예제 #6
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))
예제 #7
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"))