Skip to content

Commit

Permalink
fixup! feat(cli): build based on configuration
Browse files Browse the repository at this point in the history
integration tests
  • Loading branch information
tuler committed Oct 15, 2024
1 parent fdc423d commit 379fe2d
Show file tree
Hide file tree
Showing 16 changed files with 7,695 additions and 6,023 deletions.
1 change: 1 addition & 0 deletions apps/cli/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ node_modules
oclif.manifest.json
src/contracts.ts
src/graphql/
test/builder/output
1 change: 1 addition & 0 deletions apps/cli/src/builder/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const build = async (
cwd: destination,
image: sdkImage,
});
break;
}
}
} finally {
Expand Down
2 changes: 2 additions & 0 deletions apps/cli/test/builder/data/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM --platform=linux/riscv64 ubuntu:22.04
ADD ./file1 .
2 changes: 2 additions & 0 deletions apps/cli/test/builder/data/Dockerfile.nonriscv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM scratch
ADD ./file1 .
Binary file added apps/cli/test/builder/data/data.ext2
Binary file not shown.
Binary file added apps/cli/test/builder/data/data.sqfs
Binary file not shown.
Binary file added apps/cli/test/builder/data/data.tar
Binary file not shown.
Empty file.
1 change: 1 addition & 0 deletions apps/cli/test/builder/data/sample1/file1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lorem ipsum
1 change: 1 addition & 0 deletions apps/cli/test/builder/data/sample1/file2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lorem ipsum lorem ipsum
68 changes: 68 additions & 0 deletions apps/cli/test/builder/diretory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import fs from "fs-extra";
import path from "path";
import { afterAll, describe, expect, it } from "vitest";
import { build } from "../../src/builder/directory";
import { DirectoryDriveConfig } from "../../src/config";

describe("directory_builder", () => {
const image = "cartesi/sdk:0.11.0";

it("non-existent directory", async ({ task }) => {
const destination = path.join(__dirname, "output", task.suite!.name);
const drive: DirectoryDriveConfig = {
builder: "directory",
directory: path.join(__dirname, "data", "invalid"),
extraSize: 0,
format: "ext2",
};
await expect(build(task.id, drive, image, destination)).rejects.toThrow(
"no such file or directory",
);
});

it("empty directory", async ({ task }) => {
const destination = path.join(__dirname, "output", task.suite!.name);
const drive: DirectoryDriveConfig = {
builder: "directory",
directory: path.join(__dirname, "data", "empty"),
extraSize: 0,
format: "ext2",
};
await expect(build(task.id, drive, image, destination)).rejects.toThrow(

Check failure on line 31 in apps/cli/test/builder/diretory.test.ts

View workflow job for this annotation

GitHub Actions / build

test/builder/diretory.test.ts > directory_builder > empty directory

AssertionError: expected [Function] to throw error including 'too few blocks' but got 'ENOENT: no such file or directory, ls…' Expected: "too few blocks" Received: "ENOENT: no such file or directory, lstat '/home/runner/work/cli/cli/apps/cli/test/builder/data/empty'" ❯ test/builder/diretory.test.ts:31:9
"too few blocks",
);
});

it("empty directory with extra size", async ({ task }) => {
const destination = path.join(__dirname, "output", task.suite!.name);
const drive: DirectoryDriveConfig = {
builder: "directory",
directory: path.join(__dirname, "data", "empty"),
extraSize: 1024 * 1024, // 1Mb
format: "ext2",
};
await build(task.id, drive, image, destination);
const filename = path.join(destination, `${task.id}.ext2`);
const stat = fs.statSync(filename);
expect(stat.size).toEqual(1069056);
});

it("success", async ({ task }) => {
const destination = path.join(__dirname, "output", task.suite!.name);
const drive: DirectoryDriveConfig = {
builder: "directory",
directory: path.join(__dirname, "data", "sample1"),
extraSize: 0,
format: "ext2",
};
await build(task.id, drive, image, destination);
const filename = path.join(destination, `${task.id}.ext2`);
const stat = fs.statSync(filename);
expect(stat.size).toEqual(32768);
});

afterAll(({ name }) => {
const destination = path.join(__dirname, "output", name);
fs.rmSync(destination, { recursive: true, force: true });
});
});
68 changes: 68 additions & 0 deletions apps/cli/test/builder/docker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import fs from "fs-extra";
import path from "path";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { build } from "../../src/builder/docker";
import { DockerDriveConfig } from "../../src/config";

describe("docker_builder", () => {
const image = "cartesi/sdk:0.11.0";

beforeAll(({ name }) => {
const destination = path.join(__dirname, "output", name);
fs.mkdirpSync(destination);
});

it("should not build withoua correct context", async ({ task }) => {
const destination = path.join(__dirname, "output", task.suite!.name);
const drive: DockerDriveConfig = {
builder: "docker",
context: ".",
dockerfile: "Dockerfile",
extraSize: 0,
format: "ext2",
tags: [],
image: undefined,
target: undefined,
};
await expect(build(task.id, drive, image, destination)).rejects.toThrow(
"exit code 1",
);
});

it("should not build a non-riscv image", async ({ task }) => {
const destination = path.join(__dirname, "output", task.suite!.name);
const drive: DockerDriveConfig = {
builder: "docker",
context: path.join(__dirname, "data"),
dockerfile: path.join(__dirname, "data", "Dockerfile.nonriscv"),
extraSize: 0,
format: "ext2",
tags: [],
image: undefined,
target: undefined,
};
await expect(build(task.id, drive, image, destination)).rejects.toThrow(

Check failure on line 44 in apps/cli/test/builder/docker.test.ts

View workflow job for this annotation

GitHub Actions / build

test/builder/docker.test.ts > docker_builder > should not build a non-riscv image

AssertionError: expected [Function] to throw error including 'Invalid image Architecture: arm64. Ex…' but got 'Invalid image Architecture: amd64. Ex…' Expected: "Invalid image Architecture: arm64. Expected riscv64" Received: "Invalid image Architecture: amd64. Expected riscv64" ❯ test/builder/docker.test.ts:44:9
"Invalid image Architecture: arm64. Expected riscv64",
);
});

it("should build a docker drive", { timeout: 60000 }, async ({ task }) => {
const destination = path.join(__dirname, "output", task.suite!.name);
const drive: DockerDriveConfig = {
builder: "docker",
context: path.join(__dirname, "data"),
dockerfile: path.join(__dirname, "data", "Dockerfile"),
extraSize: 0,
format: "ext2",
tags: [],
image: undefined,
target: undefined,
};
await build(task.id, drive, image, destination);
});

afterAll(({ name }) => {
const destination = path.join(__dirname, "output", name);
fs.rmSync(destination, { recursive: true, force: true });
});
});
49 changes: 49 additions & 0 deletions apps/cli/test/builder/empty.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import fs from "fs-extra";
import path from "path";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { build } from "../../src/builder/empty";
import { EmptyDriveConfig } from "../../src/config";

describe("empty_builder", () => {
const image = "cartesi/sdk:0.11.0";

beforeAll(({ name }) => {
const destination = path.join(__dirname, "output", name);
fs.mkdirpSync(destination);
});

it("invalid size", async ({ task }) => {
const destination = path.join(__dirname, "output", task.suite!.name);
const drive: EmptyDriveConfig = {
builder: "empty",
format: "ext2",
size: 0,
};
await expect(build(task.id, drive, image, destination)).rejects.toThrow(

Check failure on line 22 in apps/cli/test/builder/empty.test.ts

View workflow job for this annotation

GitHub Actions / build

test/builder/empty.test.ts > empty_builder > invalid size

AssertionError: expected [Function] to throw error including 'too few blocks' but got 'Command failed with exit code 1: dock…' - Expected + Received - too few blocks + Command failed with exit code 1: docker run --volume '/home/runner/work/cli/cli/apps/cli/test/builder/output/empty_builder:/work' --workdir /work --interactive --tty --user '1001:127' 'cartesi/sdk:0.11.0' xgenext2fs --block-size 4096 --faketime --size-in-blocks 0 389563529_0_0.ext2 + + the input device is not a TTY ❯ test/builder/empty.test.ts:22:9
"too few blocks",
);
});

it("success", async ({ task }) => {
const destination = path.join(__dirname, "output", task.suite!.name);
const driveName = `${task.id}.ext2`;
const drive: EmptyDriveConfig = {
builder: "empty",
format: "ext2",
size: 1024 * 1024 * 1, // 1Mb
};
await build(task.id, drive, image, destination);

const filename = path.join(destination, driveName);
expect(fs.existsSync(filename)).toBeTruthy();
const stat = await fs.stat(filename);
expect(stat.isFile()).toBeTruthy();
expect(stat.size).toEqual(1 * 1024 * 1024);
});

afterAll(({ name }) => {
const destination = path.join(__dirname, "output", name);
fs.emptyDirSync(destination);
fs.rmSync(destination, { recursive: true, force: true });
});
});
43 changes: 43 additions & 0 deletions apps/cli/test/builder/none.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import fs from "fs-extra";
import path from "path";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { build } from "../../src/builder/none";
import { ExistingDriveConfig } from "../../src/config";

describe("none_build", () => {
beforeAll(({ name }) => {
const destination = path.join(__dirname, "output", name);
fs.mkdirpSync(destination);
});

it("should not build a missing file", async ({ task }) => {
const destination = path.join(__dirname, "output", task.suite!.name);
const drive: ExistingDriveConfig = {
builder: "none",
filename: path.join(__dirname, "data", "missing.ext2"),
format: "ext2",
};
await expect(build(task.id, drive, destination)).rejects.toThrow(
"no such file or directory",
);
});

it("should just copy an existing drive", async ({ task }) => {
const destination = path.join(__dirname, "output", task.suite!.name);
const filename = path.join(__dirname, "data", "data.ext2");
const drive: ExistingDriveConfig = {
builder: "none",
filename,
format: "ext2",
};
await build(task.id, drive, destination);
const src = fs.statSync(filename);
const dest = fs.statSync(path.join(destination, `${task.id}.ext2`));
expect(dest.size).toEqual(src.size);
});

afterAll(({ name }) => {
const destination = path.join(__dirname, "output", name);
fs.rmSync(destination, { recursive: true, force: true });
});
});
38 changes: 38 additions & 0 deletions apps/cli/test/builder/tar.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import fs from "fs-extra";
import path from "path";
import { afterAll, describe, expect, it } from "vitest";
import { build } from "../../src/builder/tar";
import { TarDriveConfig } from "../../src/config";

describe("tar_builder", () => {
const image = "cartesi/sdk:0.11.0";

it("should not build a missing file", async ({ task }) => {
const destination = path.join(__dirname, "output", task.suite!.name);
const drive: TarDriveConfig = {
builder: "tar",
filename: path.join(__dirname, "data", "unexisting.tar"),
extraSize: 0,
format: "ext2",
};
await expect(build(task.id, drive, image, destination)).rejects.toThrow(
"no such file or directory",
);
});

it("should build a tar drive", async ({ task }) => {
const destination = path.join(__dirname, "output", task.suite!.name);
const drive: TarDriveConfig = {
builder: "tar",
filename: path.join(__dirname, "data", "data.tar"),
extraSize: 0,
format: "ext2",
};
await build(task.id, drive, image, destination);
});

afterAll(({ name }) => {
const destination = path.join(__dirname, "output", name);
fs.rmSync(destination, { recursive: true, force: true });
});
});
Loading

0 comments on commit 379fe2d

Please sign in to comment.