Ejemplo n.º 1
0
def validate_config():
    """Check config file against schema."""
    path, schema = Paths.config_file(), JsonSchemas.config()
    _validate_file(path, schema)
    errors = _check_properties_deprecations(path, schema)
    for err in errors:
        echo_warning(str(err))
Ejemplo n.º 2
0
def test_validate_config_valid(tmp_path, monkeypatch):
    monkeypatch.setattr(Path, 'home', lambda: tmp_path)

    Paths.config_dir().mkdir()
    with Paths.config_file().open('w') as f:
        f.write(yaml.dump(VALID_CONFIG))

    validate_config()
Ejemplo n.º 3
0
def update_config(section: str, data: Dict[str, Any]) -> None:
    data = {section: data}
    config_file = Paths.config_file()
    if config_file.is_file():
        config_yaml = read_yaml(config_file)
        config_yaml.update(data)
        write_yaml(config_file, config_yaml)
    else:
        write_yaml(config_file, data)
Ejemplo n.º 4
0
def read_config(section: str = '') -> Dict[str, Any]:
    config_file = Paths.config_file()
    if not config_file.is_file():
        return {}

    config = read_yaml(config_file)
    if section != '':
        return config.get(section, {})
    return config
Ejemplo n.º 5
0
def test_validate_config_invalid_yaml(tmp_path, monkeypatch):
    monkeypatch.setattr(Path, 'home', lambda: tmp_path)

    Paths.config_dir().mkdir()
    with Paths.config_file().open('w') as f:
        f.write('not:\nyaml')

    with pytest.raises(InvalidYamlFile):
        validate_config()
Ejemplo n.º 6
0
    def __init__(self, *, path: Path):
        if path == Paths.context_file():
            msg = f'Context file ({path.name}) not found in current working directory. Run pano init to create it.'
        elif path == Paths.config_file():
            msg = f'Config file ({path.absolute()}) not found. Run pano configure to create it.'
        else:
            # Should not happen => we only check above files exist explicitly
            msg = f'File Missing - {path}'

        super().__init__(msg)
Ejemplo n.º 7
0
def test_connections_e2e(mock_create_engine, monkeypatch, tmpdir):
    monkeypatch.setattr(Path, 'home', lambda: Path(tmpdir))
    runner = CliRunner()

    # Create config
    runner.invoke(cli, ['configure'])

    # Create connection
    result = runner.invoke(
        cli,
        [
            'connection',
            'create',
            'my-connection',
            'sqlite://',
            '--no-test',
        ],
    )

    assert result.exit_code == 0, result.output
    connections_json = {
        'auth': {},
        'connections': {
            'my-connection': {
                'connection_string': 'sqlite://',
            },
        },
    }
    with Paths.config_file().open() as f:
        assert yaml.safe_load(f.read()) == connections_json

    # List
    result = runner.invoke(cli, ['connection', 'list'])
    assert result.exit_code == 0, result.output
    assert result.output == yaml.dump(connections_json['connections']) + "\n"

    # Update
    result = runner.invoke(cli, ['connection', 'update', 'my-connection', 'sqlite://'])
    assert result.exit_code == 0, result.output

    # List
    result = runner.invoke(cli, ['connection', 'list'])
    assert result.exit_code == 0, result.output
    connections_json['connections']['my-connection']['connection_string'] = 'sqlite://'
    assert result.output == yaml.dump(connections_json['connections']) + "\n"

    # Update
    result = runner.invoke(cli, ['connection', 'remove', 'my-connection'])
    assert result.exit_code == 0, result.output

    # List
    result = runner.invoke(cli, ['connection', 'list'])
    assert result.exit_code == 0, result.output
    assert result.stdout.startswith('No connections found.\nUse "pano connection create" to create')
Ejemplo n.º 8
0
def list_connections_command() -> None:
    """CLI command. List all connections."""
    connections = Connections.load()
    if not connections:
        config_file = Paths.config_file()
        echo_info(
            f'No connections found.\n'
            f'Use "pano connection create" to create connection or edit "{config_file}" file.'
        )
        exit(0)

    echo_info(yaml.dump(connections))
def test_configure_e2e(monkeypatch, tmpdir):
    monkeypatch.setattr(Path, 'home', lambda: Path(tmpdir))
    runner = CliRunner()

    result = runner.invoke(cli, ['configure'], input='test-client-id\ntest-client-secret')

    assert result.exit_code == 0, result.output
    with Paths.config_file().open() as f:
        assert yaml.safe_load(f.read()) == {
            'auth': {
                'client_id': 'test-client-id',
                'client_secret': 'test-client-secret',
            },
        }
Ejemplo n.º 10
0
def test_analytics_e2e(monkeypatch, tmpdir):
    flushed = False
    def flush_mock():
        nonlocal flushed
        flushed = True

    monkeypatch.setattr(Path, 'home', lambda: Path(tmpdir))
    monkeypatch.setattr(analytics, '_flush', flush_mock)
    # Enable writing of events
    monkeypatch.setattr(analytics, 'config_is_enabled', lambda: True)
    # Enabled prompt
    monkeypatch.setattr(command, 'analytics_module_is_enabled', lambda: True)
    runner = CliRunner()

    result = runner.invoke(cli, ['configure'], input='test-client-id\ntest-client-secret\ny')
    assert result.exit_code == 0, result.output
    with Paths.config_file().open() as f:
        assert yaml.safe_load(f.read()) == {
            'analytics': {
                'enabled': True,
            },
            'auth': {
                'client_id': 'test-client-id',
                'client_secret': 'test-client-secret',
            },
        }

    # Create connection
    result = runner.invoke(
        cli,
        [
            'connection',
            'create',
            'my-connection',
            '--type',
            'postgres',
            '--user',
            'my-user',
            '--host',
            'localhost',
            '--port',
            '5432',
            '--database',
            'my_db',
            '--password',
            'my-password',
            '--no-test',
        ],
    )
    assert result.exit_code == 0, result.output

    assert len(analytics._read_events()) == 1

    for i in range(analytics.MINIMAL_FLUSH_EVENTS):
        result = runner.invoke(
            cli, ['connection', 'update', 'my-connection', '--database', f'my-new-db-{i}', '--no-test']
        )
        assert result.exit_code == 0, result.output

    assert flushed is True
    assert len(analytics._read_events()) == 11

    with Paths.analytics_events_file().open() as f:
        lines = list(f.readlines())
        data = json.loads(lines[0])
        assert data['name'] == 'connection create'
        for line in lines[1:]:
            data = json.loads(line)
            assert data['name'] == 'connection update'
Ejemplo n.º 11
0
def test_connections_e2e(monkeypatch, tmpdir):
    monkeypatch.setattr(Path, 'home', lambda: Path(tmpdir))
    runner = CliRunner()

    # Create config
    runner.invoke(cli, ['configure'],
                  input='test-client-id\ntest-client-secret')

    # Create connection
    result = runner.invoke(
        cli,
        [
            'connection',
            'create',
            'my-connection',
            '--type',
            'postgres',
            '--user',
            'my-user',
            '--host',
            'localhost',
            '--port',
            '5432',
            '--database',
            'my_db',
            '--password-stdin',
            '--no-test',
        ],
        input='my-password',
    )

    assert result.exit_code == 0, result.output
    connections_json = {
        'auth': {
            'client_id': 'test-client-id',
            'client_secret': 'test-client-secret',
        },
        'connections': {
            'my-connection': {
                'type': 'postgres',
                'user': '******',
                'host': 'localhost',
                'port': 5432,
                'database': 'my_db',
                'password': '******',
            },
        },
    }
    with Paths.config_file().open() as f:
        assert yaml.safe_load(f.read()) == connections_json

    # List
    result = runner.invoke(cli, ['connection', 'list', '--show-password'])
    assert result.exit_code == 0, result.output
    assert result.output == yaml.dump(connections_json['connections']) + "\n"

    # Update
    result = runner.invoke(cli, [
        'connection', 'update', 'my-connection', '--database', 'my-new-db',
        '--no-test'
    ])
    assert result.exit_code == 0, result.output

    # List
    result = runner.invoke(cli, ['connection', 'list'])
    assert result.exit_code == 0, result.output
    connections_json['connections']['my-connection']['password'] = '******'
    connections_json['connections']['my-connection']['database'] = 'my-new-db'
    assert result.output == yaml.dump(connections_json['connections']) + "\n"

    # Update
    result = runner.invoke(cli, ['connection', 'remove', 'my-connection'])
    assert result.exit_code == 0, result.output

    # List
    result = runner.invoke(cli, ['connection', 'list'])
    assert result.exit_code == 0, result.output
    assert result.stdout.startswith(
        'No connections found.\nUse "pano connection create" to create')