Example #1
0
    def test_check_arguments_3(self, mock_log, mock_login, mock_sys, mock_arg):
        argparse.ArgumentParser.parse_args = MagicMock()
        argparse.ArgumentParser.parse_args.return_value = argparse.Namespace(
            check=False,
            diff=False,
            file=None,
            hostname=None,
            login=None,
            passwd=None,
            port=None,
            post_snapfile=None,
            pre_snapfile=None,
            snap=False,
            snapcheck=False,
            verbosity=None,
            version=False)

        js = SnapAdmin()
        js.args.snapcheck = True
        with patch('argparse.ArgumentParser.print_help') as mock_parser:
            js.check_arguments()
            mock_sys.assert_called_with(1)
            mock_parser.assert_called_with()
Example #2
0
 def test_hostname(self, mock_connect):
     argparse.ArgumentParser.parse_args = MagicMock()
     argparse.ArgumentParser.parse_args.return_value = argparse.Namespace(
         check=False,
         diff=False,
         file=None,
         hostname=None,
         login=None,
         passwd=None,
         port=None,
         post_snapfile=None,
         pre_snapfile=None,
         snap=False,
         snapcheck=False,
         verbosity=None,
         version=False)
     js = SnapAdmin()
     conf_file = os.path.join(os.path.dirname(__file__), 'configs',
                              'main_1.yml')
     config_file = open(conf_file, 'r')
     js.main_file = yaml.load(config_file)
     js.login("snap_1")
     hosts = ['10.216.193.114']
     self.assertEqual(js.host_list, hosts)
Example #3
0
    def test_multiple_hostname(self, mock_path, mock_connect, mock_arg):
        argparse.ArgumentParser.parse_args = MagicMock()
        argparse.ArgumentParser.parse_args.return_value = argparse.Namespace(
            check=False,
            diff=False,
            file=None,
            hostname=None,
            login=None,
            passwd=None,
            port=None,
            post_snapfile=None,
            pre_snapfile=None,
            snap=False,
            snapcheck=False,
            verbosity=None,
            version=False)
        mock_path.return_value = os.path.join(os.path.dirname(__file__),
                                              'configs')
        js = SnapAdmin()
        conf_file = os.path.join(os.path.dirname(__file__), 'configs',
                                 'main1.yml')
        config_file = open(conf_file, 'r')
        js.main_file = yaml.load(config_file)
        js.login("snap_1")
        hosts = ['10.209.16.203', '10.209.16.204', '10.209.16.205']
        self.assertEqual(js.host_list, hosts)

        #extending the test to check for device overlap among groups
        js.main_file['hosts'][0]['group'] = 'MX, EX'
        expected_calls_made = [
            call('10.209.16.203', 'abc', 'def', 'snap_1'),
            call('10.209.16.204', 'abc', 'def', 'snap_1'),
            call('10.209.16.205', 'abc', 'def', 'snap_1'),
            call('10.209.16.206', 'abc', 'def', 'snap_1'),
            call('10.209.16.212', 'abc', 'def', 'snap_1'),
        ]

        js.login("snap_1")
        mock_connect.assert_has_calls(expected_calls_made, any_order=True)
Example #4
0
 def test_generate_rpc_reply_correct_data(self, mock_parse, mock_path):
     # Testcase to check if proper config_data is passed to generate_rpc_reply
     # it should not give an error in that function
     argparse.ArgumentParser.parse_args = MagicMock()
     argparse.ArgumentParser.parse_args.return_value = argparse.Namespace()
     js = SnapAdmin()
     conf_file = os.path.join(os.path.dirname(__file__),
                              'configs', 'main.yml')
     config_file = open(conf_file, 'r')
     js.main_file = yaml.load(config_file, Loader=yaml.FullLoader)
     mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs')
     js.test_cases = None
     js.generate_rpc_reply(
         None,
         "snap_mock",
         "1.1.1.1",
         js.main_file)
     self.assertTrue(mock_parse.called)
Example #5
0
    def test_port_with_include(self, mock_connect, mock_path):
        #this test case is for scenarios when devices are included using some other file
        js = SnapAdmin()
        js.args.snap = True
        js.args.hostname = None
        mock_path.return_value = os.path.join(os.path.dirname(__file__),
                                              'configs')

        conf_file = os.path.join(os.path.dirname(__file__), 'configs',
                                 'main2_with_port.yml')
        config_file = open(conf_file, 'r')
        js.main_file = yaml.load(config_file)

        hosts = ['10.209.16.203', '10.209.16.204', '10.209.16.205']
        expected_calls_made = [
            call('10.209.16.203', 'abc', 'def', 'snap_1', port=100),
            call('10.209.16.204', 'abc', 'def', 'snap_1', port=101),
            call('10.209.16.205', 'abc', 'def', 'snap_1', port=102)
        ]

        js.login("snap_1")

        self.assertEqual(js.host_list, hosts)
        mock_connect.assert_has_calls(expected_calls_made, any_order=True)
        #    mock_connect.assert_called_with('10.216.193.114','abc','xyz','snap_1',port=44)

        #Adding the cmd-line port param and checking the precedence b/w cmd and config params
        js.args.port = 55
        expected_calls_made = [
            call('10.209.16.203', 'abc', 'def', 'snap_1', port=55),
            call('10.209.16.204', 'abc', 'def', 'snap_1', port=55),
            call('10.209.16.205', 'abc', 'def', 'snap_1', port=55)
        ]
        js.login("snap_1")

        mock_connect.assert_has_calls(expected_calls_made, any_order=True)
Example #6
0
 def test_generate_rpc_reply_testcase_extracted_data(self, mock_parse, mock_extract, mock_path):
     # Testcase to check if proper config_data is passed to generate_rpc_reply
     # testcases is already extracted so it should not be called.
     # it should not give an error in that function
     argparse.ArgumentParser.parse_args = MagicMock()
     argparse.ArgumentParser.parse_args.return_value = argparse.Namespace()
     js = SnapAdmin()
     conf_file = os.path.join(os.path.dirname(__file__),
                              'configs', 'main.yml')
     config_file = open(conf_file, 'r')
     js.main_file = yaml.load(config_file, Loader=yaml.FullLoader)
     mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs')
     js.test_cases = [{'tests_include': ['check_chassis_fpc'], 'check_chassis_fpc':
         [{'command': 'show chassis fpc'}, {'item': {'xpath': '//fpc', 'id': 'slot',
                                                     'tests': [{'delta': 'memory-heap-utilization, 50%'}]}}]}]
     js.generate_rpc_reply(
         None,
         "snap_mock",
         "1.1.1.1",
         js.main_file)
     self.assertFalse(mock_extract.called)
     self.assertTrue(mock_parse.called)
Example #7
0
 def test_check_arguments_test_file_2(self, mock_log, mock_login, mock_sys,
                                      mock_arg):
     js = SnapAdmin()
     js.args.snap = True
     js.args.file = None
     js.args.testfiles = os.path.join(os.path.dirname(__file__), 'configs',
                                      'test_diff.yml')
     js.args.check = False
     js.args.snapcheck = False
     js.args.diff = False
     js.args.login = "******"
     js.args.hostname = "10.216.193.114"
     js.args.passwd = "xyz"
     js.args.post_snapfile = None
     js.args.pre_snapfile = "mock_snap"
     with patch('argparse.ArgumentParser.print_help') as mock_parser:
         js.check_arguments()
         js.get_hosts()
         self.assertTrue(js.main_file)
         self.assertEqual(js.main_file['hosts'][0]['device'],
                          "10.216.193.114")
         self.assertEqual(js.main_file['hosts'][0]['username'], "abc")
         self.assertEqual(js.main_file['hosts'][0]['passwd'], "xyz")
         self.assertEqual(js.main_file['tests'][0], js.args.testfiles[0])
Example #8
0
 def test_action_api_based_error_file(self, mock_data, mock_exit):
     js = SnapAdmin()
     js.args.file = os.path.join(os.path.dirname(__file__), 'configs', 'main.yml')
     js.snapcheck(js.args.file, 'mock_file')
     mock_exit.assert_called()
Example #9
0
 def test_extract_data_error(self):
     with self.assertRaises(Exception):
         js = SnapAdmin()
         js.args.file = os.path.join(os.path.dirname(__file__), 'configs', 'no_such_file.yml')
         js.extract_data(js.args.file, 'mock_pre', None, 'mock_post')
Example #10
0
 def test_get_config_file_no_file(self, mock_exit):
     js = SnapAdmin()
     js.main_file = "hosts"
     js.args.file = "file_not_found.yml"
     js.get_config_file()
     mock_exit.assert_called_with(1)
Example #11
0
    def test_port_without_include(self, mock_connect):
        #this test case is for scenarios when devices are mentioned in the cfg file itself
        js = SnapAdmin()
        # js.args.snap = True
        js.args.hostname = None

        conf_file = os.path.join(os.path.dirname(__file__), 'configs',
                                 'main_with_port.yml')
        config_file = open(conf_file, 'r')
        js.main_file = yaml.load(config_file)
        js.login("snap_1")
        hosts = ['10.216.193.114']
        self.assertEqual(js.host_list, hosts)
        mock_connect.assert_called_with('10.216.193.114',
                                        'abc',
                                        'xyz',
                                        'snap_1',
                                        port=44)

        #adding another device in the config dictionary
        #and checking the precedence b/w cmd and config params
        js.main_file['hosts'].append({
            'device': '10.216.193.115',
            'username': '******',
            'passwd': 'xyz',
            'port': 45
        })
        js.args.port = 100

        expected_calls_made = [
            call('10.216.193.114', 'abc', 'xyz', 'snap_1', port=100),
            call('10.216.193.115', 'abc', 'xyz', 'snap_1', port=100)
        ]
        js.login("snap_1")
        mock_connect.assert_has_calls(expected_calls_made, any_order=True)

        #deleting the port paramater from the config file and keeping the port param on cmd args
        for host in js.main_file['hosts']:
            if 'port' in host:
                del host['port']

        js.login("snap_1")
        mock_connect.assert_has_calls(expected_calls_made, any_order=True)

        #deleting the cmd line port param
        expected_calls_made = [
            call('10.216.193.114', 'abc', 'xyz', 'snap_1'),
            call('10.216.193.115', 'abc', 'xyz', 'snap_1')
        ]
        js.args.port = None
        js.login("snap_1")
        mock_connect.assert_has_calls(expected_calls_made, any_order=True)