From be2542d894b75e41cc68556206dae5996cce3ad8 Mon Sep 17 00:00:00 2001 From: Trevor McMaster Date: Thu, 19 Dec 2024 14:07:56 -0700 Subject: [PATCH] Fixed timeout issue in unit tests by only calling metadata service in AWS --- common/integration/ec2.spec.ts | 3 +-- common/src/util/ec2.ts | 7 ++++--- common/src/util/util.ts | 1 + 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/common/integration/ec2.spec.ts b/common/integration/ec2.spec.ts index cb86cde2..84ed643e 100644 --- a/common/integration/ec2.spec.ts +++ b/common/integration/ec2.spec.ts @@ -3,8 +3,7 @@ import { expect } from "chai"; describe("EC2 Integration", () => { it("getInstanceId should get instanceId", (done: Mocha.Done) => { - // Our current mocks do not support the metadata service. bypassMetaDataService to bypass so we don't timeout on integration tests - ec2.getInstanceId(true).then((result: string) => { + ec2.getInstanceId().then((result: string) => { log("getInstanceId", LogLevel.INFO, { result }); expect(result, "result").to.not.equal(undefined); expect(ec2.INSTANCE_ID_REGEX.test(result), `${ec2.INSTANCE_ID_REGEX}.test("${result}")`).to.equal(true); diff --git a/common/src/util/ec2.ts b/common/src/util/ec2.ts index 18fabee0..40abfa8d 100644 --- a/common/src/util/ec2.ts +++ b/common/src/util/ec2.ts @@ -1,4 +1,5 @@ import { LogLevel, log } from "./log"; +import { IS_RUNNING_IN_AWS } from "./util"; import { MetadataService } from "@aws-sdk/ec2-metadata-service"; import { exec as _exec } from "child_process"; import { promisify } from "util"; @@ -16,9 +17,9 @@ const metadata: MetadataService = new MetadataService({ // host: "169.254.169.254" }); -// Our current mocks do not support the metadata service. bypassMetaDataService to bypass so we don't timeout on integration tests -export async function getInstanceId (bypassMetaDataService?: boolean): Promise { - if (!bypassMetaDataService) { +export async function getInstanceId (): Promise { + // Our current mocks do not support the metadata service. bypass when not in AWS so we don't timeout on integration and unit tests + if (IS_RUNNING_IN_AWS) { try { // curl http://169.254.169.254/latest/meta-data/instance-id -> "i-0cfd3309705a3ce79" const instanceId: string = await metadata.request("/latest/meta-data/instance-id", {}); diff --git a/common/src/util/util.ts b/common/src/util/util.ts index dd3d634e..3d0a07a7 100644 --- a/common/src/util/util.ts +++ b/common/src/util/util.ts @@ -7,6 +7,7 @@ export const CONTROLLER_APPLICATION_PREFIX: string = CONTROLLER_APPLICATION_NAME export const SYSTEM_NAME: string = process.env.SYSTEM_NAME || "unittests"; export const CONTROLLER_ENV = process.env.CONTROLLER_ENV; export const AGENT_ENV = process.env.AGENT_ENV; +export const IS_RUNNING_IN_AWS: boolean = process.env.APPLICATION_NAME !== undefined && process.env.SYSTEM_NAME !== undefined; export const PEWPEW_BINARY_FOLDER: string = "pewpew"; export const PEWPEW_VERSION_LATEST: string = "latest";