Skip to content

Commit

Permalink
Happy path tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cleve-fauna committed Dec 13, 2024
1 parent e6c53cd commit 641fe76
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 85 deletions.
16 changes: 7 additions & 9 deletions src/commands/local.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,18 @@ async function createDatabase(argv) {
secret: "secret",
url: `http://${argv.hostIp}:${argv.hostPort}`,
query: fql`
let name = ${argv.name}
let name = ${argv.database}
let database = Database.byName(name)
let protected = ${argv.protected ?? null}
let typechecked = ${argv.typechecked ?? null}
let priority = ${argv.priority ?? null}
let params = {
name: name,
protected: ${argv.protected ?? null},
typechecked: ${argv.typechecked ?? null},
priority: ${argv.priority ?? null},
}
if (database == null) {
Database.create(params)
Database.create({
name: name,
protected: protected,
typechecked: typechecked,
priority: priority,
})
} else if (protected == database.protected && typechecked == database.typechecked && priority == database.priority) {
database
} else {
Expand All @@ -81,7 +80,6 @@ async function createDatabase(argv) {
${colorize(e.abort, { format: Format.FQL, color })}
-----------------
${chalk.red("Please use choose a different name using --name or align the --typechecked, --priority, and --protected with what is currently present.")}`,
{ hideHelp: false },
);
}
throw e;
Expand Down
2 changes: 0 additions & 2 deletions src/lib/docker-containers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ async function findContainer({ containerName, hostPort }) {
`[FindContainer] Container '${containerName}' is already \
in use on hostPort '${diffPort.PublicPort}'. Please use a new name via \
arguments --name <newName> --hostPort ${hostPort} to start the container.`,
{ hideHelp: false },
);
}
return result;
Expand Down Expand Up @@ -212,7 +211,6 @@ async function createContainer({
throw new CommandError(
`[StartContainer] The hostPort '${hostPort}' on IP '${hostIp}' is already occupied. \
Please pass a --hostPort other than '${hostPort}'.`,
{ hideHelp: false },
);
}
const dockerContainer = await docker.createContainer({
Expand Down
151 changes: 77 additions & 74 deletions test/local.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//@ts-check

import { expect } from "chai";
import { fql } from "fauna";
import sinon, { stub } from "sinon";

import { run } from "../src/cli.mjs";
Expand Down Expand Up @@ -78,6 +79,24 @@ describe("ensureContainerRunning", () => {
net.createServer.returns(serverMock);
});

function setupCreateContainerMocks() {
docker.pull.onCall(0).resolves();
docker.modem.followProgress.callsFake((stream, onFinished) => {
onFinished();
});
docker.listContainers.onCall(0).resolves([]);
fetch.onCall(0).resolves(f({})); // fast succeed the health check
logsStub.callsFake(async () => ({
on: () => {},
destroy: () => {},
}));
docker.createContainer.resolves({
start: startStub,
logs: logsStub,
unpause: unpauseStub,
});
}

it("Shows a clear error to the user if something is already running on the desired port.", async () => {
simulateError = true;
docker.pull.onCall(0).resolves();
Expand All @@ -100,26 +119,63 @@ describe("ensureContainerRunning", () => {
"[StartContainer] The hostPort '8443' on IP '0.0.0.0' is already occupied. \
Please pass a --hostPort other than '8443'.",
);
expect(written).to.contain("fauna local");
expect(written).not.to.contain("fauna local");
expect(written).not.to.contain("An unexpected");
});

it("Creates and starts a container when none exists", async () => {
docker.pull.onCall(0).resolves();
docker.modem.followProgress.callsFake((stream, onFinished) => {
onFinished();
});
docker.listContainers.onCall(0).resolves([]);
fetch.onCall(0).resolves(f({})); // fast succeed the health check
logsStub.callsFake(async () => ({
on: () => {},
destroy: () => {},
}));
docker.createContainer.resolves({
start: startStub,
logs: logsStub,
unpause: unpauseStub,
[
{
args: "--database Foo",
argv: { database: "Foo" },
},
{
args: "--database Foo --typechecked --protected --priority 1",
argv: {
database: "Foo",
typechecked: true,
protected: true,
priority: 1,
},
},
].forEach(({ args, argv }) => {
it("Creates a database if requested", async () => {
setupCreateContainerMocks();
const { runQuery } = container.resolve("faunaClientV10");
runQuery.resolves({
data: JSON.stringify({ name: "Foo" }, null, 2),
});
await run(`local --no-color ${args}`, container);
expect(runQuery).to.have.been.calledWith({
secret: "secret",
url: "http://0.0.0.0:8443",
query: fql`
let name = ${argv.database}
let database = Database.byName(name)
let protected = ${argv.protected ?? null}
let typechecked = ${argv.typechecked ?? null}
let priority = ${argv.priority ?? null}
if (database == null) {
Database.create({
name: name,
protected: protected,
typechecked: typechecked,
priority: priority,
})
} else if (protected == database.protected && typechecked == database.typechecked && priority == database.priority) {
database
} else {
abort(database)
}`,
options: { format: "decorated" },
});
const written = stderrStream.getWritten();
expect(written).to.contain("[CreateDatabase] Database 'Foo' created.");
expect(written).to.contain('"name": "Foo"');
});
});

it("Creates and starts a container when none exists", async () => {
setupCreateContainerMocks();
await run("local --no-color", container);
expect(unpauseStub).not.to.have.been.called;
expect(startStub).to.have.been.called;
Expand Down Expand Up @@ -153,21 +209,7 @@ Please pass a --hostPort other than '8443'.",
});

it("The user can control the hostIp, hostPort, containerPort, and name", async () => {
docker.pull.onCall(0).resolves();
docker.modem.followProgress.callsFake((stream, onFinished) => {
onFinished();
});
docker.listContainers.onCall(0).resolves([]);
fetch.onCall(0).resolves(f({})); // fast succeed the health check
logsStub.callsFake(async () => ({
on: () => {},
destroy: () => {},
}));
docker.createContainer.resolves({
start: startStub,
logs: logsStub,
unpause: unpauseStub,
});
setupCreateContainerMocks();
await run(
"local --no-color --hostPort 10 --containerPort 11 --name Taco --hostIp 127.0.0.1",
container,
Expand All @@ -193,17 +235,7 @@ Please pass a --hostPort other than '8443'.",
});

it("Skips pull if --pull is false.", async () => {
docker.listContainers.onCall(0).resolves([]);
fetch.onCall(0).resolves(f({})); // fast succeed the health check
logsStub.callsFake(async () => ({
on: () => {},
destroy: () => {},
}));
docker.createContainer.resolves({
start: startStub,
logs: logsStub,
unpause: unpauseStub,
});
setupCreateContainerMocks();
await run("local --no-color --pull false", container);
expect(docker.pull).not.to.have.been.called;
expect(docker.modem.followProgress).not.to.have.been.called;
Expand All @@ -216,18 +248,8 @@ Please pass a --hostPort other than '8443'.",
});

it("Fails start with a prompt to contact Fauna if pull fails.", async () => {
setupCreateContainerMocks();
docker.pull.onCall(0).rejects(new Error("Remote repository not found"));
docker.listContainers.onCall(0).resolves([]);
fetch.onCall(0).resolves(f({})); // fast succeed the health check
logsStub.callsFake(async () => ({
on: () => {},
destroy: () => {},
}));
docker.createContainer.resolves({
start: startStub,
logs: logsStub,
unpause: unpauseStub,
});
try {
await run("local --no-color", container);
throw new Error("Expected an error to be thrown.");
Expand All @@ -248,26 +270,7 @@ https://support.fauna.com/hc/en-us/requests/new`,
});

it("Throws an error if the health check fails", async () => {
docker.pull.onCall(0).resolves();
docker.modem.followProgress.callsFake((stream, onFinished) => {
onFinished();
});
docker.listContainers.onCall(0).resolves([
{
State: "created",
Names: ["/faunadb"],
Ports: [{ PublicPort: 8443 }],
},
]);
logsStub.callsFake(async () => ({
on: () => {},
destroy: () => {},
}));
docker.getContainer.onCall(0).returns({
logs: logsStub,
start: startStub,
unpause: unpauseStub,
});
setupCreateContainerMocks();
fetch.onCall(0).rejects();
fetch.resolves(f({}, 503)); // fail from http
try {
Expand Down Expand Up @@ -489,6 +492,6 @@ Please use a new name via arguments --name <newName> --hostPort ${desiredPort} \
to start the container.`,
);
expect(written).not.to.contain("An unexpected");
expect(written).to.contain("fauna local"); // help text
expect(written).not.to.contain("fauna local"); // help text
});
});

0 comments on commit 641fe76

Please sign in to comment.