Skip to content

Commit

Permalink
test: SmartStack
Browse files Browse the repository at this point in the history
  • Loading branch information
aripalo committed Nov 21, 2024
1 parent 568dc50 commit 011c405
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/__test__/TestableProjectStack.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as cdk from "aws-cdk-lib";
import {
Project,
SmartStack,
Expand All @@ -8,21 +9,35 @@ import {
} from "..";

interface TestStackInSharedAccountProps {
defaultRegion?: string;
accounts: Record<string, Account>;
accountType: AccountType;
environmentType?: string;
stackProps?: cdk.StackProps;
}

export class TestableProjectStack extends SmartStack {
public readonly projectName: string;
public readonly stackConstructId: string;

constructor(props: TestStackInSharedAccountProps) {
const { accounts, accountType, environmentType } = props;
const {
accounts,
accountType,
environmentType,
defaultRegion,
stackProps,
} = props;

const projectName = "test-project";

const project = new Project({
name: "test",
name: projectName,
author: {
name: "test",
email: "[email protected]",
},
defaultRegion,
accounts,
});

Expand All @@ -40,8 +55,14 @@ export class TestableProjectStack extends SmartStack {
wrapper = new AccountWrapper(project);
}

super(wrapper, "Test", {
const stackConstructId = "TestStack";

super(wrapper, stackConstructId, {
description: "This stack is for testing purposes only",
...stackProps,
});

this.projectName = projectName;
this.stackConstructId = stackConstructId;
}
}
164 changes: 164 additions & 0 deletions src/smartstack/stack.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { pascalCase } from "change-case";
import { expectErrorMetadata } from "../__test__/expectErrorMetadata";
import { TestableProjectStack } from "../__test__/TestableProjectStack";
import { AccountStrategy } from "../configurations";
import { SmartStack } from "./stack";
import { AccountType } from "../configurations/accounts";

test("TestableProjectStack is instance of SmartStack", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.two({
dev: { id: "111111111111" },
prod: { id: "222222222222" },
}),
accountType: AccountType.DEV,
});
expect(stack).toBeInstanceOf(SmartStack);
});

describe("stackName", () => {
test("for account-type stacks", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.two({
dev: { id: "111111111111" },
prod: { id: "222222222222" },
}),
accountType: AccountType.DEV,
});
expect(stack.stackName).toEqual(
`${pascalCase(stack.projectName)}-Account-${pascalCase(stack.stackConstructId)}`,
);
});
test("for environment-type stacks", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.two({
dev: { id: "111111111111" },
prod: { id: "222222222222" },
}),
accountType: AccountType.DEV,
environmentType: "staging",
});
expect(stack.stackName).toEqual(
`${pascalCase(stack.projectName)}-Environment-Staging-${pascalCase(stack.stackConstructId)}`,
);
});
});

describe("description", () => {
test("adds error metadata if too short stack description", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.two({
dev: { id: "111111111111" },
prod: { id: "222222222222" },
}),
accountType: AccountType.DEV,
stackProps: {
description: "a".repeat(11),
},
});
expectErrorMetadata(
stack,
expect.stringContaining(
"Description is required and should be at least 12 characters",
),
);
});

test("adds error metadata if description is too long", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.two({
dev: { id: "111111111111" },
prod: { id: "222222222222" },
}),
accountType: AccountType.DEV,
stackProps: {
description: "a".repeat(281),
},
});
expectErrorMetadata(
stack,
expect.stringContaining("Description is should be at max 280 characters"),
);
});

test("does not add error metadata if description length is valid", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.two({
dev: { id: "111111111111" },
prod: { id: "222222222222" },
}),
accountType: AccountType.DEV,
stackProps: {
description: "a".repeat(12),
},
});
expectErrorMetadata(stack, undefined);
});
});

describe("terminationProtection", () => {
test.each([
["staging", true],
["production", true],
["development", false],
["feature/foo-bar", false],
["test", false],
["preproduction", false],
])("is %s for %s environment", (envType, expected) => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.one({
shared: { id: "111111111111" },
}),
accountType: AccountType.SHARED,
environmentType: envType,
});
expect(stack.terminationProtection).toBe(expected);
});
});

describe("env", () => {
test("uses accountId and region from project context if not overridden", () => {
const stack = new TestableProjectStack({
defaultRegion: "eu-north-1",
accounts: AccountStrategy.one({
shared: { id: "111111111111" },
}),
accountType: AccountType.SHARED,
});
expect(stack.account).toEqual("111111111111");
expect(stack.region).toEqual("eu-north-1");
});
test("override accountId", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.one({
shared: { id: "111111111111" },
}),
accountType: AccountType.SHARED,
defaultRegion: "eu-north-1",
stackProps: {
env: {
account: "222222222222",
},
},
});
expect(stack.account).toEqual("222222222222");
expect(stack.region).toEqual("eu-north-1");
});

test("override region", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.one({
shared: { id: "111111111111" },
}),
accountType: AccountType.SHARED,
defaultRegion: "eu-north-1",
stackProps: {
env: {
region: "eu-central-1",
},
},
});
expect(stack.account).toEqual("111111111111");
expect(stack.region).toEqual("eu-central-1");
});
});

0 comments on commit 011c405

Please sign in to comment.