|
| 1 | +import { KubernetesServiceProvidersService } from './kubernetes-service-providers.service'; |
| 2 | + |
| 3 | +const listClusterCustomObject = jest.fn(); |
| 4 | + |
| 5 | +jest.mock('@kubernetes/client-node', () => { |
| 6 | + class KubeConfig { |
| 7 | + loadFromDefault = jest.fn(); |
| 8 | + getCurrentCluster = jest |
| 9 | + .fn() |
| 10 | + .mockReturnValue({ server: 'https://k8s.example.com/base' }); |
| 11 | + makeApiClient = jest.fn().mockImplementation(() => ({ |
| 12 | + listClusterCustomObject, |
| 13 | + })); |
| 14 | + } |
| 15 | + class CustomObjectsApi {} |
| 16 | + return { KubeConfig, CustomObjectsApi }; |
| 17 | +}); |
| 18 | + |
| 19 | +jest.mock('@kubernetes/client-node/dist/gen/middleware.js', () => ({ |
| 20 | + PromiseMiddlewareWrapper: class { |
| 21 | + pre?: (ctx: any) => Promise<any> | any; |
| 22 | + post?: (ctx: any) => Promise<any> | any; |
| 23 | + constructor(opts: any) { |
| 24 | + this.pre = opts.pre; |
| 25 | + this.post = opts.post; |
| 26 | + } |
| 27 | + }, |
| 28 | +})); |
| 29 | + |
| 30 | +describe('KubernetesServiceProvidersService', () => { |
| 31 | + beforeEach(() => { |
| 32 | + jest.resetAllMocks(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should return empty list when API returns no items', async () => { |
| 36 | + listClusterCustomObject.mockImplementation( |
| 37 | + async (_gvr: any, _opts: any) => { |
| 38 | + return {}; |
| 39 | + }, |
| 40 | + ); |
| 41 | + |
| 42 | + const svc = new KubernetesServiceProvidersService(); |
| 43 | + const res = await svc.getServiceProviders('token', [], { |
| 44 | + organization: 'org', |
| 45 | + }); |
| 46 | + expect(res.rawServiceProviders).toEqual([]); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should map items to contentConfiguration and fill url from spec when missing', async () => { |
| 50 | + let capturedUrl = ''; |
| 51 | + listClusterCustomObject.mockImplementation(async (_gvr: any, opts: any) => { |
| 52 | + const mw = opts?.middleware?.[0]; |
| 53 | + const ctx = { |
| 54 | + _url: 'https://k8s.example.com/base', |
| 55 | + getUrl() { |
| 56 | + return this._url; |
| 57 | + }, |
| 58 | + setUrl(u: string) { |
| 59 | + this._url = u; |
| 60 | + }, |
| 61 | + }; |
| 62 | + if (mw?.pre) await mw.pre(ctx); |
| 63 | + capturedUrl = ctx._url; |
| 64 | + return { |
| 65 | + items: [ |
| 66 | + { |
| 67 | + status: { configurationResult: JSON.stringify({}) }, |
| 68 | + spec: { |
| 69 | + remoteConfiguration: { url: 'http://fallback.example/app' }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + ], |
| 73 | + }; |
| 74 | + }); |
| 75 | + |
| 76 | + const svc = new KubernetesServiceProvidersService(); |
| 77 | + const res = await svc.getServiceProviders('token', ['main'], { |
| 78 | + organization: 'acme', |
| 79 | + account: 'a1', |
| 80 | + }); |
| 81 | + |
| 82 | + expect(res.rawServiceProviders[0].contentConfiguration).toHaveLength(1); |
| 83 | + expect(res.rawServiceProviders[0].contentConfiguration[0].url).toBe( |
| 84 | + 'http://fallback.example/app', |
| 85 | + ); |
| 86 | + |
| 87 | + expect(capturedUrl).toContain( |
| 88 | + '/clusters/root:orgs:acme:a1/apis/core.openmfp.io/v1alpha1/contentconfigurations', |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should retry once on HTTP 429 and log retry message', async () => { |
| 93 | + jest.useFakeTimers(); |
| 94 | + const sequence: any[] = [ |
| 95 | + Object.assign(new Error('Too Many Requests'), { code: 429 }), |
| 96 | + { items: [] }, |
| 97 | + ]; |
| 98 | + listClusterCustomObject.mockImplementation(async () => { |
| 99 | + const next = sequence.shift(); |
| 100 | + if (next instanceof Error || next?.code === 429) { |
| 101 | + throw next; |
| 102 | + } |
| 103 | + return next; |
| 104 | + }); |
| 105 | + |
| 106 | + const logSpy = jest |
| 107 | + .spyOn(console, 'log') |
| 108 | + .mockImplementation(() => undefined as unknown as never); |
| 109 | + const errSpy = jest |
| 110 | + .spyOn(console, 'error') |
| 111 | + .mockImplementation(() => undefined as unknown as never); |
| 112 | + |
| 113 | + const svc = new KubernetesServiceProvidersService(); |
| 114 | + const promise = svc.getServiceProviders('token', [], { |
| 115 | + organization: 'org', |
| 116 | + }); |
| 117 | + |
| 118 | + await jest.advanceTimersByTimeAsync(1000); |
| 119 | + |
| 120 | + const res = await promise; |
| 121 | + expect(res.rawServiceProviders).toEqual([ |
| 122 | + { |
| 123 | + name: 'openmfp-system', |
| 124 | + displayName: '', |
| 125 | + creationTimestamp: '', |
| 126 | + contentConfiguration: [], |
| 127 | + }, |
| 128 | + ]); |
| 129 | + expect(logSpy).toHaveBeenCalledWith( |
| 130 | + 'Retry after 1 second reading kubernetes resources.', |
| 131 | + ); |
| 132 | + |
| 133 | + logSpy.mockRestore(); |
| 134 | + errSpy.mockRestore(); |
| 135 | + jest.useRealTimers(); |
| 136 | + }); |
| 137 | +}); |
0 commit comments