Skip to content

Commit

Permalink
plugins: add tests for postmarketOS
Browse files Browse the repository at this point in the history
Change-Id: I7471b54981a3f3ffbe2f05909536532e5fa15d91
Signed-off-by: Maciej Sopylo <[email protected]>
  • Loading branch information
maciek134 authored and amartinz committed Sep 7, 2022
1 parent 4239e59 commit 75a8675
Show file tree
Hide file tree
Showing 3 changed files with 272 additions and 28 deletions.
132 changes: 132 additions & 0 deletions src/core/plugins/postmarketos/api.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
const axios = require("axios");
jest.mock("axios");
axios.create.mockReturnValue(axios);
const api = require("./api.js");

const MOCK_DATA = {
releases: [
{
name: "edge",
devices: [
{
name: "somedevice",
interfaces: [
{ name: "phosh" },
{
name: "plasma-mobile",
images: [
{
timestamp: 0,
url: "someurl",
sha256: "sha256-first"
},
{
timestamp: 0,
url: "someurl2",
sha256: "sha256-other"
}
]
},
{ name: "sxmo-de-sway" },
{ name: "other" }
]
}
]
}
]
};

describe("postmarketos api", () => {
beforeEach(() => {
axios.get.mockResolvedValueOnce({
data: MOCK_DATA
});
});

describe("getInterfaces()", () => {
it("should resolve interfaces", async () => {
const result = await api.getInterfaces("somedevice");
expect(result).toContainEqual({
value: "phosh",
label: "Phosh"
});
expect(result).toContainEqual({
value: "plasma-mobile",
label: "Plasma Mobile"
});
expect(result).toContainEqual({
value: "sxmo-de-sway",
label: "SXMO Sway"
});
expect(result).toContainEqual({
value: "other",
label: "other"
});
});

it("should reject on errors", async () => {
axios.get.mockReset();
axios.get.mockRejectedValueOnce({
response: {
status: 404
}
});

const test = () => api.getInterfaces("nonexistent");
await expect(test).rejects.toThrow("404");

axios.get.mockRejectedValueOnce(new Error("other"));
await expect(test).rejects.toThrow("other");
});
});

describe("getImages()", () => {
it("should resolve images", async () => {
const result = await api.getImages("edge", "plasma-mobile", "somedevice");
expect(result).toContainEqual({
url: "someurl",
checksum: {
sum: "sha256-first",
algorithm: "sha256"
}
});
});

it("should throw on 404", async () => {
axios.get.mockReset();
axios.get.mockRejectedValueOnce({
response: {
status: 404
}
});

const test = () => api.getImages("non", "existent", "stuff");
await expect(test).rejects.toThrow("404");

axios.get.mockRejectedValueOnce(new Error("other"));
await expect(test).rejects.toThrow("other");
});
});

describe("getReleases()", () => {
it("should resolve releases", async () => {
const result = await api.getReleases("somedevice");
expect(result).toEqual(["edge"]);
});

it("should throw on 404", async () => {
axios.get.mockReset();
axios.get.mockRejectedValueOnce({
response: {
status: 404
}
});

const test = () => api.getReleases("nonexistent");
await expect(test).rejects.toThrow("404");

axios.get.mockRejectedValueOnce(new Error("other"));
await expect(test).rejects.toThrow("other");
});
});
});
55 changes: 27 additions & 28 deletions src/core/plugins/postmarketos/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@

const Plugin = require("../plugin.js");
const api = require("./api.js");
const fs = require("fs");
const fs = require("fs/promises");
const path = require("path");
const util = require("util");

const fsRename = util.promisify(fs.rename);

/**
* postmarketOS plugin
Expand Down Expand Up @@ -75,31 +72,33 @@ class PostmarketOSPlugin extends Plugin {
* action rename_unpacked_files
* @returns {Promise<void>}
*/
async action__rename_unpacked_files({ group, files }) {
this.event.emit("user:write:working", "squares");
this.event.emit("user:write:status", "Preparing files", true);
this.event.emit("user:write:under", "Preparing files");
const basepath = path.join(
this.cachePath,
this.props.config.codename,
group
);
files = files.map(file => ({
...file,
path: path.join(basepath, file.name || path.basename(file.url))
}));
action__rename_unpacked_files({ group, files }) {
return Promise.resolve().then(async () => {
this.event.emit("user:write:working", "squares");
this.event.emit("user:write:status", "Preparing files", true);
this.event.emit("user:write:under", "Preparing files");
const basepath = path.join(
this.cachePath,
this.props.config.codename,
group
);
files = files.map(file => ({
...file,
path: path.join(basepath, file.name || path.basename(file.url))
}));

// Detect which image is which type, see: https://gitlab.com/postmarketOS/build.postmarketos.org/-/issues/113
const rootfs_path = files
.find(file => !file.path.endsWith("boot.img.xz"))
.path.replace(/.xz$/, "");
const boot_path = files
.find(file => file.path.endsWith("boot.img.xz"))
.path.replace(/.xz$/, "");
return Promise.all([
fsRename(rootfs_path, path.join(basepath, "rootfs.img")),
fsRename(boot_path, path.join(basepath, "boot.img"))
]);
// Detect which image is which type, see: https://gitlab.com/postmarketOS/build.postmarketos.org/-/issues/113
const rootfs_path = files
.find(file => !file.path.endsWith("boot.img.xz"))
.path.replace(/.xz$/, "");
const boot_path = files
.find(file => file.path.endsWith("boot.img.xz"))
.path.replace(/.xz$/, "");
await Promise.all([
fs.rename(rootfs_path, path.join(basepath, "rootfs.img")),
fs.rename(boot_path, path.join(basepath, "boot.img"))
]);
});
}

/**
Expand Down
113 changes: 113 additions & 0 deletions src/core/plugins/postmarketos/plugin.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const mainEvent = { emit: jest.fn() };
beforeEach(() => mainEvent.emit.mockReset());
const fs = require("fs/promises");
jest.mock("fs/promises", () => ({
rename: jest.fn()
}));
const log = require("../../../lib/log.js");
jest.mock("../../../lib/log.js");
const api = require("./api.js");
jest.mock("./api.js");
const path = require("path");

const cachePath = "surprise.xz/inthename";

const pmosPlugin = new (require("./plugin.js"))(
{
settings: {
release: "somerelease",
interface: "someinterface"
},
config: {
codename: "somecodename"
}
},
cachePath,
mainEvent,
log
);

describe("postmarketos plugin", () => {
describe("action__download()", () => {
it("should download images", async () => {
const files = [{ url: "http://somewebsite.com/somefilename.zip" }];
api.getImages.mockResolvedValueOnce(files);

const ret = await pmosPlugin.action__download();
expect(api.getImages).toHaveBeenCalledWith(
"somerelease",
"someinterface",
"somecodename"
);
expect(ret[0]).toBeDefined();
expect(ret[0].actions).toContainEqual({
"core:download": {
group: "postmarketOS",
files
}
});
expect(ret[0].actions).toContainEqual({
"postmarketos:rename_unpacked_files": {
group: "postmarketOS",
files
}
});
expect(ret[0].actions[1]["core:unpack"].files).toContainEqual({
url: files[0].url,
archive: "somefilename.zip"
});
});
});

describe("action__rename_unpacked_files()", () => {
it("should rename the files", async () => {
jest.spyOn(pmosPlugin.event, "emit").mockReturnValue();

const group = "group";
const basepath = path.join(cachePath, "somecodename", group);
const files = [
{
url: "https://asdf.io/somethingelse.img.xz"
},
{
url: "https://asdf.io/somethingelse-boot.img.xz"
}
];

await pmosPlugin.action__rename_unpacked_files({ group, files });
expect(pmosPlugin.event.emit).toHaveBeenCalledTimes(3);
expect(fs.rename).toHaveBeenCalledWith(
path.join(basepath, "somethingelse.img"),
path.join(basepath, "rootfs.img")
);
expect(fs.rename).toHaveBeenCalledWith(
path.join(basepath, "somethingelse-boot.img"),
path.join(basepath, "boot.img")
);
});
});

describe("remote_values__interfaces()", () => {
it("should get interfaces", async () => {
await pmosPlugin.remote_values__interfaces();

expect(api.getInterfaces).toHaveBeenCalledWith("somecodename");
});
});

describe("remote_values__releases()", () => {
it("should get releases", async () => {
api.getReleases.mockResolvedValueOnce(["a", "b"]);
const result = await pmosPlugin.remote_values__releases();
expect(api.getReleases).toHaveBeenCalledWith("somecodename");
expect(result).toContainEqual({
label: "a",
value: "a"
});
expect(result).toContainEqual({
label: "b",
value: "b"
});
});
});
});

0 comments on commit 75a8675

Please sign in to comment.