|
| 1 | +import { CoreV1Api, V1Service } from '@kubernetes/client-node' |
| 2 | +import { getCloudttyActiveTime, getLogTime, mergeCanaryServices, toK8sService } from './k8s-operations' |
| 3 | + |
| 4 | +// Mock the KubeConfig |
| 5 | +jest.mock('@kubernetes/client-node', () => { |
| 6 | + const actual = jest.requireActual('@kubernetes/client-node') |
| 7 | + return { |
| 8 | + ...actual, |
| 9 | + KubeConfig: jest.fn().mockImplementation(() => ({ |
| 10 | + loadFromDefault: jest.fn(), |
| 11 | + makeApiClient: jest.fn((apiClientType) => { |
| 12 | + if (apiClientType === actual.CoreV1Api) { |
| 13 | + return new actual.CoreV1Api() |
| 14 | + } |
| 15 | + return {} |
| 16 | + }), |
| 17 | + })), |
| 18 | + } |
| 19 | +}) |
| 20 | + |
| 21 | +const makeService = (overrides: Partial<V1Service> = {}): V1Service => ({ |
| 22 | + metadata: { name: 'my-svc', labels: {} }, |
| 23 | + spec: { type: 'ClusterIP', ports: [{ port: 8080 }] }, |
| 24 | + ...overrides, |
| 25 | +}) |
| 26 | + |
| 27 | +describe('toK8sService', () => { |
| 28 | + test('maps a regular service', () => { |
| 29 | + const result = toK8sService(makeService()) |
| 30 | + expect(result).toEqual({ name: 'my-svc', ports: [8080], managedByKnative: false }) |
| 31 | + }) |
| 32 | + |
| 33 | + test('returns the raw service name', () => { |
| 34 | + const svc = makeService({ metadata: { name: 'my-svc-v1', labels: {} } }) |
| 35 | + expect(toK8sService(svc)?.name).toBe('my-svc-v1') |
| 36 | + }) |
| 37 | + |
| 38 | + test('filters out knative private services', () => { |
| 39 | + const svc = makeService({ |
| 40 | + metadata: { name: 'private-svc', labels: { 'networking.internal.knative.dev/serviceType': 'Private' } }, |
| 41 | + }) |
| 42 | + expect(toK8sService(svc)).toBeNull() |
| 43 | + }) |
| 44 | + |
| 45 | + test('filters out ClusterIP knative revision services', () => { |
| 46 | + const svc = makeService({ |
| 47 | + metadata: { name: 'revision-svc', labels: { 'serving.knative.dev/service': 'my-ksvc' } }, |
| 48 | + spec: { type: 'ClusterIP', ports: [{ port: 80 }] }, |
| 49 | + }) |
| 50 | + expect(toK8sService(svc)).toBeNull() |
| 51 | + }) |
| 52 | + |
| 53 | + test('maps ExternalName knative service and sets managedByKnative', () => { |
| 54 | + const svc = makeService({ |
| 55 | + metadata: { name: 'external-svc', labels: { 'serving.knative.dev/service': 'my-ksvc' } }, |
| 56 | + spec: { type: 'ExternalName', ports: [{ port: 80 }] }, |
| 57 | + }) |
| 58 | + const result = toK8sService(svc) |
| 59 | + expect(result).toEqual({ name: 'my-ksvc', ports: [80], managedByKnative: true }) |
| 60 | + }) |
| 61 | +}) |
| 62 | + |
| 63 | +describe('mergeCanaryServices', () => { |
| 64 | + test('returns services unchanged when no canary variants present', () => { |
| 65 | + const services = [ |
| 66 | + { name: 'svc-a', ports: [80], managedByKnative: false }, |
| 67 | + { name: 'svc-b', ports: [8080], managedByKnative: false }, |
| 68 | + ] |
| 69 | + expect(mergeCanaryServices(services)).toEqual(services) |
| 70 | + }) |
| 71 | + |
| 72 | + test('groups -v1 and -v2 variants into a single entry with the base name', () => { |
| 73 | + const services = [ |
| 74 | + { name: 'my-svc-v1', ports: [80], managedByKnative: false }, |
| 75 | + { name: 'my-svc-v2', ports: [80], managedByKnative: false }, |
| 76 | + ] |
| 77 | + expect(mergeCanaryServices(services)).toEqual([{ name: 'my-svc', ports: [80], managedByKnative: false }]) |
| 78 | + }) |
| 79 | + |
| 80 | + test('does not strip suffix when only one variant exists', () => { |
| 81 | + const services = [{ name: 'my-svc-v1', ports: [80], managedByKnative: false }] |
| 82 | + expect(mergeCanaryServices(services)).toEqual([{ name: 'my-svc-v1', ports: [80], managedByKnative: false }]) |
| 83 | + }) |
| 84 | + |
| 85 | + test('retains the data from the -v1 variant', () => { |
| 86 | + const services = [ |
| 87 | + { name: 'my-svc-v1', ports: [80, 443], managedByKnative: true }, |
| 88 | + { name: 'my-svc-v2', ports: [8080], managedByKnative: false }, |
| 89 | + ] |
| 90 | + expect(mergeCanaryServices(services)).toEqual([{ name: 'my-svc', ports: [80, 443], managedByKnative: true }]) |
| 91 | + }) |
| 92 | +}) |
| 93 | + |
| 94 | +describe('getCloudttyLogTime', () => { |
| 95 | + test('should return the timestamp for a valid log timestamp', () => { |
| 96 | + const timestampMatch = ['[2023/10/10 00:00:00:0000]', '2023/10/10 00:00:00:0000'] |
| 97 | + const result = getLogTime(timestampMatch) |
| 98 | + const timestamp = new Date('2023-10-10T00:00:00.000').getTime() |
| 99 | + expect(result).toBe(timestamp) |
| 100 | + }) |
| 101 | + |
| 102 | + test('should return NaN for an invalid log timestamp', () => { |
| 103 | + const timestampMatch = ['[invalid-timestamp]', 'invalid-date invalid-time'] |
| 104 | + const result = getLogTime(timestampMatch) |
| 105 | + expect(result).toBeNaN() |
| 106 | + }) |
| 107 | +}) |
| 108 | + |
| 109 | +describe('getCloudttyActiveTime', () => { |
| 110 | + afterEach(() => { |
| 111 | + jest.clearAllMocks() |
| 112 | + }) |
| 113 | + |
| 114 | + test('should return the time difference if no clients', async () => { |
| 115 | + const namespace = 'test-namespace' |
| 116 | + const podName = 'test-pod' |
| 117 | + const log = '[2023/10/10 00:00:00:0000] [INFO] clients: 0' |
| 118 | + jest.spyOn(CoreV1Api.prototype, 'readNamespacedPodLog').mockResolvedValue(log) |
| 119 | + |
| 120 | + const result = await getCloudttyActiveTime(namespace, podName) |
| 121 | + expect(result).toBeGreaterThan(0) |
| 122 | + }) |
| 123 | + |
| 124 | + test('should return 0 if clients are connected', async () => { |
| 125 | + const namespace = 'test-namespace' |
| 126 | + const podName = 'test-pod' |
| 127 | + const log = '[2023/10/10 00:00:00:0000] [INFO] clients: 1' |
| 128 | + jest.spyOn(CoreV1Api.prototype, 'readNamespacedPodLog').mockResolvedValue(log) |
| 129 | + |
| 130 | + const result = await getCloudttyActiveTime(namespace, podName) |
| 131 | + expect(result).toBe(0) |
| 132 | + }) |
| 133 | + |
| 134 | + test('should return undefined if log does not contain client count', async () => { |
| 135 | + const namespace = 'test-namespace' |
| 136 | + const podName = 'test-pod' |
| 137 | + const log = '[2023/10/10 00:00:00:0000] [INFO] some other log message' |
| 138 | + jest.spyOn(CoreV1Api.prototype, 'readNamespacedPodLog').mockResolvedValue(log) |
| 139 | + |
| 140 | + const result = await getCloudttyActiveTime(namespace, podName) |
| 141 | + expect(result).toBeUndefined() |
| 142 | + }) |
| 143 | + |
| 144 | + test('should return undefined if log is empty', async () => { |
| 145 | + const namespace = 'test-namespace' |
| 146 | + const podName = 'test-pod' |
| 147 | + const log = '' |
| 148 | + jest.spyOn(CoreV1Api.prototype, 'readNamespacedPodLog').mockResolvedValue(log) |
| 149 | + |
| 150 | + const result = await getCloudttyActiveTime(namespace, podName) |
| 151 | + expect(result).toBeUndefined() |
| 152 | + }) |
| 153 | + |
| 154 | + test('should return undefined if an error occurs', async () => { |
| 155 | + const namespace = 'test-namespace' |
| 156 | + const podName = 'test-pod' |
| 157 | + jest.spyOn(CoreV1Api.prototype, 'readNamespacedPodLog').mockRejectedValue(new Error('test error')) |
| 158 | + |
| 159 | + const result = await getCloudttyActiveTime(namespace, podName) |
| 160 | + expect(result).toBeUndefined() |
| 161 | + }) |
| 162 | +}) |
0 commit comments