def test_restore_ftp(self, send_mock, recv_mock): host = '192.168.122.10' file_name = 'Test-running-100418-163658' user = '******' password = '******' remote_path = 'ftp://{}:{}@{}/{}'.format(user, password, host, file_name) configuration_type = 'running' emu = CliEmulator([ Command( 'execute restore config ftp {} {} {} {}'.format(file_name, host, user, password), 'This operation will overwrite the current setting and could possibly ' 'reboot the system!\n' 'Do you want to continue? (y/n)' ), Command( 'y', '\n' 'Please wait...\n' 'Connect to ftp server 192.168.42.102 ...\n' 'Get config file from ftp server OK.\n' 'File check OK.\n' '{}'.format(ENABLE_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_fail_to_save(self, send_mock, recv_mock): host = '192.168.122.10' ftp_path = 'ftp://{}'.format(host) configuration_type = 'running' emu = CliEmulator([ Command( 'execute backup config ftp FortiNet-{}-\d+-\d+ {}'.format( configuration_type, host), 'Connect to ftp server 192.168.122.10 ...\n' 'Send config file to ftp server via vdom root failed.\n' 'Command fail. Return code 4' '{}'.format(ENABLE_PROMPT), regexp=True, ) ]) send_mock.side_effect = emu.send_line recv_mock.side_effect = emu.receive_all self.assertRaisesRegexp( Exception, 'Session returned \'Fail to backup config\'', self.runner.save, ftp_path, configuration_type, ) emu.check_calls()
def test_restore_from_device(self, send_mock, recv_mock): configuration_type = 'running' emu = CliEmulator([ Command( 'execute revision list config', 'Last Firmware Version: V0.0.0-build000-REL0\n' 'ID TIME ADMIN FIRMWARE VERSION COMMENT\n' ' 4 2018-10-08 11:54:28 admin V6.0.0-build163-REL0 Test-running-081018-215424\n' ' 6 2018-10-08 12:33:47 admin V6.0.0-build163-REL0 Test-running-081018-223342\n' '{}'.format(ENABLE_PROMPT), ), Command( 'execute restore config flash 4', 'This operation will overwrite the current setting and could possibly ' 'reboot the system!\n' 'Do you want to continue? (y/n)' ), Command( 'y', '\n' 'Please wait...\n' 'Get config from local disk OK.\n' 'File check OK.\n' '{}'.format(ENABLE_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_snmp_v3_user_didnt_created(self, send_mock, recv_mock): self._setUp({'SNMP Version': 'v3'}) emu = CliEmulator([ Command( 'show system snmp user', 'config system snmp user\n' 'end\n' '{}'.format(ENABLE_PROMPT)), Command('config system snmp sysinfo', CONFIG_SNMP_SYSINFO_PROMPT), Command('set status enable', CONFIG_SNMP_SYSINFO_PROMPT), Command('end', ENABLE_PROMPT), Command('config system snmp user', CONFIG_SNMP_V3_PROMPT), Command('edit quali_user', EDIT_SNMP_USER_PROMPT), Command('set status enable', EDIT_SNMP_USER_PROMPT), Command('end', ENABLE_PROMPT), Command( 'show system snmp user', 'config system snmp user\n' 'end\n' '{}'.format(ENABLE_PROMPT)), ]) send_mock.side_effect = emu.send_line recv_mock.side_effect = emu.receive_all self.assertRaisesRegexp( FortiNetException, r'^Failed to create SNMP User "quali_user"$', self.runner.discover, ) emu.check_calls()
def test_save_anonymous(self, send_mock, recv_mock): host = '192.168.122.10' ftp_path = 'ftp://{}'.format(host) configuration_type = 'running' emu = CliEmulator([ Command( '^execute backup config ftp FortiNet-{}-\d+-\d+ {}$'.format( configuration_type, host), 'Please wait...\n' '\n' 'Connect to ftp server 192.168.122.10 ...\n' '\n' 'Send config file to ftp server OK.\n' '\n' '{}'.format(ENABLE_PROMPT), regexp=True, ), ]) send_mock.side_effect = emu.send_line recv_mock.side_effect = emu.receive_all self.runner.save(ftp_path, configuration_type) emu.check_calls()
def test_restore_from_device_not_exists_config(self, send_mock, recv_mock): path = 'flash://Test-running-081018-215425' configuration_type = 'running' emu = CliEmulator([ Command( 'execute revision list config', 'Last Firmware Version: V0.0.0-build000-REL0\n' 'ID TIME ADMIN FIRMWARE VERSION COMMENT\n' ' 4 2018-10-08 11:54:28 admin V6.0.0-build163-REL0 Test-running-081018-215424\n' ' 6 2018-10-08 12:33:47 admin V6.0.0-build163-REL0 Test-running-081018-223342\n' '{}'.format(ENABLE_PROMPT), ), ]) send_mock.side_effect = emu.send_line recv_mock.side_effect = emu.receive_all self.assertRaisesRegexp( FortiNetException, 'Cannot find config file ".*?" on the device', self.runner.restore, path, configuration_type, ) emu.check_calls()
def test_enable_snmp_v3(self, send_mock, recv_mock): self._setUp({'SNMP Version': 'v3'}) emu = CliEmulator([ Command( 'show system snmp user', 'config system snmp user\n' 'end\n' '{}'.format(ENABLE_PROMPT)), Command('config system snmp sysinfo', CONFIG_SNMP_SYSINFO_PROMPT), Command('set status enable', CONFIG_SNMP_SYSINFO_PROMPT), Command('end', ENABLE_PROMPT), Command('config system snmp user', CONFIG_SNMP_V3_PROMPT), Command('edit quali_user', EDIT_SNMP_USER_PROMPT), Command('set status enable', EDIT_SNMP_USER_PROMPT), Command('end', ENABLE_PROMPT), Command( 'show system snmp user', 'config system snmp user\n' ' edit "quali_user"\n' ' next\n' 'end\n' '{}'.format(ENABLE_PROMPT)), ]) send_mock.side_effect = emu.send_line recv_mock.side_effect = emu.receive_all self.runner.discover() emu.check_calls()
def test_disable_snmp_v2(self, send_mock, recv_mock): self._setUp({ 'Enable SNMP': 'False', 'Disable SNMP': 'True', }) emu = CliEmulator([ Command( 'show system snmp community', 'config system snmp community\n' ' edit 100\n' ' set name "public"\n' ' config hosts\n' ' edit 1\n' ' next\n' ' end\n' ' next\n' 'end' '{}'.format(ENABLE_PROMPT)), Command('config system snmp community', CONFIG_SNMP_V2_PROMPT), Command('delete 100', CONFIG_SNMP_V2_PROMPT), Command('end', ENABLE_PROMPT), Command( 'show system snmp community', 'config system snmp community\n' 'end' '{}'.format(ENABLE_PROMPT)), ]) send_mock.side_effect = emu.send_line recv_mock.side_effect = emu.receive_all self.runner.discover() emu.check_calls()
def test_save_ftp(self, send_mock, recv_mock): user = '******' password = '******' host = '192.168.122.10' ftp_server = 'ftp://{}:{}@{}'.format(user, password, host) configuration_type = 'running' emu = CliEmulator([ Command('execute backup config ftp FortiNet-{}-\d+-\d+ {} {} {}'. format(configuration_type, host, user, password), 'Please wait...\n' '\n' 'Connect to ftp server 192.168.122.10 ...\n' '\n' 'Send config file to ftp server OK.\n' '\n' '{}'.format(ENABLE_PROMPT), regexp=True) ]) 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_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( 'show system snmp user', 'config system snmp user\n' ' edit "quali_user"\n' ' set security-level auth-priv\n' ' set auth-proto sha\n' ' set auth-pwd ENC 142A8NXCPlqD...\n' ' set priv-pwd ENC KTmph2yQ...\n' ' next\n' 'end\n' '{}'.format(ENABLE_PROMPT)), Command('config system snmp user', CONFIG_SNMP_V3_PROMPT), Command('delete quali_user', CONFIG_SNMP_V3_PROMPT), Command('end', ENABLE_PROMPT), Command( 'show system snmp user', 'end\n' '{}'.format(ENABLE_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_v2_community_index_is_used(self, send_mock, recv_mock): edit_community_prompt = EDIT_COMMUNITY_PROMPT.replace('100', '101') self._setUp() emu = CliEmulator([ Command( 'show system snmp community', ' edit 100\n' ' set name "other_community"\n' ' config hosts\n' ' edit 1\n' ' next\n' ' end\n' ' next\n' 'config system snmp community\n' 'end\n' '{}'.format(ENABLE_PROMPT)), Command('config system snmp sysinfo', CONFIG_SNMP_SYSINFO_PROMPT), Command('set status enable', CONFIG_SNMP_SYSINFO_PROMPT), Command('end', ENABLE_PROMPT), Command('config system snmp community', CONFIG_SNMP_V2_PROMPT), Command('edit 101', edit_community_prompt), Command('set name public', edit_community_prompt), Command('set status enable', edit_community_prompt), Command('set query-v2c-status enable', edit_community_prompt), Command('config hosts', CONFIG_SNMP_HOSTS_PROMPT), Command('edit 1', EDIT_SNMP_HOSTS_PROMPT), Command('end', edit_community_prompt), Command('end', ENABLE_PROMPT), Command( 'show system snmp community', 'config system snmp community\n' ' edit 100\n' ' set name "other_community"\n' ' config hosts\n' ' edit 1\n' ' next\n' ' end\n' ' next\n' ' edit 101\n' ' set name "public"\n' ' config hosts\n' ' edit 1\n' ' next\n' ' end\n' ' next\n' 'end' '{}'.format(ENABLE_PROMPT)), ]) 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_already_created(self, send_mock, recv_mock): self._setUp({'SNMP Version': 'v3'}) emu = CliEmulator([ Command( 'show system snmp user', 'config system snmp user\n' ' edit "quali_user"\n' ' next\n' 'end\n' '{}'.format(ENABLE_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_and_priv(self, send_mock, recv_mock): self._setUp({ 'SNMP Version': 'v3', 'SNMP V3 Authentication Protocol': 'SHA', 'SNMP V3 Privacy Protocol': 'AES-128', }) emu = CliEmulator([ Command( 'show system snmp user', 'config system snmp user\n' 'end\n' '{}'.format(ENABLE_PROMPT)), Command('config system snmp sysinfo', CONFIG_SNMP_SYSINFO_PROMPT), Command('set status enable', CONFIG_SNMP_SYSINFO_PROMPT), Command('end', ENABLE_PROMPT), Command('config system snmp user', CONFIG_SNMP_V3_PROMPT), Command('edit quali_user', EDIT_SNMP_USER_PROMPT), Command('set status enable', EDIT_SNMP_USER_PROMPT), Command('set security-level auth-priv', EDIT_SNMP_USER_PROMPT), Command('set auth-proto sha', EDIT_SNMP_USER_PROMPT), Command('set auth-pwd password', EDIT_SNMP_USER_PROMPT), Command('set priv-proto aes', EDIT_SNMP_USER_PROMPT), Command('set priv-pwd private_key', EDIT_SNMP_USER_PROMPT), Command('end', ENABLE_PROMPT), Command( 'show system snmp user', 'config system snmp user\n' ' edit "quali_user"\n' ' set security-level auth-priv\n' ' set auth-proto sha\n' ' set auth-pwd ENC 142A8NXCPlqD...\n' ' set priv-pwd ENC KTmph2yQ...\n' ' next\n' 'end\n' '{}'.format(ENABLE_PROMPT)), ]) send_mock.side_effect = emu.send_line recv_mock.side_effect = emu.receive_all self.runner.discover() emu.check_calls()
def test_disable_snmp_v2_already_disabled(self, send_mock, recv_mock): self._setUp({ 'Enable SNMP': 'False', 'Disable SNMP': 'True', }) emu = CliEmulator([ Command( 'show system snmp community', 'config system snmp community\n' 'end' '{}'.format(ENABLE_PROMPT)), ]) send_mock.side_effect = emu.send_line recv_mock.side_effect = emu.receive_all self.runner.discover() 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( 'show system snmp user', 'config system snmp user\n' 'end\n' '{}'.format(ENABLE_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( 'execute backup config flash FortiNet-{}-\d+-\d+'.format( configuration_type), 'Please wait...\n' 'Config backed up to flash disk done.\n' '\n' '{}'.format(ENABLE_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_load_firmware_file_system(self, send_mock, recv_mock): revision_id = '2' file_path = 'flash://{}'.format(revision_id) emu = CliEmulator([ Command( 'execute restore image flash {}'.format(revision_id), 'This operation will replace the current firmware version!\n' 'Do you want to continue? (y/n)'), Command( 'y', 'Start updating firmware\n' '{}'.format(ENABLE_PROMPT), ) ]) send_mock.side_effect = emu.send_line recv_mock.side_effect = emu.receive_all self.runner.load_firmware(file_path) emu.check_calls()
def test_load_firmware_ftp(self, send_mock, recv_mock): host = '192.168.122.10' file_name = 'firmware' file_path = 'ftp://{}/{}'.format(host, file_name) emu = CliEmulator([ Command( 'execute restore image ftp {} {}'.format(file_name, host), 'This operation will replace the current firmware version!\n' 'Do you want to continue? (y/n)'), Command( 'y', 'Start updating firmware\n' '{}'.format(ENABLE_PROMPT), ) ]) send_mock.side_effect = emu.send_line recv_mock.side_effect = emu.receive_all self.runner.load_firmware(file_path) 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( 'show system snmp user', 'config system snmp user\n' 'end\n' '{}'.format(ENABLE_PROMPT)), Command('config system snmp sysinfo', CONFIG_SNMP_SYSINFO_PROMPT), Command('set status enable', CONFIG_SNMP_SYSINFO_PROMPT), Command('end', ENABLE_PROMPT), Command('config system snmp user', CONFIG_SNMP_V3_PROMPT), Command('edit quali_user', EDIT_SNMP_USER_PROMPT), Command('set status enable', EDIT_SNMP_USER_PROMPT), Command('set security-level auth-no-priv', EDIT_SNMP_USER_PROMPT), Command('set auth-proto md5', EDIT_SNMP_USER_PROMPT), Command('set auth-pwd password', EDIT_SNMP_USER_PROMPT), Command('end', ENABLE_PROMPT), Command( 'show system snmp user', 'config system snmp user\n' ' edit "quali_user"\n' ' set security-level auth-no-priv\n' ' set auth-proto md5\n' ' set auth-pwd ENC 3iJai3...\n' ' next\n' 'end\n' '{}'.format(ENABLE_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_v2_already_enabled(self, send_mock, recv_mock): self._setUp() emu = CliEmulator([ Command( 'show system snmp community', 'config system snmp community\n' ' edit 100\n' ' set name "public"\n' ' config hosts\n' ' edit 1\n' ' next\n' ' end\n' ' next\n' 'end\n' '{}'.format(ENABLE_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_v2_not_enabled(self, send_mock, recv_mock): self._setUp() emu = CliEmulator([ Command( 'show system snmp community', 'config system snmp community\n' 'end\n' '{}'.format(ENABLE_PROMPT)), Command('config system snmp sysinfo', CONFIG_SNMP_SYSINFO_PROMPT), Command('set status enable', CONFIG_SNMP_SYSINFO_PROMPT), Command('end', ENABLE_PROMPT), Command('config system snmp community', CONFIG_SNMP_V2_PROMPT), Command('edit 100', EDIT_COMMUNITY_PROMPT), Command('set name public', EDIT_COMMUNITY_PROMPT), Command('set status enable', EDIT_COMMUNITY_PROMPT), Command('set query-v2c-status enable', EDIT_COMMUNITY_PROMPT), Command('config hosts', CONFIG_SNMP_HOSTS_PROMPT), Command('edit 1', EDIT_SNMP_HOSTS_PROMPT), Command('end', EDIT_COMMUNITY_PROMPT), Command('end', ENABLE_PROMPT), Command( 'show system snmp community', 'config system snmp community\n' 'end' '{}'.format(ENABLE_PROMPT)), ]) send_mock.side_effect = emu.send_line recv_mock.side_effect = emu.receive_all self.assertRaisesRegexp( FortiNetException, 'Failed to create SNMP community "public"', self.runner.discover, ) emu.check_calls()
def test_fail_to_restore(self, send_mock, recv_mock): host = '192.168.122.10' file_name = 'Test-running-100418-163658' remote_path = 'ftp://{}/{}'.format(host, file_name) configuration_type = 'running' emu = CliEmulator([ Command( 'execute restore config ftp {} {}'.format(file_name, host), 'This operation will overwrite the current setting and could possibly ' 'reboot the system!\n' 'Do you want to continue? (y/n)' ), Command( 'y', '\n' 'Please wait...\n' 'Connect to ftp server 192.168.122.240 ...\n' 'Can not get config file from ftp server via vdom root. Err code: 5.\n' 'Command fail. Return code -28\n' '\n' '{}'.format(ENABLE_PROMPT), ) ]) send_mock.side_effect = emu.send_line recv_mock.side_effect = emu.receive_all self.assertRaisesRegexp( Exception, 'Session returned \'Fail to restore config\'', self.runner.restore, remote_path, configuration_type, ) 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( 'show system snmp user', 'config system snmp user\n' ' edit "quali_user"\n' ' next\n' 'end\n' '{}'.format(ENABLE_PROMPT)), Command('config system snmp user', CONFIG_SNMP_V3_PROMPT), Command('delete quali_user', CONFIG_SNMP_V3_PROMPT), Command('end', ENABLE_PROMPT), Command( 'show system snmp user', 'config system snmp user\n' ' edit "quali_user"\n' ' next\n' 'end\n' '{}'.format(ENABLE_PROMPT)), ]) send_mock.side_effect = emu.send_line recv_mock.side_effect = emu.receive_all self.assertRaisesRegexp( FortiNetException, r'^Failed to disable SNMP User "quali_user"$', self.runner.discover, ) emu.check_calls()