From 18dbcd4dcc84a250b7bf3306de16ff4e701baacd Mon Sep 17 00:00:00 2001 From: David Supplee Date: Tue, 8 Nov 2016 03:59:46 -1000 Subject: [PATCH] initial logging system tests (#229) * initial logging system tests * Fix capitalization of namespace (#233) * initial pubsub system tests (#230) * initial datastore system tests (#228) * initial datastore system tests * Fix capitalization of namespace (#233) * initial pubsub system tests (#230) * initial storage system tests (#231) * initial storage system tests * Fix capitalization of namespace (#233) * initial pubsub system tests (#230) * initial datastore system tests (#228) * initial datastore system tests * Fix capitalization of namespace (#233) * initial pubsub system tests (#230) --- tests/system/Logging/LoggingTestCase.php | 95 +++++++ tests/system/Logging/ManageMetricsTest.php | 100 +++++++ tests/system/Logging/ManageSinksTest.php | 136 +++++++++ .../system/Logging/WriteAndListEntryTest.php | 263 ++++++++++++++++++ tests/system/bootstrap.php | 2 + 5 files changed, 596 insertions(+) create mode 100644 tests/system/Logging/LoggingTestCase.php create mode 100644 tests/system/Logging/ManageMetricsTest.php create mode 100644 tests/system/Logging/ManageSinksTest.php create mode 100644 tests/system/Logging/WriteAndListEntryTest.php diff --git a/tests/system/Logging/LoggingTestCase.php b/tests/system/Logging/LoggingTestCase.php new file mode 100644 index 000000000000..9c93fec7d2cc --- /dev/null +++ b/tests/system/Logging/LoggingTestCase.php @@ -0,0 +1,95 @@ + $keyFilePath + ]))->createBucket(uniqid(self::TESTING_PREFIX)); + self::$dataset = (new BigQueryClient([ + 'keyFilePath' => $keyFilePath + ]))->createDataset(uniqid(self::TESTING_PREFIX)); + self::$restClient = new LoggingClient([ + 'keyFilePath' => $keyFilePath, + 'transport' => 'rest' + ]); + self::$grpcClient = new LoggingClient([ + 'keyFilePath' => $keyFilePath, + 'transport' => 'grpc' + ]); + self::$topic = (new PubSubClient([ + 'keyFilePath' => $keyFilePath, + 'transport' => 'rest' + ]))->createTopic(uniqid(self::TESTING_PREFIX)); + self::$hasSetUp = true; + } + + public static function tearDownFixtures() + { + if (!self::$hasSetUp) { + return; + } + + self::$deletionQueue[] = self::$dataset; + self::$deletionQueue[] = self::$bucket; + self::$deletionQueue[] = self::$topic; + $backoff = new ExponentialBackoff(8); + + foreach (self::$deletionQueue as $item) { + $backoff->execute(function () use ($item) { + $item->delete(); + }); + } + } +} + + diff --git a/tests/system/Logging/ManageMetricsTest.php b/tests/system/Logging/ManageMetricsTest.php new file mode 100644 index 000000000000..b8cafeedc8e8 --- /dev/null +++ b/tests/system/Logging/ManageMetricsTest.php @@ -0,0 +1,100 @@ +createMetric($name, 'severity >= DEBUG', [ + 'description' => 'A description.' + ]); + self::$deletionQueue[] = $metric; + + $metrics = iterator_to_array($client->metrics()); + + foreach ($metrics as $metric) { + if ($metric->name() === $name) { + $found = true; + } + } + + $this->assertTrue($found); + } + + /** + * @dataProvider clientProvider + */ + public function testCreateMetric($client) + { + $name = uniqid(self::TESTING_PREFIX); + $filter = 'severity >= DEBUG'; + $options = [ + 'description' => 'A description.', + ]; + $this->assertFalse($client->metric($name)->exists()); + + $metric = $client->createMetric($name, $filter, $options); + self::$deletionQueue[] = $metric; + + $this->assertTrue($client->metric($name)->exists()); + $this->assertEquals($filter, $metric->info()['filter']); + $this->assertEquals($options['description'], $metric->info()['description']); + } + + /** + * @dataProvider clientProvider + */ + public function testUpdateMetric($client) + { + $name = uniqid(self::TESTING_PREFIX); + $updateOptions = [ + 'description' => 'A new description', + 'filter' => 'severity >= INFO' + ]; + $metric = $client->createMetric($name, 'severity >= DEBUG', [ + 'description' => 'A description.', + ]); + self::$deletionQueue[] = $metric; + + $info = $metric->update($updateOptions); + + $this->assertEquals($name, $metric->name()); + $this->assertEquals($updateOptions['filter'], $info['filter']); + $this->assertEquals($updateOptions['description'], $info['description']); + } + + /** + * @dataProvider clientProvider + */ + public function testReloadMetric($client) + { + $name = uniqid(self::TESTING_PREFIX); + $filter = 'severity >= ERROR'; + $metric = $client->createMetric($name, $filter, [ + 'description' => 'A description.' + ]); + + $this->assertEquals($filter, $metric->reload()['filter']); + } +} diff --git a/tests/system/Logging/ManageSinksTest.php b/tests/system/Logging/ManageSinksTest.php new file mode 100644 index 000000000000..939e0fba529f --- /dev/null +++ b/tests/system/Logging/ManageSinksTest.php @@ -0,0 +1,136 @@ +createSink( + $name, + sprintf( + 'bigquery.googleapis.com/projects/%s/datasets/%s', + self::$dataset->identity()['projectId'], + self::$dataset->identity()['datasetId'] + ), + [ + 'outputVersionFormat' => 'V2', + 'filter' => 'severity >= ERROR' + ] + ); + self::$deletionQueue[] = $sink; + + $sinks = iterator_to_array($client->sinks()); + + foreach ($sinks as $sink) { + if ($sink->name() === $name) { + $found = true; + } + } + + $this->assertTrue($found); + } + + /** + * @dataProvider createSinkProvider + */ + public function testCreateSink($client, $destination) + { + $name = uniqid(self::TESTING_PREFIX); + $options = [ + 'outputVersionFormat' => 'V2', + 'filter' => 'severity >= ERROR' + ]; + $this->assertFalse($client->sink($name)->exists()); + + $sink = $client->createSink($name, $destination, $options); + self::$deletionQueue[] = $sink; + + $this->assertTrue($client->sink($name)->exists()); + $this->assertEquals($destination, $sink->info()['destination']); + $this->assertEquals($options['outputVersionFormat'], $sink->info()['outputVersionFormat']); + $this->assertEquals($options['filter'], $sink->info()['filter']); + } + + public function createSinkProvider() + { + self::setUpBeforeClass(); + $bucket = self::$bucket; + $bucket->acl()->add('group-cloud-logs@google.com', 'OWNER');- + $bucketDest = sprintf('storage.googleapis.com/%s', $bucket->name()); + $datasetDest = sprintf( + 'bigquery.googleapis.com/projects/%s/datasets/%s', + self::$dataset->identity()['projectId'], + self::$dataset->identity()['datasetId'] + ); + $topicDest = sprintf('pubsub.googleapis.com/%s', self::$topic->info()['name']); + + return [ + [self::$restClient, $bucketDest], + [self::$restClient, $datasetDest], + [self::$restClient, $topicDest], + [self::$grpcClient, $bucketDest], + [self::$grpcClient, $datasetDest], + [self::$grpcClient, $topicDest] + ]; + } + + /** + * @dataProvider clientProvider + */ + public function testUpdateSink($client) + { + $name = uniqid(self::TESTING_PREFIX); + $destination = sprintf('pubsub.googleapis.com/%s', self::$topic->info()['name']); + $createOptions = [ + 'outputVersionFormat' => 'V2', + 'filter' => 'severity >= ERROR' + ]; + $updateOptions = [ + 'filter' => 'severity >= DEBUG' + ]; + $sink = $client->createSink($name, $destination, $createOptions); + self::$deletionQueue[] = $sink; + + $info = $sink->update($updateOptions); + + $this->assertEquals($name, $sink->name()); + $this->assertEquals($updateOptions['filter'], $info['filter']); + } + + /** + * @dataProvider clientProvider + */ + public function testReloadSink($client) + { + $name = uniqid(self::TESTING_PREFIX); + $options = [ + 'outputVersionFormat' => 'V2', + 'filter' => 'severity >= ERROR' + ]; + $destination = sprintf('pubsub.googleapis.com/%s', self::$topic->info()['name']); + $sink = $client->createSink($name, $destination, $options); + + $this->assertEquals($options['outputVersionFormat'], $sink->reload()['outputVersionFormat']); + } +} diff --git a/tests/system/Logging/WriteAndListEntryTest.php b/tests/system/Logging/WriteAndListEntryTest.php new file mode 100644 index 000000000000..0872097ebc11 --- /dev/null +++ b/tests/system/Logging/WriteAndListEntryTest.php @@ -0,0 +1,263 @@ +logger(uniqid(self::TESTING_PREFIX)); + self::$deletionQueue[] = $logger; + $data = 'test'; + $entry = $logger->entry($data, [ + 'type' => 'global' + ]); + + $logger->write($entry); + + $backoff = new ExponentialBackoff(8); + $entries = $backoff->execute(function () use ($logger) { + $entries = iterator_to_array($logger->entries()); + + if (count($entries) === 0) { + throw new \Exception(); + } + + return $entries; + }); + + $this->assertEquals($data, $entries[0]->info()['textPayload']); + } + + /** + * @dataProvider clientProvider + */ + public function testWriteJsonEntry($client) + { + $logger = $client->logger(uniqid(self::TESTING_PREFIX)); + self::$deletionQueue[] = $logger; + $data = [ + 'test' => true, + 'hello' => 'world', + 'some' => [ + 'data' + ] + ]; + + $entry = $logger->entry($data, [ + 'type' => 'global' + ]); + + $logger->write($entry); + + $backoff = new ExponentialBackoff(8); + $entries = $backoff->execute(function () use ($logger) { + $entries = iterator_to_array($logger->entries()); + + if (count($entries) === 0) { + throw new \Exception(); + } + + return $entries; + }); + + $this->assertEquals($data, $entries[0]->info()['jsonPayload']); + } + + /** + * @dataProvider clientProvider + */ + public function testWritesMultipleTextEntries($client) + { + $logger = $client->logger(uniqid(self::TESTING_PREFIX)); + self::$deletionQueue[] = $logger; + $data = 'test'; + $entriesToWrite = [ + $logger->entry($data, [ + 'type' => 'global' + ]), + $logger->entry($data, [ + 'type' => 'global' + ]) + ]; + + $logger->writeBatch($entriesToWrite); + + $backoff = new ExponentialBackoff(8); + $entries = $backoff->execute(function () use ($entriesToWrite, $logger) { + $entries = iterator_to_array($logger->entries()); + + if (count($entries) !== count($entriesToWrite)) { + throw new \Exception(); + } + + return $entries; + }); + + $this->assertEquals($data, $entries[0]->info()['textPayload']); + $this->assertEquals($data, $entries[1]->info()['textPayload']); + } + + /** + * @dataProvider clientProvider + */ + public function testWritesEntryWithMetadata($client) + { + $logger = $client->logger(uniqid(self::TESTING_PREFIX)); + self::$deletionQueue[] = $logger; + $data = 'test'; + $httpRequest = [ + 'requestMethod' => 'GET', + 'requestUrl' => 'http://www.example.com', + 'status' => 200 + ]; + $labels = [ + 'test' => 'label' + ]; + $severity = 'INFO'; + + $entry = $logger->entry($data, ['type' => 'global'], [ + 'httpRequest' => $httpRequest, + 'labels' => $labels, + 'severity' => 200 + ]); + + $logger->write($entry); + + $backoff = new ExponentialBackoff(8); + $entries = $backoff->execute(function () use ($logger) { + $entries = iterator_to_array($logger->entries()); + + if (count($entries) === 0) { + throw new \Exception(); + } + + return $entries; + }); + $actualEntryInfo = $entries[0]->info(); + + $this->assertEquals($data, $actualEntryInfo['textPayload']); + $this->assertEquals($httpRequest['requestMethod'], $actualEntryInfo['httpRequest']['requestMethod']); + $this->assertEquals($httpRequest['requestUrl'], $actualEntryInfo['httpRequest']['requestUrl']); + $this->assertEquals($httpRequest['status'], $actualEntryInfo['httpRequest']['status']); + $this->assertEquals($labels['test'], $actualEntryInfo['labels']['test']); + $this->assertEquals($severity, $actualEntryInfo['severity']); + } + + /** + * @dataProvider clientProvider + */ + public function testWritesEmergencyLogWithPsrLogger($client) + { + $this->assertPsrLoggerWrites($client, 'emergency'); + } + + /** + * @dataProvider clientProvider + */ + public function testWritesAlertLogWithPsrLogger($client) + { + $this->assertPsrLoggerWrites($client, 'alert'); + } + + /** + * @dataProvider clientProvider + */ + public function testWritesCriticalLogWithPsrLogger($client) + { + $this->assertPsrLoggerWrites($client, 'critical'); + } + + /** + * @dataProvider clientProvider + */ + public function testWritesErrorLogWithPsrLogger($client) + { + $this->assertPsrLoggerWrites($client, 'error'); + } + + /** + * @dataProvider clientProvider + */ + public function testWritesWarningLogWithPsrLogger($client) + { + $this->assertPsrLoggerWrites($client, 'warning'); + } + + /** + * @dataProvider clientProvider + */ + public function testWritesNoticeLogWithPsrLogger($client) + { + $this->assertPsrLoggerWrites($client, 'notice'); + } + + /** + * @dataProvider clientProvider + */ + public function testWritesInfoLogWithPsrLogger($client) + { + $this->assertPsrLoggerWrites($client, 'info'); + } + + /** + * @dataProvider clientProvider + */ + public function testWritesInfoDebugWithPsrLogger($client) + { + $this->assertPsrLoggerWrites($client, 'debug'); + } + + private function assertPsrLoggerWrites($client, $level) + { + $logName = uniqid(self::TESTING_PREFIX); + $psrLogger = $client->psrLogger($logName, [ + 'type' => 'global' + ]); + $logger = $client->logger($logName); + self::$deletionQueue[] = $logger; + $data = $level; + $httpRequest = [ + 'requestMethod' => 'GET' + ]; + + $psrLogger->$level($data, [ + 'httpRequest' => $httpRequest + ]); + + $backoff = new ExponentialBackoff(8); + $entries = $backoff->execute(function () use ($logger) { + $entries = iterator_to_array($logger->entries()); + + if (count($entries) === 0) { + throw new \Exception(); + } + + return $entries; + }); + $actualEntryInfo = $entries[0]->info(); + + $this->assertEquals($data, $actualEntryInfo['textPayload']); + $this->assertEquals($httpRequest['requestMethod'], $actualEntryInfo['httpRequest']['requestMethod']); + } +} diff --git a/tests/system/bootstrap.php b/tests/system/bootstrap.php index 69b0a19aba95..329e00754c2f 100644 --- a/tests/system/bootstrap.php +++ b/tests/system/bootstrap.php @@ -5,6 +5,7 @@ use Google\Cloud\Tests\System\PubSub\PubSubTestCase; use Google\Cloud\Tests\System\Datastore\DatastoreTestCase; use Google\Cloud\Tests\System\Storage\StorageTestCase; +use Google\Cloud\Tests\System\Logging\LoggingTestCase; if (!getenv('GOOGLE_CLOUD_PHP_TESTS_KEY_PATH')) { throw new \Exception( @@ -16,4 +17,5 @@ PubSubTestCase::tearDownFixtures(); DatastoreTestCase::tearDownFixtures(); StorageTestCase::tearDownFixtures(); + LoggingTestCase::tearDownFixtures(); });