|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import {describe, it, before, after} from 'mocha'; |
| 16 | +import {generateId} from './common'; |
| 17 | +import {AppProfileOptions, Bigtable, Instance} from '../src'; |
| 18 | +import {AppProfile} from '../src'; |
| 19 | +import assert = require('assert'); |
| 20 | + |
| 21 | +describe('📦 App Profile', () => { |
| 22 | + const bigtable = new Bigtable(); |
| 23 | + |
| 24 | + describe('📦 Create a profile', () => { |
| 25 | + let instance: Instance; |
| 26 | + let clusterIds: string[]; |
| 27 | + |
| 28 | + // Creates an app profile and returns information containing the app profile response. |
| 29 | + async function createProfile( |
| 30 | + instance: Instance, |
| 31 | + options: AppProfileOptions |
| 32 | + ): Promise<AppProfile> { |
| 33 | + const appProfileId = generateId('app-profile'); |
| 34 | + await instance.createAppProfile(appProfileId, options); |
| 35 | + const appProfile = instance.appProfile(appProfileId); |
| 36 | + const getAppProfileResponse = await appProfile.get(); |
| 37 | + return getAppProfileResponse[0]; |
| 38 | + } |
| 39 | + |
| 40 | + before(async () => { |
| 41 | + // Creates an instance with clusters |
| 42 | + const instanceClusters = [ |
| 43 | + 'us-east1-c', |
| 44 | + 'us-central1-b', |
| 45 | + 'us-west1-b', |
| 46 | + ].map(location => { |
| 47 | + return { |
| 48 | + id: generateId('cluster'), |
| 49 | + location, |
| 50 | + }; |
| 51 | + }); |
| 52 | + clusterIds = instanceClusters.map(cluster => cluster.id); |
| 53 | + const instanceId = generateId('instance'); |
| 54 | + instance = bigtable.instance(instanceId); |
| 55 | + const [, operation] = await instance.create({ |
| 56 | + clusters: instanceClusters.map(cluster => { |
| 57 | + return { |
| 58 | + ...cluster, |
| 59 | + nodes: 1, |
| 60 | + }; |
| 61 | + }), |
| 62 | + labels: { |
| 63 | + time_created: Date.now(), |
| 64 | + }, |
| 65 | + }); |
| 66 | + await operation.promise(); |
| 67 | + }); |
| 68 | + |
| 69 | + after(async () => { |
| 70 | + await instance.delete(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should create a profile with a single cluster', async () => { |
| 74 | + const options = { |
| 75 | + routing: instance.cluster(clusterIds[1]), |
| 76 | + }; |
| 77 | + const appProfile = await createProfile(instance, options); |
| 78 | + assert.deepStrictEqual( |
| 79 | + appProfile.metadata?.singleClusterRouting?.clusterId, |
| 80 | + options.routing.id |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should create a profile with multiple clusters', async () => { |
| 85 | + const options = { |
| 86 | + routing: new Set([ |
| 87 | + instance.cluster(clusterIds[1]), |
| 88 | + instance.cluster(clusterIds[2]), |
| 89 | + ]), |
| 90 | + }; |
| 91 | + const appProfile = await createProfile(instance, options); |
| 92 | + assert.deepStrictEqual( |
| 93 | + new Set(appProfile.metadata?.multiClusterRoutingUseAny?.clusterIds), |
| 94 | + new Set([...options.routing].map(cluster => cluster.id)) |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should create a profile with multiple clusters using strings', async () => { |
| 99 | + const options = { |
| 100 | + routing: new Set([clusterIds[1], clusterIds[2]]), |
| 101 | + }; |
| 102 | + const appProfile = await createProfile(instance, options); |
| 103 | + assert.deepStrictEqual( |
| 104 | + new Set(appProfile.metadata?.multiClusterRoutingUseAny?.clusterIds), |
| 105 | + new Set([...options.routing]) |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should create a profile with no clusters', async () => { |
| 110 | + const options: {routing: 'any'} = { |
| 111 | + routing: 'any', |
| 112 | + }; |
| 113 | + const appProfile = await createProfile(instance, options); |
| 114 | + assert.deepStrictEqual( |
| 115 | + appProfile.metadata?.multiClusterRoutingUseAny?.clusterIds, |
| 116 | + [] |
| 117 | + ); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should ensure clusters match an updated profile', async () => { |
| 121 | + const options = { |
| 122 | + routing: instance.cluster(clusterIds[1]), |
| 123 | + }; |
| 124 | + const appProfile = await createProfile(instance, options); |
| 125 | + assert.deepStrictEqual( |
| 126 | + appProfile.metadata?.singleClusterRouting?.clusterId, |
| 127 | + clusterIds[1] |
| 128 | + ); |
| 129 | + const newOptions = { |
| 130 | + routing: new Set([ |
| 131 | + instance.cluster(clusterIds[1]), |
| 132 | + instance.cluster(clusterIds[2]), |
| 133 | + ]), |
| 134 | + }; |
| 135 | + await appProfile.setMetadata(newOptions); |
| 136 | + const appProfileAfterUpdate = (await appProfile.get())[0]; |
| 137 | + assert.deepStrictEqual( |
| 138 | + new Set( |
| 139 | + appProfileAfterUpdate.metadata?.multiClusterRoutingUseAny?.clusterIds |
| 140 | + ), |
| 141 | + new Set([...newOptions.routing].map(cluster => cluster.id)) |
| 142 | + ); |
| 143 | + }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments