Skip to content
This repository has been archived by the owner on Apr 13, 2020. It is now read-only.

Commit

Permalink
Default Routing for isDefault Ring (#427)
Browse files Browse the repository at this point in the history
- When a ring is marked as `isDefault: true`, `spk hld reconcile` will now
  generate an additional IngressRoute and Middleware.
  - The IngressRoute will not have a `Ring` route match rule
  - The IngressRoute points to the same backend service as its ringed
    counterpart.
  - Only one ring can be marked `isDefault: true` -- validation is run at
    `spk hld reconcile` execute time, throwing an error if more than one
    `isDefault`.
- Refactored IngressRoute tests
- Added compatibility configuration for eslint and prettier.

closes microsoft/bedrock#1084
  • Loading branch information
evanlouie authored Mar 23, 2020
1 parent a7c2ce4 commit d1862fc
Show file tree
Hide file tree
Showing 12 changed files with 634 additions and 252 deletions.
7 changes: 4 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
preset: "ts-jest",
testEnvironment: "node",
rootDir: "src"
};
151 changes: 151 additions & 0 deletions src/commands/hld/reconcile-unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import * as fs from "fs";
import * as mocks from "../../test/mockFactory";
import { BedrockFile } from "../../types";
import * as reconcile from "./reconcile";

jest.mock("fs");

beforeEach(() => {
jest.resetAllMocks();
});

describe("createMiddlewareForRing", () => {
const tests: {
name: string;
actual: () => unknown;
expected: unknown;
effects?: (() => void)[];
}[] = [
{
name: "isDefault === false; creates one (1) middleware",
actual: (): unknown =>
reconcile.createMiddlewareForRing(
"path-to-ring",
"my-service",
"my-ring",
"v1",
false
),
expected: { ringed: expect.anything(), default: undefined },
effects: [
(): void => {
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
expect(fs.writeFileSync).toHaveBeenCalledWith(
expect.anything(),
expect.not.stringContaining("\n---\n")
);
},
],
},

{
name: "isDefault === true; creates two (2) middleware",
actual: (): unknown =>
reconcile.createMiddlewareForRing(
"path-to-ring",
"my-service",
"my-ring",
"v1",
true
),
expected: {
ringed: expect.objectContaining({
metadata: { name: "my-service-my-ring" },
}),
default: expect.objectContaining({ metadata: { name: "my-service" } }),
},
effects: [
(): void => {
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
expect(fs.writeFileSync).toHaveBeenCalledWith(
expect.anything(),
expect.stringContaining("\n---\n")
);
},
],
},
];

for (const { name, actual, expected, effects } of tests) {
it(name, () => {
expect(actual()).toStrictEqual(expected);
for (const effect of effects ?? []) {
effect();
}
});
}
});

describe("createIngressRouteForRing", () => {
const { services } = mocks.createTestBedrockYaml(false) as BedrockFile;
const tests: {
name: string;
actual: () => unknown;
expected: unknown;
effects?: (() => void)[];
}[] = [
{
name: "isDefault === false; creates one (1) IngressRoute",
actual: (): unknown =>
reconcile.createIngressRouteForRing(
"path-to-ring",
"my-service",
Object.values(services)[0],
{ ringed: { metadata: { name: "my-service-my-ring" } } },
"my-ring",
"version-path",
false
),
expected: [
expect.objectContaining({ metadata: { name: "my-service-my-ring" } }),
],
effects: [
(): void => {
// Should write out one yaml document (no "---")
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
expect(fs.writeFileSync).toHaveBeenCalledWith(
expect.anything(),
expect.not.stringContaining("\n---\n")
);
},
],
},

{
name: "isDefault === true; creates two (2) IngressRoute",
actual: (): unknown =>
reconcile.createIngressRouteForRing(
"foo",
"my-service",
Object.values(services)[0],
{ ringed: { metadata: { name: "my-service-my-ring" } } },
"my-ring",
"version-path",
true
),
expected: [
expect.objectContaining({ metadata: { name: "my-service-my-ring" } }),
expect.objectContaining({ metadata: { name: "my-service" } }),
],
effects: [
(): void => {
// Should write out two yaml documents (with "---")
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
expect(fs.writeFileSync).toHaveBeenCalledWith(
expect.anything(),
expect.stringContaining("\n---\n")
);
},
],
},
];

for (const { name, actual, expected, effects } of tests) {
it(name, () => {
expect(actual()).toStrictEqual(expected);
for (const effect of effects ?? []) {
effect();
}
});
}
});
4 changes: 2 additions & 2 deletions src/commands/hld/reconcile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from "path";
import { create as createBedrockYaml } from "../../lib/bedrockYaml";
import { disableVerboseLogging, enableVerboseLogging } from "../../logger";
import { BedrockFile, BedrockServiceConfig } from "../../types";
import * as reconcile from "./reconcile";
import {
addChartToRing,
checkForFabrikate,
Expand All @@ -14,13 +15,12 @@ import {
execAndLog,
execute,
getFullPathPrefix,
ReconcileDependencies,
normalizedName,
ReconcileDependencies,
reconcileHld,
testAndGetAbsPath,
validateInputs,
} from "./reconcile";
import * as reconcile from "./reconcile";

beforeAll(() => {
enableVerboseLogging();
Expand Down
Loading

0 comments on commit d1862fc

Please sign in to comment.