Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions workspaces/quay/plugins/quay/dev/__data__/entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Entity } from '@backstage/catalog-model';

export const mockQuayEntity: Entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'quay-instance',
description: 'Quay example instance',
annotations: {
'quay.io/repository-slug': 'backstage-test/test-images',
},
},
spec: {
lifecycle: 'production',
type: 'service',
owner: 'user:guest',
},
};

export const mockQuayInstanceDevelEntity: Entity = {
...mockQuayEntity,
metadata: {
...mockQuayEntity.metadata,
name: 'quay-instance-devel',
annotations: {
...mockQuayEntity.metadata.annotations,
'quay.io/instance-name': 'devel',
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,16 @@ export const v4securityDetails: SecurityDetailsResponse = {
} as Layer,
},
};

export const digestSecurityDetails: Record<string, SecurityDetailsResponse> = {
'sha256:29c96c750aa532d92d9cb56cad59159b7cc26b10e39ff4a895c28345d2cd775d':
v4securityDetails,
'sha256:79c96c750aa532d92d9cb56cad59159b7cc26b10e39ff4a895c28345d2cd775d':
v3securityDetails,
'sha256:89c96c750aa532d92d9cb56cad59159b7cc26b10e39ff4a895c28345d2cd775e':
v2securityDetails,
'sha256:99c96c750aa532d92d9cb56cad59159b7cc26b10e39ff4a895c28345d2cd775f':
v1securityDetails,
Comment thread
dzemanov marked this conversation as resolved.
'sha256:69c96c750aa532d92d9cb56cad59159b7cc26b10e39ff4a895c28345d2cd775c':
securityDetails,
};
103 changes: 52 additions & 51 deletions workspaces/quay/plugins/quay/dev/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,50 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// eslint-disable-next-line @backstage/no-ui-css-imports-in-non-frontend
import '@backstage/ui/css/styles.css';

import { Entity } from '@backstage/catalog-model';
import { createDevApp } from '@backstage/dev-utils';
import { EntityProvider } from '@backstage/plugin-catalog-react';
import { permissionApiRef } from '@backstage/plugin-permission-react';
import { mockApis, TestApiProvider } from '@backstage/test-utils';

import { quayApiRef, QuayApiV1, QuayInstanceConfig } from '../src/api';
import { QuayPage, quayPlugin } from '../src/plugin';
import { mockQuayEntity, mockQuayInstanceDevelEntity } from './__data__/entity';
import { labels } from './__data__/labels';
import { manifestDigest } from './__data__/manifest_digest';
import {
digestSecurityDetails,
securityDetails,
v1securityDetails,
v2securityDetails,
v3securityDetails,
v4securityDetails,
} from './__data__/security_vulnerabilities';
import { tags } from './__data__/tags';

const mockEntity: Entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'backstage',
description: 'backstage.io',
annotations: {
'quay.io/repository-slug': 'backstage-test/test-images',
},
},
spec: {
lifecycle: 'production',
type: 'service',
owner: 'user:guest',
},
};

export class MockQuayApiClient implements QuayApiV1 {
getQuayInstance(_?: string): QuayInstanceConfig | undefined {
getQuayInstance(instanceName?: string): QuayInstanceConfig | undefined {
if (instanceName === 'devel') {
return { name: 'devel', apiUrl: 'https://quay-devel.io' };
}

return { name: 'default', apiUrl: 'https://quay.io' };
}

async getTags() {
async getTags(instanceName?: string) {
if (instanceName === 'devel') {
return {
tags: [
{
...tags.tags[0],
name: 'v5-devel-only',
},
],
page: 1,
has_additional: false,
};
}

return tags;
}

Expand All @@ -68,35 +68,17 @@ export class MockQuayApiClient implements QuayApiV1 {
return manifestDigest;
}

async getSecurityDetails(_: string, __: string, ___: string, digest: string) {
if (
digest ===
'sha256:79c96c750aa532d92d9cb56cad59159b7cc26b10e39ff4a895c28345d2cd775d'
) {
return v3securityDetails;
async getSecurityDetails(
instanceName: string | undefined,
_: string,
__: string,
digest: string,
) {
if (instanceName === 'devel') {
return { ...v1securityDetails };
}

if (
digest ===
'sha256:89c96c750aa532d92d9cb56cad59159b7cc26b10e39ff4a895c28345d2cd775e'
) {
return v2securityDetails;
}
if (
digest ===
'sha256:99c96c750aa532d92d9cb56cad59159b7cc26b10e39ff4a895c28345d2cd775f'
) {
return v1securityDetails;
}

if (
digest ===
'sha256:29c96c750aa532d92d9cb56cad59159b7cc26b10e39ff4a895c28345d2cd775d'
) {
return v4securityDetails;
}

return securityDetails;
return digestSecurityDetails[digest] ?? securityDetails;
}
}

Expand All @@ -110,12 +92,31 @@ createDevApp()
[permissionApiRef, mockApis.permission()],
]}
>
<EntityProvider entity={mockEntity}>
<EntityProvider entity={mockQuayEntity}>
<QuayPage />
</EntityProvider>
</TestApiProvider>
),
title: 'Root Page',
path: '/quay',
})
.addPage({
element: (
<TestApiProvider
apis={[
[quayApiRef, new MockQuayApiClient()],
[permissionApiRef, mockApis.permission()],
]}
>
<EntityProvider
key="multi-instance"
entity={mockQuayInstanceDevelEntity}
>
<QuayPage />
</EntityProvider>
</TestApiProvider>
),
title: 'Multi-instance',
path: '/quay/multi-instance',
})
.render();
21 changes: 21 additions & 0 deletions workspaces/quay/plugins/quay/src/api/index.backend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,27 @@ describe('QuayApiClient-Backend', () => {
);
});

it('should throw when a non-existent instance is requested', async () => {
quayApi = QuayApiClient.fromConfig({
configApi: mockApis.config({
data: {
quay: {
instances: [
{ name: 'devel', apiUrl: 'https://quay.devel.example.io' },
{ name: 'staging', apiUrl: 'https://quay.staging.example.io' },
],
},
},
}),
discoveryApi: mockDiscoveryApi,
identityApi: identityApi,
});

await expect(quayApi.getTags('unknown', 'foo', 'bar')).rejects.toEqual(
new Error('Quay instance "unknown" not found in configuration.'),
);
});

it('should throw an error when the response is not ok', async () => {
await expect(quayApi.getTags(undefined, 'not', 'found')).rejects.toEqual(
new Error('failed to fetch data, status 404: Not Found'),
Expand Down
52 changes: 52 additions & 0 deletions workspaces/quay/plugins/quay/src/api/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,58 @@ describe('QuayApiClient', () => {
);
});

it('should throw when single-instance and multi-instance configs are mixed', () => {
expect(() =>
QuayApiClient.fromConfig({
configApi: mockApis.config({
data: {
quay: {
apiUrl: 'https://quay.io',
instances: [{ name: 'devel' }],
},
},
}),
discoveryApi: mockDiscoveryApi,
identityApi: identityApi,
}),
).toThrow(
'Invalid Quay configuration: Cannot use both "quay.instances" (multi-instance) and "quay.apiUrl", "quay.proxyPath", "quay.uiUrl" (single-instance) at the same time.',
);
});

it('should throw when a non-existent instance is requested', async () => {
quayApi = QuayApiClient.fromConfig({
configApi: mockApis.config({
data: {
quay: {
instances: [{ name: 'devel' }, { name: 'staging' }],
},
},
}),
discoveryApi: mockDiscoveryApi,
identityApi: identityApi,
});

await expect(quayApi.getTags('unknown', 'foo', 'bar')).rejects.toEqual(
new Error('Quay instance "unknown" not found in configuration.'),
);
await expect(
quayApi.getLabels('unknown', 'foo', 'bar', 'sha256:123'),
).rejects.toEqual(
new Error('Quay instance "unknown" not found in configuration.'),
);
await expect(
quayApi.getManifestByDigest('unknown', 'foo', 'bar', 'sha256:123'),
).rejects.toEqual(
new Error('Quay instance "unknown" not found in configuration.'),
);
await expect(
quayApi.getSecurityDetails('unknown', 'foo', 'bar', 'sha256:123'),
).rejects.toEqual(
new Error('Quay instance "unknown" not found in configuration.'),
);
});

it('should throw an error when the response is not ok', async () => {
await expect(
quayApi.getTags(QUAY_SINGLE_INSTANCE_NAME, 'not', 'found'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ const mockUsePermission = usePermission as jest.MockedFunction<
>;

const mockQuayApi: Partial<QuayApiV1> = {
getQuayInstance: jest.fn().mockReturnValue({
name: 'default',
apiUrl: 'https://quay.example.com',
}),
getQuayInstance: jest.fn(),
};

describe('QuayRepository', () => {
Expand All @@ -60,6 +57,10 @@ describe('QuayRepository', () => {

beforeEach(() => {
mockUsePermission.mockReturnValue({ loading: false, allowed: true });
(mockQuayApi.getQuayInstance as jest.Mock).mockReturnValue({
name: 'default',
apiUrl: 'https://quay.example.com',
});
});

afterAll(() => {
Expand All @@ -82,7 +83,8 @@ describe('QuayRepository', () => {
expect(getByTestId('quay-repo-progress')).not.toBeNull();
});

it('should show empty table if loaded and data is not present', async () => {
it('should show empty table and no title links if loaded and data is not present', async () => {
(mockQuayApi.getQuayInstance as jest.Mock).mockReturnValue(undefined);
(useTags as jest.Mock).mockReturnValue({ loading: false, data: [] });
const { getByTestId, queryByText } = await renderComponent();
expect(getByTestId('quay-repo-table')).not.toBeNull();
Expand All @@ -94,9 +96,14 @@ describe('QuayRepository', () => {
"This repository doesn't contain any images yet, or there might be an access issue.",
),
).toBeInTheDocument();
expect(
queryByText('backstage-community/redhat-backstage-build', {
selector: 'a',
}),
).not.toBeInTheDocument();
});

it('should show table if loaded and data is present', async () => {
it('should show table and title links if loaded and data is present', async () => {
(useTags as jest.Mock).mockReturnValue({
loading: false,
data: [
Expand All @@ -116,6 +123,17 @@ describe('QuayRepository', () => {
expect(
queryByText('There are no images available.'),
).not.toBeInTheDocument();
const repositoryLink = queryByText(
'backstage-community/redhat-backstage-build',
{
selector: 'a',
},
);
expect(repositoryLink).toBeInTheDocument();
expect(repositoryLink).toHaveAttribute(
'href',
'https://quay.example.com/repository/backstage-community/redhat-backstage-build',
);
});

it('should show table if loaded and data is present but shows progress if security scan is not loaded', async () => {
Expand Down
Loading
Loading