def test_sqlite_parameters_5(self, mock_login, 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.file = os.path.join(os.path.dirname(__file__), 'configs', 'main_5.yml') js.args.snap = False js.args.check = True js.args.snapcheck = False js.args.diff = False js.args.pre_snapfile = "mock_snap" self.db['store_in_sqlite'] = False self.db['check_from_sqlite'] = True self.db['db_name'] = 'jbb.db' self.db['first_snap_id'] = 0 self.db['second_snap_id'] = 1 js.get_hosts() self.assertEqual(js.db, self.db)
def test_connect_snap(self, mock_log, mock_gen_reply, mock_dev, 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.file = os.path.join(os.path.dirname(__file__), 'configs', 'main_1.yml') js.args.snap = True js.args.pre_snapfile = "mock_snap" js.get_hosts() self.assertTrue(mock_gen_reply.called) self.assertTrue(mock_dev.called)
def test_check_arguments_4(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.snap = False js.args.file = os.path.join(os.path.dirname(__file__), 'configs', 'main_3.yml') js.args.check = False js.args.snapcheck = False js.args.diff = True js.args.post_snapfile = None js.args.pre_snapfile = "mock_snap" with patch('jnpr.jsnapy.jsnapy.argparse.ArgumentParser.print_help' ) as mock_parser: js.check_arguments() js.get_hosts() mock_sys.assert_called_with(1) mock_parser.assert_called_with()
def test_sqlite_parameters_1(self, mock_login, mock_arg): js = SnapAdmin() 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.args.file = os.path.join(os.path.dirname(__file__), 'configs', 'main_1.yml') js.args.snap = True js.args.check = False js.args.snapcheck = False js.args.diff = False js.args.pre_snapfile = "mock_snap" js.get_hosts() self.assertEqual(js.db, self.db)
def test_check_mail(self, mock_path, mock_notify, mock_pass, mock_compare, 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.file = os.path.join(os.path.dirname(__file__), 'configs', 'main_mail_2.yml') js.args.check = True js.args.snap = False js.args.snapcheck = False js.args.pre_snapfile = "mock_snap" js.args.post_snapfile = "mock_snap2" mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') js.get_hosts() self.assertTrue(mock_notify.called) self.assertTrue(mock_pass.called) self.assertTrue(mock_compare.called)
def test_connect_diff(self, mock_compare, arg_exit): 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.file = os.path.join(os.path.dirname(__file__), 'configs', 'main_1.yml') config_file = open(js.args.file, 'r') config_data = yaml.load(config_file) js.args.diff = True js.args.pre_snapfile = "mock_snap1" js.args.post_snapfile = "mock_snap2" js.get_hosts() mock_compare.assert_called_once_with('10.216.193.114', config_data, js.args.pre_snapfile, None, None)
def test_check_arguments_test_file_3(self, mock_log, mock_login, mock_sys, mock_arg): js = SnapAdmin() js.args.snap = False js.args.file = None js.args.testfiles = [ os.path.join(os.path.dirname(__file__), 'configs', 'test_diff.yml'), os.path.join(os.path.dirname(__file__), 'configs', 'tests.yml') ] js.args.check = False js.args.snapcheck = True js.args.diff = False js.args.login = "******" js.args.hostname = "10.216.193.114" js.args.passwd = "xyz" js.args.post_snapfile = "mock_snap2" 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]) self.assertEqual(js.main_file['tests'][1], js.args.testfiles[1])
def test_sqlite_parameters_7(self, mock_login, mock_arg): js = SnapAdmin() js.args.file = os.path.join(os.path.dirname(__file__), 'configs', 'main_1.yml') js.args.snap = False js.args.check = True js.args.snapcheck = False js.args.diff = False js.args.pre_snapfile = "mock_snap" js.args.post_snapfile = "mock_snap2" js.get_hosts() self.assertEqual(js.db, self.db)
def test_check_mail_password(self, mock_path, mock_notify, mock_pass, mock_compare, mock_arg): js = SnapAdmin() js.args.file = os.path.join(os.path.dirname(__file__), 'configs', 'main_mail_2.yml') js.args.check = True js.args.pre_snapfile = "mock_snap" mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') js.get_hosts() self.assertTrue(mock_pass.called) self.assertTrue(mock_notify.called)
def test_sqlite_parameters_6(self, mock_login, mock_arg): js = SnapAdmin() js.args.file = os.path.join(os.path.dirname(__file__), 'configs', 'main_2.yml') js.args.snap = False js.args.check = False js.args.snapcheck = True js.args.diff = False js.args.pre_snapfile = "mock_snap" self.db['store_in_sqlite'] = True self.db['db_name'] = 'jbb.db' js.get_hosts() self.assertEqual(js.db, self.db)
def test_connect_snapcheck_local_cmd(self, mock_log, mock_snap, mock_check, mock_dev): js = SnapAdmin() js.args.snapcheck = True js.args.local = True js.args.file = os.path.join(os.path.dirname(__file__), 'configs', 'main_1.yml') config_file = open(js.args.file, 'r') config_data = yaml.load(config_file) js.args.pre_snapfile = "mock_snap" js.get_hosts() self.assertFalse(mock_dev.called) self.assertFalse(mock_snap.called) #we check whether get_test was called, indirectly checking whether compare_tests was called. mock_check.assert_called_once_with('10.216.193.114', config_data, "mock_snap", None, None)
def test_conditional_mail_5(self, mock_path, mock_notify, mock_pass, mock_compare, mock_arg): js = SnapAdmin() js.args.file = os.path.join(os.path.dirname(__file__), 'configs', 'main_mail_wrong.yml') js.args.check = True js.args.snap = False js.args.snapcheck = False js.args.pre_snapfile = "mock_snap" js.args.post_snapfile = "mock_snap2" mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') mock_compare.return_value = MagicMock(result='Passed') js.get_hosts() self.assertFalse(mock_notify.called) self.assertFalse(mock_pass.called) self.assertTrue(mock_compare.called)
def test_connect_snapcheck_local_config(self, mock_log, mock_snap, mock_check, mock_dev): js = SnapAdmin() js.args.snapcheck = True js.args.file = os.path.join(os.path.dirname(__file__), 'configs', 'main_local_snapcheck.yml') config_file = open(js.args.file, 'r') config_data = yaml.load(config_file) js.args.pre_snapfile = "mock_snap" js.get_hosts() self.assertTrue(js.args.local) self.assertFalse(mock_dev.called) self.assertFalse(mock_snap.called) #we check whether get_test was called, indirectly checking whether compare_tests was called. expected_calls_made = [ call('10.216.193.114', config_data, 'PRE', None, None), call('10.216.193.114', config_data, 'PRE_42', None, None), call('10.216.193.114', config_data, 'PRE_314', None, None) ] mock_check.assert_has_calls(expected_calls_made, any_order=True)