diff --git a/samples/logs.js b/samples/logs.js index 20f5bf52..f01a6fcb 100644 --- a/samples/logs.js +++ b/samples/logs.js @@ -39,7 +39,7 @@ async function writeLogEntry(logName) { }; // A text log entry - const entry = log.entry({resource: resource}, 'Hello, world!'); + const entry = log.entry({resource}, 'Hello, world!'); // A structured log entry const secondEntry = log.entry( diff --git a/samples/package.json b/samples/package.json index 3a9d15fb..572d1fda 100644 --- a/samples/package.json +++ b/samples/package.json @@ -1,35 +1,32 @@ { "name": "nodejs-docs-samples-logging", - "version": "0.0.1", "private": true, "license": "Apache-2.0", "author": "Google Inc.", "repository": "googleapis/nodejs-logging", + "files": [ + "*.js" + ], "engines": { "node": ">=8" }, "scripts": { - "test": "nyc mocha system-test/ test/" + "test": "mocha system-test --timeout 60000" }, "dependencies": { "@google-cloud/logging": "^4.1.1", - "@google-cloud/logging-bunyan": "^0.9.0", - "@google-cloud/logging-winston": "^0.10.0", "@google-cloud/storage": "^2.0.2", - "bunyan": "^1.8.12", "express": "^4.16.3", "fluent-logger": "^3.0.0", - "winston": "^2.4.0", "yargs": "^12.0.0" }, "devDependencies": { - "@google-cloud/nodejs-repo-tools": "^3.0.0", "assert-rejects": "^1.0.0", + "chai": "^4.2.0", + "execa": "^1.0.0", "mocha": "^5.2.0", - "nyc": "^13.0.0", - "proxyquire": "^2.0.1", - "sinon": "^7.0.0", - "supertest": "^3.1.0", + "proxyquire": "^2.1.0", + "supertest": "^3.3.0", "uuid": "^3.3.0" } } diff --git a/samples/quickstart.js b/samples/quickstart.js index acf7e39f..734ca8a7 100644 --- a/samples/quickstart.js +++ b/samples/quickstart.js @@ -14,38 +14,37 @@ */ 'use strict'; -async function main() { - // [START logging_quickstart] + +// [START logging_quickstart] +async function quickstart( + projectId = 'YOUR_PROJECT_ID', // Your Google Cloud Platform project ID + logName = 'my-log' // The name of the log to write to +) { // Imports the Google Cloud client library const {Logging} = require('@google-cloud/logging'); - // Your Google Cloud Platform project ID - const projectId = 'YOUR_PROJECT_ID'; - // Creates a client - const logging = new Logging({ - projectId: projectId, - }); + const logging = new Logging({projectId}); - // The name of the log to write to - const logName = 'my-log'; // Selects the log to write to const log = logging.log(logName); // The data to write to the log const text = 'Hello, world!'; + // The metadata associated with the entry const metadata = { resource: {type: 'global'}, }; + // Prepares a log entry const entry = log.entry(metadata, text); // Writes the log entry await log.write(entry); console.log(`Logged: ${text}`); - - // [END logging_quickstart] } +// [END logging_quickstart] -main().catch(console.error); +const args = process.argv.slice(2); +quickstart(...args).catch(console.error); diff --git a/samples/system-test/.eslintrc.yml b/samples/system-test/.eslintrc.yml index 75216436..6db2a46c 100644 --- a/samples/system-test/.eslintrc.yml +++ b/samples/system-test/.eslintrc.yml @@ -1,7 +1,3 @@ --- env: mocha: true -rules: - node/no-unpublished-require: off - node/no-unsupported-features: off - no-empty: off diff --git a/samples/system-test/fluent.test.js b/samples/system-test/fluent.test.js new file mode 100644 index 00000000..d8188e0c --- /dev/null +++ b/samples/system-test/fluent.test.js @@ -0,0 +1,55 @@ +/** + * Copyright 2017, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const proxyquire = require('proxyquire'); +const request = require('supertest'); +const {assert} = require('chai'); + +describe('fluent', () => { + it('should log error', done => { + let loggerCalled = false; + + const structuredLogger = { + emit: name => { + loggerCalled = true; + assert.strictEqual(name, 'errors'); + }, + }; + + const app = proxyquire('../fluent', { + 'fluent-logger': { + createFluentSender: (name, options) => { + assert.strictEqual(name, 'myapp'); + assert.deepStrictEqual(options, { + host: 'localhost', + port: 24224, + timeout: 3.0, + }); + return structuredLogger; + }, + }, + }); + + request(app) + .get('/') + .expect(500) + .expect(() => { + assert(loggerCalled, 'structuredLogger.emit should have been called'); + }) + .end(done); + }); +}); diff --git a/samples/system-test/logs.test.js b/samples/system-test/logs.test.js index 24e19b4f..463b7014 100644 --- a/samples/system-test/logs.test.js +++ b/samples/system-test/logs.test.js @@ -15,30 +15,25 @@ 'use strict'; -const path = require(`path`); -const assert = require('assert'); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); - -const cwd = path.join(__dirname, `..`); -const cmd = `node logs.js`; +const {assert} = require('chai'); +const execa = require('execa'); +const uuid = require('uuid'); +const cmd = 'node logs'; +const exec = async cmd => (await execa.shell(cmd)).stdout; const logName = `nodejs-logging-logs-test-${uuid.v4()}`; -const message = `Hello world!`; - -before(async () => { - await tools.checkCredentials(); -}); +const message = 'Hello world!'; -it(`should write a log entry`, async () => { - const output = await tools.runAsync( - `${cmd} write ${logName} '{"type":"global"}' '{"message":"${message}"}'`, - cwd - ); - assert.strictEqual(output, `Wrote to ${logName}`); -}); +describe('logs', () => { + it('should write a log entry', async () => { + const output = await exec( + `${cmd} write ${logName} '{"type":"global"}' '{"message":"${message}"}'` + ); + assert.strictEqual(output, `Wrote to ${logName}`); + }); -it(`should write a simple log entry`, async () => { - const output = await tools.runAsync(`${cmd} write-simple ${logName}`, cwd); - assert.strictEqual(output, `Wrote to ${logName}`); + it('should write a simple log entry', async () => { + const output = await exec(`${cmd} write-simple ${logName}`); + assert.strictEqual(output, `Wrote to ${logName}`); + }); }); diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index 015a25ca..5767942e 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -15,64 +15,26 @@ 'use strict'; -const proxyquire = require(`proxyquire`).noPreserveCache(); -const sinon = require(`sinon`); -const assert = require('assert'); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); +const {assert} = require('chai'); +const uuid = require('uuid'); +const {Logging} = require('@google-cloud/logging'); +const execa = require('execa'); -const {Logging} = proxyquire(`@google-cloud/logging`, {}); const logging = new Logging(); - const logName = `nodejs-docs-samples-test-${uuid.v4()}`; +const projectId = process.env.GCLOUD_PROJECT; +const cmd = 'node quickstart'; + +describe('quickstart', () => { + after(async () => { + await logging + .log(logName) + .delete() + .catch(console.warn); + }); -after(async () => { - try { - await logging.log(logName).delete(); - } catch (err) {} // ignore error -}); - -beforeEach(tools.stubConsole); -afterEach(tools.restoreConsole); - -it(`should log an entry`, done => { - const expectedlogName = `my-log`; - - const logMock = { - entry: sinon.stub().returns({}), - write: _entry => { - assert.deepStrictEqual(_entry, {}); - - const log = logging.log(logName); - const text = `Hello, world!`; - const entry = log.entry({resource: {type: `global`}}, text); - - return log.write(entry).then(results => { - setTimeout(() => { - try { - assert(console.log.calledOnce); - assert.deepStrictEqual(console.log.firstCall.args, [ - `Logged: ${text}`, - ]); - done(); - } catch (err) { - done(err); - } - }, 200); - - return results; - }); - }, - }; - const loggingMock = { - log: _logName => { - assert.strictEqual(_logName, expectedlogName); - return logMock; - }, - }; - proxyquire(`../quickstart`, { - '@google-cloud/logging': { - Logging: sinon.stub().returns(loggingMock), - }, + it('should log an entry', async () => { + const {stdout} = await execa.shell(`${cmd} ${projectId} ${logName}`); + assert.match(stdout, /Logged: Hello, world!/); }); }); diff --git a/samples/system-test/sinks.test.js b/samples/system-test/sinks.test.js index b7b330d0..6003ebf3 100644 --- a/samples/system-test/sinks.test.js +++ b/samples/system-test/sinks.test.js @@ -16,78 +16,71 @@ 'use strict'; const {Logging} = new require('@google-cloud/logging'); -const path = require(`path`); const {Storage} = new require('@google-cloud/storage'); -const assert = require('assert'); -const tools = require(`@google-cloud/nodejs-repo-tools`); +const {assert} = require('chai'); +const execa = require('execa'); const uuid = require(`uuid`); const assertRejects = require('assert-rejects'); -const cwd = path.join(__dirname, `..`); -const cmd = `node sinks.js`; - +const exec = async cmd => (await execa.shell(cmd)).stdout; +const cmd = 'node sinks.js'; const bucketName = `nodejs-logging-sinks-test-${uuid.v4()}`; const sinkName = `nodejs-logging-sinks-test-${uuid.v4()}`; -const filter = `severity > WARNING`; +const filter = 'severity > WARNING'; const logging = new Logging(); const storage = new Storage(); -before(tools.checkCredentials); -before(async () => { - await storage.createBucket(bucketName); -}); +describe('sinks', () => { + before(async () => { + await storage.createBucket(bucketName); + }); -after(async () => { - try { - await logging.sink(sinkName).delete(); - } catch (err) {} // ignore error - try { - await storage.bucket(bucketName).delete(); - } catch (err) {} // ignore error -}); + after(async () => { + await logging + .sink(sinkName) + .delete() + .catch(console.warn); + await storage + .bucket(bucketName) + .delete() + .catch(console.warn); + }); -it(`should create a sink`, async () => { - const output = await tools.runAsync( - `${cmd} create ${sinkName} ${bucketName} "${filter}"`, - cwd - ); - assert.strictEqual(output, `Created sink ${sinkName} to ${bucketName}`); - const [metadata] = await logging.sink(sinkName).getMetadata(); - assert.strictEqual(metadata.name, sinkName); - assert.strictEqual(metadata.destination.includes(bucketName), true); - assert.strictEqual(metadata.filter, filter); -}); + it(`should create a sink`, async () => { + const output = await exec( + `${cmd} create ${sinkName} ${bucketName} "${filter}"` + ); + assert.strictEqual(output, `Created sink ${sinkName} to ${bucketName}`); + const [metadata] = await logging.sink(sinkName).getMetadata(); + assert.strictEqual(metadata.name, sinkName); + assert.match(metadata.destination, new RegExp(bucketName)); + assert.strictEqual(metadata.filter, filter); + }); -it(`should get a sink`, async () => { - const output = await tools.runAsync(`${cmd} get ${sinkName}`, cwd); - assert.strictEqual(output.includes(sinkName), true); -}); + it(`should get a sink`, async () => { + const output = await exec(`${cmd} get ${sinkName}`); + assert.match(output, new RegExp(sinkName)); + }); -it(`should list sinks`, async () => { - await tools - .tryTest(async assert => { - const output = await tools.runAsync(`${cmd} list`, cwd); - assert(output.includes(`Sinks:`)); - assert(output.includes(sinkName)); - }) - .start(); -}); + it(`should list sinks`, async () => { + const output = await exec(`${cmd} list`); + assert.match(output, /Sinks:/); + assert.match(output, new RegExp(sinkName)); + }); -it(`should update a sink`, async () => { - const newFilter = 'severity >= WARNING'; - const output = await tools.runAsync( - `${cmd} update ${sinkName} "${newFilter}"`, - cwd - ); - assert(output.indexOf(`Sink ${sinkName} updated.`) > -1); - const [metadata] = await logging.sink(sinkName).getMetadata(); - assert.strictEqual(metadata.name, sinkName); - assert.strictEqual(metadata.destination.includes(bucketName), true); - assert.strictEqual(metadata.filter, newFilter); -}); + it(`should update a sink`, async () => { + const newFilter = 'severity >= WARNING'; + const output = await exec(`${cmd} update ${sinkName} "${newFilter}"`); + assert.match(output, new RegExp(`Sink ${sinkName} updated.`)); + const [metadata] = await logging.sink(sinkName).getMetadata(); + assert.strictEqual(metadata.name, sinkName); + assert.match(metadata.destination, new RegExp(bucketName)); + assert.strictEqual(metadata.filter, newFilter); + }); -it(`should delete a sink`, async () => { - const output = await tools.runAsync(`${cmd} delete ${sinkName}`, cwd); - assert.strictEqual(output, `Sink ${sinkName} deleted.`); - await assertRejects(logging.sink(sinkName).getMetadata()); + it(`should delete a sink`, async () => { + const output = await exec(`${cmd} delete ${sinkName}`); + assert.strictEqual(output, `Sink ${sinkName} deleted.`); + await assertRejects(logging.sink(sinkName).getMetadata()); + }); }); diff --git a/samples/test/.eslintrc.yml b/samples/test/.eslintrc.yml deleted file mode 100644 index 75216436..00000000 --- a/samples/test/.eslintrc.yml +++ /dev/null @@ -1,7 +0,0 @@ ---- -env: - mocha: true -rules: - node/no-unpublished-require: off - node/no-unsupported-features: off - no-empty: off diff --git a/samples/test/fluent.test.js b/samples/test/fluent.test.js deleted file mode 100644 index 80f847f5..00000000 --- a/samples/test/fluent.test.js +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -const proxyquire = require(`proxyquire`).noPreserveCache(); -const request = require(`supertest`); -const assert = require('assert'); - -it(`should log error`, done => { - let loggerCalled = false; - - const structuredLogger = { - emit: name => { - loggerCalled = true; - assert.strictEqual(name, `errors`); - }, - }; - - const app = proxyquire(`../fluent`, { - 'fluent-logger': { - createFluentSender: (name, options) => { - assert.strictEqual(name, `myapp`); - assert.deepStrictEqual(options, { - host: `localhost`, - port: 24224, - timeout: 3.0, - }); - return structuredLogger; - }, - }, - }); - - request(app) - .get(`/`) - .expect(500) - .expect(() => { - assert(loggerCalled, `structuredLogger.emit should have been called`); - }) - .end(done); -}); diff --git a/samples/test/mocha.opts b/samples/test/mocha.opts deleted file mode 100644 index 8751e7ba..00000000 --- a/samples/test/mocha.opts +++ /dev/null @@ -1,3 +0,0 @@ ---require intelli-espower-loader ---timeout 10000 ---throw-deprecation