]>
Commit | Line | Data |
---|---|---|
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | |
2 | ||
3 | import { expect } from 'chai' | |
4 | import { wait } from '@shared/core-utils' | |
5 | import { LiveVideoError, VideoPrivacy } from '@shared/models' | |
6 | import { | |
7 | cleanupTests, | |
8 | ConfigCommand, | |
9 | createMultipleServers, | |
10 | doubleFollow, | |
11 | PeerTubeServer, | |
12 | setAccessTokensToServers, | |
13 | setDefaultVideoChannel, | |
14 | stopFfmpeg, | |
15 | waitJobs, | |
16 | waitUntilLiveReplacedByReplayOnAllServers, | |
17 | waitUntilLiveWaitingOnAllServers | |
18 | } from '@shared/server-commands' | |
19 | import { checkLiveCleanup } from '../../shared' | |
20 | ||
21 | describe('Test live constraints', function () { | |
22 | let servers: PeerTubeServer[] = [] | |
23 | let userId: number | |
24 | let userAccessToken: string | |
25 | let userChannelId: number | |
26 | ||
27 | async function createLiveWrapper (options: { | |
28 | replay: boolean | |
29 | permanent: boolean | |
30 | }) { | |
31 | const { replay, permanent } = options | |
32 | ||
33 | const liveAttributes = { | |
34 | name: 'user live', | |
35 | channelId: userChannelId, | |
36 | privacy: VideoPrivacy.PUBLIC, | |
37 | saveReplay: replay, | |
38 | permanentLive: permanent | |
39 | } | |
40 | ||
41 | const { uuid } = await servers[0].live.create({ token: userAccessToken, fields: liveAttributes }) | |
42 | return uuid | |
43 | } | |
44 | ||
45 | async function checkSaveReplay (videoId: string, resolutions = [ 720 ]) { | |
46 | for (const server of servers) { | |
47 | const video = await server.videos.get({ id: videoId }) | |
48 | expect(video.isLive).to.be.false | |
49 | expect(video.duration).to.be.greaterThan(0) | |
50 | } | |
51 | ||
52 | await checkLiveCleanup({ server: servers[0], permanent: false, videoUUID: videoId, savedResolutions: resolutions }) | |
53 | } | |
54 | ||
55 | function updateQuota (options: { total: number, daily: number }) { | |
56 | return servers[0].users.update({ | |
57 | userId, | |
58 | videoQuota: options.total, | |
59 | videoQuotaDaily: options.daily | |
60 | }) | |
61 | } | |
62 | ||
63 | before(async function () { | |
64 | this.timeout(120000) | |
65 | ||
66 | servers = await createMultipleServers(2) | |
67 | ||
68 | // Get the access tokens | |
69 | await setAccessTokensToServers(servers) | |
70 | await setDefaultVideoChannel(servers) | |
71 | ||
72 | await servers[0].config.updateCustomSubConfig({ | |
73 | newConfig: { | |
74 | live: { | |
75 | enabled: true, | |
76 | allowReplay: true, | |
77 | transcoding: { | |
78 | enabled: false | |
79 | } | |
80 | } | |
81 | } | |
82 | }) | |
83 | ||
84 | { | |
85 | const res = await servers[0].users.generate('user1') | |
86 | userId = res.userId | |
87 | userChannelId = res.userChannelId | |
88 | userAccessToken = res.token | |
89 | ||
90 | await updateQuota({ total: 1, daily: -1 }) | |
91 | } | |
92 | ||
93 | // Server 1 and server 2 follow each other | |
94 | await doubleFollow(servers[0], servers[1]) | |
95 | }) | |
96 | ||
97 | it('Should not have size limit if save replay is disabled', async function () { | |
98 | this.timeout(60000) | |
99 | ||
100 | const userVideoLiveoId = await createLiveWrapper({ replay: false, permanent: false }) | |
101 | await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: false }) | |
102 | }) | |
103 | ||
104 | it('Should have size limit depending on user global quota if save replay is enabled on non permanent live', async function () { | |
105 | this.timeout(60000) | |
106 | ||
107 | // Wait for user quota memoize cache invalidation | |
108 | await wait(5000) | |
109 | ||
110 | const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false }) | |
111 | await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true }) | |
112 | ||
113 | await waitUntilLiveReplacedByReplayOnAllServers(servers, userVideoLiveoId) | |
114 | await waitJobs(servers) | |
115 | ||
116 | await checkSaveReplay(userVideoLiveoId) | |
117 | ||
118 | const session = await servers[0].live.getReplaySession({ videoId: userVideoLiveoId }) | |
119 | expect(session.error).to.equal(LiveVideoError.QUOTA_EXCEEDED) | |
120 | }) | |
121 | ||
122 | it('Should have size limit depending on user global quota if save replay is enabled on a permanent live', async function () { | |
123 | this.timeout(60000) | |
124 | ||
125 | // Wait for user quota memoize cache invalidation | |
126 | await wait(5000) | |
127 | ||
128 | const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: true }) | |
129 | await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true }) | |
130 | ||
131 | await waitJobs(servers) | |
132 | await waitUntilLiveWaitingOnAllServers(servers, userVideoLiveoId) | |
133 | ||
134 | const session = await servers[0].live.findLatestSession({ videoId: userVideoLiveoId }) | |
135 | expect(session.error).to.equal(LiveVideoError.QUOTA_EXCEEDED) | |
136 | }) | |
137 | ||
138 | it('Should have size limit depending on user daily quota if save replay is enabled', async function () { | |
139 | this.timeout(60000) | |
140 | ||
141 | // Wait for user quota memoize cache invalidation | |
142 | await wait(5000) | |
143 | ||
144 | await updateQuota({ total: -1, daily: 1 }) | |
145 | ||
146 | const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false }) | |
147 | await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true }) | |
148 | ||
149 | await waitUntilLiveReplacedByReplayOnAllServers(servers, userVideoLiveoId) | |
150 | await waitJobs(servers) | |
151 | ||
152 | await checkSaveReplay(userVideoLiveoId) | |
153 | ||
154 | const session = await servers[0].live.getReplaySession({ videoId: userVideoLiveoId }) | |
155 | expect(session.error).to.equal(LiveVideoError.QUOTA_EXCEEDED) | |
156 | }) | |
157 | ||
158 | it('Should succeed without quota limit', async function () { | |
159 | this.timeout(60000) | |
160 | ||
161 | // Wait for user quota memoize cache invalidation | |
162 | await wait(5000) | |
163 | ||
164 | await updateQuota({ total: 10 * 1000 * 1000, daily: -1 }) | |
165 | ||
166 | const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false }) | |
167 | await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: false }) | |
168 | }) | |
169 | ||
170 | it('Should have the same quota in admin and as a user', async function () { | |
171 | this.timeout(120000) | |
172 | ||
173 | const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false }) | |
174 | const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ token: userAccessToken, videoId: userVideoLiveoId }) | |
175 | ||
176 | await servers[0].live.waitUntilPublished({ videoId: userVideoLiveoId }) | |
177 | ||
178 | await wait(3000) | |
179 | ||
180 | const quotaUser = await servers[0].users.getMyQuotaUsed({ token: userAccessToken }) | |
181 | ||
182 | const { data } = await servers[0].users.list() | |
183 | const quotaAdmin = data.find(u => u.username === 'user1') | |
184 | ||
185 | expect(quotaUser.videoQuotaUsed).to.equal(quotaAdmin.videoQuotaUsed) | |
186 | expect(quotaUser.videoQuotaUsedDaily).to.equal(quotaAdmin.videoQuotaUsedDaily) | |
187 | ||
188 | expect(quotaUser.videoQuotaUsed).to.be.above(10) | |
189 | expect(quotaUser.videoQuotaUsedDaily).to.be.above(10) | |
190 | ||
191 | await stopFfmpeg(ffmpegCommand) | |
192 | }) | |
193 | ||
194 | it('Should have max duration limit', async function () { | |
195 | this.timeout(60000) | |
196 | ||
197 | await servers[0].config.updateCustomSubConfig({ | |
198 | newConfig: { | |
199 | live: { | |
200 | enabled: true, | |
201 | allowReplay: true, | |
202 | maxDuration: 1, | |
203 | transcoding: { | |
204 | enabled: true, | |
205 | resolutions: ConfigCommand.getCustomConfigResolutions(true) | |
206 | } | |
207 | } | |
208 | } | |
209 | }) | |
210 | ||
211 | const userVideoLiveoId = await createLiveWrapper({ replay: true, permanent: false }) | |
212 | await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true }) | |
213 | ||
214 | await waitUntilLiveReplacedByReplayOnAllServers(servers, userVideoLiveoId) | |
215 | await waitJobs(servers) | |
216 | ||
217 | await checkSaveReplay(userVideoLiveoId, [ 720, 480, 360, 240, 144 ]) | |
218 | ||
219 | const session = await servers[0].live.getReplaySession({ videoId: userVideoLiveoId }) | |
220 | expect(session.error).to.equal(LiveVideoError.DURATION_EXCEEDED) | |
221 | }) | |
222 | ||
223 | after(async function () { | |
224 | await cleanupTests(servers) | |
225 | }) | |
226 | }) |