| 1 |
use Test::More tests => 32; |
|---|
| 2 |
use WWW::Curl::Easy; |
|---|
| 3 |
use JSON; |
|---|
| 4 |
use XML::LibXML; |
|---|
| 5 |
use XML::LibXML::XPathContext; |
|---|
| 6 |
use testconfig; |
|---|
| 7 |
use apiclient; |
|---|
| 8 |
use stomp; |
|---|
| 9 |
use Data::Dumper; |
|---|
| 10 |
|
|---|
| 11 |
use strict; |
|---|
| 12 |
my $uuid_re = qr/^[0-9a-fA-F]{4}(?:[0-9a-fA-F]{4}-){4}[0-9a-fA-F]{12}$/; |
|---|
| 13 |
my $uuid = '9c2163aa-f4bd-11df-851b-979bd290a553'; |
|---|
| 14 |
my $xp = XML::LibXML->new(); |
|---|
| 15 |
my $xpc = XML::LibXML::XPathContext->new(); |
|---|
| 16 |
|
|---|
| 17 |
my $iep_queries = [ |
|---|
| 18 |
{ id => '52f1f2ec-0275-11e0-a846-b757d1de0f4a', |
|---|
| 19 |
topic => 'numeric', |
|---|
| 20 |
epl => 'select * from NoitMetricNumeric as r' |
|---|
| 21 |
}, |
|---|
| 22 |
{ id => '8f04d54c-0275-11e0-b62c-6fdd90cb8bde', |
|---|
| 23 |
topic => 'text', |
|---|
| 24 |
epl => 'select * from NoitMetricText as r' |
|---|
| 25 |
}, |
|---|
| 26 |
{ id => '95f4ed2e-0275-11e0-bdc7-27110e43915b', |
|---|
| 27 |
topic => 'status', |
|---|
| 28 |
epl => 'select * from NoitStatus as r' |
|---|
| 29 |
}, |
|---|
| 30 |
]; |
|---|
| 31 |
|
|---|
| 32 |
ok(start_noit("108", { logs_debug => { '' => 'false' } }), 'starting noit'); |
|---|
| 33 |
ok(start_stratcon("108", { noits => [ { address => "127.0.0.1", port => "$NOIT_API_PORT" } ], iep => { queries => $iep_queries } }), 'starting stratcon'); |
|---|
| 34 |
sleep(1); |
|---|
| 35 |
my $c = apiclient->new('localhost', $NOIT_API_PORT); |
|---|
| 36 |
my @r = $c->get("/checks/show/$uuid"); |
|---|
| 37 |
is($r[0], 404, 'get checks'); |
|---|
| 38 |
|
|---|
| 39 |
@r = $c->put("/checks/set/$uuid", |
|---|
| 40 |
qq{<?xml version="1.0" encoding="utf8"?> |
|---|
| 41 |
<check><attributes><target>127.0.0.1</target><period>5000</period><timeout>500</timeout><name>selfcheck</name><filterset>allowall</filterset><module>selfcheck</module></attributes><config/></check>}); |
|---|
| 42 |
|
|---|
| 43 |
is($r[0], 200, 'add selfcheck'); |
|---|
| 44 |
my $doc = $xp->parse_string($r[1]); |
|---|
| 45 |
is($xpc->findvalue('/check/attributes/uuid', $doc), $uuid, 'saved'); |
|---|
| 46 |
|
|---|
| 47 |
sleep(1); |
|---|
| 48 |
|
|---|
| 49 |
@r = $c->get("/checks/show/$uuid"); |
|---|
| 50 |
is($r[0], 200, 'get checks'); |
|---|
| 51 |
$doc = $xp->parse_string($r[1]); |
|---|
| 52 |
is($xpc->findvalue('/check/state/state', $doc), 'good', 'results'); |
|---|
| 53 |
|
|---|
| 54 |
ok(1, 'going to sleep 1 seconds for mapping'); |
|---|
| 55 |
sleep(1); |
|---|
| 56 |
|
|---|
| 57 |
my $conn = pg('reconnoiter','reconnoiter'); |
|---|
| 58 |
ok($conn, 'data store connection'); |
|---|
| 59 |
my $sid = undef; |
|---|
| 60 |
if($conn) { |
|---|
| 61 |
my $st=$conn->prepare("select sid from stratcon.map_uuid_to_sid where id = ?"); |
|---|
| 62 |
$st->execute($uuid); |
|---|
| 63 |
($sid) = $st->fetchrow(); |
|---|
| 64 |
$st->finish(); |
|---|
| 65 |
} |
|---|
| 66 |
ok($sid, 'uuid mapped to sid'); |
|---|
| 67 |
|
|---|
| 68 |
sub do_counts { |
|---|
| 69 |
return (0,0) unless $conn; |
|---|
| 70 |
my $artext = $conn->prepare("select count(*) from noit.metric_text_archive ". |
|---|
| 71 |
" where sid = ?"); |
|---|
| 72 |
my $arnum = $conn->prepare("select count(*) from noit.metric_numeric_archive ". |
|---|
| 73 |
" where sid = ?"); |
|---|
| 74 |
$artext->execute($sid); |
|---|
| 75 |
my ($text_rows) = $artext->fetchrow(); |
|---|
| 76 |
$arnum->execute($sid); |
|---|
| 77 |
my ($numeric_rows) = $arnum->fetchrow(); |
|---|
| 78 |
return ($text_rows, $numeric_rows); |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
my($st_t, $st_n) = do_counts(); |
|---|
| 82 |
|
|---|
| 83 |
ok(1, 'going to sleep 7 seconds for data to stream'); |
|---|
| 84 |
sleep(7); |
|---|
| 85 |
my $sc = apiclient->new('localhost', $STRATCON_API_PORT); |
|---|
| 86 |
@r = $sc->get('/noits/show'); |
|---|
| 87 |
is($r[0], '200', 'get noits'); |
|---|
| 88 |
$doc = $xp->parse_string($r[1]); |
|---|
| 89 |
cmp_ok($xpc->findvalue('/noits/noit[@type="durable/storage"]/@session_events', $doc), '>', 0, 'durable connection (events)'); |
|---|
| 90 |
cmp_ok($xpc->findvalue('/noits/noit[@type="transient/iep"]/@session_events', $doc), '>', 0, 'iep connection (events)'); |
|---|
| 91 |
|
|---|
| 92 |
ok(1, 'going to wait 2 more seconds for load into postgres'); |
|---|
| 93 |
sleep(2); |
|---|
| 94 |
my($f_t, $f_n) = do_counts(); |
|---|
| 95 |
cmp_ok($st_t, '<', $f_t, 'text metrics loaded'); |
|---|
| 96 |
cmp_ok($st_n, '<', $f_n, 'numeric metrics loaded'); |
|---|
| 97 |
|
|---|
| 98 |
### Test real-time streaming. |
|---|
| 99 |
|
|---|
| 100 |
my $curl = WWW::Curl::Easy->new; |
|---|
| 101 |
$curl->setopt(CURLOPT_URL, "http://localhost:$STRATCON_WEB_PORT/data/$uuid\@500"); |
|---|
| 102 |
$curl->setopt(CURLOPT_TIMEOUT, 5); |
|---|
| 103 |
my $response_body; |
|---|
| 104 |
$curl->setopt(CURLOPT_WRITEDATA,\$response_body); |
|---|
| 105 |
my $retcode = 0; |
|---|
| 106 |
$retcode = $curl->perform; |
|---|
| 107 |
is($retcode, 28, 'needed to timeout stream'); |
|---|
| 108 |
|
|---|
| 109 |
my @rdata; |
|---|
| 110 |
my $json_text; |
|---|
| 111 |
my $test_S = 0; |
|---|
| 112 |
eval { |
|---|
| 113 |
foreach (split(/\R/,$response_body)) { |
|---|
| 114 |
if(/^\s*<script id=.*window\.parent\.plot_iframe_data\((\{.*?\})\)/) { |
|---|
| 115 |
$json_text = $1; |
|---|
| 116 |
my $json = from_json($json_text); |
|---|
| 117 |
push @rdata, $json; |
|---|
| 118 |
if(!$test_S && $json->{type} eq 'S') { |
|---|
| 119 |
like($json->{id}, $uuid_re, 'status line uuid'); |
|---|
| 120 |
is($json->{check_module}, 'selfcheck', 'status line module'); |
|---|
| 121 |
$test_S = 1; |
|---|
| 122 |
} |
|---|
| 123 |
} |
|---|
| 124 |
} |
|---|
| 125 |
}; |
|---|
| 126 |
if(!$test_S) { |
|---|
| 127 |
ok(0, "status line uuid"); |
|---|
| 128 |
ok(0, "status line module"); |
|---|
| 129 |
} |
|---|
| 130 |
is($@, '', 'json parse errors: ' . ($@ ? $json_text : 'none')); |
|---|
| 131 |
|
|---|
| 132 |
# There are at least 4 metrics for the self check. |
|---|
| 133 |
# in 5 seconds - (1 second lag) - jittered start at 500ms period, |
|---|
| 134 |
# it should run at least 7 times. |
|---|
| 135 |
cmp_ok(scalar(@rdata), '>=', 7*4, 'streamed data'); |
|---|
| 136 |
|
|---|
| 137 |
my $stomp; |
|---|
| 138 |
my $payload; |
|---|
| 139 |
my $json; |
|---|
| 140 |
|
|---|
| 141 |
$stomp = stomp->new(); |
|---|
| 142 |
ok($stomp, 'stomp connection'); |
|---|
| 143 |
ok($stomp->subscribe('/queue/noit.firehose'), 'subscribed'); |
|---|
| 144 |
$payload = $stomp->get({timeout => 6}); |
|---|
| 145 |
undef $stomp; |
|---|
| 146 |
ok($payload, 'firehose traffic'); |
|---|
| 147 |
|
|---|
| 148 |
$stomp = stomp->new(); |
|---|
| 149 |
ok($stomp, 'stomp connection'); |
|---|
| 150 |
ok($stomp->subscribe('/topic/noit.alerts.numeric'), 'subscribed'); |
|---|
| 151 |
$payload = $stomp->get({timeout => 6}); |
|---|
| 152 |
eval { die "no data" unless defined($payload); |
|---|
| 153 |
$json = from_json($payload); }; |
|---|
| 154 |
is($@, '', 'json numeric payload'); |
|---|
| 155 |
undef $stomp; |
|---|
| 156 |
$json ||= {}; |
|---|
| 157 |
like($json->{r}->{uuid} || '', $uuid_re, 'numeric match has uuid'); |
|---|
| 158 |
is($json->{r}->{check_module} || '', 'selfcheck', 'modules is set'); |
|---|
| 159 |
|
|---|
| 160 |
ok(stop_noit, 'shutdown noit'); |
|---|
| 161 |
ok(stop_stratcon, 'shutdown stratcon'); |
|---|
| 162 |
1; |
|---|