Skip to content

Commit 28d376a

Browse files
Merge pull request #103 from pinanks/DOT-3435
Upload logs and dom
2 parents cb99d64 + efef85d commit 28d376a

File tree

9 files changed

+69
-36
lines changed

9 files changed

+69
-36
lines changed

src/commander/exec.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ command
5656
await tasks.run(ctx);
5757
} catch (error) {
5858
ctx.log.info('\nRefer docs: https://www.lambdatest.com/support/docs/smart-visual-regression-testing/');
59-
} finally {
60-
await ctx.browser?.close();
61-
ctx.log.debug(`Closed browser`);
62-
await ctx.server?.close();
63-
ctx.log.debug(`Closed server`);
6459
}
6560
})
6661

src/lib/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ export default {
6464
// CI
6565
GITHUB_API_HOST: 'https://api.github.com',
6666

67+
// log file path
68+
LOG_FILE_PATH: '.smartui.log',
69+
6770
SUPPORTED_MOBILE_DEVICES: {
6871
'Blackberry KEY2 LE': { os: 'android', viewport: {width: 412, height: 618}},
6972
'Galaxy A12': { os: 'android', viewport: {width: 360, height: 800}},

src/lib/env.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ export default (): Env => {
44
const {
55
PROJECT_TOKEN = '',
66
SMARTUI_CLIENT_API_URL = 'https://api.lambdatest.com/visualui/1.0',
7-
LT_SDK_LOG_LEVEL,
8-
LT_SDK_DEBUG,
97
SMARTUI_GIT_INFO_FILEPATH,
108
HTTP_PROXY,
119
HTTPS_PROXY,
@@ -20,8 +18,6 @@ export default (): Env => {
2018
return {
2119
PROJECT_TOKEN,
2220
SMARTUI_CLIENT_API_URL,
23-
LT_SDK_LOG_LEVEL,
24-
LT_SDK_DEBUG,
2521
SMARTUI_GIT_INFO_FILEPATH,
2622
HTTP_PROXY,
2723
HTTPS_PROXY,

src/lib/httpClient.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,32 @@ export default class httpClient {
157157
data: JSON.stringify(requestBody)
158158
}, log);
159159
}
160+
161+
getS3PreSignedURL(ctx: Context) {
162+
return this.request({
163+
url: `/loguploadurl`,
164+
method: 'POST',
165+
headers: { 'Content-Type': 'application/json' },
166+
data: {
167+
buildId: ctx.build.id
168+
}
169+
}, ctx.log)
170+
}
171+
172+
uploadLogs(ctx: Context, uploadURL: string) {
173+
const fileStream = fs.createReadStream(constants.LOG_FILE_PATH);
174+
const { size } = fs.statSync(constants.LOG_FILE_PATH);
175+
176+
return this.request({
177+
url: uploadURL,
178+
method: 'PUT',
179+
headers: {
180+
'Content-Type': 'text/plain',
181+
'Content-Length': size,
182+
},
183+
data: fileStream,
184+
maxBodyLength: Infinity, // prevent axios from limiting the body size
185+
maxContentLength: Infinity, // prevent axios from limiting the content size
186+
}, ctx.log)
187+
}
160188
}

src/lib/logger.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,41 @@
1-
import { Env } from '../types.js'
2-
import { createLogger, format, transports } from 'winston'
3-
import getEnv from '../lib/env.js'
1+
import { createLogger, format, transports, log } from 'winston'
2+
import constants from './constants.js'
43
import chalk from 'chalk'
54

65
interface LogContext {
76
task?: string;
87
}
98

10-
let logContext: LogContext = { task: 'smartui-cli' };
9+
let logContext: LogContext = {};
1110

1211
// Function to update context
1312
export function updateLogContext(newContext: LogContext) {
1413
logContext = { ...logContext, ...newContext };
1514
}
1615

17-
const logLevel = (): string => {
18-
let env: Env = getEnv();
19-
let debug: any = (env.LT_SDK_DEBUG === 'true') ? 'debug' : undefined;
20-
return debug || env.LT_SDK_LOG_LEVEL || 'info'
21-
}
22-
2316
// Create a Winston logger
2417
const logger = createLogger({
25-
level: logLevel(),
2618
format: format.combine(
2719
format.timestamp(),
2820
format.printf(info => {
2921
let contextString = Object.values(logContext).join(' | ');
30-
let message = (typeof info.message === 'object') ? JSON.stringify(info.message) : info.message;
22+
let message = (typeof info.message === 'object') ? JSON.stringify(info.message).trim() : info.message.trim();
3123
switch (info.level) {
32-
case 'debug':
33-
message = chalk.blue(message);
34-
break;
3524
case 'warn':
3625
message = chalk.yellow(message);
3726
break;
38-
case 'error':
39-
message = chalk.red(message);
40-
break;
4127
}
4228
return (info.level === 'info') ? message : `[${contextString}:${info.level}] ` + message;
4329
})
4430
),
45-
transports: [new transports.Console(), new transports.File({ filename: '.smartui.log' })]
31+
transports: [
32+
new transports.Console({
33+
level: 'info'
34+
}),
35+
new transports.File({
36+
level: 'debug',
37+
filename: constants.LOG_FILE_PATH
38+
})]
4639
});
4740

4841
export default logger

src/lib/processSnapshot.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Snapshot, Context, ProcessedSnapshot } from "../types.js";
22
import { scrollToBottomAndBackToTop, getRenderViewports } from "./utils.js"
33
import { firefox, Locator } from "@playwright/test"
44
import constants from "./constants.js";
5+
import { updateLogContext } from '../lib/logger.js'
56

67
const MAX_RESOURCE_SIZE = 15 * (1024 ** 2); // 15MB
78
var ALLOWED_RESOURCES = ['document', 'stylesheet', 'image', 'media', 'font', 'other'];
@@ -76,6 +77,7 @@ export default class Queue {
7677
}
7778

7879
async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise<Record<string, any>> {
80+
updateLogContext({task: 'discovery'});
7981
ctx.log.debug(`Processing snapshot ${snapshot.name}`);
8082

8183
let launchOptions: Record<string, any> = { headless: true }

src/lib/server.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ import path from 'path';
33
import fastify, { FastifyInstance, RouteShorthandOptions } from 'fastify';
44
import { readFileSync } from 'fs'
55
import { Context } from '../types.js'
6-
import processSnapshot from './processSnapshot.js'
76
import { validateSnapshot } from './schemaValidation.js'
8-
import constants from './constants.js';
97

108
export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMessage, ServerResponse>> => {
119

12-
const server: FastifyInstance<Server, IncomingMessage, ServerResponse> = fastify({ logger: ctx.env.LT_SDK_DEBUG ? true : false, bodyLimit: 30000000 });
10+
const server: FastifyInstance<Server, IncomingMessage, ServerResponse> = fastify({
11+
logger: {
12+
level: 'debug',
13+
stream: { write: (message) => { ctx.log.debug(message) }}
14+
},
15+
bodyLimit: 30000000
16+
});
1317
const opts: RouteShorthandOptions = {};
1418
const SMARTUI_DOM = readFileSync(path.resolve(__dirname, 'dom-serializer.js'), 'utf-8');
1519

src/tasks/finalizeBuild.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { ListrTask, ListrRendererFactory } from 'listr2';
22
import { Context } from '../types.js'
3-
import chalk from 'chalk';
43
import { updateLogContext } from '../lib/logger.js';
4+
import chalk from 'chalk';
5+
import { unlinkSync } from 'fs';
6+
import constants from '../lib/constants.js';
57

68
export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRendererFactory> => {
79
return {
@@ -10,15 +12,27 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
1012
updateLogContext({task: 'finalizeBuild'});
1113

1214
try {
13-
await new Promise(resolve => (setTimeout(resolve, 2000)));
1415
await ctx.client.finalizeBuild(ctx.build.id, ctx.totalSnapshots, ctx.log);
1516
task.output = chalk.gray(`build url: ${ctx.build.url}`);
16-
task.title = 'Finalized build'
17+
task.title = 'Finalized build';
1718
} catch (error: any) {
1819
ctx.log.debug(error);
1920
task.output = chalk.gray(error.message);
2021
throw new Error('Finalize build failed');
2122
}
23+
24+
// cleanup and upload logs
25+
try {
26+
await ctx.browser?.close();
27+
ctx.log.debug(`Closed browser`);
28+
await ctx.server?.close();
29+
ctx.log.debug(`Closed server`);
30+
let resp = await ctx.client.getS3PreSignedURL(ctx);
31+
await ctx.client.uploadLogs(ctx, resp.data.url);
32+
unlinkSync(constants.LOG_FILE_PATH);
33+
} catch (error: any) {
34+
ctx.log.debug(error);
35+
}
2236
},
2337
rendererOptions: { persistentOutput: true }
2438
}

src/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ export interface Context {
4848
export interface Env {
4949
PROJECT_TOKEN: string;
5050
SMARTUI_CLIENT_API_URL: string;
51-
LT_SDK_LOG_LEVEL: string | undefined;
52-
LT_SDK_DEBUG: string | undefined;
5351
SMARTUI_GIT_INFO_FILEPATH: string | undefined;
5452
HTTP_PROXY: string | undefined;
5553
HTTPS_PROXY: string | undefined;

0 commit comments

Comments
 (0)