public function testCreateNewMetric()
 {
     $diObj = new DI();
     $expectedMetric = new Metric();
     $expectedMetric->setName('DiskUsage');
     $expectedMetric->setUnit('Percent');
     $expectedMetric->setValue('56');
     $expectedMetric->setNamespace('CustomMetric/Test');
     $fakeCmdRunner = Stub::make('\\AWSCustomMetric\\CommandRunner', ['execute' => function () {
     }, 'getReturnCode' => 0, 'getReturnValue' => '56']);
     $diObj->setCommandRunner($fakeCmdRunner);
     $diskUsage = new DiskUsage($diObj, 'CustomMetric/Test');
     $this->assertEquals($expectedMetric, $diskUsage->createNewMetric('DiskUsage', 'Percent', '56'), 'DiskUsage::createNewMetric test failed!');
 }
 public function testGetMetrics()
 {
     $expectedMetric = new Metric();
     $expectedMetric->setName('HttpCheck');
     $expectedMetric->setUnit('Seconds');
     $expectedMetric->setValue('0');
     $expectedMetric->setNamespace('CustomMetric/Test');
     $expectedMetric2 = new Metric();
     $expectedMetric2->setName('HttpCheckFail');
     $expectedMetric2->setUnit('Count');
     $expectedMetric2->setValue('0');
     $expectedMetric2->setNamespace('CustomMetric/Test');
     $expectedFailMetric = new Metric();
     $expectedFailMetric->setName('HttpCheckFail');
     $expectedFailMetric->setUnit('Count');
     $expectedFailMetric->setValue('1');
     $expectedFailMetric->setNamespace('CustomMetric/Test');
     $diObj = new DI();
     $diObj->setLogger(new DefaultLogger());
     $mock = new MockHandler([new Response(200, ['Content-Type' => 'application/json'], 'response body'), new Response(500, ['Content-Type' => 'application/json'], 'response failed!'), new Response(200, ['Content-Type' => 'application/json'], 'response OK'), new Response(200, ['Content-Type' => 'application/json'], 'response FAIL'), new Response(200, ['Content-Type' => 'application/json', 'Content-Length' => 11], 'response OK'), new Response(200, ['Content-Type' => 'application/json', 'Content-Length' => 11], 'response OK'), new Response(200, ['Content-Type' => 'application/json', 'Content-Length' => 11], 'response OK'), new TransferException('Transfer failed', 500), new Response(200, ['Content-Type' => 'application/json', 'Content-Length' => 11], 'amount=0.0&cmd=SALE&status=-10220&subscriptionType=&batchID=1443429052723' . '&part=999999&txnID=1443429052724'), new Response(200, ['Content-Type' => 'application/json', 'Content-Length' => 11], 'amount=0.0&cmd=SALE&status=-10220&subscriptionType=&batchID=1443429052723' . '&part=999999&txnID=1443429052724'), new Response(200, ['Content-Type' => 'application/json', 'X-Header-Exists' => 'random value'], 'response OK')]);
     $handler = HandlerStack::create($mock);
     $client = new Client(['handler' => $handler]);
     $diObj->setGuzzleHttpClient($client);
     $httpCheck = new HttpCheck($diObj, 'CustomMetric/Test');
     $httpCheck->setUrl('https://github.com/fustundag/awscw-custom-metrics');
     $metrics = $httpCheck->getMetrics();
     $this->assertCount(2, $metrics, 'HttpCheck::getMetrics test failed!');
     $this->assertEquals($expectedMetric, $metrics[0], 'HttpCheck::getMetrics expected metric test failed!');
     $this->assertEquals($expectedMetric2, $metrics[1], 'HttpCheck::getMetrics expected fail metric test failed!');
     $metrics = $httpCheck->getMetrics();
     $this->assertCount(1, $metrics, 'HttpCheck::getMetrics test failed!');
     $this->assertEquals($expectedFailMetric, $metrics[0], 'HttpCheck::getMetrics check status test failed!');
     $httpCheck->setBodyToCheck('response OK');
     $metrics = $httpCheck->getMetrics();
     $this->assertCount(2, $metrics, 'HttpCheck::getMetrics test failed!');
     $this->assertEquals($expectedMetric, $metrics[0], 'HttpCheck::getMetrics body check ok test failed!');
     $this->assertEquals($expectedMetric2, $metrics[1], 'HttpCheck::getMetrics expected fail metric test failed!');
     $metrics = $httpCheck->getMetrics();
     $this->assertCount(1, $metrics, 'HttpCheck::getMetrics test failed!');
     $this->assertEquals($expectedFailMetric, $metrics[0], 'HttpCheck::getMetrics body check fail test failed!');
     $httpCheck->setHeadersToCheck(['Content-Type' => 'application/json']);
     $metrics = $httpCheck->getMetrics();
     $this->assertCount(2, $metrics, 'HttpCheck::getMetrics test failed!');
     $this->assertEquals($expectedMetric, $metrics[0], 'HttpCheck::getMetrics headers check ok test failed!');
     $this->assertEquals($expectedMetric2, $metrics[1], 'HttpCheck::getMetrics expected fail metric test failed!');
     $httpCheck->setHeadersToCheck(['Content-Type' => 'text/html']);
     $metrics = $httpCheck->getMetrics();
     $this->assertCount(1, $metrics, 'HttpCheck::getMetrics test failed!');
     $this->assertEquals($expectedFailMetric, $metrics[0], 'HttpCheck::getMetrics headers check fail test failed!');
     $httpCheck->setHeadersToCheck(['Non-Exists' => 'foobar']);
     $metrics = $httpCheck->getMetrics();
     $this->assertCount(1, $metrics, 'HttpCheck::getMetrics test failed!');
     $this->assertEquals($expectedFailMetric, $metrics[0], 'HttpCheck::getMetrics headers check fail test failed!');
     $this->expectOutputString("[" . date('Y-m-d H:i:s') . "][ERROR] Guzzle Http client thrown exception! Msg: Transfer failed\n");
     $metrics = $httpCheck->getMetrics();
     $this->assertCount(1, $metrics, 'HttpCheck::getMetrics exception test failed!');
     $this->assertEquals($expectedFailMetric, $metrics[0], 'HttpCheck::getMetrics exception test failed!');
     $httpCheck->setHeadersToCheck([]);
     $httpCheck->setBodyToCheck('status=-10220', HttpCheck::$containsFunc);
     $metrics = $httpCheck->getMetrics();
     $this->assertCount(2, $metrics, 'HttpCheck::getMetrics test failed!');
     $this->assertEquals($expectedMetric, $metrics[0], 'HttpCheck::getMetrics body contains test failed!');
     $this->assertEquals($expectedMetric2, $metrics[1], 'HttpCheck::getMetrics expected fail metric test failed!');
     $httpCheck->setBodyToCheck('amount=0.0', HttpCheck::$containsFunc);
     $metrics = $httpCheck->getMetrics();
     $this->assertCount(2, $metrics, 'HttpCheck::getMetrics test failed!');
     $this->assertEquals($expectedMetric, $metrics[0], 'HttpCheck::getMetrics body contains at the beginning test failed!');
     $this->assertEquals($expectedMetric2, $metrics[1], 'HttpCheck::getMetrics expected fail metric test failed!');
     $httpCheck->setHeadersToCheck(['X-Header-Exists' => '']);
     $httpCheck->setBodyToCheck('');
     $metrics = $httpCheck->getMetrics();
     $this->assertCount(2, $metrics, 'HttpCheck::getMetrics test failed!');
     $this->assertEquals($expectedMetric, $metrics[0], 'HttpCheck::getMetrics headers exists test failed!');
     $this->assertEquals($expectedMetric2, $metrics[1], 'HttpCheck::getMetrics expected fail metric test failed!');
 }
 public function testGetMetrics()
 {
     $diObj = new DI();
     $diObj->setLogger(new DefaultLogger());
     $objToCheck = new FileWatch($diObj, 'CustomMetric/Test');
     $objToCheck->addPattern('ALERT');
     $objToCheck->addPattern('EMERGENCY');
     $objToCheck->setFileToWatch('notfound.log');
     $metrics = $objToCheck->getMetrics();
     $this->assertCount(1, $metrics, 'FileWatch::getMetrics notfound test failed!');
     $this->assertEquals(new Metric('FileWatchException', 'Count', 1, 'CustomMetric/Test'), $metrics[0], 'FileWatch::getMetrics notfound test failed!');
     $testFile = '/tmp/' . uniqid() . '.log';
     touch($testFile);
     $objToCheck->setFileToWatch($testFile);
     $metrics = $objToCheck->getMetrics();
     $this->assertCount(1, $metrics, 'FileWatch::getMetrics zerosize test failed!');
     $this->assertEquals(new Metric('FileWatchError', 'Count', 0, 'CustomMetric/Test'), $metrics[0], 'FileWatch::getMetrics zerosize test failed!');
     $testStr = "[INFO] deneme\n[ALERT] deneme2\n[WARNING] deneme3\n";
     file_put_contents($testFile, $testStr);
     $metrics = $objToCheck->getMetrics();
     $this->assertCount(1, $metrics, 'FileWatch::getMetrics test failed!');
     $this->assertEquals(new Metric('FileWatchError', 'Count', 1, 'CustomMetric/Test'), $metrics[0], 'FileWatch::getMetrics test failed!');
     $this->assertFileExists($testFile . '.checkpoint', 'FileWatch::getMetrics checkpoint file failed!');
     $this->assertEquals(strlen($testStr), file_get_contents($testFile . '.checkpoint'), 'FileWatch::getMetrics checkpoint file failed!');
 }
 public function testGetMetrics()
 {
     $expectedMetric = new Metric();
     $expectedMetric->setName('GearmanCheckFail');
     $expectedMetric->setUnit('Count');
     $expectedMetric->setValue('0');
     $expectedMetric->setNamespace('CustomMetric/Test');
     $expectedFailMetric = new Metric();
     $expectedFailMetric->setName('ChatNodeJsCheckFail');
     $expectedFailMetric->setUnit('Count');
     $expectedFailMetric->setValue('1');
     $expectedFailMetric->setNamespace('CustomMetric/Test');
     $diObj = new DI();
     $diObj->setLogger(new DefaultLogger());
     $servicePortCheck = new ServicePortCheck($diObj, 'CustomMetric/Test');
     $servicePortCheck->setServer('127.0.0.1');
     $servicePortCheck->setPort('11211');
     $servicePortCheck->setServiceName('Gearman');
     $metrics = $servicePortCheck->getMetrics();
     $this->assertCount(1, $metrics, 'ServicePortCheck::getMetrics test failed!');
     $this->assertEquals($expectedMetric, $metrics[0], 'ServicePortCheck::getMetrics expected metric test failed!');
     $servicePortCheck->setServer('127.0.0.1');
     $servicePortCheck->setPort('3434');
     $servicePortCheck->setServiceName('ChatNodeJs');
     $metrics = $servicePortCheck->getMetrics();
     $this->assertCount(1, $metrics, 'ServicePortCheck::getMetrics test failed!');
     $this->assertEquals($expectedFailMetric, $metrics[0], 'ServicePortCheck::getMetrics check status test failed!');
 }
 public function testRun()
 {
     /* @var CommandRunner $fakeCmdRunner1 */
     $fakeCmdRunner1 = Stub::make('\\AWSCustomMetric\\CommandRunner', ['execute' => function () {
     }, 'getReturnValue' => '56']);
     /* @var CommandRunner $fakeCmdRunner2 */
     $fakeCmdRunner2 = Stub::make('\\AWSCustomMetric\\CommandRunner', ['execute' => function () {
     }, 'getReturnCode' => 0, 'getOutput' => ['MemTotal:        10000 kB', 'MemFree:          2000 kB', 'MemAvailable:     419980 kB', 'Buffers:          1000 kB', 'Cached:           1000 kB', 'SwapCached:            0 kB', 'Active:           526652 kB', 'Inactive:         164928 kB', 'SwapTotal:        10000 kB', 'SwapFree:          9000 kB']]);
     $diObj1 = new DI();
     $diObj1->setCommandRunner($fakeCmdRunner1);
     $diObj2 = new DI();
     $diObj2->setCommandRunner($fakeCmdRunner2);
     $plugin1 = new DiskUsage($diObj1);
     $plugin2 = new MemoryUsage($diObj2);
     $testObj = new Sender('fakekey', 'fakesecret', 'fakeregion', $fakeCmdRunner1, 'testInstance', 'testns');
     $testObj->addPlugin([$plugin1, $plugin2]);
     $testObj->run();
     $actualStr = file_get_contents(rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'cloud_watch_client.txt');
     $exceptedStr = '';
     $exceptedStr .= json_encode($this->tester->getMetricDataArray($plugin1->getMetrics(), 'testInstance', 'testns')) . "\n";
     $exceptedStr .= json_encode($this->tester->getMetricDataArray($plugin2->getMetrics(), 'testInstance', 'testns')) . "\n";
     $this->assertEquals($exceptedStr, $actualStr, 'Sender::run default test failed!');
     $plugin3 = new DiskUsage($diObj1, 'Test/System', '* * * * *');
     $plugin4 = new MemoryUsage($diObj2, 'Test/System', '*/' . (date('i') + 1) . ' * * * *');
     $testObj2 = new Sender('fakekey', 'fakesecret', 'fakeregion', $fakeCmdRunner1, 'testInstance', 'testns');
     $testObj2->addPlugin([$plugin3, $plugin4]);
     $testObj2->run();
     $actualStr = file_get_contents(rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'cloud_watch_client.txt');
     $exceptedStr = '';
     $exceptedStr .= json_encode($this->tester->getMetricDataArray($plugin3->getMetrics(), 'testInstance', 'Test/System')) . "\n";
     $this->assertEquals($exceptedStr, $actualStr, 'Sender::run cron test failed!');
 }
 public function testGetMetrics()
 {
     $expectedMetric = new Metric();
     $expectedMetric->setName('MemcachedCheckFail');
     $expectedMetric->setUnit('Count');
     $expectedMetric->setValue('0');
     $expectedMetric->setNamespace('CustomMetric/Test');
     $expectedFailMetric = new Metric();
     $expectedFailMetric->setName('MemcachedCheckFail');
     $expectedFailMetric->setUnit('Count');
     $expectedFailMetric->setValue('1');
     $expectedFailMetric->setNamespace('CustomMetric/Test');
     $diObj = new DI();
     $diObj->setLogger(new DefaultLogger());
     $memcachedCheck = new MemcachedCheck($diObj, 'CustomMetric/Test');
     $memcachedCheck->setServer('127.0.0.1');
     $memcachedCheck->setPort('11211');
     $memcachedCheck->setMemcached(new \Memcached());
     $metrics = $memcachedCheck->getMetrics();
     $this->assertCount(1, $metrics, 'MemcachedCheck::getMetrics test failed!');
     $this->assertEquals($expectedMetric, $metrics[0], 'MemcachedCheck::getMetrics expected metric test failed!');
     $memcachedCheck->setServer('127.0.0.1');
     $memcachedCheck->setPort('11221');
     $metrics = $memcachedCheck->getMetrics();
     $this->assertCount(1, $metrics, 'MemcachedCheck::getMetrics test failed!');
     $this->assertEquals($expectedFailMetric, $metrics[0], 'MemcachedCheck::getMetrics check status test failed!');
     $fakeMemcached = Stub::make('\\Memcached', ['addServer' => function () {
     }, 'resetServerList' => function () {
     }, 'set' => function () {
         return true;
     }, 'get' => 2]);
     $memcachedCheck->setMemcached($fakeMemcached);
     $metrics = $memcachedCheck->getMetrics();
     $this->assertCount(1, $metrics, 'MemcachedCheck::getMetrics test failed!');
     $this->assertEquals($expectedFailMetric, $metrics[0], 'MemcachedCheck::getMetrics check status test failed!');
     $fakeMemcached = Stub::make('\\Memcached', ['addServer' => function () {
     }, 'resetServerList' => function () {
     }, 'set' => function () {
         throw new \Exception('fake exception on memcached set');
     }, 'get' => 2]);
     $memcachedCheck->setMemcached($fakeMemcached);
     $metrics = $memcachedCheck->getMetrics();
     $this->assertCount(1, $metrics, 'MemcachedCheck::getMetrics test failed!');
     $this->assertEquals($expectedFailMetric, $metrics[0], 'MemcachedCheck::getMetrics check status test failed!');
 }