コード例 #1
0
    def execute_flow(self,
                     folder_path,
                     configuration_type,
                     vrf_management_name=None):
        """ Execute flow which save selected file to the provided destination

        :param folder_path: destination path where file will be saved
        :param configuration_type: source file, which will be saved
        :param vrf_management_name: Virtual Routing and Forwarding Name
        :return: saved configuration file name
        """

        with self._cli_handler.get_cli_service(
                self._cli_handler.enable_mode) as enable_session:
            save_action = SystemActions(enable_session, self._logger)
            action_map = save_action.prepare_action_map(
                configuration_type, folder_path)
            save_action.copy(configuration_type,
                             folder_path,
                             vrf=vrf_management_name,
                             action_map=action_map)
class TestCiscoSystemActionsCopy(TestCase):
    def setUp(self):
        self.system_action = SystemActions(cli_service=MagicMock(),
                                           logger=MagicMock())

    def test_restore_override_success(self):
        with patch("cloudshell.networking.cisco.command_actions.system_actions"
                   ".CommandTemplateExecutor") as cte_mock:
            cte_mock.return_value.execute_command.return_value = (
                system_actions_output.SUCCESS_OUTPUT_CONFIG_OVERRIDE)
            self.system_action.override_running(MagicMock(), MagicMock())

    def test_copy_error_opening_output(self):
        with patch("cloudshell.networking.cisco.command_actions.system_actions"
                   ".CommandTemplateExecutor") as cte_mock:
            try:
                cte_mock.return_value.execute_command.return_value = (
                    system_actions_output.ERROR_OVERRIDE_RUNNING)
                self.system_action.override_running(MagicMock(), MagicMock())
            except Exception as e:
                self.assertIn("Copy Command failed.", e.args)

    def test_override_running_error_opening_output(self):
        with patch("cloudshell.networking.cisco.command_actions.system_actions"
                   ".CommandTemplateExecutor") as cte_mock:
            try:
                cte_mock.return_value.execute_command.return_value = (
                    system_actions_output.ERROR_ROLL_BACK)
                self.system_action.override_running(MagicMock(), MagicMock())
            except Exception as e:
                self.assertIn("Copy Command failed.", e.args)
コード例 #3
0
    def _restore_flow(self, path, configuration_type, restore_method,
                      vrf_management_name):
        """Execute flow which save selected file to the provided destination.

        :param path: the path to the configuration file, including the
                     configuration file name
        :param restore_method: the restore method to use when restoring the
                               configuration file. Possible Values are append
                               and override
        :param configuration_type: the configuration type to restore. Possible
                                   values are startup and running
        :param vrf_management_name: Virtual Routing and Forwarding Name
        """
        if "-config" not in configuration_type:
            configuration_type += "-config"

        with self._cli_handler.get_cli_service(
                self._cli_handler.enable_mode) as enable_session:
            restore_action = SystemActions(enable_session, self._logger)
            reload_action_map = self._prepare_reload_act_map()

            if restore_method == "override":
                if self._cli_handler.cli_type.lower() != "console":
                    raise Exception(
                        self.__class__.__name__,
                        "Unsupported cli session type: {0}. "
                        "Only Console allowed for restore override".format(
                            self._cli_handler.cli_type.lower()),
                    )

                restore_action.copy(
                    source=path,
                    destination=self.TEMP_STARTUP_LOCATION,
                    vrf=vrf_management_name,
                    action_map=restore_action.prepare_action_map(
                        path, self.TEMP_STARTUP_LOCATION),
                )

                restore_action.write_erase()
                restore_action.reload_device_via_console(
                    action_map=reload_action_map)

                restore_action.copy(
                    source=self.TEMP_STARTUP_LOCATION,
                    destination=self.RUNNING_LOCATION,
                    action_map=restore_action.prepare_action_map(
                        self.TEMP_STARTUP_LOCATION, self.RUNNING_LOCATION),
                )

                time.sleep(5)
                restore_action.copy(
                    source=self.RUNNING_LOCATION,
                    destination=self.STARTUP_LOCATION,
                    action_map=restore_action.prepare_action_map(
                        self.RUNNING_LOCATION, self.STARTUP_LOCATION),
                    timeout=200,
                )

            elif "startup" in configuration_type:
                raise Exception(
                    self.__class__.__name__,
                    "Restore of startup config in append mode is not supported",
                )
            else:
                restore_action.copy(
                    source=path,
                    destination=configuration_type,
                    vrf=vrf_management_name,
                    action_map=restore_action.prepare_action_map(
                        path, configuration_type),
                )
 def setUp(self):
     self.system_action = SystemActions(cli_service=MagicMock(),
                                        logger=MagicMock())
コード例 #5
0
    def _restore_flow(self, path, configuration_type, restore_method,
                      vrf_management_name):
        """Execute flow which save selected file to the provided destination.

        :param path: the path to the configuration file, including the configuration
            file name
        :param restore_method: the restore method to use when restoring the
            configuration file. Possible Values are append and override
        :param configuration_type: the configuration type to restore.
            Possible values are startup and running
        :param vrf_management_name: Virtual Routing and Forwarding Name
        """
        if "-config" not in configuration_type:
            configuration_type += "-config"

        with self._cli_handler.get_cli_service(
                self._cli_handler.enable_mode) as enable_session:
            restore_action = SystemActions(enable_session, self._logger)
            copy_action_map = restore_action.prepare_action_map(
                path, configuration_type)

            if "startup" in configuration_type:
                if restore_method == "override":
                    del_action_map = {
                        "[Dd]elete [Ff]ilename ":
                        lambda s, l: s.send_line(self.STARTUP_CONFIG_NAME, l)
                    }
                    restore_action.delete_file(path=self.STARTUP_LOCATION,
                                               action_map=del_action_map)
                    restore_action.copy(
                        path,
                        configuration_type,
                        vrf=vrf_management_name,
                        action_map=copy_action_map,
                    )
                else:
                    restore_action.copy(
                        path,
                        configuration_type,
                        vrf=vrf_management_name,
                        action_map=copy_action_map,
                    )

            elif "running" in configuration_type:
                if restore_method == "override":
                    restore_action.override_running(path)
                else:
                    restore_action.copy(
                        path,
                        configuration_type,
                        vrf=vrf_management_name,
                        action_map=copy_action_map,
                    )
    def execute_flow(self, path, vrf, timeout):
        """Load a firmware onto the device

        :param path: The path to the firmware file, including the firmware file name
        :param vrf: Virtual Routing and Forwarding Name
        :param timeout:
        :return:
        """

        full_path_dict = UrlParser().parse_url(path)
        firmware_file_name = full_path_dict.get(UrlParser.FILENAME)
        if not firmware_file_name:
            raise Exception(self.__class__.__name__,
                            "Unable to find firmware file")

        with self._cli_handler.get_cli_service(
                self._cli_handler.enable_mode) as enable_session:
            system_action = SystemActions(enable_session, self._logger)
            dst_file_system = self._file_system

            firmware_dst_path = "{0}/{1}".format(dst_file_system,
                                                 firmware_file_name)

            device_file_system = system_action.get_flash_folders_list()
            self._logger.info(
                "Discovered folders: {}".format(device_file_system))
            if device_file_system:
                device_file_system.sort()
                for flash in device_file_system:
                    if flash in self.BOOTFOLDER:
                        self._logger.info(
                            "Device has a {} folder".format(flash))
                        firmware_dst_path = "{0}/{1}".format(
                            flash, firmware_file_name)
                        self._logger.info(
                            "Copying {} image".format(firmware_dst_path))
                        system_action.copy(
                            path,
                            firmware_dst_path,
                            vrf=vrf,
                            action_map=system_action.prepare_action_map(
                                path, firmware_dst_path))
                        break
                    if "flash-" in flash:
                        firmware_dst_file_path = "{0}/{1}".format(
                            flash, firmware_file_name)
                        self._logger.info(
                            "Copying {} image".format(firmware_dst_file_path))
                        system_action.copy(
                            path,
                            firmware_dst_file_path,
                            vrf=vrf,
                            action_map=system_action.prepare_action_map(
                                path, firmware_dst_file_path))
            else:
                self._logger.info("Copying {} image".format(firmware_dst_path))
                system_action.copy(path,
                                   firmware_dst_path,
                                   vrf=vrf,
                                   action_map=system_action.prepare_action_map(
                                       path, firmware_dst_path))

            self._logger.info("Get current boot configuration")
            current_boot = system_action.get_current_boot_image()
            self._logger.info("Modifying boot configuration")
            self._apply_firmware(enable_session, current_boot,
                                 firmware_dst_path)

            output = system_action.get_current_boot_config()
            new_boot_settings = re.sub(
                "^.*boot-start-marker|boot-end-marker.*", "", output)
            self._logger.info(
                "Boot config lines updated: {0}".format(new_boot_settings))

            if output.find(firmware_file_name) == -1:
                raise Exception(
                    self.__class__.__name__,
                    "Can't add firmware '{}' for boot!".format(
                        firmware_file_name))

            system_action.copy(self.RUNNING_CONFIG,
                               self.STARTUP_CONFIG,
                               vrf=vrf,
                               action_map=system_action.prepare_action_map(
                                   self.RUNNING_CONFIG, self.STARTUP_CONFIG))
            if "CONSOLE" in enable_session.session.SESSION_TYPE:
                system_action.reload_device_via_console(timeout)
            else:
                system_action.reload_device(timeout)

            os_version = system_action.get_current_os_version()
            if os_version.find(firmware_file_name) == -1:
                raise Exception(self.__class__.__name__,
                                "Failed to load firmware, Please check logs")
class TestCiscoSystemActions(TestCase):
    TEST_RUNNING_CONFIG_SHORT_PATH = "running-config"
    TEST_RUNNING_CONFIG_FULL_PATH = "bootflash:/running-config"
    TEST_RUNNING_CONFIG_REMOTE_TFTP_TEXT_PATH = "tftp://localhost/running-config"
    TEST_RUNNING_CONFIG_REMOTE_TFTP_IP_PATH = "tftp://127.0.0.1/running-config"
    TEST_RUNNING_CONFIG_REMOTE_FTP_TEXT_PATH = (
        "ftp://*****:*****@localhost/running-config")
    TEST_RUNNING_CONFIG_REMOTE_FTP_IP_PATH = "ftp://*****:*****@127.0.0.1/running-config"

    TEST_RESULT_TEXT_HOST = r"(?!/)localhost(?!/)\D*\s*$"
    TEST_RESULT_IP_HOST = r"(?!/)127.0.0.1(?!/)\D*\s*$"
    TEST_RESULT_PASSWORD = "******"
    TEST_RESULT_SRC_FILE_NAME = "[\\[\\(].*running-config[\\)\\]]"
    TEST_RESULT_DST_FILE_NAME = "[\\[\\(]running-config[\\)\\]]"
    TEST_RESULT_FROM_REMOTE_DST_FILE_NAME = "(?!/)[\\[\\(]running-config[\\)\\]]"

    def setUp(self):
        self.system_action = SystemActions(cli_service=MagicMock(),
                                           logger=MagicMock())

    def test_prepare_action_map_tftp_text_path(self):
        short_path_action_map = self.system_action.prepare_action_map(
            self.TEST_RUNNING_CONFIG_SHORT_PATH,
            self.TEST_RUNNING_CONFIG_REMOTE_TFTP_TEXT_PATH,
        )
        self.assertIsNotNone(short_path_action_map)
        self.assertIsNotNone(
            short_path_action_map.get(self.TEST_RESULT_TEXT_HOST))
        self.assertIsNotNone(
            short_path_action_map.get(self.TEST_RESULT_SRC_FILE_NAME))
        self.assertIsNotNone(
            short_path_action_map.get(self.TEST_RESULT_DST_FILE_NAME))

        full_path_action_map = self.system_action.prepare_action_map(
            self.TEST_RUNNING_CONFIG_FULL_PATH,
            self.TEST_RUNNING_CONFIG_REMOTE_TFTP_TEXT_PATH,
        )
        self.assertIsNotNone(full_path_action_map)

        self.assertIsNotNone(full_path_action_map)
        self.assertIsNotNone(
            full_path_action_map.get(self.TEST_RESULT_TEXT_HOST))
        self.assertIsNotNone(
            full_path_action_map.get(self.TEST_RESULT_SRC_FILE_NAME))
        self.assertIsNotNone(
            full_path_action_map.get(self.TEST_RESULT_DST_FILE_NAME))

        short_invert_path_map = self.system_action.prepare_action_map(
            self.TEST_RUNNING_CONFIG_REMOTE_TFTP_TEXT_PATH,
            self.TEST_RUNNING_CONFIG_SHORT_PATH,
        )
        self.assertIsNotNone(short_invert_path_map)
        self.assertIsNotNone(
            short_invert_path_map.get(self.TEST_RESULT_TEXT_HOST))
        self.assertIsNotNone(
            short_invert_path_map.get(
                self.TEST_RESULT_FROM_REMOTE_DST_FILE_NAME))

        full_invert_path_map = self.system_action.prepare_action_map(
            self.TEST_RUNNING_CONFIG_REMOTE_TFTP_TEXT_PATH,
            self.TEST_RUNNING_CONFIG_FULL_PATH,
        )
        self.assertIsNotNone(full_invert_path_map)
        self.assertIsNotNone(
            full_invert_path_map.get(self.TEST_RESULT_TEXT_HOST))
        self.assertIsNotNone(
            full_invert_path_map.get(
                self.TEST_RESULT_FROM_REMOTE_DST_FILE_NAME))

    def test_prepare_action_map_tftp_ip_path(self):
        short_path_action_map = self.system_action.prepare_action_map(
            self.TEST_RUNNING_CONFIG_SHORT_PATH,
            self.TEST_RUNNING_CONFIG_REMOTE_TFTP_IP_PATH,
        )

        self.assertIsNotNone(short_path_action_map)
        self.assertIsNotNone(
            short_path_action_map.get(self.TEST_RESULT_IP_HOST))
        self.assertIsNotNone(
            short_path_action_map.get(self.TEST_RESULT_SRC_FILE_NAME))
        self.assertIsNotNone(
            short_path_action_map.get(self.TEST_RESULT_DST_FILE_NAME))

        full_path_action_map = self.system_action.prepare_action_map(
            self.TEST_RUNNING_CONFIG_FULL_PATH,
            self.TEST_RUNNING_CONFIG_REMOTE_TFTP_IP_PATH,
        )

        self.assertIsNotNone(full_path_action_map)
        self.assertIsNotNone(full_path_action_map.get(
            self.TEST_RESULT_IP_HOST))
        self.assertIsNotNone(
            full_path_action_map.get(self.TEST_RESULT_SRC_FILE_NAME))
        self.assertIsNotNone(
            full_path_action_map.get(self.TEST_RESULT_DST_FILE_NAME))

        short_invert_path_map = self.system_action.prepare_action_map(
            self.TEST_RUNNING_CONFIG_REMOTE_TFTP_IP_PATH,
            self.TEST_RUNNING_CONFIG_SHORT_PATH,
        )
        self.assertIsNotNone(short_invert_path_map)
        self.assertIsNotNone(
            short_invert_path_map.get(self.TEST_RESULT_IP_HOST))
        self.assertIsNotNone(
            short_invert_path_map.get(
                self.TEST_RESULT_FROM_REMOTE_DST_FILE_NAME))

        full_invert_path_map = self.system_action.prepare_action_map(
            self.TEST_RUNNING_CONFIG_REMOTE_TFTP_IP_PATH,
            self.TEST_RUNNING_CONFIG_FULL_PATH,
        )
        self.assertIsNotNone(full_invert_path_map)
        self.assertIsNotNone(full_invert_path_map.get(
            self.TEST_RESULT_IP_HOST))
        self.assertIsNotNone(
            full_invert_path_map.get(
                self.TEST_RESULT_FROM_REMOTE_DST_FILE_NAME))

    def test_prepare_action_map_ftp_text_path(self):
        short_path_action_map = self.system_action.prepare_action_map(
            self.TEST_RUNNING_CONFIG_SHORT_PATH,
            self.TEST_RUNNING_CONFIG_REMOTE_FTP_TEXT_PATH,
        )
        self.assertIsNotNone(short_path_action_map)
        self.assertIsNotNone(
            short_path_action_map.get(self.TEST_RESULT_PASSWORD))

        self.assertIsNotNone(
            short_path_action_map.get(self.TEST_RESULT_TEXT_HOST))
        self.assertIsNotNone(
            short_path_action_map.get(self.TEST_RESULT_SRC_FILE_NAME))
        self.assertIsNotNone(
            short_path_action_map.get(self.TEST_RESULT_DST_FILE_NAME))

        full_path_action_map = self.system_action.prepare_action_map(
            self.TEST_RUNNING_CONFIG_FULL_PATH,
            self.TEST_RUNNING_CONFIG_REMOTE_FTP_TEXT_PATH,
        )
        self.assertIsNotNone(full_path_action_map)

        self.assertIsNotNone(full_path_action_map)
        self.assertIsNotNone(
            full_path_action_map.get(self.TEST_RESULT_PASSWORD))

        self.assertIsNotNone(
            full_path_action_map.get(self.TEST_RESULT_TEXT_HOST))
        self.assertIsNotNone(
            full_path_action_map.get(self.TEST_RESULT_SRC_FILE_NAME))
        self.assertIsNotNone(
            full_path_action_map.get(self.TEST_RESULT_DST_FILE_NAME))

        short_invert_path_map = self.system_action.prepare_action_map(
            self.TEST_RUNNING_CONFIG_REMOTE_FTP_TEXT_PATH,
            self.TEST_RUNNING_CONFIG_SHORT_PATH,
        )
        self.assertIsNotNone(short_invert_path_map)
        self.assertIsNotNone(
            short_invert_path_map.get(self.TEST_RESULT_PASSWORD))
        self.assertIsNotNone(
            short_invert_path_map.get(self.TEST_RESULT_TEXT_HOST))
        self.assertIsNotNone(
            short_invert_path_map.get(
                self.TEST_RESULT_FROM_REMOTE_DST_FILE_NAME))

        full_invert_path_map = self.system_action.prepare_action_map(
            self.TEST_RUNNING_CONFIG_REMOTE_FTP_TEXT_PATH,
            self.TEST_RUNNING_CONFIG_FULL_PATH,
        )
        self.assertIsNotNone(full_invert_path_map)
        self.assertIsNotNone(
            full_invert_path_map.get(self.TEST_RESULT_PASSWORD))
        self.assertIsNotNone(
            full_invert_path_map.get(self.TEST_RESULT_TEXT_HOST))
        self.assertIsNotNone(
            full_invert_path_map.get(
                self.TEST_RESULT_FROM_REMOTE_DST_FILE_NAME))

    def test_prepare_action_map_ftp_ip_path(self):
        short_path_action_map = self.system_action.prepare_action_map(
            self.TEST_RUNNING_CONFIG_SHORT_PATH,
            self.TEST_RUNNING_CONFIG_REMOTE_FTP_IP_PATH,
        )

        self.assertIsNotNone(short_path_action_map)
        self.assertIsNotNone(
            short_path_action_map.get(self.TEST_RESULT_IP_HOST))
        self.assertIsNotNone(
            short_path_action_map.get(self.TEST_RESULT_PASSWORD))

        self.assertIsNotNone(
            short_path_action_map.get(self.TEST_RESULT_SRC_FILE_NAME))
        self.assertIsNotNone(
            short_path_action_map.get(self.TEST_RESULT_DST_FILE_NAME))

        full_path_action_map = self.system_action.prepare_action_map(
            self.TEST_RUNNING_CONFIG_FULL_PATH,
            self.TEST_RUNNING_CONFIG_REMOTE_FTP_IP_PATH,
        )

        self.assertIsNotNone(full_path_action_map)
        self.assertIsNotNone(full_path_action_map.get(
            self.TEST_RESULT_IP_HOST))
        self.assertIsNotNone(
            full_path_action_map.get(self.TEST_RESULT_PASSWORD))

        self.assertIsNotNone(
            full_path_action_map.get(self.TEST_RESULT_SRC_FILE_NAME))
        self.assertIsNotNone(
            full_path_action_map.get(self.TEST_RESULT_DST_FILE_NAME))

        short_invert_path_map = self.system_action.prepare_action_map(
            self.TEST_RUNNING_CONFIG_REMOTE_FTP_IP_PATH,
            self.TEST_RUNNING_CONFIG_SHORT_PATH,
        )
        self.assertIsNotNone(short_invert_path_map)
        self.assertIsNotNone(
            short_invert_path_map.get(self.TEST_RESULT_IP_HOST))
        self.assertIsNotNone(
            short_invert_path_map.get(self.TEST_RESULT_PASSWORD))
        self.assertIsNotNone(
            short_invert_path_map.get(
                self.TEST_RESULT_FROM_REMOTE_DST_FILE_NAME))

        full_invert_path_map = self.system_action.prepare_action_map(
            self.TEST_RUNNING_CONFIG_REMOTE_FTP_IP_PATH,
            self.TEST_RUNNING_CONFIG_FULL_PATH,
        )
        self.assertIsNotNone(full_invert_path_map)
        self.assertIsNotNone(full_invert_path_map.get(
            self.TEST_RESULT_IP_HOST))
        self.assertIsNotNone(
            full_invert_path_map.get(self.TEST_RESULT_PASSWORD))
        self.assertIsNotNone(
            full_invert_path_map.get(
                self.TEST_RESULT_FROM_REMOTE_DST_FILE_NAME))
class TestCiscoSystemActionsCopy(TestCase):
    def setUp(self):
        self.system_action = SystemActions(cli_service=MagicMock(),
                                           logger=MagicMock())

    def test_copy_success(self):
        with patch("cloudshell.networking.cisco.command_actions.system_actions"
                   ".CommandTemplateExecutor") as cte_mock:
            cte_mock.return_value.execute_command.return_value = (
                system_actions_output.SUCCESS_OUTPUT_IOS)
            self.system_action.copy(MagicMock(), MagicMock())

        with patch("cloudshell.networking.cisco.command_actions.system_actions"
                   ".CommandTemplateExecutor") as cte_mock:
            cte_mock.return_value.execute_command.return_value = (
                system_actions_output.SUCCESS_OUTPUT)
            self.system_action.copy(MagicMock(), MagicMock())

        with patch("cloudshell.networking.cisco.command_actions.system_actions"
                   ".CommandTemplateExecutor") as cte_mock:
            cte_mock.return_value.execute_command.return_value = (
                system_actions_output.TEST_COPY_OUTPUT)
            self.system_action.copy(MagicMock(), MagicMock())

    def test_copy_error_opening_file(self):
        with patch("cloudshell.networking.cisco.command_actions.system_actions"
                   ".CommandTemplateExecutor") as cte_mock:
            try:
                cte_mock.return_value.execute_command.return_value = (
                    system_actions_output.ERROR_OPENING_OUTPUT)
                self.system_action.copy(MagicMock(), MagicMock())
            except Exception as e:
                self.assertIn(
                    "Copy Command failed. "
                    "%Error opening tftp://10.10.10.10/ASR1004-2-running-100516-084841 "
                    "(Timed out)",
                    e.args,
                )

    def test_copy_error_access_violation(self):
        with patch("cloudshell.networking.cisco.command_actions.system_actions"
                   ".CommandTemplateExecutor") as cte_mock:
            try:
                cte_mock.return_value.execute_command.return_value = (
                    system_actions_output.ERROR_ACCESS_VIOLATION)
                self.system_action.copy(MagicMock(), MagicMock())
            except Exception as e:
                self.assertIn(
                    "Copy Command failed. "
                    "TFTP put operation failed:Access violation",
                    e.args,
                )

    def test_copy_error(self):
        with patch("cloudshell.networking.cisco.command_actions.system_actions"
                   ".CommandTemplateExecutor") as cte_mock:
            cte_mock.return_value.execute_command.return_value = (
                system_actions_output.SUCCESS_OUTPUT)
            self.system_action.copy(MagicMock(), MagicMock())