AnyEvent-InfluxDB

 view release on metacpan or  search on metacpan

xt/influxdb.t  view on Meta::CPAN


use strict;
use warnings;

use Test::More;
use Test::Deep;

use AnyEvent::InfluxDB;
use EV;
use AnyEvent;
use JSON;

my $true = JSON::true;
my $false = JSON::false;

my $db = AnyEvent::InfluxDB->new(
    server => $ENV{INFLUXDB_SERVER} || 'http://127.0.0.1:8086',
    username => 'admin',
    password => 'admin',
);

# random data
my @measurements = qw(cpu_load mem_free cpu_temp disk_free);
my @regions = qw(us-east us-west eu-east eu-east);
my @hosts = map { sprintf('server%02d', $_) } 1 .. 10;
my @fields = map { sprintf('field%02d', $_) } 1 .. 10;
my $_15days_ago = time() - int(15 * 24 * 3600);
my $existing_region;

# common patterns
my $dt_re = re('^\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}Z$');
my $pos_int = code(sub { $_[0] >= 1 });
my $pos_num = code(sub { $_[0] > 0 });

my $cv;
{
    note "=== ping ===";
    $cv = AE::cv;
    $db->ping(
        on_success => $cv,
        on_error => sub {
            $cv->croak("Failed to ping database: @_");
        }
    );
    my $version;
    eval {
     $version = $cv->recv;
    };
    if ( $version ) {
        plan tests => 62;
        ok(1, "Connected to InfluxDB server version $version at: ". $db->server);
    } else {
        plan skip_all => 'InfluxDB server not found at: '. $db->server;
    }
}
{
    note "=== create_database ===";
    $cv = AE::cv;
    $db->create_database(
        database => "mydb",
        on_success => sub { $cv->send("test ok") },
        on_error => sub {
            $cv->croak("Failed to create database: @_");
        }
    );
    ok($cv->recv, "database mydb created");
}
{
    note "=== create_database ===";
    $cv = AE::cv;
    $db->create_database(
        database => "foo",
        on_success => sub { $cv->send("test ok") },
        on_error => sub {
            $cv->croak("Failed to create database: @_");
        }
    );
    ok($cv->recv, "database foo created");
}

xt/influxdb.t  view on Meta::CPAN

}
{
    note "=== show_continuous_queries ===";
    $cv = AE::cv;
    $db->show_continuous_queries(
        database => 'mydb',

        on_success => $cv,
        on_error => sub {
            $cv->croak("Failed to list continuous queries: @_");
        }
    );
    note "show_continuous_queries";
    my $continuous_queries = $cv->recv;
    is_deeply( $continuous_queries,
        {
            _internal => [],
            foo => [],
            mydb => [
                {
                    name => 'per5minutes',
                    query => 'CREATE CONTINUOUS QUERY per5minutes ON mydb'
                        .' RESAMPLE EVERY 10s FOR 10m BEGIN'
                        .' SELECT mean(value) INTO mydb.autogen.cpu_load_per5m'
                        .' FROM mydb.autogen.cpu_load GROUP BY time(5m) END',
                }
            ]
        },
        "cqs listed"
    );
    for my $database ( sort keys %{ $continuous_queries } ) {
        note "Database: $database";
        for my $s ( @{ $continuous_queries->{$database} } ) {
            note " Name: $s->{name}";
            note " Query: $s->{query}";
        }
    }
}
{
    note "=== drop_continuous_query ===";

    $cv = AE::cv;
    $db->drop_continuous_query(
        database => 'mydb',
        name => 'per5minutes',

        on_success => sub { $cv->send("test ok") },
        on_error => sub {
            $cv->croak("Failed to drop continuous query: @_");
        }
    );
    ok($cv->recv, "cq per5minutes dropped");

}
{
    note "=== create_user ===";

    $cv = AE::cv;
    $db->create_user(
        username => 'jdoe',
        password => 'mypassword',

        on_success => sub { $cv->send("test ok") },
        on_error => sub {
            $cv->croak("Failed to create user: @_");
        }
    );
    ok($cv->recv, "user created");

}
{
    note "=== set_user_password ===";

    $cv = AE::cv;
    $db->set_user_password(
        username => 'jdoe',
        password => 'otherpassword',

        on_success => sub { $cv->send("test ok") },
        on_error => sub {
            $cv->croak("Failed to set password: @_");
        }
    );
    ok($cv->recv, "password changed");
}
{
    note "=== show_users ===";

    $cv = AE::cv;
    $db->show_users(
        on_success => $cv,
        on_error => sub {
            $cv->croak("Failed to list users: @_");
        }
    );
    my @users = $cv->recv;
    is_deeply(
        [ @users ],
        [
            {
                user => 'jdoe',
                admin => $false,
            }
        ],
        "users listed"
    );
    for my $u ( @users ) {
        note "Name: $u->{user}";
        note "Admin?: $u->{admin}";
    }

}
{
    note "=== grant_privileges ===";

    $cv = AE::cv;
    $db->grant_privileges(
        username => 'jdoe',

        database => 'mydb',
        access => 'ALL',

        on_success => sub { $cv->send("test ok") },
        on_error => sub {
            $cv->croak("Failed to grant privileges: @_");
        }
    );
    ok($cv->recv, "privileges granted");
}
{
    note "=== show_grants ===";

    $cv = AE::cv;
    $db->show_grants(
        username => 'jdoe',

        on_success => $cv,
        on_error => sub {
            $cv->croak("Failed to list users: @_");
        }
    );
    my @grants = $cv->recv;
    is_deeply(
        [ @grants ],



( run in 1.062 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )