Debian Codegraph @

pkg/shell/machines/test-machines.js

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 import cockpit from "cockpit";
3 import QUnit, { skipWithPybridge } from "qunit-tests";
4 import { machines } from "./machines.js";
5
6 const dbus = cockpit.dbus(null, { bus: "internal" });
7 let configDir;
8
9 QUnit.module("machines.d parsing tests", {
10 beforeEach: () => cockpit.spawn(["mkdir", "-p", configDir + "/cockpit/machines.d"]),
11 afterEach: () => cockpit.spawn(["rm", "-rf", configDir + "/cockpit/machines.d"]),
12 after: () => cockpit.spawn(["rm", "-r", configDir]),
13 });
14
15 /***
16 * Tests for parsing on-disk JSON configuration
17 */
18
19 async function machinesParseTest(assert, machines_defs, expectedProperty, mode = null) {
20 for (const fname in machines_defs) {
21 let path = fname;
22 if (fname.indexOf('/') < 0)
23 path = configDir + "/cockpit/machines.d/" + fname;
24 await cockpit.file(path).replace(machines_defs[fname]);
25 if (mode !== null)
26 await cockpit.script(`chmod ${mode} ${path}`);
27 }
28
29 const reply = await dbus.call("/machines", "org.freedesktop.DBus.Properties", "Get",
30 ["cockpit.Machines", "Machines"], { type: "ss" });
31 assert.equal(reply[0].t, "a{sa{sv}}", "expected return type");
32 assert.deepEqual(reply[0].v, expectedProperty, "expected property value");
33 }
34
35 QUnit.test("no machine definitions", async assert => machinesParseTest(assert, {}, {}));
36
37 QUnit.test("empty json", async assert => machinesParseTest(assert, { "01.json": "" }, {}));
38
39 QUnit.test("two definitions", async assert => machinesParseTest(
40 assert,
41 {
42 "01.json": '{"green": {"visible": true, "address": "1.2.3.4"}, ' +
43 ' "9.8.7.6": {"visible": false, "address": "9.8.7.6", "user": "admin"}}'
44 },
45 {
46 green: {
47 address: { t: "s", v: "1.2.3.4" },
48 visible: { t: "b", v: true }
49 },
50 "9.8.7.6": {
51 address: { t: "s", v: "9.8.7.6" },
52 user: { t: "s", v: "admin" },
53 visible: { t: "b", v: false }
54 }
55 })
56 );
57
58 QUnit.test("invalid json", async assert => machinesParseTest(assert, { "01.json": '{"green":' }, {}));
59
60 QUnit.test("no read permissions", async assert => machinesParseTest(assert, { "02.json": '{"green": {} }' }, {}, 0));
61
62 skipWithPybridge("invalid data types", async assert => machinesParseTest(assert, { "01.json": '{"green": []}' }, {}));
63
64 skipWithPybridge("merge several JSON files", async assert => machinesParseTest(
65 assert,
66 /* 99-webui.json changes a property in green, adds a
67 * property to blue, and adds an entire new host yellow */
68 {
69 "01-green.json": '{"green": {"visible": true, "address": "1.2.3.4"}}',
70 "02-blue.json": '{"blue": {"address": "9.8.7.6"}}',
71 "09-webui.json": '{"green": {"visible": false}, ' +
72 ' "blue": {"user": "joe"}, ' +
73 ' "yellow": {"address": "fe80::1", "user": "sue"}}'
74 },
75 {
76 green: {
77 address: { t: "s", v: "1.2.3.4" },
78 visible: { t: "b", v: false }
79 },
80 blue: {
81 address: { t: "s", v: "9.8.7.6" },
82 user: { t: "s", v: "joe" },
83 },
84 yellow: {
85 address: { t: "s", v: "fe80::1" },
86 user: { t: "s", v: "sue" },
87 }
88 }
89 ));
90
91 skipWithPybridge("merge JSON files with errors", async assert => machinesParseTest(
92 assert,
93 {
94 "01-valid.json": '{"green": {"visible": true, "address": "1.2.3.4"}}',
95 "02-syntax.json": '[a',
96 "03-toptype.json": '["green"]',
97 "04-toptype.json": '{"green": ["visible"]}',
98 "05-valid.json": '{"blue": {"address": "fe80::1"}}',
99 // goodprop should still be considered
100 "06-proptype.json": '{"green": {"visible": [], "address": {"bar": null}, "goodprop": "yeah"}}',
101 "07-valid.json": '{"green": {"user": "joe"}}',
102 "08-empty.json": ''
103 },
104 {
105 green: {
106 address: { t: "s", v: "1.2.3.4" },
107 visible: { t: "b", v: true },
108 user: { t: "s", v: "joe" },
109 goodprop: { t: "s", v: "yeah" },
110 },
111 blue: {
112 address: { t: "s", v: "fe80::1" }
113 }
114 }
115 ));
116
117 /***
118 * Tests for Update()
119 */
120
121 async function machinesUpdateTest(assert, origJson, host, props, expectedJson) {
122 const f = configDir + "/cockpit/machines.d/99-webui.json";
123
124 await cockpit.file(f).replace(origJson);
125 const reply = await dbus.call("/machines", "cockpit.Machines", "Update",
126 ["99-webui.json", host, props], { type: "ssa{sv}" });
127 assert.deepEqual(reply, [], "no expected return value");
128 const content = await cockpit.file(f, { syntax: JSON }).read();
129 assert.deepEqual(content, expectedJson, "expected file content");
130 }
131
132 QUnit.test("no config files", async assert => machinesUpdateTest(
133 assert,
134 null,
135 "green",
136 { visible: cockpit.variant('b', true), address: cockpit.variant('s', "1.2.3.4") },
137 { green: { address: "1.2.3.4", visible: true } }
138 ));
139
140 QUnit.test("add host to existing map", async assert => machinesUpdateTest(
141 assert,
142 '{"green": {"visible": true, "address": "1.2.3.4"}}',
143 "blue",
144 { visible: cockpit.variant('b', false), address: cockpit.variant('s', "9.8.7.6") },
145 {
146 green: { address: "1.2.3.4", visible: true },
147 blue: { address: "9.8.7.6", visible: false }
148 }
149 ));
150
151 QUnit.test("change bool host property", async assert => machinesUpdateTest(
152 assert,
153 '{"green": {"visible": true, "address": "1.2.3.4"}}',
154 "green",
155 { visible: cockpit.variant('b', false) },
156 { green: { address: "1.2.3.4", visible: false } }
157 ));
158
159 QUnit.test("change string host property", async assert => machinesUpdateTest(
160 assert,
161 '{"green": {"visible": true, "address": "1.2.3.4"}}',
162 "green",
163 { address: cockpit.variant('s', "fe80::1") },
164 { green: { address: "fe80::1", visible: true } }
165 ));
166
167 QUnit.test("add host property", async assert => machinesUpdateTest(
168 assert,
169 '{"green": {"visible": true, "address": "1.2.3.4"}}',
170 "green",
171 { color: cockpit.variant('s', "pitchblack") },
172 { green: { address: "1.2.3.4", visible: true, color: "pitchblack" } }
173 ));
174
175 QUnit.test("Update() only writes delta", async assert => {
176 await cockpit.file(configDir + "/cockpit/machines.d/01-green.json")
177 .replace('{"green": {"address": "1.2.3.4"}, "blue": {"address": "fe80::1"}}');
178 return machinesUpdateTest(
179 assert,
180 null,
181 "green",
182 { color: cockpit.variant('s', "pitchblack") },
183 { green: { color: "pitchblack" } });
184 });
185
186 QUnit.test("updating and existing delta file", async assert => {
187 await cockpit.file(configDir + "/cockpit/machines.d/01-green.json")
188 .replace('{"green": {"address": "1.2.3.4"}, "blue": {"address": "fe80::1"}}');
189
190 return machinesUpdateTest(
191 assert,
192 '{"green": {"address": "9.8.7.6", "user": "joe"}}',
193 "green",
194 { color: cockpit.variant('s', "pitchblack") },
195 { green: { address: "9.8.7.6", user: "joe", color: "pitchblack" } });
196 });
197
198 QUnit.test("colors.parse()", assert => {
199 const colors = [
200 ["#960064", "rgb(150, 0, 100)"],
201 ["rgb(150, 0, 100)", "rgb(150, 0, 100)"],
202 ["#ccc", "rgb(204, 204, 204)"],
203 ];
204 assert.expect(colors.length);
205 colors.forEach(color => assert.equal(machines.colors.parse(color[0]), color[1], "parsed color " + color[0]));
206 });
207
208 QUnit.test("no path traversal allowed", async assert => {
209 const filename = configDir + "/cockpit/flag";
210 await cockpit.file(filename)
211 .replace('{"green": {"visible": true, "address": "1.2.3.4"}}');
212
213 const host = "red";
214 const props = { visible: cockpit.variant('b', false), address: cockpit.variant('s', "9.8.7.6") };
215 await assert.rejects(
216 dbus.call("/machines", "cockpit.Machines", "Update",
217 ["../flag", host, props], { type: "ssa{sv}" }),
218 err => err.message.indexOf("Invalid filename") >= 0,
219 "path traversal is rejected");
220 const content = await cockpit.file(filename, { syntax: JSON }).read();
221 assert.deepEqual(content, { green: { visible: true, address: "1.2.3.4" } }, "flag file was not modified");
222 });
223
224 /* The test cockpit-bridge gets started with temp $XDG_CONFIG_DIRS instead of defaulting to /etc/.
225 * Read it from the bridge so that we can put our test files into it. */
226 const proxy = dbus.proxy("cockpit.Environment", "/environment");
227 proxy.wait(() => {
228 configDir = proxy.Variables.XDG_CONFIG_DIRS;
229 QUnit.start();
230 });