ClsiCookieManager.test.mjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. import sinon from 'sinon'
  2. import { beforeEach, describe, expect, it, vi } from 'vitest'
  3. const modulePath = '../../../../app/src/Features/Compile/ClsiCookieManager.mjs'
  4. describe('ClsiCookieManager', function () {
  5. beforeEach(async function (ctx) {
  6. ctx.redis = {
  7. auth() {},
  8. del: sinon.stub(),
  9. get: sinon.stub(),
  10. setex: sinon.stub().resolves(),
  11. }
  12. ctx.project_id = '123423431321-proj-id'
  13. ctx.user_id = 'abc-user-id'
  14. ctx.fetchUtils = {
  15. fetchNothing: sinon.stub().returns(Promise.resolve()),
  16. fetchStringWithResponse: sinon.stub().returns(Promise.resolve()),
  17. }
  18. ctx.metrics = { inc: sinon.stub() }
  19. ctx.settings = {
  20. redis: {
  21. web: 'redis.something',
  22. },
  23. apis: {
  24. clsi: {
  25. url: 'http://clsi.example.com',
  26. },
  27. },
  28. clsiCookie: {
  29. ttlInSeconds: Math.random().toString(),
  30. ttlInSecondsRegular: Math.random().toString(),
  31. key: 'coooookie',
  32. },
  33. }
  34. vi.doMock('../../../../app/src/infrastructure/RedisWrapper', () => ({
  35. default: (ctx.RedisWrapper = {
  36. client: () => ctx.redis,
  37. }),
  38. }))
  39. vi.doMock('@overleaf/settings', () => ({
  40. default: ctx.settings,
  41. }))
  42. vi.doMock('@overleaf/fetch-utils', () => ctx.fetchUtils)
  43. vi.doMock('@overleaf/metrics', () => ({
  44. default: ctx.metrics,
  45. }))
  46. ctx.ClsiCookieManager = (await import(modulePath)).default()
  47. })
  48. describe('getServerId', function () {
  49. it('should call get for the key', async function (ctx) {
  50. ctx.redis.get.resolves('clsi-7')
  51. const serverId = await ctx.ClsiCookieManager.promises.getServerId(
  52. ctx.project_id,
  53. ctx.user_id,
  54. '',
  55. 'n2d'
  56. )
  57. ctx.redis.get
  58. .calledWith(`clsiserver:n2d:${ctx.project_id}:${ctx.user_id}`)
  59. .should.equal(true)
  60. serverId.should.equal('clsi-7')
  61. })
  62. it('should fallback to old key', async function (ctx) {
  63. ctx.redis.get
  64. .withArgs(`clsiserver:n2d:${ctx.project_id}:${ctx.user_id}`)
  65. .resolves(null)
  66. ctx.redis.get
  67. .withArgs(`clsiserver:${ctx.project_id}:${ctx.user_id}`)
  68. .resolves('clsi-7')
  69. const serverId = await ctx.ClsiCookieManager.promises.getServerId(
  70. ctx.project_id,
  71. ctx.user_id,
  72. '',
  73. 'n2d'
  74. )
  75. ctx.redis.get
  76. .calledWith(`clsiserver:n2d:${ctx.project_id}:${ctx.user_id}`)
  77. .should.equal(true)
  78. ctx.redis.get
  79. .calledWith(`clsiserver:${ctx.project_id}:${ctx.user_id}`)
  80. .should.equal(true)
  81. serverId.should.equal('clsi-7')
  82. })
  83. it('should _populateServerIdViaRequest if no key is found', async function (ctx) {
  84. ctx.ClsiCookieManager.promises._populateServerIdViaRequest = sinon
  85. .stub()
  86. .resolves()
  87. ctx.redis.get.resolves(null)
  88. await ctx.ClsiCookieManager.promises.getServerId(
  89. ctx.project_id,
  90. ctx.user_id,
  91. ''
  92. )
  93. ctx.ClsiCookieManager.promises._populateServerIdViaRequest
  94. .calledWith(ctx.project_id, ctx.user_id)
  95. .should.equal(true)
  96. })
  97. it('should _populateServerIdViaRequest if no key is blank', async function (ctx) {
  98. ctx.ClsiCookieManager.promises._populateServerIdViaRequest = sinon
  99. .stub()
  100. .resolves(null)
  101. ctx.redis.get.resolves('')
  102. await ctx.ClsiCookieManager.promises.getServerId(
  103. ctx.project_id,
  104. ctx.user_id,
  105. '',
  106. 'n2d'
  107. )
  108. ctx.ClsiCookieManager.promises._populateServerIdViaRequest
  109. .calledWith(ctx.project_id, ctx.user_id)
  110. .should.equal(true)
  111. })
  112. })
  113. describe('_populateServerIdViaRequest', function () {
  114. beforeEach(function (ctx) {
  115. ctx.clsiServerId = 'server-id'
  116. ctx.ClsiCookieManager.promises.setServerId = sinon.stub().resolves()
  117. })
  118. describe('with a server id in the response', function () {
  119. beforeEach(function (ctx) {
  120. ctx.response = {
  121. headers: {
  122. 'set-cookie': [
  123. `${ctx.settings.clsiCookie.key}=${ctx.clsiServerId}`,
  124. ],
  125. },
  126. }
  127. ctx.fetchUtils.fetchNothing.returns(ctx.response)
  128. })
  129. it('should make a request to the clsi', async function (ctx) {
  130. await ctx.ClsiCookieManager.promises._populateServerIdViaRequest(
  131. ctx.project_id,
  132. ctx.user_id,
  133. 'standard',
  134. 'n2d'
  135. )
  136. const args = ctx.ClsiCookieManager.promises.setServerId.args[0]
  137. args[0].should.equal(ctx.project_id)
  138. args[1].should.equal(ctx.user_id)
  139. args[2].should.equal('standard')
  140. args[3].should.equal('n2d')
  141. args[4].should.deep.equal(ctx.clsiServerId)
  142. })
  143. it('should return the server id', async function (ctx) {
  144. const serverId =
  145. await ctx.ClsiCookieManager.promises._populateServerIdViaRequest(
  146. ctx.project_id,
  147. ctx.user_id,
  148. '',
  149. 'n2d'
  150. )
  151. serverId.should.equal(ctx.clsiServerId)
  152. })
  153. })
  154. describe('without a server id in the response', function () {
  155. beforeEach(function (ctx) {
  156. ctx.response = { headers: {} }
  157. ctx.fetchUtils.fetchNothing.returns(ctx.response)
  158. })
  159. it('should not set the server id there is no server id in the response', async function (ctx) {
  160. ctx.ClsiCookieManager._parseServerIdFromResponse = sinon
  161. .stub()
  162. .returns(null)
  163. await ctx.ClsiCookieManager.promises.setServerId(
  164. ctx.project_id,
  165. ctx.user_id,
  166. 'standard',
  167. 'n2d',
  168. ctx.clsiServerId,
  169. null
  170. )
  171. ctx.redis.setex.called.should.equal(false)
  172. })
  173. })
  174. })
  175. describe('clearServerId', function () {
  176. it('should clear both keys', async function (ctx) {
  177. await ctx.ClsiCookieManager.promises.clearServerId(
  178. ctx.project_id,
  179. ctx.user_id,
  180. 'n2d'
  181. )
  182. ctx.redis.del.should.have.been.calledWith(
  183. `clsiserver:n2d:${ctx.project_id}:${ctx.user_id}`,
  184. `clsiserver:${ctx.project_id}:${ctx.user_id}`
  185. )
  186. })
  187. })
  188. describe('setServerId', function () {
  189. beforeEach(function (ctx) {
  190. ctx.clsiServerId = 'server-id'
  191. ctx.ClsiCookieManager._parseServerIdFromResponse = sinon
  192. .stub()
  193. .returns('clsi-8')
  194. })
  195. it('should set the server id with a ttl', async function (ctx) {
  196. await ctx.ClsiCookieManager.promises.setServerId(
  197. ctx.project_id,
  198. ctx.user_id,
  199. 'standard',
  200. 'n2d',
  201. ctx.clsiServerId,
  202. null
  203. )
  204. ctx.redis.setex.should.have.been.calledWith(
  205. `clsiserver:n2d:${ctx.project_id}:${ctx.user_id}`,
  206. ctx.settings.clsiCookie.ttlInSeconds,
  207. ctx.clsiServerId
  208. )
  209. })
  210. it('should set the server id with the regular ttl for reg instance', async function (ctx) {
  211. ctx.clsiServerId = 'clsi-reg-8'
  212. await ctx.ClsiCookieManager.promises.setServerId(
  213. ctx.project_id,
  214. ctx.user_id,
  215. 'standard',
  216. 'n2d',
  217. ctx.clsiServerId,
  218. null
  219. )
  220. expect(ctx.redis.setex).to.have.been.calledWith(
  221. `clsiserver:n2d:${ctx.project_id}:${ctx.user_id}`,
  222. ctx.settings.clsiCookie.ttlInSecondsRegular,
  223. ctx.clsiServerId
  224. )
  225. })
  226. describe('when clsiCookies are not enabled', function (ctx) {
  227. let oldKey
  228. beforeEach(async function (ctx) {
  229. oldKey = ctx.settings.clsiCookie.key
  230. delete ctx.settings.clsiCookie.key
  231. vi.resetModules()
  232. ctx.ClsiCookieManager2 = (await import(modulePath)).default()
  233. })
  234. afterEach(function (ctx) {
  235. ctx.settings.clsiCookie.key = oldKey
  236. })
  237. it('should not set the server id if clsiCookies are not enabled', async function (ctx) {
  238. await ctx.ClsiCookieManager2.promises.setServerId(
  239. ctx.project_id,
  240. ctx.user_id,
  241. 'standard',
  242. 'n2d',
  243. ctx.clsiServerId,
  244. null
  245. )
  246. ctx.redis.setex.called.should.equal(false)
  247. })
  248. })
  249. it('should also set in the secondary if secondary redis is enabled', async function (ctx) {
  250. ctx.redis_secondary = { setex: sinon.stub().resolves() }
  251. ctx.settings.redis.clsi_cookie_secondary = {}
  252. ctx.RedisWrapper.client = sinon.stub()
  253. ctx.RedisWrapper.client.withArgs('clsi_cookie').returns(ctx.redis)
  254. ctx.RedisWrapper.client
  255. .withArgs('clsi_cookie_secondary')
  256. .returns(ctx.redis_secondary)
  257. vi.resetModules()
  258. ctx.ClsiCookieManager2 = (await import(modulePath)).default()
  259. ctx.ClsiCookieManager2._parseServerIdFromResponse = sinon
  260. .stub()
  261. .returns('clsi-8')
  262. await ctx.ClsiCookieManager2.promises.setServerId(
  263. ctx.project_id,
  264. ctx.user_id,
  265. 'standard',
  266. 'n2d',
  267. ctx.clsiServerId,
  268. null
  269. )
  270. ctx.redis_secondary.setex.should.have.been.calledWith(
  271. `clsiserver:n2d:${ctx.project_id}:${ctx.user_id}`,
  272. ctx.settings.clsiCookie.ttlInSeconds,
  273. ctx.clsiServerId
  274. )
  275. })
  276. describe('checkIsLoadSheddingEvent', function () {
  277. beforeEach(function (ctx) {
  278. ctx.fetchUtils.fetchStringWithResponse.reset()
  279. ctx.call = async () => {
  280. await ctx.ClsiCookieManager.promises.setServerId(
  281. ctx.project_id,
  282. ctx.user_id,
  283. 'standard',
  284. 'n2d',
  285. ctx.clsiServerId,
  286. 'previous-clsi-server-id'
  287. )
  288. expect(
  289. ctx.fetchUtils.fetchStringWithResponse
  290. ).to.have.been.calledWith(
  291. `${ctx.settings.apis.clsi.url}/instance-state?clsiserverid=previous-clsi-server-id&compileGroup=standard&compileBackendClass=n2d`,
  292. { method: 'GET', signal: sinon.match.instanceOf(AbortSignal) }
  293. )
  294. }
  295. })
  296. it('should report "load-shedding" when previous is UP', async function (ctx) {
  297. ctx.fetchUtils.fetchStringWithResponse.resolves({
  298. response: { status: 200 },
  299. body: 'previous-clsi-server-id,UP\n',
  300. })
  301. await ctx.call()
  302. expect(ctx.metrics.inc).to.have.been.calledWith(
  303. 'clsi-lb-switch-backend',
  304. 1,
  305. { status: 'load-shedding' }
  306. )
  307. })
  308. it('should report "cycle" when other is UP', async function (ctx) {
  309. ctx.fetchUtils.fetchStringWithResponse.resolves({
  310. response: { status: 200 },
  311. body: 'other-clsi-server-id,UP\n',
  312. })
  313. await ctx.call()
  314. expect(ctx.metrics.inc).to.have.been.calledWith(
  315. 'clsi-lb-switch-backend',
  316. 1,
  317. { status: 'cycle' }
  318. )
  319. })
  320. it('should report "cycle" when previous is 404', async function (ctx) {
  321. ctx.fetchUtils.fetchStringWithResponse.resolves({
  322. response: { status: 404 },
  323. })
  324. await ctx.call()
  325. expect(ctx.metrics.inc).to.have.been.calledWith(
  326. 'clsi-lb-switch-backend',
  327. 1,
  328. { status: 'cycle' }
  329. )
  330. })
  331. })
  332. })
  333. })