ClsiCookieManager.test.mjs 10.0 KB

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