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 1ebaab9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 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 @@ -155,6 +155,10 @@ describe('gcpDetector', () => {
});

it('should retry if the initial request fails', async () => {
console.log(INSTANCE_PATH);
console.log(INSTANCE_ID_PATH);
console.log(HOST_ADDRESS);
console.log(SECONDARY_HOST_ADDRESS);
const scope = nock(HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(500)
Expand All @@ -165,7 +169,8 @@ describe('gcpDetector', () => {
.get(INSTANCE_ID_PATH)
.reply(200, () => 4520031799277581759, HEADERS)
.get(CLUSTER_NAME_PATH)
.reply(413);
.reply(413)
.persist();
const secondaryScope = nock(SECONDARY_HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(200, {}, HEADERS);
Expand Down

0 comments on commit 1ebaab9

Please sign in to comment.