예제 #1
0
class IgnoreConfigParserTestCase(unittest.TestCase):
    def test_unkown_config_file(self):
        test_config_path = __here__ / 'foo.cfg'

        self.assertFalse(test_config_path.is_file())

        self.parser = CliParser('TestParser', 'test.log')

        args = self.parser.parse_args(
            ['--config', str(test_config_path), 'socket'])

        self.assertEqual(args.timeout, DEFAULT_TIMEOUT)
        self.assertEqual(args.gmp_password, '')
        self.assertEqual(args.gmp_username, '')
        self.assertEqual(args.socketpath, DEFAULT_UNIX_SOCKET_PATH)

    def test_unkown_config_file_in_unkown_dir(self):
        test_config_path = __here__ / 'foo' / 'foo.cfg'

        self.assertFalse(test_config_path.is_file())

        self.parser = CliParser('TestParser', 'test.log')

        args = self.parser.parse_args(
            ['--config', str(test_config_path), 'socket'])

        self.assertEqual(args.timeout, DEFAULT_TIMEOUT)
        self.assertEqual(args.gmp_password, '')
        self.assertEqual(args.gmp_username, '')
        self.assertEqual(args.socketpath, DEFAULT_UNIX_SOCKET_PATH)
예제 #2
0
class ConfigParserTestCase(unittest.TestCase):
    def setUp(self):
        self.test_config_path = __here__ / 'test.cfg'

        self.assertTrue(self.test_config_path.is_file())

        self.parser = CliParser('TestParser', 'test.log')

    def test_socket_defaults_from_config(self):
        args = self.parser.parse_args(
            ['--config', str(self.test_config_path), 'socket'])

        self.assertEqual(args.foo, 'bar')
        self.assertEqual(args.timeout, 1000)
        self.assertEqual(args.gmp_password, 'bar')
        self.assertEqual(args.gmp_username, 'bar')
        self.assertEqual(args.socketpath, '/foo/bar.sock')

    def test_ssh_defaults_from_config(self):
        args = self.parser.parse_args([
            '--config',
            str(self.test_config_path), 'ssh', '--hostname', 'foo'
        ])

        self.assertEqual(args.foo, 'bar')
        self.assertEqual(args.timeout, 1000)
        self.assertEqual(args.gmp_password, 'bar')
        self.assertEqual(args.gmp_username, 'bar')
        self.assertEqual(args.ssh_password, 'lorem')
        self.assertEqual(args.ssh_username, 'ipsum')
        self.assertEqual(args.port, 123)

    def test_tls_defaults_from_config(self):
        args = self.parser.parse_args([
            '--config',
            str(self.test_config_path), 'tls', '--hostname', 'foo'
        ])

        self.assertEqual(args.foo, 'bar')
        self.assertEqual(args.timeout, 1000)
        self.assertEqual(args.gmp_password, 'bar')
        self.assertEqual(args.gmp_username, 'bar')
        self.assertEqual(args.certfile, 'foo.cert')
        self.assertEqual(args.keyfile, 'foo.key')
        self.assertEqual(args.cafile, 'foo.ca')
        self.assertEqual(args.port, 123)
예제 #3
0
    def setUp(self):
        self.test_config_path = __here__ / 'test.cfg'

        self.assertTrue(self.test_config_path.is_file())

        self.parser = CliParser('TestParser', 'test.log')
예제 #4
0
 def setUp(self):
     self.parser = CliParser('TestParser',
                             'test.log',
                             ignore_config=True,
                             prog='gvm-test-cli')
예제 #5
0
class ConfigParserTestCase(unittest.TestCase):
    def setUp(self):
        self.test_config_path = __here__ / 'test.cfg'

        self.assertTrue(self.test_config_path.is_file())

        self.parser = CliParser('TestParser', 'test.log')

    def test_socket_defaults_from_config(self):
        args = self.parser.parse_args(
            ['--config', str(self.test_config_path), 'socket'])

        self.assertEqual(args.foo, 'bar')
        self.assertEqual(args.timeout, 1000)
        self.assertEqual(args.gmp_password, 'bar')
        self.assertEqual(args.gmp_username, 'bar')
        self.assertEqual(args.socketpath, '/foo/bar.sock')

    def test_ssh_defaults_from_config(self):
        args = self.parser.parse_args([
            '--config',
            str(self.test_config_path), 'ssh', '--hostname', 'foo'
        ])

        self.assertEqual(args.foo, 'bar')
        self.assertEqual(args.timeout, 1000)
        self.assertEqual(args.gmp_password, 'bar')
        self.assertEqual(args.gmp_username, 'bar')
        self.assertEqual(args.ssh_password, 'lorem')
        self.assertEqual(args.ssh_username, 'ipsum')
        self.assertEqual(args.port, 123)

    def test_tls_defaults_from_config(self):
        args = self.parser.parse_args([
            '--config',
            str(self.test_config_path), 'tls', '--hostname', 'foo'
        ])

        self.assertEqual(args.foo, 'bar')
        self.assertEqual(args.timeout, 1000)
        self.assertEqual(args.gmp_password, 'bar')
        self.assertEqual(args.gmp_username, 'bar')
        self.assertEqual(args.certfile, 'foo.cert')
        self.assertEqual(args.keyfile, 'foo.key')
        self.assertEqual(args.cafile, 'foo.ca')
        self.assertEqual(args.port, 123)

    @patch('gvmtools.parser.logger')
    @patch('gvmtools.parser.Path')
    def test_resolve_file_not_found_error(self, path_mock, logger_mock):
        # Making sure that resolve raises an error
        def resolve_raises_error():
            raise FileNotFoundError()

        configpath = unittest.mock.MagicMock()
        configpath.expanduser().resolve = unittest.mock.MagicMock(
            side_effect=resolve_raises_error)
        path_mock.return_value = configpath

        logger_mock.debug = unittest.mock.MagicMock()

        args = self.parser.parse_args(['socket'])

        self.assertIsInstance(args, Namespace)
        self.assertEqual(args.connection_type, 'socket')
        self.assertEqual(args.config, '~/.config/gvm-tools.conf')
        logger_mock.debug.assert_any_call(
            'Ignoring non existing config file %s', '~/.config/gvm-tools.conf')

    @patch('gvmtools.parser.Path')
    @patch('gvmtools.parser.Config')
    def test_config_load_raises_error(self, config_mock, path_mock):
        def config_load_error():
            raise Exception

        config = unittest.mock.MagicMock()
        config.load = unittest.mock.MagicMock(side_effect=config_load_error)
        config_mock.return_value = config

        # Making sure that the function thinks the config file exists
        configpath_exists = unittest.mock.Mock()
        configpath_exists.expanduser().resolve().exists = (
            unittest.mock.MagicMock(return_value=True))
        path_mock.return_value = configpath_exists

        self.assertRaises(RuntimeError, self.parser.parse_args, ['socket'])