Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed May 13, 2024
1 parent 4be7618 commit 8902050
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
30 changes: 10 additions & 20 deletions web/src/client/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,24 +197,6 @@ const EncryptionMethods = Object.freeze({
TPM: "tpm_fde"
});

/**
* Removes properties with undefined value
*
* @example
* removeUndefinedCockpitProperties({
* property1: { t: "s", v: "foo" },
* property2: { t: b, v: false },
* property3: { t: "s", v: undefined }
* });
* //returns { property1: { t: "s", v: "foo" }, property2: { t: "b", v: false } }
*
* @param {object} cockpitObject
* @returns {object}
*/
const removeUndefinedCockpitProperties = (cockpitObject) => {
const filtered = Object.entries(cockpitObject).filter(([, { v }]) => v !== undefined);
return Object.fromEntries(filtered);
};

/**
* Gets the basename of a D-Bus path
Expand Down Expand Up @@ -373,6 +355,7 @@ class DevicesManager {
const response = await this.client.get(`/storage/devices/${this.rootPath}`);
if (!response.ok) {
console.warn("Failed to get storage devices: ", response);
return [];
}
const jsonDevices = await response.json();
return jsonDevices.map(d => buildDevice(d, jsonDevices));
Expand Down Expand Up @@ -411,6 +394,7 @@ class ProposalManager {
const response = await this.client.get("/storage/proposal/usable_devices");
if (!response.ok) {
console.warn("Failed to get usable devices: ", response);
return [];
}
const usable_devices = await response.json();
return usable_devices.map(name => findDevice(systemDevices, name)).filter(d => d);
Expand Down Expand Up @@ -459,6 +443,7 @@ class ProposalManager {
const response = await this.client.get("/storage/product/params");
if (!response.ok) {
console.warn("Failed to get product params: ", response);
return [];
}

return response.json().then(params => params.mountPoints);
Expand All @@ -473,6 +458,7 @@ class ProposalManager {
const response = await this.client.get("/storage/product/params");
if (!response.ok) {
console.warn("Failed to get product params: ", response);
return [];
}

return response.json().then(params => params.encryptionMethods);
Expand All @@ -482,13 +468,14 @@ class ProposalManager {
* Obtains the default volume for the given mount path
*
* @param {string} mountPath
* @returns {Promise<Volume>}
* @returns {Promise<Volume|undefined>}
*/
async defaultVolume(mountPath) {
const param = encodeURIComponent(mountPath);
const response = await this.client.get(`/storage/product/volume_for?mount_path=${param}`);
if (!response.ok) {
console.warn("Failed to get product volume: ", response);
return undefined;
}

const systemDevices = await this.system.getDevices();
Expand Down Expand Up @@ -1314,7 +1301,7 @@ class ISCSIManager {
/**
* Gets the iSCSI initiator
*
* @return {Promise<ISCSIInitiator>}
* @return {Promise<ISCSIInitiator|undefined>}
*
* @typedef {object} ISCSIInitiator
* @property {string} name
Expand All @@ -1324,6 +1311,7 @@ class ISCSIManager {
const response = await this.client.get("/storage/iscsi/initiator");
if (!response.ok) {
console.error("Failed to get the iSCSI initiator", response);
return undefined;
}

return response.json();
Expand Down Expand Up @@ -1357,6 +1345,7 @@ class ISCSIManager {
const response = await this.client.get("/storage/iscsi/nodes");
if (!response.ok) {
console.error("Failed to get the list of iSCSI nodes", response);
return [];
}

return response.json();
Expand Down Expand Up @@ -1574,6 +1563,7 @@ class StorageBaseClient {
const response = await this.client.get("/storage/devices/dirty");
if (!response.ok) {
console.warn("Failed to get storage devices dirty: ", response);
return false;
}
return response.json();
}
Expand Down
38 changes: 38 additions & 0 deletions web/src/client/storage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,25 @@ describe("#system", () => {
expect(devices).toEqual([]);
});
});

describe("when the HTTP call fails", () => {
beforeEach(() => {
mockGetFn.mockImplementation(path => {
if (path === "/storage/devices/system")
return { ok: false };
else
return { ok: true, json: mockJsonFn };
}
);

client = new StorageClient(http);
});

it("returns an empty list", async () => {
const devices = await client.system.getDevices();
expect(devices).toEqual([]);
});
});
});
});

Expand Down Expand Up @@ -1381,6 +1400,25 @@ describe("#staging", () => {
expect(devices).toEqual([]);
});
});

describe.only("when the HTTP call fails", () => {
beforeEach(() => {
mockGetFn.mockImplementation(path => {
if (path === "/storage/devices/result")
return { ok: false };
else
return { ok: true, json: mockJsonFn };
}
);

client = new StorageClient(http);
});

it("returns an empty list", async () => {
const devices = await client.staging.getDevices();
expect(devices).toEqual([]);
});
});
});
});

Expand Down

0 comments on commit 8902050

Please sign in to comment.