Skip to content

Commit

Permalink
Fixed timeout issue in unit tests by only calling metadata service in…
Browse files Browse the repository at this point in the history
… AWS
  • Loading branch information
tkmcmaster committed Dec 19, 2024
1 parent 2fcabc5 commit be2542d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
3 changes: 1 addition & 2 deletions common/integration/ec2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions common/src/util/ec2.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<string> {
if (!bypassMetaDataService) {
export async function getInstanceId (): Promise<string> {
// 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", {});
Expand Down
1 change: 1 addition & 0 deletions common/src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit be2542d

Please sign in to comment.