|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * The OpenSearch Contributors require contributions made to |
| 6 | + * this file be licensed under the Apache-2.0 license or a |
| 7 | + * compatible open source license. |
| 8 | + */ |
| 9 | + |
| 10 | +import { Logger, Probot } from 'probot'; |
| 11 | +import githubLabelCanaryMonitor, { LabelCanaryMonitorParams } from '../../src/call/github-label-canary-monitor'; |
| 12 | +import { CloudWatchClient, PutMetricDataCommand } from '@aws-sdk/client-cloudwatch'; |
| 13 | + |
| 14 | +jest.mock('@aws-sdk/client-cloudwatch'); |
| 15 | + |
| 16 | +describe('githubEventsToS3', () => { |
| 17 | + let app: Probot; |
| 18 | + let context: any; |
| 19 | + let mockCloudWatchClient: any; |
| 20 | + let args: LabelCanaryMonitorParams; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + args = { |
| 24 | + text: 'testMetric', |
| 25 | + }; |
| 26 | + |
| 27 | + app = new Probot({ appId: 1, secret: 'test', privateKey: 'test' }); |
| 28 | + app.log = { |
| 29 | + info: jest.fn(), |
| 30 | + error: jest.fn(), |
| 31 | + } as unknown as Logger; |
| 32 | + |
| 33 | + context = { |
| 34 | + name: 'name', |
| 35 | + id: 'id', |
| 36 | + payload: { |
| 37 | + repository: { |
| 38 | + name: 'repo', |
| 39 | + owner: { login: 'org' }, |
| 40 | + private: false, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }; |
| 44 | + |
| 45 | + mockCloudWatchClient = { |
| 46 | + send: jest.fn(), |
| 47 | + }; |
| 48 | + (CloudWatchClient as jest.Mock).mockImplementation(() => mockCloudWatchClient); |
| 49 | + }); |
| 50 | + |
| 51 | + afterEach(() => { |
| 52 | + jest.clearAllMocks(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should publish CloudWatch metric if event is label canary', async () => { |
| 56 | + context = { |
| 57 | + name: 'label', |
| 58 | + id: 'id', |
| 59 | + payload: { |
| 60 | + action: 'deleted', |
| 61 | + label: { |
| 62 | + name: 's3-data-lake-app-canary-label', |
| 63 | + }, |
| 64 | + repository: { |
| 65 | + name: 'opensearch-metrics', |
| 66 | + private: false, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }; |
| 70 | + |
| 71 | + mockCloudWatchClient.send.mockResolvedValue({}); |
| 72 | + |
| 73 | + await githubLabelCanaryMonitor(app, context, args); |
| 74 | + |
| 75 | + expect(mockCloudWatchClient.send).toHaveBeenCalledWith(expect.any(PutMetricDataCommand)); |
| 76 | + expect(app.log.info).toHaveBeenCalledWith('CloudWatch metric for monitoring published.testMetric'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should not publish CloudWatch metric if event is not label canary', async () => { |
| 80 | + context = { |
| 81 | + name: 'label', |
| 82 | + id: 'id', |
| 83 | + payload: { |
| 84 | + label: { |
| 85 | + name: 'normal-label', |
| 86 | + }, |
| 87 | + repository: { |
| 88 | + name: 'opensearch-metrics', |
| 89 | + private: false, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }; |
| 93 | + |
| 94 | + mockCloudWatchClient.send.mockResolvedValue({}); |
| 95 | + |
| 96 | + await githubLabelCanaryMonitor(app, context, args); |
| 97 | + |
| 98 | + expect(mockCloudWatchClient.send).not.toHaveBeenCalledWith(expect.any(PutMetricDataCommand)); |
| 99 | + expect(app.log.info).not.toHaveBeenCalledWith('CloudWatch metric for monitoring published.testMetric'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should log an error if CloudWatch metric publishing fails', async () => { |
| 103 | + context = { |
| 104 | + name: 'label', |
| 105 | + id: 'id', |
| 106 | + payload: { |
| 107 | + action: 'deleted', |
| 108 | + label: { |
| 109 | + name: 's3-data-lake-app-canary-label', |
| 110 | + }, |
| 111 | + repository: { |
| 112 | + name: 'opensearch-metrics', |
| 113 | + private: false, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }; |
| 117 | + |
| 118 | + mockCloudWatchClient.send.mockRejectedValue(new Error('CloudWatch error')); |
| 119 | + |
| 120 | + await githubLabelCanaryMonitor(app, context, args); |
| 121 | + |
| 122 | + expect(app.log.error).toHaveBeenCalledWith('Error Publishing CloudWatch metric for monitoring : Error: CloudWatch error'); |
| 123 | + }); |
| 124 | +}); |
0 commit comments