From b7067dea16e90d4eb3271ab6b2c5498914ab4fd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20Haro?= Date: Thu, 15 Apr 2021 20:04:52 +0200 Subject: [PATCH] [Usage Collection/Cloud Provider] Fix isReady bug (#97279) --- .../collectors/cloud/cloud_provider_collector.test.ts | 5 +++++ .../server/collectors/cloud/cloud_provider_collector.ts | 2 +- .../collectors/cloud/detector/cloud_detector.test.ts | 8 ++++---- .../server/collectors/cloud/detector/cloud_detector.ts | 6 +++--- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/plugins/kibana_usage_collection/server/collectors/cloud/cloud_provider_collector.test.ts b/src/plugins/kibana_usage_collection/server/collectors/cloud/cloud_provider_collector.test.ts index a2f08ddb465cc..b3f9c2a329468 100644 --- a/src/plugins/kibana_usage_collection/server/collectors/cloud/cloud_provider_collector.test.ts +++ b/src/plugins/kibana_usage_collection/server/collectors/cloud/cloud_provider_collector.test.ts @@ -46,6 +46,11 @@ describe('registerCloudProviderUsageCollector', () => { expect(collector.isReady()).toBe(true); }); + test('isReady() => true when cloud details have been retrieved but none have been found', () => { + cloudDetailsMock.mockReturnValueOnce(null); + expect(collector.isReady()).toBe(true); + }); + test('initiates CloudDetector.detectCloudDetails when called', () => { expect(detectCloudServiceMock).toHaveBeenCalledTimes(1); }); diff --git a/src/plugins/kibana_usage_collection/server/collectors/cloud/cloud_provider_collector.ts b/src/plugins/kibana_usage_collection/server/collectors/cloud/cloud_provider_collector.ts index eafce56d7cf2e..2b3cbce6fa3e1 100644 --- a/src/plugins/kibana_usage_collection/server/collectors/cloud/cloud_provider_collector.ts +++ b/src/plugins/kibana_usage_collection/server/collectors/cloud/cloud_provider_collector.ts @@ -23,7 +23,7 @@ export function registerCloudProviderUsageCollector(usageCollection: UsageCollec const collector = usageCollection.makeUsageCollector({ type: 'cloud_provider', - isReady: () => Boolean(cloudDetector.getCloudDetails()), + isReady: () => typeof cloudDetector.getCloudDetails() !== 'undefined', async fetch() { const details = cloudDetector.getCloudDetails(); if (!details) { diff --git a/src/plugins/kibana_usage_collection/server/collectors/cloud/detector/cloud_detector.test.ts b/src/plugins/kibana_usage_collection/server/collectors/cloud/detector/cloud_detector.test.ts index 4b88ed5b4064f..f0b54046565c9 100644 --- a/src/plugins/kibana_usage_collection/server/collectors/cloud/detector/cloud_detector.test.ts +++ b/src/plugins/kibana_usage_collection/server/collectors/cloud/detector/cloud_detector.test.ts @@ -69,16 +69,16 @@ describe('CloudDetector', () => { expect(detector.getCloudDetails()).toEqual({ name: 'good-match' }); }); - it('returns undefined if none match', async () => { + it('returns null if none match', async () => { const services = ([cloudService1, cloudService2] as unknown) as CloudService[]; const detector1 = new CloudDetector({ cloudServices: services }); await detector1.detectCloudService(); - expect(detector1.getCloudDetails()).toBeUndefined(); + expect(detector1.getCloudDetails()).toBeNull(); const detector2 = new CloudDetector({ cloudServices: [] }); await detector2.detectCloudService(); - expect(detector2.getCloudDetails()).toBeUndefined(); + expect(detector2.getCloudDetails()).toBeNull(); }); // this is already tested above, but this just tests it explicitly @@ -87,7 +87,7 @@ describe('CloudDetector', () => { const detector = new CloudDetector({ cloudServices: services }); await detector.detectCloudService(); - expect(detector.getCloudDetails()).toBeUndefined(); + expect(detector.getCloudDetails()).toBeNull(); }); }); }); diff --git a/src/plugins/kibana_usage_collection/server/collectors/cloud/detector/cloud_detector.ts b/src/plugins/kibana_usage_collection/server/collectors/cloud/detector/cloud_detector.ts index 6f6405d9852b6..3d093c81f8896 100644 --- a/src/plugins/kibana_usage_collection/server/collectors/cloud/detector/cloud_detector.ts +++ b/src/plugins/kibana_usage_collection/server/collectors/cloud/detector/cloud_detector.ts @@ -27,7 +27,7 @@ interface CloudDetectorOptions { */ export class CloudDetector { private readonly cloudServices: CloudService[]; - private cloudDetails?: CloudServiceResponseJson; + private cloudDetails?: CloudServiceResponseJson | null; constructor(options: CloudDetectorOptions = {}) { this.cloudServices = @@ -70,7 +70,7 @@ export class CloudDetector { } } - // explicitly undefined rather than null so that it can be ignored in JSON - return undefined; + // explicitly null to differentiate from not having populated the field yet + return null; } }