|
| 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 resource: any; |
| 20 | + let mockCloudWatchClient: any; |
| 21 | + let args: LabelCanaryMonitorParams; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + args = { |
| 25 | + nameSpace: 'testNameSpace', |
| 26 | + metricName: 'testMetric', |
| 27 | + value: '1', |
| 28 | + unit: 'Count', |
| 29 | + }; |
| 30 | + |
| 31 | + app = new Probot({ appId: 1, secret: 'test', privateKey: 'test' }); |
| 32 | + app.log = { |
| 33 | + info: jest.fn(), |
| 34 | + error: jest.fn(), |
| 35 | + } as unknown as Logger; |
| 36 | + |
| 37 | + context = { |
| 38 | + name: 'name', |
| 39 | + id: 'id', |
| 40 | + payload: { |
| 41 | + repository: { |
| 42 | + name: 'repo', |
| 43 | + owner: { login: 'org' }, |
| 44 | + private: false, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }; |
| 48 | + |
| 49 | + resource = { |
| 50 | + organizations: new Map([ |
| 51 | + [ |
| 52 | + 'org', |
| 53 | + { |
| 54 | + repositories: new Map([['repo', 'repo object']]), |
| 55 | + }, |
| 56 | + ], |
| 57 | + ]), |
| 58 | + }; |
| 59 | + |
| 60 | + mockCloudWatchClient = { |
| 61 | + send: jest.fn(), |
| 62 | + }; |
| 63 | + (CloudWatchClient as jest.Mock).mockImplementation(() => mockCloudWatchClient); |
| 64 | + }); |
| 65 | + |
| 66 | + afterEach(() => { |
| 67 | + jest.clearAllMocks(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should publish CloudWatch metric if event is label canary', async () => { |
| 71 | + context = { |
| 72 | + name: 'label', |
| 73 | + id: 'id', |
| 74 | + payload: { |
| 75 | + action: 'deleted', |
| 76 | + label: { |
| 77 | + name: 's3-data-lake-app-canary-label', |
| 78 | + }, |
| 79 | + repository: { |
| 80 | + name: 'opensearch-metrics', |
| 81 | + private: false, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }; |
| 85 | + |
| 86 | + mockCloudWatchClient.send.mockResolvedValue({}); |
| 87 | + |
| 88 | + await githubLabelCanaryMonitor(app, context, resource, args); |
| 89 | + |
| 90 | + expect(mockCloudWatchClient.send).toHaveBeenCalledWith(expect.any(PutMetricDataCommand)); |
| 91 | + expect(app.log.info).toHaveBeenCalledWith('CloudWatch metric for monitoring published.'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should not publish CloudWatch metric if event is not label canary', async () => { |
| 95 | + context = { |
| 96 | + name: 'label', |
| 97 | + id: 'id', |
| 98 | + payload: { |
| 99 | + label: { |
| 100 | + name: 'normal-label', |
| 101 | + }, |
| 102 | + repository: { |
| 103 | + name: 'opensearch-metrics', |
| 104 | + private: false, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }; |
| 108 | + |
| 109 | + mockCloudWatchClient.send.mockResolvedValue({}); |
| 110 | + |
| 111 | + await githubLabelCanaryMonitor(app, context, resource, args); |
| 112 | + |
| 113 | + expect(mockCloudWatchClient.send).not.toHaveBeenCalledWith(expect.any(PutMetricDataCommand)); |
| 114 | + expect(app.log.info).not.toHaveBeenCalledWith('CloudWatch metric for monitoring published.'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should log an error if CloudWatch metric publishing fails', async () => { |
| 118 | + context = { |
| 119 | + name: 'label', |
| 120 | + id: 'id', |
| 121 | + payload: { |
| 122 | + action: 'deleted', |
| 123 | + label: { |
| 124 | + name: 's3-data-lake-app-canary-label', |
| 125 | + }, |
| 126 | + repository: { |
| 127 | + name: 'opensearch-metrics', |
| 128 | + private: false, |
| 129 | + }, |
| 130 | + }, |
| 131 | + }; |
| 132 | + |
| 133 | + mockCloudWatchClient.send.mockRejectedValue(new Error('CloudWatch error')); |
| 134 | + |
| 135 | + await githubLabelCanaryMonitor(app, context, resource, args); |
| 136 | + |
| 137 | + expect(app.log.error).toHaveBeenCalledWith('Error Publishing CloudWatch metric for monitoring : Error: CloudWatch error'); |
| 138 | + }); |
| 139 | +}); |
0 commit comments