Skip to content

Commit

Permalink
[Usage Collection/Cloud Provider] Fix isReady bug (elastic#97279)
Browse files Browse the repository at this point in the history
  • Loading branch information
afharo authored and kibanamachine committed Apr 15, 2021
1 parent cb730d3 commit 8e8663d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function registerCloudProviderUsageCollector(usageCollection: UsageCollec

const collector = usageCollection.makeUsageCollector<Usage | undefined>({
type: 'cloud_provider',
isReady: () => Boolean(cloudDetector.getCloudDetails()),
isReady: () => typeof cloudDetector.getCloudDetails() !== 'undefined',
async fetch() {
const details = cloudDetector.getCloudDetails();
if (!details) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -87,7 +87,7 @@ describe('CloudDetector', () => {
const detector = new CloudDetector({ cloudServices: services });

await detector.detectCloudService();
expect(detector.getCloudDetails()).toBeUndefined();
expect(detector.getCloudDetails()).toBeNull();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 8e8663d

Please sign in to comment.