aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-07-06 10:34:29 +0200
committerChocobozzz <me@florianbigard.com>2021-07-20 15:27:16 +0200
commita92ddacb38a4a17e117ca9ed41680a03580fb81d (patch)
tree7b7fc6ef31e86870cee20b915e05c49af1910055
parentf59545d97a80bf06025bf6343a80d834c7eb237f (diff)
downloadPeerTube-a92ddacb38a4a17e117ca9ed41680a03580fb81d.tar.gz
PeerTube-a92ddacb38a4a17e117ca9ed41680a03580fb81d.tar.zst
PeerTube-a92ddacb38a4a17e117ca9ed41680a03580fb81d.zip
Introduce logs command
-rw-r--r--server/tests/api/server/logs.ts49
-rw-r--r--shared/extra-utils/index.ts2
-rw-r--r--shared/extra-utils/logs/index.ts1
-rw-r--r--shared/extra-utils/logs/logs-command.ts41
-rw-r--r--shared/extra-utils/logs/logs.ts32
-rw-r--r--shared/extra-utils/server/servers.ts3
-rw-r--r--shared/extra-utils/shared/abstract-command.ts2
7 files changed, 74 insertions, 56 deletions
diff --git a/server/tests/api/server/logs.ts b/server/tests/api/server/logs.ts
index bc398ea73..ab83a329f 100644
--- a/server/tests/api/server/logs.ts
+++ b/server/tests/api/server/logs.ts
@@ -6,25 +6,28 @@ import {
6 cleanupTests, 6 cleanupTests,
7 flushAndRunServer, 7 flushAndRunServer,
8 killallServers, 8 killallServers,
9 LogsCommand,
9 makePingRequest, 10 makePingRequest,
10 reRunServer, 11 reRunServer,
11 ServerInfo, 12 ServerInfo,
12 setAccessTokensToServers 13 setAccessTokensToServers,
13} from '../../../../shared/extra-utils/index' 14 uploadVideo,
14import { getAuditLogs, getLogs } from '../../../../shared/extra-utils/logs/logs' 15 waitJobs
15import { waitJobs } from '../../../../shared/extra-utils/server/jobs' 16} from '@shared/extra-utils'
16import { uploadVideo } from '../../../../shared/extra-utils/videos/videos'
17 17
18const expect = chai.expect 18const expect = chai.expect
19 19
20describe('Test logs', function () { 20describe('Test logs', function () {
21 let server: ServerInfo 21 let server: ServerInfo
22 let logsCommand: LogsCommand
22 23
23 before(async function () { 24 before(async function () {
24 this.timeout(30000) 25 this.timeout(30000)
25 26
26 server = await flushAndRunServer(1) 27 server = await flushAndRunServer(1)
27 await setAccessTokensToServers([ server ]) 28 await setAccessTokensToServers([ server ])
29
30 logsCommand = server.logsCommand
28 }) 31 })
29 32
30 describe('With the standard log file', function () { 33 describe('With the standard log file', function () {
@@ -40,8 +43,8 @@ describe('Test logs', function () {
40 await uploadVideo(server.url, server.accessToken, { name: 'video 2' }) 43 await uploadVideo(server.url, server.accessToken, { name: 'video 2' })
41 await waitJobs([ server ]) 44 await waitJobs([ server ])
42 45
43 const res = await getLogs(server.url, server.accessToken, now) 46 const body = await logsCommand.getLogs({ startDate: now })
44 const logsString = JSON.stringify(res.body) 47 const logsString = JSON.stringify(body)
45 48
46 expect(logsString.includes('video 1')).to.be.false 49 expect(logsString.includes('video 1')).to.be.false
47 expect(logsString.includes('video 2')).to.be.true 50 expect(logsString.includes('video 2')).to.be.true
@@ -63,8 +66,8 @@ describe('Test logs', function () {
63 await uploadVideo(server.url, server.accessToken, { name: 'video 5' }) 66 await uploadVideo(server.url, server.accessToken, { name: 'video 5' })
64 await waitJobs([ server ]) 67 await waitJobs([ server ])
65 68
66 const res = await getLogs(server.url, server.accessToken, now1, now2) 69 const body = await logsCommand.getLogs({ startDate: now1, endDate: now2 })
67 const logsString = JSON.stringify(res.body) 70 const logsString = JSON.stringify(body)
68 71
69 expect(logsString.includes('video 3')).to.be.false 72 expect(logsString.includes('video 3')).to.be.false
70 expect(logsString.includes('video 4')).to.be.true 73 expect(logsString.includes('video 4')).to.be.true
@@ -80,15 +83,15 @@ describe('Test logs', function () {
80 await waitJobs([ server ]) 83 await waitJobs([ server ])
81 84
82 { 85 {
83 const res = await getLogs(server.url, server.accessToken, now, undefined, 'info') 86 const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
84 const logsString = JSON.stringify(res.body) 87 const logsString = JSON.stringify(body)
85 88
86 expect(logsString.includes('video 6')).to.be.true 89 expect(logsString.includes('video 6')).to.be.true
87 } 90 }
88 91
89 { 92 {
90 const res = await getLogs(server.url, server.accessToken, now, undefined, 'warn') 93 const body = await logsCommand.getLogs({ startDate: now, level: 'warn' })
91 const logsString = JSON.stringify(res.body) 94 const logsString = JSON.stringify(body)
92 95
93 expect(logsString.includes('video 6')).to.be.false 96 expect(logsString.includes('video 6')).to.be.false
94 } 97 }
@@ -101,8 +104,8 @@ describe('Test logs', function () {
101 104
102 await makePingRequest(server) 105 await makePingRequest(server)
103 106
104 const res = await getLogs(server.url, server.accessToken, now, undefined, 'info') 107 const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
105 const logsString = JSON.stringify(res.body) 108 const logsString = JSON.stringify(body)
106 109
107 expect(logsString.includes('/api/v1/ping')).to.be.true 110 expect(logsString.includes('/api/v1/ping')).to.be.true
108 }) 111 })
@@ -118,8 +121,8 @@ describe('Test logs', function () {
118 121
119 await makePingRequest(server) 122 await makePingRequest(server)
120 123
121 const res = await getLogs(server.url, server.accessToken, now, undefined, 'info') 124 const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
122 const logsString = JSON.stringify(res.body) 125 const logsString = JSON.stringify(body)
123 126
124 expect(logsString.includes('/api/v1/ping')).to.be.false 127 expect(logsString.includes('/api/v1/ping')).to.be.false
125 }) 128 })
@@ -137,15 +140,15 @@ describe('Test logs', function () {
137 await uploadVideo(server.url, server.accessToken, { name: 'video 8' }) 140 await uploadVideo(server.url, server.accessToken, { name: 'video 8' })
138 await waitJobs([ server ]) 141 await waitJobs([ server ])
139 142
140 const res = await getAuditLogs(server.url, server.accessToken, now) 143 const body = await logsCommand.getAuditLogs({ startDate: now })
141 const logsString = JSON.stringify(res.body) 144 const logsString = JSON.stringify(body)
142 145
143 expect(logsString.includes('video 7')).to.be.false 146 expect(logsString.includes('video 7')).to.be.false
144 expect(logsString.includes('video 8')).to.be.true 147 expect(logsString.includes('video 8')).to.be.true
145 148
146 expect(res.body).to.have.lengthOf(1) 149 expect(body).to.have.lengthOf(1)
147 150
148 const item = res.body[0] 151 const item = body[0]
149 152
150 const message = JSON.parse(item.message) 153 const message = JSON.parse(item.message)
151 expect(message.domain).to.equal('videos') 154 expect(message.domain).to.equal('videos')
@@ -168,8 +171,8 @@ describe('Test logs', function () {
168 await uploadVideo(server.url, server.accessToken, { name: 'video 11' }) 171 await uploadVideo(server.url, server.accessToken, { name: 'video 11' })
169 await waitJobs([ server ]) 172 await waitJobs([ server ])
170 173
171 const res = await getAuditLogs(server.url, server.accessToken, now1, now2) 174 const body = await logsCommand.getAuditLogs({ startDate: now1, endDate: now2 })
172 const logsString = JSON.stringify(res.body) 175 const logsString = JSON.stringify(body)
173 176
174 expect(logsString.includes('video 9')).to.be.false 177 expect(logsString.includes('video 9')).to.be.false
175 expect(logsString.includes('video 10')).to.be.true 178 expect(logsString.includes('video 10')).to.be.true
diff --git a/shared/extra-utils/index.ts b/shared/extra-utils/index.ts
index c4f0afdc4..a3d3970af 100644
--- a/shared/extra-utils/index.ts
+++ b/shared/extra-utils/index.ts
@@ -6,6 +6,8 @@ export * from './custom-pages'
6 6
7export * from './feeds' 7export * from './feeds'
8 8
9export * from './logs'
10
9export * from './mock-servers/mock-instances-index' 11export * from './mock-servers/mock-instances-index'
10 12
11export * from './miscs/email' 13export * from './miscs/email'
diff --git a/shared/extra-utils/logs/index.ts b/shared/extra-utils/logs/index.ts
new file mode 100644
index 000000000..69452d7f0
--- /dev/null
+++ b/shared/extra-utils/logs/index.ts
@@ -0,0 +1 @@
export * from './logs-command'
diff --git a/shared/extra-utils/logs/logs-command.ts b/shared/extra-utils/logs/logs-command.ts
new file mode 100644
index 000000000..f7594734d
--- /dev/null
+++ b/shared/extra-utils/logs/logs-command.ts
@@ -0,0 +1,41 @@
1import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes'
2import { LogLevel } from '../../models/server/log-level.type'
3import { AbstractCommand, OverrideCommandOptions } from '../shared'
4
5export class LogsCommand extends AbstractCommand {
6
7 getLogs (options: OverrideCommandOptions & {
8 startDate: Date
9 endDate?: Date
10 level?: LogLevel
11 }) {
12 const { startDate, endDate, level } = options
13 const path = '/api/v1/server/logs'
14
15 return this.getRequestBody({
16 ...options,
17
18 path,
19 query: { startDate, endDate, level },
20 defaultExpectedStatus: HttpStatusCode.OK_200
21 })
22 }
23
24 getAuditLogs (options: OverrideCommandOptions & {
25 startDate: Date
26 endDate?: Date
27 }) {
28 const { startDate, endDate } = options
29
30 const path = '/api/v1/server/audit-logs'
31
32 return this.getRequestBody({
33 ...options,
34
35 path,
36 query: { startDate, endDate },
37 defaultExpectedStatus: HttpStatusCode.OK_200
38 })
39 }
40
41}
diff --git a/shared/extra-utils/logs/logs.ts b/shared/extra-utils/logs/logs.ts
deleted file mode 100644
index 8d741276c..000000000
--- a/shared/extra-utils/logs/logs.ts
+++ /dev/null
@@ -1,32 +0,0 @@
1import { makeGetRequest } from '../requests/requests'
2import { LogLevel } from '../../models/server/log-level.type'
3import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
4
5function getLogs (url: string, accessToken: string, startDate: Date, endDate?: Date, level?: LogLevel) {
6 const path = '/api/v1/server/logs'
7
8 return makeGetRequest({
9 url,
10 path,
11 token: accessToken,
12 query: { startDate, endDate, level },
13 statusCodeExpected: HttpStatusCode.OK_200
14 })
15}
16
17function getAuditLogs (url: string, accessToken: string, startDate: Date, endDate?: Date) {
18 const path = '/api/v1/server/audit-logs'
19
20 return makeGetRequest({
21 url,
22 path,
23 token: accessToken,
24 query: { startDate, endDate },
25 statusCodeExpected: HttpStatusCode.OK_200
26 })
27}
28
29export {
30 getLogs,
31 getAuditLogs
32}
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts
index b64c9eec6..4343eab93 100644
--- a/shared/extra-utils/server/servers.ts
+++ b/shared/extra-utils/server/servers.ts
@@ -10,6 +10,7 @@ import { BulkCommand } from '../bulk'
10import { CLICommand } from '../cli' 10import { CLICommand } from '../cli'
11import { CustomPagesCommand } from '../custom-pages' 11import { CustomPagesCommand } from '../custom-pages'
12import { FeedCommand } from '../feeds' 12import { FeedCommand } from '../feeds'
13import { LogsCommand } from '../logs'
13import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs' 14import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs'
14import { makeGetRequest } from '../requests/requests' 15import { makeGetRequest } from '../requests/requests'
15 16
@@ -69,6 +70,7 @@ interface ServerInfo {
69 cliCommand?: CLICommand 70 cliCommand?: CLICommand
70 customPageCommand?: CustomPagesCommand 71 customPageCommand?: CustomPagesCommand
71 feedCommand?: FeedCommand 72 feedCommand?: FeedCommand
73 logsCommand?: LogsCommand
72} 74}
73 75
74function parallelTests () { 76function parallelTests () {
@@ -278,6 +280,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = []
278 server.cliCommand = new CLICommand(server) 280 server.cliCommand = new CLICommand(server)
279 server.customPageCommand = new CustomPagesCommand(server) 281 server.customPageCommand = new CustomPagesCommand(server)
280 server.feedCommand = new FeedCommand(server) 282 server.feedCommand = new FeedCommand(server)
283 server.logsCommand = new LogsCommand(server)
281 284
282 res(server) 285 res(server)
283 }) 286 })
diff --git a/shared/extra-utils/shared/abstract-command.ts b/shared/extra-utils/shared/abstract-command.ts
index 7f812daa8..a57222216 100644
--- a/shared/extra-utils/shared/abstract-command.ts
+++ b/shared/extra-utils/shared/abstract-command.ts
@@ -13,7 +13,7 @@ interface CommonCommandOptions extends OverrideCommandOptions {
13} 13}
14 14
15interface GetCommandOptions extends CommonCommandOptions { 15interface GetCommandOptions extends CommonCommandOptions {
16 query?: { [ id: string ]: string } 16 query?: { [ id: string ]: any }
17 contentType?: string 17 contentType?: string
18 accept?: string 18 accept?: string
19} 19}