|
| 1 | +/* |
| 2 | + * Copyright Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { Entity } from '@backstage/catalog-model'; |
| 18 | +import { SchedulerServiceTaskRunner } from '@backstage/backend-plugin-api'; |
| 19 | +import { BaseEntityProvider } from './BaseEntityProvider'; |
| 20 | +import { JsonFileData } from '../types'; |
| 21 | + |
| 22 | +class TestEntityProvider extends BaseEntityProvider<Entity> { |
| 23 | + getProviderName(): string { |
| 24 | + return 'test-entity-provider'; |
| 25 | + } |
| 26 | + |
| 27 | + getKind(): string { |
| 28 | + return 'Plugin'; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +const taskRunner: SchedulerServiceTaskRunner = { |
| 33 | + run: jest.fn(async ({ fn }) => fn()), |
| 34 | +}; |
| 35 | + |
| 36 | +const createEntity = (overrides?: Partial<Entity>): Entity => ({ |
| 37 | + apiVersion: 'extensions.backstage.io/v1alpha1', |
| 38 | + kind: 'Plugin', |
| 39 | + metadata: { |
| 40 | + name: 'duplicate-plugin', |
| 41 | + ...overrides?.metadata, |
| 42 | + }, |
| 43 | + spec: { |
| 44 | + owner: 'test-owner', |
| 45 | + ...(overrides?.spec as object), |
| 46 | + }, |
| 47 | + ...overrides, |
| 48 | +}); |
| 49 | + |
| 50 | +const createFileData = ( |
| 51 | + filePath: string, |
| 52 | + entity: Entity, |
| 53 | +): JsonFileData<Entity> => ({ |
| 54 | + filePath, |
| 55 | + content: entity, |
| 56 | +}); |
| 57 | + |
| 58 | +describe('BaseEntityProvider collision policy', () => { |
| 59 | + beforeEach(() => { |
| 60 | + jest.spyOn(console, 'warn').mockImplementation(() => undefined); |
| 61 | + }); |
| 62 | + |
| 63 | + afterEach(() => { |
| 64 | + jest.restoreAllMocks(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('keeps first definition when duplicate entities are equivalent', () => { |
| 68 | + const provider = new TestEntityProvider(taskRunner); |
| 69 | + const duplicate = createEntity(); |
| 70 | + |
| 71 | + const entities = provider.getEntities([ |
| 72 | + createFileData('/extensions/primary/plugin.yaml', duplicate), |
| 73 | + createFileData('/extensions/extra/community/plugin.yaml', duplicate), |
| 74 | + ]); |
| 75 | + |
| 76 | + expect(entities).toHaveLength(1); |
| 77 | + expect(console.warn).toHaveBeenCalledWith( |
| 78 | + expect.stringContaining( |
| 79 | + "Skipping duplicate Extensions entity 'plugin/default/duplicate-plugin'", |
| 80 | + ), |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it('throws when duplicate entities have conflicting definitions', () => { |
| 85 | + const provider = new TestEntityProvider(taskRunner); |
| 86 | + const firstEntity = createEntity({ |
| 87 | + spec: { owner: 'owner-a' }, |
| 88 | + }); |
| 89 | + const secondEntity = createEntity({ |
| 90 | + spec: { owner: 'owner-b' }, |
| 91 | + }); |
| 92 | + |
| 93 | + expect(() => |
| 94 | + provider.getEntities([ |
| 95 | + createFileData('/extensions/primary/plugin.yaml', firstEntity), |
| 96 | + createFileData('/extensions/extra/community/plugin.yaml', secondEntity), |
| 97 | + ]), |
| 98 | + ).toThrow( |
| 99 | + "Conflicting Extensions entities detected for 'plugin/default/duplicate-plugin'", |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it('keeps entities with same name when namespaces differ', () => { |
| 104 | + const provider = new TestEntityProvider(taskRunner); |
| 105 | + const defaultNamespaceEntity = createEntity({ |
| 106 | + metadata: { name: 'shared-name' }, |
| 107 | + }); |
| 108 | + const customNamespaceEntity = createEntity({ |
| 109 | + metadata: { name: 'shared-name', namespace: 'community' }, |
| 110 | + }); |
| 111 | + |
| 112 | + const entities = provider.getEntities([ |
| 113 | + createFileData( |
| 114 | + '/extensions/primary/plugin-default.yaml', |
| 115 | + defaultNamespaceEntity, |
| 116 | + ), |
| 117 | + createFileData( |
| 118 | + '/extensions/extra/community/plugin-custom.yaml', |
| 119 | + customNamespaceEntity, |
| 120 | + ), |
| 121 | + ]); |
| 122 | + |
| 123 | + expect(entities).toHaveLength(2); |
| 124 | + }); |
| 125 | +}); |
0 commit comments