def test_disable_snmp_v2_write_community(self, send_mock, recv_mock):
        self._setUp(
            {
                "Enable SNMP": "False",
                "Disable SNMP": "True",
                "SNMP Write Community": "private",
            }
        )

        emu = CliEmulator(
            [
                Command("configure terminal", CONFIG_PROMPT),
                Command("no snmp-server community private", CONFIG_PROMPT),
                Command("show snmp community", CONFIG_PROMPT),
            ]
        )
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        enable_disable_snmp_flow = AristaEnableDisableSNMPFlow(
            self.cli_configurator, self.logger
        )
        enable_disable_snmp_flow.disable_snmp(self.snmp_parameters)

        emu.check_calls()
    def test_enable_snmp_v2(self, send_mock, recv_mock):
        self._setUp()

        emu = CliEmulator(
            [
                Command("configure terminal", CONFIG_PROMPT),
                Command("show snmp community", CONFIG_PROMPT),
                Command("snmp-server community public ro", CONFIG_PROMPT),
                Command(
                    "show snmp community",
                    "Community name: public\n"
                    "Community access: read-only\n"
                    "{}".format(CONFIG_PROMPT),
                ),
            ]
        )
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        enable_disable_snmp_flow = AristaEnableDisableSNMPFlow(
            self.cli_configurator, self.logger
        )
        enable_disable_snmp_flow.enable_snmp(self.snmp_parameters)

        emu.check_calls()
Example #3
0
    def test_save_with_vrf(self, send_mock, recv_mock):
        vrf_name = "vrf_name"
        self._setUp({"VRF Management Name": vrf_name})
        user = "******"
        password = "******"
        host = "192.168.122.10"
        ftp_path = f"ftp://{user}:{password}@{host}"
        configuration_type = "running"

        emu = CliEmulator(
            [
                Command(
                    f"routing-context vrf {vrf_name}",
                    VRF_PROMPT.format(vrf_name=vrf_name),
                ),
                Command(
                    r"^copy {0} {1}/Arista-{0}-\d+-\d+$".format(
                        configuration_type, ftp_path
                    ),
                    "Copy complete\n" "{}".format(VRF_PROMPT.format(vrf_name=vrf_name)),
                    regexp=True,
                ),
                Command("routing-context vrf default", ENABLE_PROMPT),
            ]
        )
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        self.flow.save(ftp_path, configuration_type, vrf_name)

        emu.check_calls()
    def test_disable_snmp_v2_is_not_disabled(self, send_mock, recv_mock):
        self._setUp(
            {
                "Enable SNMP": "False",
                "Disable SNMP": "True",
            }
        )

        emu = CliEmulator(
            [
                Command("configure terminal", CONFIG_PROMPT),
                Command("no snmp-server community public", CONFIG_PROMPT),
                Command(
                    "show snmp community",
                    "Community name: public\n"
                    "Community access: read-only\n"
                    "{}".format(CONFIG_PROMPT),
                ),
            ]
        )
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        enable_disable_snmp_flow = AristaEnableDisableSNMPFlow(
            self.cli_configurator, self.logger
        )
        with self.assertRaisesRegex(Exception, "Failed to remove SNMP community"):
            enable_disable_snmp_flow.disable_snmp(self.snmp_parameters)

        emu.check_calls()
    def test_enter_config_mode_fail(self, send_mock, recv_mock):
        emu = CliEmulator([Command(
            "",
            ENABLE_PROMPT,
        )])
        configuration_command = emu.commands[7]
        self.assertEqual(
            configuration_command.request,
            "configure terminal",
            "We have to change particular command",
        )
        configuration_lock_command = Command(
            configuration_command.request,
            f"configuration locked\n{ENABLE_PROMPT}",
            configuration_command.regexp,
        )

        check_config_mode_commands = [
            configuration_lock_command,
            Command("", ENABLE_PROMPT),
        ]
        check_config_mode_commands.extend([configuration_lock_command] * 5)
        emu.commands[7:len(emu.commands) + 1] = check_config_mode_commands

        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        with self.assertRaisesRegex(
                SessionManagerException,
                "Failed to create new session for type SSH, see logs for details",
        ):
            with self.cli_configurator.config_mode_service() as session:
                session.send_command("")

        emu.check_calls()
    def test_enter_config_mode_with_multiple_retries(self, send_mock,
                                                     recv_mock):
        emu = CliEmulator([Command(
            "",
            ENABLE_PROMPT,
        )])
        configuration_command = emu.commands[7]
        self.assertEqual(
            configuration_command.request,
            "configure terminal",
            "We have to change particular command",
        )
        configuration_lock_command = Command(
            configuration_command.request,
            f"configuration locked\n{ENABLE_PROMPT}",
            configuration_command.regexp,
        )

        check_config_mode_commands = [
            configuration_lock_command,
            Command("", ENABLE_PROMPT),
        ]
        check_config_mode_commands.extend([configuration_lock_command] * 3)
        check_config_mode_commands.extend(
            [configuration_command,
             Command("", CONFIG_PROMPT)])
        emu.commands[7:8] = check_config_mode_commands

        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        with self.cli_configurator.enable_mode_service() as session:
            session.send_command("")

        emu.check_calls()
    def test_enter_enable_and_config_mode_with_parentheses(
            self, send_mock, recv_mock):
        default_prompt = "fgs-7508-a1-2(b3)>"
        enable_prompt = "fgs-7508-a1-2(b3)#"
        conf_prompt = "fgs-7508-a1-2(b3)(config)#"

        prompts_map = {
            DEFAULT_PROMPT: default_prompt,
            ENABLE_PROMPT: enable_prompt,
            CONFIG_PROMPT: conf_prompt,
        }

        emu = CliEmulator([Command(
            "",
            ENABLE_PROMPT,
        )])

        for command in emu.commands:
            prompt = prompts_map.get(command.response, command.response)
            command.response = prompt

        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        with self.cli_configurator.enable_mode_service() as session:
            session.send_command("")

        emu.check_calls()
Example #8
0
    def test_save_ftp(self, send_mock, recv_mock):
        self._setUp()
        user = "******"
        password = "******"
        host = "192.168.122.10"
        ftp_path = f"ftp://{user}:{password}@{host}"
        configuration_type = "running"

        emu = CliEmulator(
            [
                Command(
                    r"^copy {0} {1}/Arista-{0}-\d+-\d+$".format(
                        configuration_type, ftp_path
                    ),
                    "Copy complete\n" "{}".format(ENABLE_PROMPT),
                    regexp=True,
                )
            ]
        )
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        self.flow.save(ftp_path, configuration_type, "")

        emu.check_calls()
Example #9
0
    def test_restore_startup(self, send_mock, recv_mock):
        self._setUp()
        host = "192.168.122.10"
        file_name = "Test-startup-100418-163658"
        remote_path = f"ftp://{host}/{file_name}"
        configuration_type = "startup"

        emu = CliEmulator(
            [
                Command(
                    f"copy {remote_path} {configuration_type}-config",
                    "Copy completed successfully.\n" "{}".format(ENABLE_PROMPT),
                )
            ]
        )
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        self.flow._restore_flow(
            RemoteURL.from_str(remote_path),
            ConfigurationType.STARTUP,
            RestoreMethod.OVERRIDE,
            "",
        )

        emu.check_calls()
Example #10
0
    def test_save_to_device(self, send_mock, recv_mock):
        self._setUp(
            {
                "Backup Location": "",
                "Backup Type": AristaConfigurationFlow.FILE_SYSTEM_SCHEME,
            }
        )
        path = ""
        configuration_type = "running"

        emu = CliEmulator(
            [
                Command(
                    r"copy {0} flash:/Arista-{0}-\d+-\d+".format(configuration_type),
                    "Copy complete\n" "{}".format(ENABLE_PROMPT),
                    regexp=True,
                )
            ]
        )
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        self.flow.save(path, configuration_type)

        emu.check_calls()
    def test_enable_snmp_v2_with_vrf_already_enabled(self, send_mock, recv_mock):
        self._setUp(
            {
                "VRF Management Name": "management",
            }
        )

        emu = CliEmulator(
            [
                Command("configure terminal", CONFIG_PROMPT),
                Command(
                    "show snmp",
                    "0 SNMP packets input\n"
                    "    0 Bad SNMP version errors\n"
                    "    0 Unknown community name\n"
                    "    0 Illegal operation for community name supplied\n"
                    "    0 Encoding errors\n"
                    "    0 Number of requested variables\n"
                    "    0 Number of altered variables\n"
                    "    0 Get-request PDUs\n"
                    "    0 Get-next PDUs\n"
                    "    0 Set-request PDUs\n"
                    "...\n"
                    "SNMP logging: disabled\n"
                    "SNMP agent enabled in VRFs: default, management\n"
                    "{}".format(CONFIG_PROMPT),
                ),
                Command("show snmp community", CONFIG_PROMPT),
                Command("snmp-server community public ro", CONFIG_PROMPT),
                Command(
                    "show snmp community",
                    "Community name: public\n"
                    "Community access: read-only\n"
                    "{}".format(CONFIG_PROMPT),
                ),
            ]
        )
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        enable_disable_snmp_flow = AristaEnableDisableSNMPFlow(
            self.cli_configurator, self.logger, vrf_name="management"
        )
        enable_disable_snmp_flow.enable_snmp(self.snmp_parameters)
        emu.check_calls()
    def test_enable_snmp_v2_not_enabled(self, send_mock, recv_mock):
        self._setUp()

        emu = CliEmulator(
            [
                Command("configure terminal", CONFIG_PROMPT),
                Command("show snmp community", CONFIG_PROMPT),
                Command("snmp-server community public ro", CONFIG_PROMPT),
                Command("show snmp community", CONFIG_PROMPT),
            ]
        )
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all
        enable_disable_snmp_flow = AristaEnableDisableSNMPFlow(
            self.cli_configurator, self.logger
        )
        with self.assertRaisesRegex(Exception, "Failed to create SNMP community"):
            enable_disable_snmp_flow.enable_snmp(self.snmp_parameters)

        emu.check_calls()
    def test_enter_config_mode_regular(self, send_mock, recv_mock):
        emu = CliEmulator([Command(
            "",
            ENABLE_PROMPT,
        )])

        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        with self.cli_configurator.enable_mode_service() as session:
            session.send_command("")

        emu.check_calls()
Example #14
0
    def test_restore_running_override(self, send_mock, recv_mock):
        self._setUp()
        host = "192.168.122.10"
        file_name = "Test-running-100418-163658"
        remote_path = f"ftp://{host}/{file_name}"

        emu = CliEmulator([Command(f"configure replace {remote_path}", ENABLE_PROMPT)])
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        self.flow._restore_flow(
            RemoteURL.from_str(remote_path),
            ConfigurationType.RUNNING,
            RestoreMethod.OVERRIDE,
            "",
        )

        emu.check_calls()
Example #15
0
    def test_fail_to_save(self, send_mock, recv_mock):
        self._setUp()
        host = "192.168.122.10"
        ftp_path = f"ftp://{host}"
        configuration_type = "running"

        emu = CliEmulator(
            [
                Command(
                    r"^copy {0} {1}/Arista-{0}-\d+-\d+$".format(
                        configuration_type, ftp_path
                    ),
                    "Error\n" "{}".format(ENABLE_PROMPT),
                    regexp=True,
                )
            ]
        )
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        with self.assertRaisesRegex(Exception, "Copy Command failed"):
            self.flow.save(ftp_path, configuration_type, "")

        emu.check_calls()