Added backend tests; Fixes following CR

This commit is contained in:
Nir Gazit
2021-01-12 19:45:48 +02:00
parent ba80eaa1f3
commit 44f841959b
15 changed files with 483 additions and 117 deletions
+1 -9
View File
@@ -15,11 +15,7 @@
*/
import { DiscoveryApi } from '@backstage/core';
import { KafkaApi } from './types';
import {
ConsumerGroupOffsetsResponse,
TopicOffsetsResponse,
} from '@backstage/plugin-kafka-backend';
import { KafkaApi, ConsumerGroupOffsetsResponse } from './types';
export class KafkaBackendClient implements KafkaApi {
private readonly discoveryApi: DiscoveryApi;
@@ -46,10 +42,6 @@ export class KafkaBackendClient implements KafkaApi {
return await response.json();
}
async getTopicOffsets(topic: string): Promise<TopicOffsetsResponse> {
return await this.getRequired(`/topic/${topic}/offsets`);
}
async getConsumerGroupOffsets(
consumerGroup: string,
): Promise<ConsumerGroupOffsetsResponse> {
+10 -5
View File
@@ -15,10 +15,6 @@
*/
import { createApiRef } from '@backstage/core';
import {
ConsumerGroupOffsetsResponse,
TopicOffsetsResponse,
} from '@backstage/plugin-kafka-backend';
export const kafkaApiRef = createApiRef<KafkaApi>({
id: 'plugin.kafka.service',
@@ -26,8 +22,17 @@ export const kafkaApiRef = createApiRef<KafkaApi>({
'Used by the Kafka plugin to make requests to accompanying backend',
});
export type ConsumerGroupOffsetsResponse = {
consumerId: string;
offsets: {
topic: string;
partitionId: number;
topicOffset: string;
groupOffset: string;
}[];
};
export interface KafkaApi {
getTopicOffsets(topic: string): Promise<TopicOffsetsResponse>;
getConsumerGroupOffsets(
consumerGroup: string,
): Promise<ConsumerGroupOffsetsResponse>;
@@ -0,0 +1,39 @@
/*
* Copyright 2020 Spotify AB
*
* 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 React from 'react';
import { render } from '@testing-library/react';
import { ConsumerGroupOffsets } from './ConsumerGroupOffsets';
import * as testOffsets from './__fixtures__/consumerGroupoffsets.json';
import { wrapInTestApp } from '@backstage/test-utils';
describe('ConfigMaps', () => {
it('should render consumer group offsets', async () => {
const { getByText } = render(
wrapInTestApp(<ConsumerGroupOffsets topics={testOffsets} />),
);
// title
expect(getByText('dice-roller')).toBeInTheDocument();
expect(getByText('Config Map')).toBeInTheDocument();
// values
expect(getByText('Immutable')).toBeInTheDocument();
expect(getByText('false')).toBeInTheDocument();
expect(getByText('Data')).toBeInTheDocument();
expect(getByText('Foo: bar')).toBeInTheDocument(); // TODO wish this wasn't upper case
});
});
@@ -0,0 +1,20 @@
[
{
"topic": "topic1",
"partitionId": 1,
"groupOffset": "100",
"topicOffset": "500"
},
{
"topic": "topic1",
"partitionId": 2,
"groupOffset": "213",
"topicOffset": "1000"
},
{
"topic": "topic2",
"partitionId": 1,
"groupOffset": "456",
"topicOffset": "456"
}
]
@@ -17,7 +17,6 @@
import { errorApiRef, useApi } from '@backstage/core';
import { useAsyncRetry } from 'react-use';
import { kafkaApiRef } from '../../api/types';
import _ from 'lodash';
export function useConsumerGroupOffsets(groupId: string) {
const api = useApi(kafkaApiRef);
@@ -25,23 +24,8 @@ export function useConsumerGroupOffsets(groupId: string) {
const { loading, value: topics, retry } = useAsyncRetry(async () => {
try {
const groupOffsets = await api.getConsumerGroupOffsets(groupId);
const groupWithTopicOffsets = await Promise.all(
groupOffsets.map(async ({ topic, partitions }) => {
const topicOffsets = _.keyBy(
await api.getTopicOffsets(topic),
partition => partition.id,
);
return partitions.map(partition => ({
topic: topic,
partitionId: partition.id,
groupOffset: partition.offset,
topicOffset: topicOffsets[partition.id].offset,
}));
}),
);
return groupWithTopicOffsets.flat();
const response = await api.getConsumerGroupOffsets(groupId);
return response.offsets;
} catch (e) {
errorApi.post(e);
throw e;
+1 -2
View File
@@ -14,7 +14,6 @@
* limitations under the License.
*/
import {
configApiRef,
createApiFactory,
createPlugin,
createRouteRef,
@@ -33,7 +32,7 @@ export const plugin = createPlugin({
apis: [
createApiFactory({
api: kafkaApiRef,
deps: { discoveryApi: discoveryApiRef, configApi: configApiRef },
deps: { discoveryApi: discoveryApiRef },
factory: ({ discoveryApi }) => new KafkaBackendClient({ discoveryApi }),
}),
],