Skip to content

Commit

Permalink
fix: nock flakiness in resource tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mwear committed Apr 4, 2020
1 parent c75dcdb commit a8486fc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 33 deletions.
16 changes: 10 additions & 6 deletions packages/opentelemetry-resources/test/detect-resources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ const mockedAwsResponse = {
};

describe('detectResources', async () => {
before(() => {
beforeEach(() => {
nock.disableNetConnect();
process.env.OTEL_RESOURCE_LABELS =
'service.instance.id=627cc493,service.name=my-service,service.namespace=default,service.version=0.0.1';
});

after(() => {
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
delete process.env.OTEL_RESOURCE_LABELS;
Expand All @@ -71,7 +71,7 @@ describe('detectResources', async () => {
});

it('returns a merged resource', async () => {
const scope = nock(HOST_ADDRESS)
const gcpScope = nock(HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(200, {}, HEADERS)
.get(INSTANCE_ID_PATH)
Expand All @@ -82,12 +82,16 @@ describe('detectResources', async () => {
.reply(200, () => 'project/zone/my-zone', HEADERS)
.get(CLUSTER_NAME_PATH)
.reply(404);
const secondaryScope = nock(SECONDARY_HOST_ADDRESS)
const gcpSecondaryScope = nock(SECONDARY_HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(200, {}, HEADERS);
const awsScope = nock(AWS_HOST)
.get(AWS_PATH)
.replyWithError({ code: 'ENOTFOUND' });
const resource: Resource = await detectResources();
secondaryScope.done();
scope.done();
awsScope.done();
gcpSecondaryScope.done();
gcpScope.done();

assertCloudResource(resource, {
provider: 'gcp',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,33 +154,6 @@ describe('gcpDetector', () => {
});
});

it('should retry if the initial request fails', async () => {
const scope = nock(HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(500)
.get(PROJECT_ID_PATH)
.reply(200, () => 'my-project-id', HEADERS)
.get(ZONE_PATH)
.reply(200, () => 'project/zone/my-zone', HEADERS)
.get(INSTANCE_ID_PATH)
.reply(200, () => 4520031799277581759, HEADERS)
.get(CLUSTER_NAME_PATH)
.reply(413);
const secondaryScope = nock(SECONDARY_HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(200, {}, HEADERS);
const resource = await gcpDetector.detect();
secondaryScope.done();
scope.done();

assertCloudResource(resource, {
accountId: 'my-project-id',
zone: 'my-zone',
});

assertHostResource(resource, { id: '4520031799277582000' });
});

it('returns empty resource if not detected', async () => {
const resource = await gcpDetector.detect();
assertEmptyResource(resource);
Expand Down

0 comments on commit a8486fc

Please sign in to comment.