App-Sqitch

 view release on metacpan or  search on metacpan

t/clickhouse.t  view on Meta::CPAN

    '--secure',
    '--port' => 90210,
], 'clickhouse command should be configured with query vals';

# Make sure the TLS HTTP ports trigger the encrypted native port.
for my $port (8443, 443) {
    my $target = App::Sqitch::Target->new(
        sqitch => $sqitch,
        uri    => URI->new("db:clickhouse://foo.com:$port/widgets",
    ));
    ok $ch = $CLASS->new(sqitch => $sqitch, target => $target),
        "Create a clickhouse with URI port $port";
    is_deeply [$ch->cli], [
        qw(/path/to/clickhouse client),
        qw(--database widgets --host foo.com),
        @std_opts,
        '--port' => 9440,
    ], "clickhouse command should be configured with port 9449";
}

# But not when the configuration defines a port.
PORT: {
    my $mock = Test::MockModule->new($CLASS);
    $mock->mock(_clickcnf => { port => 8888 });
    my $target = App::Sqitch::Target->new(
        sqitch => $sqitch,
        uri    => URI->new("db:clickhouse://foo.com:443/widgets",
    ));
    ok $ch = $CLASS->new(sqitch => $sqitch, target => $target),
        "Create a clickhouse with default port 8888";
    is_deeply [$ch->cli], [
        qw(/path/to/clickhouse client),
        qw(--database widgets --host foo.com),
        @std_opts,
    ], "clickhouse command should not include --port";
}

##############################################################################
# Test _clickcnf
CONFIG: {
    my $orig_dir = getcwd();
    my $tmp_dir = tempdir CLEANUP => 1;
    chdir $tmp_dir;
    my $tmp_home = tempdir CLEANUP => 1;
    my $mock_config = Test::MockModule->new('App::Sqitch::Config');
    $mock_config->mock(home_dir => $tmp_home);

    # Write config files.
    for my $spec (
        [qw(temp . clickhouse-client)],
        ['home', $tmp_home, '.clickhouse-client'],
    ) {
        for my $ext (qw(xml yaml yml)) {
            my $path = file $spec->[1], "$spec->[2].$ext";
            open my $fh, '>:utf8', $path or die "Cannot open $path: $!";
            if ($ext eq 'xml') {
                print {$fh} qq{
                    <config>
                      <user>$spec->[0]</user>
                      <password>$ext</password>
                      <connections_credentials>
                        <connection>
                          <name>lol.cats</name>
                          <hostname>cats.example</hostname>
                        </connection>
                      </connections_credentials>
                    </config>
                };
            } else {
                print {$fh} qq{
                    user: $spec->[0]
                    password: $ext
                    connections_credentials:
                      connection:
                      - name: lol.cats
                        hostname: cats.example
                };
            }
        }
    }

    # Now find them in order.
    for my $spec (
        [qw(temp . clickhouse-client)],
        ['home', $tmp_home, '.clickhouse-client'],
    ) {
        for my $ext (qw(xml yaml yml)) {
            $target = App::Sqitch::Target->new(
                sqitch => $sqitch,
                uri    => URI->new('db:clickhouse://lol.cats',
            ));

            my $ch = $CLASS->new(sqitch => $sqitch, target => $target);
            ok my $cfg = $ch->_clickcnf, "Should load $ext config from $spec->[0]";
            is_deeply $cfg, {
                user     => $spec->[0],
                password => $ext,
                host     => 'cats.example',
            }, "Should have $ext config from $spec->[0]";
            unlink file $spec->[1], "$spec->[2].$ext";
        }
    }

    chdir $orig_dir;
}

##############################################################################
# Test _load_xml.
XML: {
    my $dir = dir qw(t click-conf);
    while (my $file = $dir->next) {
        next if $file !~ /\.xml$/;
        my $perl =  do{ (my $x = $file) =~ s/\.xml$/.pl/; file $x };
        my $exp = eval $perl->slurp;
        die "Failed to eval $perl: $@" if $@;
        my $got = App::Sqitch::Engine::clickhouse::_load_xml($file);
        is_deeply ($got, $exp, "Should have properly parsed $file");
    }
}

##############################################################################
# Test _is_true.
for my $t (qw(true on yes On YES True TRUE 42 99 -42 99.6)) {
    is App::Sqitch::Engine::clickhouse::_is_true $t, 1, "$t should be true";
}

for my $f ('', qw(0 0.0 false off no False FALSE Off nO)) {
    is App::Sqitch::Engine::clickhouse::_is_true $f, 0, "$f should be false";
}

##############################################################################
# Test _conn_cfg.
for my $tc (
    {
        test   => 'empty',
        config => {},
        exp    => {},
    },
    {
        test   => 'root only',
        config => {
            secure   => 'true',
            host     => 'bagel.cat',
            port     => 8000,
            user     => 'sushi',
            password => 's3cr3t',
            database => 'pets',
        },
        exp => {
            secure   => 1,
            host     => 'bagel.cat',
            port     => 8000,
            user     => 'sushi',
            password => 's3cr3t',
            database => 'pets',
        },
    },
    {
        test   => 'client TLS',
        config => {
            secure  => 'yes',
            user    => 'biscuit',
            openSSL => {
                client => {
                    caConfig => '/etc/ssl/cert.pem',
                }
            }
        },
        exp => {
            secure  => 1,
            user    => 'biscuit',
            tls => { caConfig => '/etc/ssl/cert.pem' },
        },
    },
    {
        test   => 'no client TLS',
        config => {
            secure  => 'no',
            openSSL => {
                server => {
                    caConfig => '/etc/ssl/cert.pem',
                }
            }
        },
        exp => { secure => 0 },
    },
    {
        test   => 'default connection',
        config => {
            secure   => 'true',
            user     => 'sushi',
            password => 's3cr3t',
            database => 'pets',
            connections_credentials => { connection => {
                name     => 'localhost',
                secure   => 'false',
                hostname => 'cats.lol',
                port     => 8181,
                user     => 'biscuit',
                password => 'meow',
                database => 'cats',
            }}
        },
        exp => {
            secure   => 0,
            host     => 'cats.lol',
            port     => 8181,
            user     => 'biscuit',
            password => 'meow',
            database => 'cats',
        },
    },
    {
        test   => 'different host',
        config => {
            secure   => 'true',
            host     => 'bagel.cat',
            user     => 'sushi',
            password => 's3cr3t',
            database => 'pets',
            connections_credentials => { connection => {
                name     => 'localhost',
                secure   => 'false',
                hostname => 'cats.lol',
                user     => 'biscuit',
                password => 'meow',
                database => 'cats',
            }}
        },
        exp => {
            secure   => 1,
            host     => 'bagel.cat',
            user     => 'sushi',
            password => 's3cr3t',
            database => 'pets',
        },
    },
    {
        test   => 'multiple connections',
        config => {
            connections_credentials => { connection => [
                {
                    name     => 'localhost',
                    user     => 'biscuit',
                },
                {
                    name     => 'pumpkin',
                    user     => 'pumpkin',
                },
            ]},
        },
        exp => {
            user => 'biscuit',
        },
    },
    {
        test   => 'repeat connection',
        config => {
            host => 'cats.lol',
            connections_credentials => { connection => [
                {
                    name     => 'localhost',
                    user     => 'biscuit',
                },
                {
                    user     => 'jimmy',
                },
                {
                    name     => 'cats.lol',
                    user     => 'pumpkin',
                },
                {
                    name     => 'cats.lol',
                    user     => 'strawberry',
                },
            ]},
        },
        exp => {
            host => 'cats.lol',
            user => 'strawberry',
        },
    },
    {
        test   => 'empty connections_credentials',
        config => {
            host => 'cats.lol',
            connections_credentials => {},
        },
        exp => {
            host => 'cats.lol',
        },
    },
) {
    my $got = App::Sqitch::Engine::clickhouse::_conn_cfg(
        $tc->{config}, $tc->{host},
    );
    is_deeply $got, $tc->{exp}, "Should process $tc->{test} config";
}

##############################################################################
# Test _run(), _capture(), and _spool().
can_ok $ch, qw(_run _capture _spool);
my $pass_env_name = 'CLICKHOUSE_PASSWORD';
my (@run, $exp_pass);
$mock_sqitch->mock(run => sub {
    local $Test::Builder::Level = $Test::Builder::Level + 2;
    shift;
    @run = @_;
    if (defined $exp_pass) {
        is $ENV{$pass_env_name}, $exp_pass, qq{$pass_env_name should be "$exp_pass"};
    } else {
        ok !exists $ENV{$pass_env_name}, '$pass_env_name should not exist';
    }
});

my @capture;
$mock_sqitch->mock(capture => sub {
    local $Test::Builder::Level = $Test::Builder::Level + 2;
    shift;
    @capture = @_;
    if (defined $exp_pass) {
        is $ENV{$pass_env_name}, $exp_pass, qq{$pass_env_name should be "$exp_pass"};
    } else {
        ok !exists $ENV{$pass_env_name}, '$pass_env_name should not exist';
    }
});

my @spool;
$mock_sqitch->mock(spool => sub {
    local $Test::Builder::Level = $Test::Builder::Level + 2;
    shift;
    @spool = @_;
    if (defined $exp_pass) {
        is $ENV{$pass_env_name}, $exp_pass, qq{$pass_env_name should be "$exp_pass"};
    } else {
        ok !exists $ENV{$pass_env_name}, '$pass_env_name should not exist';
    }
});

$target = App::Sqitch::Target->new(sqitch => $sqitch);
ok $ch = $CLASS->new(sqitch => $sqitch, target => $target),
    'Create a clickhouse with sqitch with options';
$exp_pass = 's3cr3t';
$target->uri->password($exp_pass);
ok $ch->_run(qw(foo bar baz)), 'Call _run';
is_deeply \@run, [$ch->cli, qw(foo bar baz)],
    'Command should be passed to run()';



( run in 0.587 second using v1.01-cache-2.11-cpan-5a3173703d6 )