| 1 |
/* |
| 2 |
* Copyright (C) 2014 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import * as utils from "./utils.js"; |
| 7 |
import QUnit, { f } from "qunit-tests"; |
| 8 |
|
| 9 |
QUnit.test("format_delay", function (assert) { |
| 10 |
const checks = [ |
| 11 |
[3000, "in less than a minute"], |
| 12 |
[60000, "in 1 minute"], |
| 13 |
[15550000, "in 4 hours"], |
| 14 |
]; |
| 15 |
|
| 16 |
assert.expect(checks.length); |
| 17 |
for (let i = 0; i < checks.length; i++) { |
| 18 |
assert.strictEqual(utils.format_delay(checks[i][0]), checks[i][1], |
| 19 |
"format_delay(" + checks[i][0] + ") = " + checks[i][1]); |
| 20 |
} |
| 21 |
}); |
| 22 |
|
| 23 |
QUnit.test("compare_versions", function (assert) { |
| 24 |
const checks = [ |
| 25 |
["", "", 0], |
| 26 |
["0", "0", 0], |
| 27 |
["1", "0", 1], |
| 28 |
["0", "1", -1], |
| 29 |
["2", "1.9", 1], |
| 30 |
["2.0", "2", 1], |
| 31 |
["2.1.6", "2.5", -1], |
| 32 |
["2..6", "2.0.6", 0], |
| 33 |
]; |
| 34 |
|
| 35 |
function sign(n) { |
| 36 |
return (n === 0) ? 0 : (n < 0) ? -1 : 1; |
| 37 |
} |
| 38 |
|
| 39 |
assert.expect(checks.length); |
| 40 |
for (let i = 0; i < checks.length; i++) { |
| 41 |
assert.strictEqual(sign(utils.compare_versions(checks[i][0], checks[i][1])), checks[i][2], |
| 42 |
"compare_versions(" + checks[i][0] + ", " + checks[i][1] + ") = " + checks[i][2]); |
| 43 |
} |
| 44 |
}); |
| 45 |
|
| 46 |
QUnit.test("mdraid_name_nohostnamed", function (assert) { |
| 47 |
utils.mock_hostnamed({ StaticHostname: undefined }); |
| 48 |
assert.strictEqual(utils.mdraid_name({ Name: "somehost:mydev" }), "mydev", "remote host name is skipped when hostnamed is not available"); |
| 49 |
utils.mock_hostnamed(null); |
| 50 |
}); |
| 51 |
|
| 52 |
QUnit.test("mdraid_name_remote", function (assert) { |
| 53 |
utils.mock_hostnamed({ StaticHostname: "sweethome" }); |
| 54 |
assert.strictEqual(utils.mdraid_name({ Name: "somehost:mydev" }), "mydev (from somehost)", "expected name for remote host"); |
| 55 |
utils.mock_hostnamed(null); |
| 56 |
}); |
| 57 |
|
| 58 |
QUnit.test("mdraid_name_local_static", function (assert) { |
| 59 |
utils.mock_hostnamed({ StaticHostname: "sweethome" }); |
| 60 |
assert.strictEqual(utils.mdraid_name({ Name: "sweethome:mydev" }), "mydev", "expected name for static local host"); |
| 61 |
utils.mock_hostnamed(null); |
| 62 |
}); |
| 63 |
|
| 64 |
QUnit.test("mdraid_name_local_transient", function (assert) { |
| 65 |
utils.mock_hostnamed({ Hostname: "sweethome" }); |
| 66 |
assert.strictEqual(utils.mdraid_name({ Name: "sweethome:mydev" }), "mydev", "expected name for transient local host"); |
| 67 |
utils.mock_hostnamed(null); |
| 68 |
}); |
| 69 |
|
| 70 |
QUnit.test("get_byte_units", function (assert) { |
| 71 |
const mb = 1000 * 1000; |
| 72 |
const gb = mb * 1000; |
| 73 |
const tb = gb * 1000; |
| 74 |
|
| 75 |
const mb_unit = { factor: mb, name: "MB" }; |
| 76 |
const gb_unit = { factor: gb, name: "GB" }; |
| 77 |
const tb_unit = { factor: tb, name: "TB" }; |
| 78 |
|
| 79 |
function selected(unit) { |
| 80 |
return { factor: unit.factor, name: unit.name, selected: true }; |
| 81 |
} |
| 82 |
|
| 83 |
const checks = [ |
| 84 |
[0 * mb, [selected(mb_unit), gb_unit, tb_unit]], |
| 85 |
[20 * mb, [selected(mb_unit), gb_unit, tb_unit]], |
| 86 |
[200 * mb, [selected(mb_unit), gb_unit, tb_unit]], |
| 87 |
[2000 * mb, [selected(mb_unit), gb_unit, tb_unit]], |
| 88 |
[20000 * mb, [mb_unit, selected(gb_unit), tb_unit]], |
| 89 |
[20 * gb, [mb_unit, selected(gb_unit), tb_unit]], |
| 90 |
[200 * gb, [mb_unit, selected(gb_unit), tb_unit]], |
| 91 |
[2000 * gb, [mb_unit, selected(gb_unit), tb_unit]], |
| 92 |
[20000 * gb, [mb_unit, gb_unit, selected(tb_unit)]] |
| 93 |
]; |
| 94 |
|
| 95 |
assert.expect(checks.length); |
| 96 |
for (let i = 0; i < checks.length; i++) { |
| 97 |
assert.deepEqual(utils.get_byte_units(checks[i][0]), checks[i][1], |
| 98 |
"get_byte_units(" + checks[i][0] + ") = " + JSON.stringify(checks[i][1])); |
| 99 |
} |
| 100 |
}); |
| 101 |
|
| 102 |
QUnit.test("format_fsys_usage", function (assert) { |
| 103 |
const [k, M, G, T] = [1_000, 1_000_000, 1_000_000_000, 1_000_000_000_000]; |
| 104 |
|
| 105 |
const sizes = [5, 200, 5 * k, 200 * k, 5 * M, 200 * M, 5 * G, 200 * G, 5 * T, 200 * T]; |
| 106 |
/* For each "total" size, format all of the "used" sizes less than or equal to it. |
| 107 |
* The results table lists the part that should come after and before the slash, respectively. |
| 108 |
* For example: ["5 kB", ["0.01", "0.20", "5"]] |
| 109 |
* means 5, 200 and 5k out of 5k are displayed as "0.01 / 5kB", "0.20 / 5kB" and "5 / 5kB" |
| 110 |
*/ |
| 111 |
const results = [ |
| 112 |
["5 B", ["5"]], |
| 113 |
["200 B", ["5", "200"]], |
| 114 |
["5 kB", ["0.01", "0.20", "5"]], |
| 115 |
["200 kB", ["0.01", "0.20", "5", "200"]], |
| 116 |
["5 MB", ["0.01", "0.01", "0.01", "0.20", "5"]], |
| 117 |
["200 MB", ["0.01", "0.01", "0.01", "0.20", "5", "200"]], |
| 118 |
["5 GB", ["0.01", "0.01", "0.01", "0.01", "0.01", "0.20", "5"]], |
| 119 |
["200 GB", ["0.01", "0.01", "0.01", "0.01", "0.01", "0.20", "5", "200"]], |
| 120 |
["5 TB", ["0.01", "0.01", "0.01", "0.01", "0.01", "0.01", "0.01", "0.20", "5"]], |
| 121 |
["200 TB", ["0.01", "0.01", "0.01", "0.01", "0.01", "0.01", "0.01", "0.20", "5", "200"]], |
| 122 |
]; |
| 123 |
|
| 124 |
for (let total_i = 0; total_i < results.length; total_i++) { |
| 125 |
const [total_string, used_strings] = results[total_i]; |
| 126 |
assert.strictEqual(used_strings.length, total_i + 1); |
| 127 |
for (let used_i = 0; used_i < used_strings.length; used_i++) { |
| 128 |
const used_string = used_strings[used_i]; |
| 129 |
|
| 130 |
const used = sizes[used_i]; |
| 131 |
const total = sizes[total_i]; |
| 132 |
const expected_string = used_string + " / " + total_string; |
| 133 |
|
| 134 |
assert.strictEqual( |
| 135 |
utils.format_fsys_usage(used, total), |
| 136 |
expected_string, |
| 137 |
f`format_fsys_usage(${used}, ${total})` |
| 138 |
); |
| 139 |
} |
| 140 |
} |
| 141 |
}); |
| 142 |
|
| 143 |
/* Wait until the hostnamed dbus proxy is actually ready; otherwise the test |
| 144 |
* finishes and kills the bridge before it can respond to the dbus channel open |
| 145 |
* request for the hostnamed connection, which can cause hangs in |
| 146 |
* ./test-server due to timing out that queued request. */ |
| 147 |
utils.hostnamed.wait(QUnit.start); |