|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const getInstalledPath = require('get-installed-path'); |
| 4 | +const BbPromise = require('bluebird'); |
| 5 | +const chai = require('chai'); |
| 6 | +const sinon = require('sinon'); |
| 7 | +const AWSAlias = require('../index'); |
| 8 | + |
| 9 | +const serverlessPath = getInstalledPath.sync('serverless', { local: true }); |
| 10 | +const AwsProvider = require(`${serverlessPath}/lib/plugins/aws/provider/awsProvider`); |
| 11 | +const Serverless = require(`${serverlessPath}/lib/Serverless`); |
| 12 | + |
| 13 | +chai.use(require('chai-as-promised')); |
| 14 | +chai.use(require('sinon-chai')); |
| 15 | +const expect = chai.expect; |
| 16 | + |
| 17 | +describe('logs', () => { |
| 18 | + let serverless; |
| 19 | + let options; |
| 20 | + let awsAlias; |
| 21 | + // Sinon and stubs for SLS CF access |
| 22 | + let sandbox; |
| 23 | + //let providerRequestStub; |
| 24 | + let logStub; |
| 25 | + |
| 26 | + before(() => { |
| 27 | + sandbox = sinon.sandbox.create(); |
| 28 | + }); |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + serverless = new Serverless(); |
| 32 | + options = { |
| 33 | + alias: 'myAlias', |
| 34 | + stage: 'dev', |
| 35 | + region: 'us-east-1', |
| 36 | + function: 'first' |
| 37 | + }; |
| 38 | + serverless.setProvider('aws', new AwsProvider(serverless)); |
| 39 | + serverless.cli = new serverless.classes.CLI(serverless); |
| 40 | + serverless.service.service = 'testService'; |
| 41 | + serverless.service.provider.compiledCloudFormationAliasTemplate = {}; |
| 42 | + awsAlias = new AWSAlias(serverless, options); |
| 43 | + //providerRequestStub = sandbox.stub(awsAlias._provider, 'request'); |
| 44 | + logStub = sandbox.stub(serverless.cli, 'log'); |
| 45 | + |
| 46 | + logStub.returns(); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + sandbox.restore(); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('#logsValidate()', () => { |
| 54 | + beforeEach(() => { |
| 55 | + serverless.config.servicePath = true; |
| 56 | + serverless.service.environment = { |
| 57 | + vars: {}, |
| 58 | + stages: { |
| 59 | + dev: { |
| 60 | + vars: {}, |
| 61 | + regions: { |
| 62 | + 'us-east-1': { |
| 63 | + vars: {}, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }; |
| 69 | + serverless.service.functions = { |
| 70 | + first: { |
| 71 | + handler: true, |
| 72 | + name: 'customName', |
| 73 | + }, |
| 74 | + }; |
| 75 | + }); |
| 76 | + |
| 77 | + it('it should throw error if function is not provided', () => { |
| 78 | + serverless.service.functions = null; |
| 79 | + expect(() => awsAlias.logsValidate()).to.throw(Error); |
| 80 | + }); |
| 81 | + |
| 82 | + it('it should set default options', () => { |
| 83 | + return expect(awsAlias.logsValidate()).to.be.fulfilled |
| 84 | + .then(() => BbPromise.all([ |
| 85 | + expect(awsAlias.options.stage).to.deep.equal('dev'), |
| 86 | + expect(awsAlias.options.region).to.deep.equal('us-east-1'), |
| 87 | + expect(awsAlias.options.function).to.deep.equal('first'), |
| 88 | + expect(awsAlias.options.interval).to.be.equal(1000), |
| 89 | + expect(awsAlias.options.logGroupName).to.deep.equal(awsAlias.provider.naming |
| 90 | + .getLogGroupName('customName')) |
| 91 | + ])); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('#logsGetLogStreams()', () => { |
| 96 | + beforeEach(() => { |
| 97 | + awsAlias.serverless.service.service = 'new-service'; |
| 98 | + awsAlias._options = { |
| 99 | + stage: 'dev', |
| 100 | + region: 'us-east-1', |
| 101 | + function: 'first', |
| 102 | + logGroupName: awsAlias.provider.naming.getLogGroupName('new-service-dev-first'), |
| 103 | + }; |
| 104 | + }); |
| 105 | + |
| 106 | + /** TODO: Use fake alias log stream responses here! |
| 107 | + it('should get log streams with correct params', () => { |
| 108 | + const replyMock = { |
| 109 | + logStreams: [ |
| 110 | + { |
| 111 | + logStreamName: '2016/07/28/[$LATEST]83f5206ab2a8488290349b9c1fbfe2ba', |
| 112 | + creationTime: 1469687512311, |
| 113 | + }, |
| 114 | + { |
| 115 | + logStreamName: '2016/07/28/[$LATEST]83f5206ab2a8488290349b9c1fbfe2ba', |
| 116 | + creationTime: 1469687512311, |
| 117 | + }, |
| 118 | + ], |
| 119 | + }; |
| 120 | + providerRequestStub.resolves(replyMock); |
| 121 | +
|
| 122 | + return expect(awsAlias.logsGetLogStreams()).to.be.fulfilled |
| 123 | + .then(logStreamNames => BbPromise.all([ |
| 124 | + expect(providerRequestStub).to.have.been.calledTwice, |
| 125 | + expect(providerRequestStub).to.have.been.calledWithExactly( |
| 126 | + 'CloudWatchLogs', |
| 127 | + 'describeLogStreams', |
| 128 | + { |
| 129 | + logGroupName: awsAlias.provider.naming.getLogGroupName('new-service-dev-first'), |
| 130 | + descending: true, |
| 131 | + limit: 50, |
| 132 | + orderBy: 'LastEventTime', |
| 133 | + }, |
| 134 | + awsAlias.options.stage, |
| 135 | + awsAlias.options.region |
| 136 | + ), |
| 137 | + expect(logStreamNames[0]) |
| 138 | + .to.be.equal('2016/07/28/[$LATEST]83f5206ab2a8488290349b9c1fbfe2ba'), |
| 139 | + expect(logStreamNames[1]) |
| 140 | + .to.be.equal('2016/07/28/[$LATEST]83f5206ab2a8488290349b9c1fbfe2ba'), |
| 141 | + ])); |
| 142 | + }); |
| 143 | +
|
| 144 | + it('should throw error if no log streams found', () => { |
| 145 | + providerRequestStub.resolves(); |
| 146 | + return expect(awsAlias.logsGetLogStreams()).to.be.rejectedWith(""); |
| 147 | + }); |
| 148 | + }); |
| 149 | +
|
| 150 | + describe('#logsShowLogs()', () => { |
| 151 | + let clock; |
| 152 | +
|
| 153 | + beforeEach(() => { |
| 154 | + // new Date() => return the fake Date 'Sat Sep 01 2016 00:00:00' |
| 155 | + clock = sinon.useFakeTimers(new Date(Date.UTC(2016, 9, 1)).getTime()); |
| 156 | + }); |
| 157 | +
|
| 158 | + afterEach(() => { |
| 159 | + // new Date() => will return the real time again (now) |
| 160 | + clock.restore(); |
| 161 | + }); |
| 162 | +
|
| 163 | + it('should call filterLogEvents API with correct params', () => { |
| 164 | + const replyMock = { |
| 165 | + events: [ |
| 166 | + { |
| 167 | + logStreamName: '2016/07/28/[$LATEST]83f5206ab2a8488290349b9c1fbfe2ba', |
| 168 | + timestamp: 1469687512311, |
| 169 | + message: 'test', |
| 170 | + }, |
| 171 | + { |
| 172 | + logStreamName: '2016/07/28/[$LATEST]83f5206ab2a8488290349b9c1fbfe2ba', |
| 173 | + timestamp: 1469687512311, |
| 174 | + message: 'test', |
| 175 | + }, |
| 176 | + ], |
| 177 | + }; |
| 178 | + const logStreamNamesMock = [ |
| 179 | + '2016/07/28/[$LATEST]83f5206ab2a8488290349b9c1fbfe2ba', |
| 180 | + '2016/07/28/[$LATEST]83f5206ab2a8488290349b9c1fbfe2ba', |
| 181 | + ]; |
| 182 | + providerRequestStub.resolves(replyMock); |
| 183 | + awsAlias.serverless.service.service = 'new-service'; |
| 184 | + awsAlias._options = { |
| 185 | + stage: 'dev', |
| 186 | + region: 'us-east-1', |
| 187 | + function: 'first', |
| 188 | + logGroupName: awsAlias.provider.naming.getLogGroupName('new-service-dev-first'), |
| 189 | + startTime: '3h', |
| 190 | + filter: 'error', |
| 191 | + }; |
| 192 | +
|
| 193 | + return expect(awsAlias.logsShowLogs(logStreamNamesMock)).to.be.fulfilled |
| 194 | + .then(() => BbPromise.all([ |
| 195 | + expect(providerRequestStub).to.have.been.calledOnce, |
| 196 | + expect(providerRequestStub).to.have.been.calledWithExactly( |
| 197 | + 'CloudWatchLogs', |
| 198 | + 'filterLogEvents', |
| 199 | + { |
| 200 | + logGroupName: awsAlias.provider.naming.getLogGroupName('new-service-dev-first'), |
| 201 | + interleaved: true, |
| 202 | + logStreamNames: logStreamNamesMock, |
| 203 | + filterPattern: 'error', |
| 204 | + startTime: 1475269200000, |
| 205 | + }, |
| 206 | + awsAlias.options.stage, |
| 207 | + awsAlias.options.region |
| 208 | + ), |
| 209 | + ])); |
| 210 | + }); |
| 211 | +
|
| 212 | + it('should call filterLogEvents API with standard start time', () => { |
| 213 | + const replyMock = { |
| 214 | + events: [ |
| 215 | + { |
| 216 | + logStreamName: '2016/07/28/[$LATEST]83f5206ab2a8488290349b9c1fbfe2ba', |
| 217 | + timestamp: 1469687512311, |
| 218 | + message: 'test', |
| 219 | + }, |
| 220 | + { |
| 221 | + logStreamName: '2016/07/28/[$LATEST]83f5206ab2a8488290349b9c1fbfe2ba', |
| 222 | + timestamp: 1469687512311, |
| 223 | + message: 'test', |
| 224 | + }, |
| 225 | + ], |
| 226 | + }; |
| 227 | + const logStreamNamesMock = [ |
| 228 | + '2016/07/28/[$LATEST]83f5206ab2a8488290349b9c1fbfe2ba', |
| 229 | + '2016/07/28/[$LATEST]83f5206ab2a8488290349b9c1fbfe2ba', |
| 230 | + ]; |
| 231 | + const filterLogEventsStub = sinon.stub(awsLogs.provider, 'request').resolves(replyMock); |
| 232 | + awsLogs.serverless.service.service = 'new-service'; |
| 233 | + awsLogs.options = { |
| 234 | + stage: 'dev', |
| 235 | + region: 'us-east-1', |
| 236 | + function: 'first', |
| 237 | + logGroupName: awsLogs.provider.naming.getLogGroupName('new-service-dev-first'), |
| 238 | + startTime: '2010-10-20', |
| 239 | + filter: 'error', |
| 240 | + }; |
| 241 | +
|
| 242 | + return awsLogs.showLogs(logStreamNamesMock) |
| 243 | + .then(() => { |
| 244 | + expect(filterLogEventsStub.calledOnce).to.be.equal(true); |
| 245 | + expect(filterLogEventsStub.calledWithExactly( |
| 246 | + 'CloudWatchLogs', |
| 247 | + 'filterLogEvents', |
| 248 | + { |
| 249 | + logGroupName: awsLogs.provider.naming.getLogGroupName('new-service-dev-first'), |
| 250 | + interleaved: true, |
| 251 | + logStreamNames: logStreamNamesMock, |
| 252 | + startTime: 1287532800000, // '2010-10-20' |
| 253 | + filterPattern: 'error', |
| 254 | + }, |
| 255 | + awsLogs.options.stage, |
| 256 | + awsLogs.options.region |
| 257 | + )).to.be.equal(true); |
| 258 | +
|
| 259 | + awsLogs.provider.request.restore(); |
| 260 | + }); |
| 261 | + }); |
| 262 | + */ |
| 263 | + }); |
| 264 | +}); |
0 commit comments