Skip to content

Commit

Permalink
AT-12785: Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rddimon committed Nov 26, 2024
1 parent a3fa831 commit 8e7091a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
3 changes: 1 addition & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ export interface ServerlessInstance {

configSchemaHandler: ConfigSchemaHandler;
cli: {
log (str: string, entity?: string): void,
consoleLog (str: string): void,
log (str: string, entity?: string): void
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/integration-tests/utils/aws/log-wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class LogWrap {

async getSubscriptionFilter (logGroupName: string, destinationArn: string): Promise<SubscriptionFilter | null> {
const { subscriptionFilters } = await this.cwlClient.send(new DescribeSubscriptionFiltersCommand({
logGroupName: logGroupName
logGroupName
}));
const filter = subscriptionFilters.find((s) => s.destinationArn === destinationArn);
if (filter === undefined) {
Expand Down
4 changes: 2 additions & 2 deletions test/integration-tests/utils/test-utilities.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as shell from "shelljs";
import { PLUGIN_IDENTIFIER, RANDOM_STRING, TEMP_DIR } from "../base";
import { PLUGIN_IDENTIFIER, TEMP_DIR } from "../base";

/**
* Executes given shell command.
Expand Down Expand Up @@ -82,7 +82,7 @@ export async function destroyResources (testName: string): Promise<void> {
* @returns name of a function's log group
*/
export function getLogGroup (testName: string, stage: string, functionId: string): string {
const functionName = `${PLUGIN_IDENTIFIER}-${testName}-${RANDOM_STRING}-${stage}-${functionId}`;
const functionName = `${PLUGIN_IDENTIFIER}-${testName}-${stage}-${functionId}`;
return `/aws/lambda/${functionName}`;
}

Expand Down
60 changes: 42 additions & 18 deletions test/unit-tests/index-test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,53 @@
import { expect } from "chai";
import Serverless from "serverless/lib/serverless";
import AwsProvider from "serverless/lib/plugins/aws/provider";
import { ServerlessConfig, ServerlessInstance } from "../../src/types";
import LogForwardingPlugin = require("../../src");

const TEST_DESTINATION_ARN = "arn:aws:lambda:us-moon-1:314159265358:function:testforward-test-forward";
const createServerless = (config, service) => {
const serverless = new Serverless(config);
serverless.cli = {
log () {
}

const createServerless = (service, options) => {
const funcLowerName = (name) => name.charAt(0).toLowerCase() + name.slice(1);
const funcUpperName = (name) => name.charAt(0).toUpperCase() + name.slice(1);

service.getFunction = (name: string) => {
return {
name: funcUpperName(name),
...service.functions[name]
};
};
return {
cli: {
log (str: string) {
}
},
providers: {},
getProvider (name: string) {
const state = options.stage || service.provider.stage;
return {
naming: {
getLogGroupName: (name: string) => `/aws/lambda/${service.service}-${state}-${funcLowerName(name)}`,
getNormalizedFunctionName: (name: string) => funcUpperName(name),
getLogGroupLogicalId: (name: string) => `${funcUpperName(name)}LogGroup`
}
};
},
configSchemaHandler: {
defineTopLevelProperty: () => null,
defineCustomProperties: () => null,
defineFunctionEvent: () => null,
defineFunctionEventProperties: () => null,
defineFunctionProperties: () => null,
defineProvider: () => null
},
service
};
// eslint-disable-next-line no-new
new AwsProvider(serverless, config);
serverless.service.update(service);
serverless.service.setFunctionNames(config);
return serverless;
};

const constructPluginResources = (logForwarding, functions?) => {
const config = {
commands: [],
options: {}
};
const serverless = createServerless(config, {
const serverless = createServerless({
service: "test-service",
provider: {
name: "aws",
Expand All @@ -46,15 +70,15 @@ const constructPluginResources = (logForwarding, functions?) => {
},
testFunctionTwo: {}
}
});
}, config);
return new LogForwardingPlugin(serverless as ServerlessInstance, config as ServerlessConfig);
};
const constructPluginNoResources = (logForwarding) => {
const config = {
commands: [],
options: {}
};
const serverless = createServerless(config, {
const serverless = createServerless({
provider: {
region: "us-moon-1",
stage: "test-stage"
Expand All @@ -67,7 +91,7 @@ const constructPluginNoResources = (logForwarding) => {
testFunctionTwo: {}
},
service: "test-service"
});
}, config);
serverless.service.resources = undefined;
return new LogForwardingPlugin(serverless as ServerlessInstance, config as ServerlessConfig);
};
Expand All @@ -77,7 +101,7 @@ const constructPluginResourcesWithParam = (logForwarding) => {
options: {},
stage: "dev"
};
const serverless = createServerless(options, {
const serverless = createServerless({
provider: {
name: "aws",
region: "us-moon-1",
Expand All @@ -100,7 +124,7 @@ const constructPluginResourcesWithParam = (logForwarding) => {
testFunctionTwo: {}
},
service: "test-service"
});
}, options);
return new LogForwardingPlugin(serverless as ServerlessInstance, options);
};

Expand Down

0 comments on commit 8e7091a

Please sign in to comment.