From 02356c7f27b1cec8aa38c29bb133bd79d6af3abc Mon Sep 17 00:00:00 2001 From: Christopher Hoskin Date: Thu, 23 Jul 2020 17:03:55 +0100 Subject: [PATCH] All logging manager tests working --- .../logger/test/unit/loggingManagerTests.js | 69 ++++++++++--------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/libraries/logger/test/unit/loggingManagerTests.js b/libraries/logger/test/unit/loggingManagerTests.js index 505928a9cb..39ab807fac 100644 --- a/libraries/logger/test/unit/loggingManagerTests.js +++ b/libraries/logger/test/unit/loggingManagerTests.js @@ -4,6 +4,8 @@ const chai = require('chai') const path = require('path') const sinon = require('sinon') const sinonChai = require('sinon-chai') +const { promises } = require('dns') +//const fetchMock = require('fetch-mock') chai.use(sinonChai) chai.should() @@ -48,7 +50,10 @@ describe('LoggingManager', function () { this.Fetch = sinon.stub().resolves(this.fetchResponse) this.Fs = { readFile: sinon.stub(), - access: sinon.stub() + access: sinon.stub(), + promises: { + readFile: sinon.stub() + } } this.stackdriverStreamConfig = { stream: 'stackdriver' } this.stackdriverClient = { @@ -296,24 +301,17 @@ describe('LoggingManager', function () { }) describe('checkLogLevel', function () { - beforeEach(function () { - this.Fs.access.yields(null) - }) - - it('should request log level override from the config map', function () { - this.logger.checkLogLevel() - this.Fs.access.should.have.been.calledWithMatch( - '/logging' - ) - this.Fs.readFile.should.have.been.calledWithMatch( + it('should request log level override from the config map', async function () { + await this.logger.checkLogLevel() + this.Fs.promises.readFile.should.have.been.calledWithMatch( '/logging/tracingEndTime' ) }) describe('when read errors', function () { beforeEach(function () { - this.Fs.readFile.yields(new Error('error')) + this.Fs.promises.readFile.yields(new Error('error')) this.logger.checkLogLevel() }) @@ -326,7 +324,7 @@ describe('LoggingManager', function () { describe('when the file is empty', function () { beforeEach(function () { - this.Fs.readFile.yields(null, '') + this.Fs.promises.readFile.yields(null, '') this.logger.checkLogLevel() }) @@ -339,7 +337,7 @@ describe('LoggingManager', function () { describe('when time value returned that is less than current time', function () { beforeEach(function () { - this.Fs.readFile.yields(null, '1') + this.Fs.promises.readFile.yields(null, '1') this.logger.checkLogLevel() }) @@ -352,10 +350,10 @@ describe('LoggingManager', function () { describe('when time value returned that is more than current time', function () { describe('when level is already set', function () { - beforeEach(function () { + beforeEach(async function () { this.bunyanLogger.level.returns(10) - this.Fs.readFile.yields(null, (this.start + 1000).toString()) - this.logger.checkLogLevel() + this.Fs.promises.readFile.returns((this.start + 1000).toString()) + await this.logger.checkLogLevel() }) it('should set trace level', function () { @@ -366,10 +364,10 @@ describe('LoggingManager', function () { }) describe('when level is not already set', function () { - beforeEach(function () { + beforeEach(async function () { this.bunyanLogger.level.returns(20) - this.Fs.readFile.yields(null, (this.start + 1000).toString()) - this.logger.checkLogLevel() + this.Fs.promises.readFile.returns((this.start + 1000).toString()) + await this.logger.checkLogLevel() }) it('should set trace level', function () { @@ -379,7 +377,7 @@ describe('LoggingManager', function () { }) }) - describe('when not running in GKE', function () { + describe('when using metadata', function () { beforeEach(function () { process.env.USE_METADATA = 'TRUE' this.logger = this.LoggingManager.initialize(this.loggerName) @@ -398,13 +396,14 @@ describe('LoggingManager', function () { } } const uri = `http://metadata.google.internal/computeMetadata/v1/project/attributes/${this.loggerName}-setLogLevelEndTime` - this.Fetch.fetch.should.have.been.calledWithMatch(uri,options) + this.Fetch.should.have.been.calledWithMatch(uri,options) }) describe('when request has error', function() { - beforeEach(function() { - this.Request.yields('error') - this.logger.checkLogLevel() + beforeEach(async function() { + this.Fetch = sinon.stub().throws() + //this.Request.yields('error') + await this.logger.checkLogLevel() }) it('should only set default level', function() { @@ -415,9 +414,10 @@ describe('LoggingManager', function () { }) describe('when statusCode is not 200', function() { - beforeEach(function() { - this.Request.yields(null, { statusCode: 404 }) - this.logger.checkLogLevel() + beforeEach(async function() { + this.fetchResponse.status = 404 + //this.Request.yields(null, { statusCode: 404 }) + await this.logger.checkLogLevel() }) it('should only set default level', function() { @@ -428,9 +428,10 @@ describe('LoggingManager', function () { }) describe('when time value returned that is less than current time', function() { - beforeEach(function() { - this.Request.yields(null, { statusCode: 200 }, '1') - this.logger.checkLogLevel() + beforeEach(async function() { + //this.Request.yields(null, { statusCode: 200 }, '1') + this.fetchResponse.text = sinon.stub().resolves('1') + await this.logger.checkLogLevel() }) it('should only set default level', function() { @@ -452,7 +453,7 @@ describe('LoggingManager', function () { await this.logger.checkLogLevel() }) - it.only('should set trace level', function() { + it('should set trace level', function() { this.bunyanLogger.level.should.have.been.calledOnce.and.calledWith( 'trace' ) @@ -460,7 +461,7 @@ describe('LoggingManager', function () { }) describe('when level is not already set', function() { - beforeEach(function() { + beforeEach(async function() { this.bunyanLogger.level.returns(20) this.fetchResponse.text = sinon.stub().resolves(this.start + 1000) this.Fetch.fetch = sinon.stub().resolves(this.fetchResponse) @@ -469,7 +470,7 @@ describe('LoggingManager', function () { //{data: this.start + 1000, status: 200} - this.logger.checkLogLevel() + await this.logger.checkLogLevel() }) it('should set trace level', function() {