|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2022 Firebase |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +import { expect } from 'chai'; |
| 24 | +import * as options from '../../../src/v2/options'; |
| 25 | +import * as eventarc from '../../../src/v2/providers/eventarc'; |
| 26 | +import { FULL_OPTIONS } from './fixtures'; |
| 27 | + |
| 28 | +const ENDPOINT_EVENT_TRIGGER = { |
| 29 | + eventType: 'event-type', |
| 30 | + retry: false, |
| 31 | + eventFilters: {}, |
| 32 | +}; |
| 33 | + |
| 34 | +describe('v2/eventarc', () => { |
| 35 | + describe('onCustomEventPublished', () => { |
| 36 | + beforeEach(() => { |
| 37 | + process.env.GCLOUD_PROJECT = 'aProject'; |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + options.setGlobalOptions({}); |
| 42 | + delete process.env.GCLOUD_PROJECT; |
| 43 | + }); |
| 44 | + |
| 45 | + it('should create a minimal trigger/endpoint with default channel', () => { |
| 46 | + const result = eventarc.onCustomEventPublished('event-type', () => 42); |
| 47 | + |
| 48 | + expect(result.__endpoint).to.deep.equal({ |
| 49 | + platform: 'gcfv2', |
| 50 | + labels: {}, |
| 51 | + eventTrigger: { |
| 52 | + ...ENDPOINT_EVENT_TRIGGER, |
| 53 | + channel: 'locations/us-central1/channels/firebase', |
| 54 | + }, |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should create a minimal trigger/endpoint with opts', () => { |
| 59 | + const result = eventarc.onCustomEventPublished( |
| 60 | + { eventType: 'event-type', region: 'us-west1' }, |
| 61 | + () => 42 |
| 62 | + ); |
| 63 | + |
| 64 | + expect(result.__endpoint).to.deep.equal({ |
| 65 | + platform: 'gcfv2', |
| 66 | + labels: {}, |
| 67 | + eventTrigger: { |
| 68 | + ...ENDPOINT_EVENT_TRIGGER, |
| 69 | + channel: 'locations/us-central1/channels/firebase', |
| 70 | + }, |
| 71 | + region: ['us-west1'], |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should create a minimal trigger with channel with opts', () => { |
| 76 | + const result = eventarc.onCustomEventPublished( |
| 77 | + { |
| 78 | + eventType: 'event-type', |
| 79 | + channel: 'locations/us-west1/channels/my-channel', |
| 80 | + filters: { foo: 'bar' }, |
| 81 | + }, |
| 82 | + () => 42 |
| 83 | + ); |
| 84 | + |
| 85 | + expect(result.__endpoint).to.deep.equal({ |
| 86 | + platform: 'gcfv2', |
| 87 | + labels: {}, |
| 88 | + eventTrigger: { |
| 89 | + ...ENDPOINT_EVENT_TRIGGER, |
| 90 | + channel: 'locations/us-west1/channels/my-channel', |
| 91 | + eventFilters: { |
| 92 | + foo: 'bar', |
| 93 | + }, |
| 94 | + }, |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should create a complex trigger/endpoint with appropriate values', () => { |
| 99 | + const result = eventarc.onCustomEventPublished( |
| 100 | + { |
| 101 | + ...FULL_OPTIONS, |
| 102 | + eventType: 'event-type', |
| 103 | + channel: 'locations/us-west1/channels/my-channel', |
| 104 | + }, |
| 105 | + () => 42 |
| 106 | + ); |
| 107 | + |
| 108 | + expect(result.__endpoint).to.deep.equal({ |
| 109 | + platform: 'gcfv2', |
| 110 | + region: ['us-west1'], |
| 111 | + availableMemoryMb: 512, |
| 112 | + timeoutSeconds: 60, |
| 113 | + minInstances: 1, |
| 114 | + maxInstances: 3, |
| 115 | + concurrency: 20, |
| 116 | + vpc: { |
| 117 | + connector: 'aConnector', |
| 118 | + egressSettings: 'ALL_TRAFFIC', |
| 119 | + }, |
| 120 | + serviceAccountEmail: 'root@', |
| 121 | + ingressSettings: 'ALLOW_ALL', |
| 122 | + labels: { |
| 123 | + hello: 'world', |
| 124 | + }, |
| 125 | + eventTrigger: { |
| 126 | + ...ENDPOINT_EVENT_TRIGGER, |
| 127 | + channel: 'locations/us-west1/channels/my-channel', |
| 128 | + }, |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should merge options and globalOptions', () => { |
| 133 | + options.setGlobalOptions({ |
| 134 | + concurrency: 20, |
| 135 | + region: 'europe-west1', |
| 136 | + minInstances: 1, |
| 137 | + }); |
| 138 | + |
| 139 | + const result = eventarc.onCustomEventPublished( |
| 140 | + { |
| 141 | + eventType: 'event-type', |
| 142 | + channel: 'locations/us-west1/channels/my-channel', |
| 143 | + region: 'us-west1', |
| 144 | + minInstances: 3, |
| 145 | + }, |
| 146 | + () => 42 |
| 147 | + ); |
| 148 | + |
| 149 | + expect(result.__endpoint).to.deep.equal({ |
| 150 | + platform: 'gcfv2', |
| 151 | + concurrency: 20, |
| 152 | + minInstances: 3, |
| 153 | + region: ['us-west1'], |
| 154 | + labels: {}, |
| 155 | + eventTrigger: { |
| 156 | + ...ENDPOINT_EVENT_TRIGGER, |
| 157 | + channel: 'locations/us-west1/channels/my-channel', |
| 158 | + }, |
| 159 | + }); |
| 160 | + }); |
| 161 | + }); |
| 162 | +}); |
0 commit comments