Skip to content

Commit

Permalink
bridge: Add /machines object to represent/update machines.json
Browse files Browse the repository at this point in the history
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
martinpitt authored and stefwalter committed Feb 27, 2017
1 parent 47ff9c4 commit 9987c3c
Show file tree
Hide file tree
Showing 9 changed files with 516 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ VALGRIND_SUPPRESSIONS = \
tools/unknown.supp \
tools/zlib.supp \
tools/libssh.supp \
tools/json-glib.supp \
$(NULL)

valgrind-suppressions: $(VALGRIND_SUPPRESSIONS)
Expand Down
1 change: 1 addition & 0 deletions src/base1/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ base_QUNIT_TESTS = \
dist/base1/test-file.html \
dist/base1/test-metrics.html \
dist/base1/test-user.html \
dist/base1/test-machines.html \
dist/base1/test-permissions.html \
dist/base1/test-series.html \
dist/base1/test-cache.html \
Expand Down
133 changes: 133 additions & 0 deletions src/base1/test-machines.js
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();
});
1 change: 1 addition & 0 deletions src/bridge/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ libcockpit_bridge_a_SOURCES = \
src/bridge/cockpitcpusamples.h \
src/bridge/cockpitdbussetup.c \
src/bridge/cockpitdbususer.c \
src/bridge/cockpitdbusmachines.c \
src/bridge/cockpitdisksamples.c \
src/bridge/cockpitdisksamples.h \
src/bridge/cockpitinternalmetrics.c \
Expand Down
2 changes: 2 additions & 0 deletions src/bridge/bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ run_bridge (const gchar *interactive,
cockpit_dbus_user_startup (pwd);
cockpit_dbus_setup_startup ();
cockpit_dbus_process_startup ();
cockpit_dbus_machines_startup ();

g_free (pwd);
pwd = NULL;
Expand All @@ -548,6 +549,7 @@ run_bridge (const gchar *interactive,
g_object_unref (router);
g_object_unref (transport);

cockpit_dbus_machines_cleanup ();
cockpit_dbus_internal_cleanup ();
cockpit_packages_free (packages);
packages = NULL;
Expand Down
4 changes: 4 additions & 0 deletions src/bridge/cockpitdbusinternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ void cockpit_dbus_setup_startup (void);

void cockpit_dbus_process_startup (void);

void cockpit_dbus_machines_startup (void);

void cockpit_dbus_machines_cleanup (void);

G_END_DECLS

#endif /* __COCKPIT_DBUS_INTERNAL_H */
Loading

0 comments on commit 9987c3c

Please sign in to comment.