Skip to content

Commit

Permalink
test: fixed core tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvilla committed Mar 5, 2024
1 parent 54a281b commit 308c832
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 96 deletions.
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@noovolari/leapp-core",
"version": "0.1.163",
"version": "0.1.162",
"author": {
"name": "Noovolari",
"email": "info@noovolari.com"
Expand Down Expand Up @@ -113,4 +113,4 @@
"url": "https://github.com/Noovolari/leapp/issues"
},
"homepage": "https://github.com/Noovolari/leapp"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { afterEach, describe, expect, jest, test } from "@jest/globals";
import { AwsSsoIntegrationService } from "./aws-sso-integration-service";
import { IntegrationType } from "../../models/integration-type";
import { Session } from "../../models/session";
import { SSO } from "aws-sdk";
import { SessionType } from "../../models/session-type";
import { ListAccountRolesRequest } from "aws-sdk/clients/sso";
import { constants } from "../../models/constants";
import { ThrottleService } from "../throttle-service";
import { ListAccountRolesCommandInput } from "@aws-sdk/client-sso";
import { ListAccountRolesCommandInput, ListAccountRolesRequest } from "@aws-sdk/client-sso";

describe("AwsSsoIntegrationService", () => {
afterEach(() => {
Expand Down Expand Up @@ -433,7 +431,7 @@ describe("AwsSsoIntegrationService", () => {
expect(region).toBe(fakeRegion);
expect(maxAttempts).toBe(30);
expect(awsIntegrationService.listAccountRolesCall).toBeInstanceOf(ThrottleService);
expect(awsIntegrationService.listAccountRolesCall.minDelay).toBe(100);
expect(awsIntegrationService.listAccountRolesCall.minDelay).toBe(200);

let actualRetryTime = retryStrategy.computeNextBackoffDelay(30);
expect(actualRetryTime).toBeLessThanOrEqual(30000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import * as uuid from "uuid";
import { SessionStatus } from "../../../models/session-status";
import { LoggedException, LogLevel } from "../../log-service";
jest.mock("uuid");
jest.mock("aws-sdk");

describe("AwsIamRoleFederatedService", () => {
beforeEach(() => {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { mockClient } from "aws-sdk-client-mock";

jest.mock("uuid");
jest.mock("console");
jest.mock("aws-sdk");

describe("AwsIamUserService", () => {
const mockedDateString = "2022-02-24T10:00:00";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { LocalstackSession } from "../../../models/localstack/localstack-session

jest.mock("uuid");
jest.mock("console");
jest.mock("aws-sdk");

describe("LocalstackSessionService", () => {
const mockedProfileId = "mocked-profile-id";
Expand Down
136 changes: 49 additions & 87 deletions packages/core/src/services/ssm-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("SsmService", () => {
let executeService: ExecuteService;
let credentialInfo: CredentialsInfo;
let mockedCallback: any;
let setConfig;
//let setConfig;
let nativeService: INativeService;

beforeEach(() => {
Expand All @@ -29,7 +29,7 @@ describe("SsmService", () => {
};

mockedCallback = jest.fn(() => {});
setConfig = jest.spyOn(SsmService, "setConfig");
//setConfig = jest.spyOn(SsmService, "setConfig");

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand All @@ -48,15 +48,6 @@ describe("SsmService", () => {
} as any;

ssmService = new SsmService(null, executeService, nativeService, null);
ssmService.aws = {
config: {
update: jest.fn((_: any) => {}),
},
// eslint-disable-next-line @typescript-eslint/naming-convention
SSM: jest.fn(() => {}),
// eslint-disable-next-line @typescript-eslint/naming-convention
EC2: jest.fn(() => {}),
};
});

test("getSsmInstances - should retrieve a list of ssm sessions given a valid region", (done) => {
Expand All @@ -66,20 +57,7 @@ describe("SsmService", () => {
ssmService.getSsmInstances(credentialInfo, "eu-west-1", mockedCallback);

setTimeout(() => {
expect(setConfig).toHaveBeenCalled();

expect(ssmService.aws.config.update).toBeCalledWith({
region: "eu-west-1",
accessKeyId: "123",
secretAccessKey: "345",
sessionToken: "678",
});

expect(ssmService.aws.SSM).toHaveBeenCalled();
expect(ssmService.aws.EC2).toHaveBeenCalled();

expect(mockedCallback).toHaveBeenCalled();

expect((ssmService as any).applyEc2MetadataInformation).toHaveBeenCalled();
done();
}, 100);
Expand Down Expand Up @@ -177,15 +155,6 @@ describe("SsmService", () => {
} as any;

ssmService = new SsmService(null, executeService, nativeService, fileService);
ssmService.aws = {
config: {
update: jest.fn((_: any) => {}),
},
// eslint-disable-next-line @typescript-eslint/naming-convention
SSM: jest.fn(() => {}),
// eslint-disable-next-line @typescript-eslint/naming-convention
EC2: jest.fn(() => {}),
};

const instanceId = "mocked-id";
const region = "eu-west-1";
Expand Down Expand Up @@ -257,13 +226,6 @@ describe("SsmService", () => {
});

test("requestSsmInstances, plus error checking", async () => {
ssmService.aws = {
config: {
update: jest.fn((_: any) => {}),
},
// eslint-disable-next-line @typescript-eslint/naming-convention
SSM: jest.fn(() => {}),
} as any;
const logService: any = {
log: jest.fn(),
};
Expand All @@ -274,32 +236,31 @@ describe("SsmService", () => {

let index = 0;
(ssmService as any).ssmClient = {
describeInstanceInformation: jest.fn(() => ({
promise: () =>
Promise.resolve({
// eslint-disable-next-line @typescript-eslint/naming-convention
InstanceInformationList: [
{
fakeInstanceId: "fake-id-1",
// eslint-disable-next-line @typescript-eslint/naming-convention
PingStatus: "Offline",
},
{
fakeInstanceId: "fake-id-2",
// eslint-disable-next-line @typescript-eslint/naming-convention
PingStatus: "Online",
},
],
// eslint-disable-next-line @typescript-eslint/naming-convention
NextToken: index++ < 3 ? "fake-next-token" : undefined,
}),
})),
send: jest.fn(async () =>
Promise.resolve({
// eslint-disable-next-line @typescript-eslint/naming-convention
InstanceInformationList: [
{
fakeInstanceId: "fake-id-1",
// eslint-disable-next-line @typescript-eslint/naming-convention
PingStatus: "Offline",
},
{
fakeInstanceId: "fake-id-2",
// eslint-disable-next-line @typescript-eslint/naming-convention
PingStatus: "Online",
},
],
// eslint-disable-next-line @typescript-eslint/naming-convention
NextToken: index++ < 3 ? "fake-next-token" : undefined,
})
),
};

jest.spyOn(ssmService as any, "requestSsmInstances");
const result = await (ssmService as any).requestSsmInstances(credentialInfo, instanceId, region);
expect(logService.log).toHaveBeenCalledWith(new LoggedEntry("Obtained smm info from aws for SSM", ssmService, LogLevel.info));
expect((ssmService as any).ssmClient.describeInstanceInformation).toHaveBeenCalledTimes(4);
expect((ssmService as any).ssmClient.send).toHaveBeenCalledTimes(4);

const resultObject = {
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -313,34 +274,33 @@ describe("SsmService", () => {
expect(result).toStrictEqual([resultObject, resultObject, resultObject, resultObject]);

(ssmService as any).ssmClient = {
describeInstanceInformation: jest.fn(() => ({
promise: () =>
Promise.resolve({
// eslint-disable-next-line @typescript-eslint/naming-convention
InstanceInformationList: [
{
fakeInstanceId: "fake-id-1",
// eslint-disable-next-line @typescript-eslint/naming-convention
PingStatus: "Offline",
},
{
fakeInstanceId: "fake-id-2",
// eslint-disable-next-line @typescript-eslint/naming-convention
PingStatus: "Offline",
},
],
}),
})),
send: jest.fn(async () =>
Promise.resolve({
// eslint-disable-next-line @typescript-eslint/naming-convention
InstanceInformationList: [
{
fakeInstanceId: "fake-id-1",
// eslint-disable-next-line @typescript-eslint/naming-convention
PingStatus: "Offline",
},
{
fakeInstanceId: "fake-id-2",
// eslint-disable-next-line @typescript-eslint/naming-convention
PingStatus: "Offline",
},
],
})
),
};

await expect(async () => {
await (ssmService as any).requestSsmInstances(credentialInfo, instanceId, region);
}).rejects.toThrow(new Error("No instances are accessible by this Role."));

(ssmService as any).ssmClient = {
describeInstanceInformation: jest.fn(() => ({
promise: () => Promise.resolve({}),
})),
send: jest.fn(async () => Promise.resolve({})),
};

await expect(async () => {
await (ssmService as any).requestSsmInstances(credentialInfo, instanceId, region);
}).rejects.toThrow(new Error("No instances are accessible by this Role."));
Expand Down Expand Up @@ -370,8 +330,9 @@ describe("SsmService", () => {
},
],
};

let ec2Client = {
describeInstances: jest.fn(() => ({ promise: jest.fn((_params) => Promise.resolve(reservations)) })),
send: jest.fn(async () => Promise.resolve(reservations)),
};

const logService: any = {
Expand Down Expand Up @@ -401,7 +362,7 @@ describe("SsmService", () => {
],
};
ec2Client = {
describeInstances: jest.fn(() => ({ promise: jest.fn((_params) => Promise.resolve(reservations)) })),
send: jest.fn(async () => Promise.resolve(reservations)),
};
(ssmService as any).ec2Client = ec2Client;
result = await (ssmService as any).applyEc2MetadataInformation(mockedInstances);
Expand All @@ -425,14 +386,14 @@ describe("SsmService", () => {
],
};
ec2Client = {
describeInstances: jest.fn(() => ({ promise: jest.fn((_params) => Promise.resolve(reservations)) })),
send: jest.fn(async () => Promise.resolve(reservations)),
};
(ssmService as any).ec2Client = ec2Client;
result = await (ssmService as any).applyEc2MetadataInformation(mockedInstances);
expect(result).toStrictEqual(mockedInstances);
expect(mockedInstances[1].Name).not.toStrictEqual("Not Mocked Name");

ec2Client.describeInstances = jest.fn(() => ({ promise: jest.fn((_params) => Promise.reject({ message: "Error" })) }));
ec2Client.send = jest.fn(async () => Promise.reject({ message: "Error" }));

await expect(async () => {
await (ssmService as any).applyEc2MetadataInformation(mockedInstances);
Expand Down Expand Up @@ -470,8 +431,9 @@ describe("SsmService", () => {

ssmService = new SsmService(logService, executeService, nativeService, null);
(ssmService as any).ec2Client = {
describeInstances: jest.fn(() => ({ promise: jest.fn((_params) => Promise.resolve(reservations)) })),
send: jest.fn(async () => Promise.resolve(reservations)),
};

const result = await (ssmService as any).applyEc2MetadataInformation(mockedInstances);
expect(result).toStrictEqual(mockedInstances);

Expand Down

0 comments on commit 308c832

Please sign in to comment.