def test_fail_to_save(self, send_mock, recv_mock):
        ftp_server = 'ftp://*****:*****@192.168.122.10'
        configuration_type = 'running'

        emu = CliEmulator([
            Command('configure', CONFIG_PROMPT),
            Command(
                'copy {0}-config {1}/A10-{0}-\d+-\d+'.format(
                    configuration_type, ftp_server),
                'Do you want to save the remote host information to a profile for later use?'
                '[yes/no]',
                regexp=True,
            ),
            Command(
                'no',
                '.\nFailed to copy file\n{}'.format(CONFIG_PROMPT),
            ),
        ])
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        self.assertRaisesRegexp(
            Exception,
            'Session returned \'Fail to copy a file\'',
            self.runner.save,
            ftp_server,
            configuration_type,
        )

        emu.check_calls()
    def test_fail_to_restore(self, send_mock, recv_mock):
        remote_path = 'ftp://*****:*****@192.168.122.10/Test-running-100418-163658'
        configuration_type = 'running'

        emu = CliEmulator([
            Command('configure', CONFIG_PROMPT),
            Command(
                'copy {} {}-config'.format(remote_path, configuration_type),
                'Do you want to save the remote host information to a profile for later use?'
                '[yes/no]',
            ),
            Command(
                'no',
                '.\nFailed to get file from ftp server. Check log for reason of failure.'
                '\n{}'.format(CONFIG_PROMPT),
            ),
        ])
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        self.assertRaisesRegexp(
            Exception,
            'Session returned \'Fail to copy a file\'',
            self.runner.restore,
            remote_path,
            configuration_type,
        )

        emu.check_calls()
    def test_save_anonymous(self, send_mock, recv_mock):
        ftp_server = 'ftp://192.168.122.10'
        configuration_type = 'running'

        emu = CliEmulator([
            Command('configure', CONFIG_PROMPT),
            Command(
                'copy {0}-config {1}/A10-{0}-\d+-\d+'.format(
                    configuration_type, ftp_server),
                'User name []?',
                regexp=True,
            ),
            Command('', 'Password []?'),
            Command(
                '',
                'Do you want to save the remote host information to a profile for later use?'
                '[yes/no]',
            ),
            Command(
                'no',
                '.\nFile copied successfully.\n{}'.format(CONFIG_PROMPT),
            ),
        ])
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        self.runner.save(ftp_server, configuration_type)

        emu.check_calls()
    def test_snmp_v3_user_already_created(self, send_mock, recv_mock):
        self._setUp({'SNMP Version': 'v3'})

        emu = CliEmulator([
            Command('configure', CONFIG_PROMPT),
            Command('snmp-server enable service', CONFIG_PROMPT),
            Command(
                'show running-config snmp-server',
                '!Section configuration: 328 bytes\n'
                '!\n'
                'snmp-server enable service\n'
                '!\n'
                'snmp-server view quali_view 1 included\n'
                '!\n'
                'snmp-server group quali_group v3 noauth read quali_view\n'
                '!\n'
                'snmp-server SNMPv3 user quali_user group quali_group v3 noauth\n'
                '!\n'
                '{}'.format(CONFIG_PROMPT),
            ),
        ])
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        self.runner.discover()

        emu.check_calls()
    def test_restore_anonymous(self, send_mock, recv_mock):
        remote_path = 'ftp://192.168.122.10/Test-running-100418-163658'
        configuration_type = 'running'

        emu = CliEmulator([
            Command('configure', CONFIG_PROMPT),
            Command(
                'copy {} {}-config'.format(remote_path, configuration_type),
                'User name []?',
            ),
            Command('', 'Password []?'),
            Command(
                '',
                'Do you want to save the remote host information to a profile for later use?'
                '[yes/no]',
            ),
            Command(
                'no',
                '.\nFile copied successfully.\n{}'.format(CONFIG_PROMPT),
            ),
        ])
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        self.runner.restore(remote_path, configuration_type)

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

        emu = CliEmulator([
            Command('configure', CONFIG_PROMPT),
            Command('no snmp-server SNMPv1-v2c user quali_user',
                    CONFIG_PROMPT),
        ])
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        self.runner.discover()

        emu.check_calls()
    def test_save_to_device(self, send_mock, recv_mock):
        path = ''
        configuration_type = 'running'

        emu = CliEmulator([
            Command('configure', CONFIG_PROMPT),
            Command(
                'copy {0}-config A10-{0}-\d+-\d+'.format(configuration_type),
                '.\nFile copied successfully.\n{}'.format(CONFIG_PROMPT),
                regexp=True,
            ),
        ])
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        self.runner.save(path, configuration_type)

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

        emu = CliEmulator([
            Command('configure', CONFIG_PROMPT),
            Command('snmp-server enable service', CONFIG_PROMPT),
            Command('snmp-server SNMPv1-v2c user quali_user',
                    'vThunder(config-user:quali)(NOLICENSE)#'),
            Command('community read public',
                    'vThunder(config-user:quali)(NOLICENSE)#'),
            Command('exit', CONFIG_PROMPT),
        ])
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        self.runner.discover()

        emu.check_calls()
    def test_restore_from_device(self, send_mock, recv_mock):
        file_name = 'test-running-100418-163658'  # we get path in lowercase
        configuration_type = 'startup'

        emu = CliEmulator([
            Command('configure', CONFIG_PROMPT),
            Command(
                'copy {} {}-config'.format(file_name, configuration_type),
                'Profile exists. Do you wish to overwrite this profile (N/Y)?:'
            ),
            Command('y',
                    '.\nFile copied successfully.\n{}'.format(CONFIG_PROMPT))
        ])
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        self.runner.restore('', configuration_type)

        emu.check_calls()
    def test_remove_snmp_v3_user_already_deleted(self, send_mock, recv_mock):
        self._setUp({
            'SNMP Version': 'v3',
            'Enable SNMP': 'False',
            'Disable SNMP': 'True',
        })

        emu = CliEmulator([
            Command('configure', CONFIG_PROMPT),
            Command('snmp-server enable service', CONFIG_PROMPT),
            Command('show running-config snmp-server',
                    CONFIG_PROMPT),  # snmp didn't enabled
        ])
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        self.runner.discover()

        emu.check_calls()
    def test_snmp_v3_user_didnt_deleted(self, send_mock, recv_mock):
        self._setUp({
            'SNMP Version': 'v3',
            'Enable SNMP': 'False',
            'Disable SNMP': 'True',
        })

        emu = CliEmulator([
            Command('configure', CONFIG_PROMPT),
            Command('snmp-server enable service', CONFIG_PROMPT),
            Command(
                'show running-config snmp-server',
                '!Section configuration: 328 bytes\n'
                '!\n'
                'snmp-server enable service\n'
                '!\n'
                'snmp-server view quali_view 1 included\n'
                '!\n'
                'snmp-server group quali_group v3 noauth read quali_view\n'
                '!\n'
                'snmp-server SNMPv3 user quali_user group quali_group v3 noauth\n'
                '!\n'
                '{}'.format(CONFIG_PROMPT),
            ),
            Command(
                'no snmp-server SNMPv3 user quali_user group quali_group v3 noauth',
                CONFIG_PROMPT),
            Command(
                'no snmp-server group quali_group v3 noauth read quali_view',
                CONFIG_PROMPT),
            Command('no snmp-server view quali_view 1 included',
                    CONFIG_PROMPT),
            Command(
                'show running-config snmp-server',
                '!Section configuration: 328 bytes\n'
                '!\n'
                'snmp-server enable service\n'
                '!\n'
                'snmp-server view quali_view 1 included\n'
                '!\n'
                'snmp-server group quali_group v3 noauth read quali_view\n'
                '!\n'
                'snmp-server SNMPv3 user quali_user group quali_group v3 noauth\n'
                '!\n'
                '{}'.format(CONFIG_PROMPT),
            ),
        ])
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        self.assertRaisesRegexp(
            A10Exception,
            r'^Failed to disable SNMP User quali_user$',
            self.runner.discover,
        )

        emu.check_calls()
    def test_disable_snmp_v3(self, send_mock, recv_mock):
        self._setUp({
            'SNMP Version': 'v3',
            'SNMP V3 Authentication Protocol': 'SHA',
            'SNMP V3 Privacy Protocol': 'AES-128',
            'Enable SNMP': 'False',
            'Disable SNMP': 'True',
        })

        emu = CliEmulator([
            Command('configure', CONFIG_PROMPT),
            Command('snmp-server enable service', CONFIG_PROMPT),
            Command(
                'show running-config snmp-server',
                '!Section configuration: 328 bytes\n'
                '!\n'
                'snmp-server enable service\n'
                '!\n'
                'snmp-server view quali_view 1 included\n'
                '!\n'
                'snmp-server group quali_group v3 priv read quali_view\n'
                '!\n'
                'snmp-server SNMPv3 user quali_user group quali_group v3 auth sha encrypted '
                '!encrypted password! priv aes encrypted !encrypted key!\n'
                '!\n'
                '{}'.format(CONFIG_PROMPT),
            ),
            Command(
                'no snmp-server SNMPv3 user quali_user group quali_group v3 auth sha password '
                'priv aes private_key',
                CONFIG_PROMPT,
            ),
            Command('no snmp-server group quali_group v3 priv read quali_view',
                    CONFIG_PROMPT),
            Command('no snmp-server view quali_view 1 included',
                    CONFIG_PROMPT),
            Command('show running-config snmp-server', CONFIG_PROMPT),
        ])
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        self.runner.discover()

        emu.check_calls()
    def test_enable_snmp_v3_with_auth(self, send_mock, recv_mock):
        self._setUp({
            'SNMP Version': 'v3',
            'SNMP V3 Authentication Protocol': 'MD5',
        })

        emu = CliEmulator([
            Command('configure', CONFIG_PROMPT),
            Command('snmp-server enable service', CONFIG_PROMPT),
            Command('show running-config snmp-server', CONFIG_PROMPT),
            Command('snmp-server view quali_view 1 included', CONFIG_PROMPT),
            Command('snmp-server group quali_group v3 auth read quali_view',
                    CONFIG_PROMPT),
            Command(
                'snmp-server SNMPv3 user quali_user group quali_group v3 auth md5 password',
                CONFIG_PROMPT,
            ),
            Command(
                'show running-config snmp-server',
                '!Section configuration: 328 bytes\n'
                '!\n'
                'snmp-server enable service\n'
                '!\n'
                'snmp-server view quali_view 1 included\n'
                '!\n'
                'snmp-server group quali_group v3 auth read quali_view\n'
                '!\n'
                'snmp-server SNMPv3 user quali_user group quali_group v3 auth md5 encrypted '
                '!encrypted password!\n'
                '!\n'
                '{}'.format(CONFIG_PROMPT),
            ),
        ])
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        self.runner.discover()

        emu.check_calls()
    def test_load_firmware_secondary_image(self, send_mock, recv_mock):
        user = '******'
        password = '******'
        path = 'ftp://{}:{}@192.168.122.10/ACOS_non_FTA_4_1_4_332.64.upg'.format(
            user, password)

        emu = CliEmulator([
            Command('configure', CONFIG_PROMPT),
            Command(
                'show version',
                'Thunder Series Unified Application Service Gateway vThunder\n'
                'Copyright 2007-2017 by A10 Networks, Inc.  All A10 Networks products are\n'
                'protected by one or more of the following US patents:\n'
                '...\n'
                '64-bit Advanced Core OS (ACOS) version 4.1.0-P10, build 105 (Oct-30-2017,17:16)\n'
                'Booted from Hard Disk secondary image\n'
                '...\n'
                '{}'.format(CONFIG_PROMPT),
            ),
            Command(
                'upgrade hd sec {} reboot-after-upgrade'.format(path),
                'Getting upgrade package ...\n'
                '.... Done (0 minutes 5 seconds)\n'
                'Decrypt upgrade package ...\n'
                '................. Done (0 minutes 18 seconds)\n'
                'Expand the upgrade package now ...\n'
                '..... Done (0 minutes 6 seconds)\n'
                'Upgrade ...\n'
                '.................................. Upgrade was successful (1 minutes 17 seconds)\n'
                'Rebooting system ...\n'
                '{}'.format(CONFIG_PROMPT),
            ),
            Command('', CONFIG_PROMPT),
            Command('', CONFIG_PROMPT),
            Command('', socket.error()),
            Command('', LOADING_PROMPT),
            Command('', LOADING_PROMPT),
            Command('', LOADING_PROMPT),
            Command('', DEFAULT_PROMPT),
            Command('enable', 'Password'),
            Command(ENABLE_PASSWORD, ENABLE_PROMPT),
            Command('terminal length 0', ENABLE_PROMPT),
            Command('terminal width 300', ENABLE_PROMPT),
            Command('configure', CONFIG_PROMPT),
            Command('logging console disable', CONFIG_PROMPT),
            Command('exit', ENABLE_PROMPT),
            Command('', ENABLE_PROMPT),
            Command('configure', CONFIG_PROMPT),
        ])
        send_mock.side_effect = emu.send_line
        recv_mock.side_effect = emu.receive_all

        self.runner.load_firmware(path)

        emu.check_calls()