Skip to content

Commit

Permalink
Improve subnet test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
danielrbradley committed Oct 30, 2023
1 parent 0b1f9de commit 73416b4
Showing 1 changed file with 82 additions and 17 deletions.
99 changes: 82 additions & 17 deletions awsx/ec2/subnetDistributor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,99 @@ import fc from "fast-check";
import { SubnetSpecInputs, SubnetTypeInputs } from "../schema-types";
import { getSubnetSpecs, SubnetSpec } from "./subnetDistributor";

describe("subnet ranges", () => {
function cidrMask(args?: { min?: number; max?: number }): fc.Arbitrary<number> {
return fc.integer({ min: args?.min ?? 16, max: args?.max ?? 27 });
}

function subnetSpecNoMask() {
return fc.constantFrom("Private", "Public", "Isolated").map(
(type): SubnetSpecInputs => ({
type: type as SubnetTypeInputs,
}),
);
}

describe("default subnet layout", () => {
it("should have smaller subnets than the vpc", () => {
fc.assert(
fc.property(
fc.integer({ min: 16, max: 27 }), // vpc mask
fc.integer({ min: 2, max: 4 }), // az count
fc.integer({ min: 1, max: 4 }), // subnet count
(vpcMask, azCount, subnetCount) => {
const vpcCidr = `10.0.0.0/${vpcMask}`;
const azs: string[] = [];
for (let index = 0; index < azCount; index++) {
azs.push("us-east-1" + String.fromCharCode("c".charCodeAt(0) + index));
}
const subnetTypes: SubnetTypeInputs[] = ["Private", "Public", "Isolated"];
const specs: SubnetSpecInputs[] = [];
for (let index = 0; index < subnetCount; index++) {
specs.push({ type: subnetTypes[index % 3] });
}
const result = getSubnetSpecs("vpcName", vpcCidr, azs, specs);
fc.record({
vpcCidrMask: cidrMask(),
azs: fc.array(fc.string({ size: "xsmall" }), { minLength: 2, maxLength: 5 }),
subnetSpecs: fc.array(subnetSpecNoMask(), { minLength: 1, maxLength: 5 }),
}),
({ vpcCidrMask, azs, subnetSpecs }) => {
const vpcCidr = `10.0.0.0/${vpcCidrMask}`;

const result = getSubnetSpecs("vpcName", vpcCidr, azs, subnetSpecs);

for (const subnet of result) {
const subnetMask = getCidrMask(subnet.cidrBlock);
// Larger mask means smaller subnet
expect(subnetMask).toBeGreaterThan(vpcMask);
expect(subnetMask).toBeGreaterThan(vpcCidrMask);
}
},
),
);
});

it("should use whole space if there's only one subnet and VPC is small", () => {
fc.assert(
fc.property(
fc.record({
vpcCidrMask: cidrMask({ min: 24 }),
subnetSpec: subnetSpecNoMask(),
}),
({ vpcCidrMask, subnetSpec }) => {
const vpcCidr = `10.0.0.0/${vpcCidrMask}`;

const result = getSubnetSpecs("vpcName", vpcCidr, ["us-east-1a"], [subnetSpec]);

expect(result.length).toBe(1);
expect(result[0].cidrBlock).toBe(vpcCidr);
},
),
);
});

it("should use default values if VPC is large", () => {
fc.assert(
fc.property(
fc.record({
vpcCidrMask: cidrMask({ max: 17 }),
subnetSpec: subnetSpecNoMask(),
}),
({ vpcCidrMask }) => {
const vpcCidr = `10.0.0.0/${vpcCidrMask}`;

const result = getSubnetSpecs(
"vpcName",
vpcCidr,
["us-east-1a"],
[{ type: "Public" }, { type: "Private" }, { type: "Isolated" }],
);

expect(result).toMatchObject([
{
cidrBlock: "10.0.0.0/19",
subnetName: "vpcName-private-1",
type: "Private",
},
{
cidrBlock: "10.0.32.0/20",
subnetName: "vpcName-public-1",
type: "Public",
},
{
cidrBlock: "10.0.48.0/24",
subnetName: "vpcName-isolated-1",
type: "Isolated",
},
]);
},
),
);
});
});

function getCidrMask(cidrBlock: string): number {
Expand Down

0 comments on commit 73416b4

Please sign in to comment.