-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bridge: Add /machines object to represent/update machines.json
As a prerequisite for reorganizing machines.json, add a new /machines D-Bus object to the bridge which enumerates the configured machines in a "Machines" property and offers an Update method to add/update properties of a machine. Add new test-machines.js test to cover the new D-Bus interface: translating file contents into the D-Bus property, updating properties, and adding hosts. The "invalid data types" test triggers a memleak in json-glib (https://bugzilla.gnome.org/show_bug.cgi?id=778674), so add a suppression file for it. Closes #5880 Reviewed-by: Stef Walter <stefw@redhat.com>
- Loading branch information
1 parent
47ff9c4
commit 9987c3c
Showing
9 changed files
with
516 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* global cockpit, QUnit */ | ||
|
||
/* To help with future migration */ | ||
var assert = QUnit; | ||
|
||
var dbus = cockpit.dbus(null, { "bus": "internal" }); | ||
var dataDir; | ||
|
||
/*** | ||
* Tests for parsing on-disk JSON configuration | ||
*/ | ||
|
||
function machinesParseTest(json, expectedProperty) { | ||
assert.expect(3); | ||
|
||
cockpit.file(dataDir + "/machines.json").replace(json). | ||
done(function(tag) { | ||
dbus.call("/machines", "org.freedesktop.DBus.Properties", | ||
"Get", [ "cockpit.Machines", "Machines" ], | ||
{ "type": "ss" }) | ||
.done(function(reply) { | ||
assert.equal(reply[0].t, "a{sa{sv}}", "expected return type"); | ||
assert.deepEqual(reply[0].v, expectedProperty, "expected property value"); | ||
}) | ||
.always(function() { | ||
assert.equal(this.state(), "resolved", "finished successfully"); | ||
QUnit.start(); | ||
}); | ||
}); | ||
} | ||
|
||
QUnit.asyncTest("no machine definitions", function() { | ||
machinesParseTest(null, {}); | ||
}); | ||
|
||
QUnit.asyncTest("empty json", function() { | ||
machinesParseTest("", {}); | ||
}); | ||
|
||
QUnit.asyncTest("two definitions", function() { | ||
machinesParseTest('{"green": {"visible": true, "address": "1.2.3.4"}, ' + | ||
' "9.8.7.6": {"visible": false, "address": "9.8.7.6", "user": "admin"}}', | ||
{ "green": { | ||
"address": { "t": "s", "v": "1.2.3.4" }, | ||
"visible": { "t": "b", "v": true } | ||
}, | ||
"9.8.7.6": { | ||
"address": { "t": "s", "v": "9.8.7.6" }, | ||
"user": { "t": "s", "v": "admin" }, | ||
"visible": { "t": "b", "v": false } | ||
} | ||
}); | ||
}); | ||
|
||
QUnit.asyncTest("invalid json", function() { | ||
machinesParseTest('{"green":', {}); | ||
}); | ||
|
||
QUnit.asyncTest("invalid data types", function() { | ||
machinesParseTest('{"green": []}', {}); | ||
}); | ||
|
||
/*** | ||
* Tests for Update() | ||
*/ | ||
|
||
function machinesUpdateTest(origJson, host, props, expectedJson) | ||
{ | ||
assert.expect(3); | ||
|
||
cockpit.file(dataDir + "/machines.json").replace(origJson). | ||
done(function(tag) { | ||
dbus.call("/machines", "cockpit.Machines", "Update", [ host, props ], { "type": "sa{sv}" }) | ||
.done(function(reply) { | ||
assert.deepEqual(reply, [], "no expected return value"); | ||
cockpit.file(dataDir + "/machines.json", { syntax: JSON }).read(). | ||
done(function(content, tag) { | ||
assert.deepEqual(content, expectedJson, "expected file content"); | ||
}). | ||
fail(function(err) { | ||
assert.equal(err, undefined, "expected no error"); | ||
}). | ||
always(QUnit.start); | ||
}) | ||
.always(function() { | ||
assert.equal(this.state(), "resolved", "finished successfully"); | ||
}); | ||
}); | ||
} | ||
|
||
QUnit.asyncTest("no config files", function() { | ||
machinesUpdateTest(null, "green", { "visible": cockpit.variant('b', true), "address": cockpit.variant('s', "1.2.3.4") }, | ||
{ "green": { "address": "1.2.3.4", "visible": true } }); | ||
}); | ||
|
||
QUnit.asyncTest("add host to existing map", function() { | ||
machinesUpdateTest('{"green": {"visible": true, "address": "1.2.3.4"}}', | ||
"blue", | ||
{ "visible": cockpit.variant('b', false), "address": cockpit.variant('s', "9.8.7.6") }, | ||
{ "green": { "address": "1.2.3.4", "visible": true }, | ||
"blue": { "address": "9.8.7.6", "visible": false} }); | ||
}); | ||
|
||
QUnit.asyncTest("change bool host property", function() { | ||
machinesUpdateTest('{"green": {"visible": true, "address": "1.2.3.4"}}', | ||
"green", | ||
{ "visible": cockpit.variant('b', false)}, | ||
{ "green": { "address": "1.2.3.4", "visible": false } }); | ||
}); | ||
|
||
QUnit.asyncTest("change string host property", function() { | ||
machinesUpdateTest('{"green": {"visible": true, "address": "1.2.3.4"}}', | ||
"green", | ||
{ "address": cockpit.variant('s', "fe80::1")}, | ||
{ "green": { "address": "fe80::1", "visible": true } }); | ||
}); | ||
|
||
QUnit.asyncTest("add host property", function() { | ||
machinesUpdateTest('{"green": {"visible": true, "address": "1.2.3.4"}}', | ||
"green", | ||
{ "color": cockpit.variant('s', "pitchblack")}, | ||
{ "green": { "address": "1.2.3.4", "visible": true, "color": "pitchblack" } }); | ||
}); | ||
|
||
|
||
/* The test cockpit-bridge gets started with a temp COCKPIT_DATA_DIR instead | ||
* of defaulting to /var/lib/cockpit/. Read it from the bridge so that we can | ||
* put our test files into it. */ | ||
var proxy = dbus.proxy("cockpit.Environment", "/environment"); | ||
proxy.wait(function () { | ||
dataDir = proxy.Variables["COCKPIT_DATA_DIR"]; | ||
QUnit.start(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.