| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
import cockpit from "cockpit"; |
| 3 |
import QUnit from "qunit-tests"; |
| 4 |
|
| 5 |
function MockPeer() { |
| 6 |
/* |
| 7 |
* Events triggered here: |
| 8 |
* opened(event, args) |
| 9 |
* recv(event, payload) |
| 10 |
* closed(event, problem) |
| 11 |
*/ |
| 12 |
cockpit.event_target(this); |
| 13 |
|
| 14 |
let channel = null; |
| 15 |
|
| 16 |
/* open: triggered when mock Channel is created */ |
| 17 |
this.onopened = function(event, channel, options) { |
| 18 |
/* nada */ |
| 19 |
}; |
| 20 |
|
| 21 |
/* close event: triggered when mock Channel is closed */ |
| 22 |
this.onclosed = function(event, channel, options) { |
| 23 |
/* nada */ |
| 24 |
}; |
| 25 |
|
| 26 |
/* send a message from peer back to channel */ |
| 27 |
this.send = function(payload) { |
| 28 |
if (typeof (payload) != "string") |
| 29 |
payload = String(payload); |
| 30 |
if (!channel) |
| 31 |
console.log("dropping message before open"); |
| 32 |
else if (channel.valid) |
| 33 |
channel.dispatchEvent("message", payload); |
| 34 |
else |
| 35 |
console.log("dropping message after close"); |
| 36 |
}; |
| 37 |
|
| 38 |
/* send a object as JSON */ |
| 39 |
this.send_json = function(payload) { |
| 40 |
this.send(JSON.stringify(payload)); |
| 41 |
}; |
| 42 |
|
| 43 |
/* peer closes the channel */ |
| 44 |
this.close = function(channel, options) { |
| 45 |
console.assert(channel); |
| 46 |
if (channel.valid) { |
| 47 |
channel.valid = false; |
| 48 |
channel.dispatchEvent("close", options || { }); |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
const peer = this; |
| 53 |
let last_channel = 0; |
| 54 |
|
| 55 |
function MockChannel(options) { |
| 56 |
cockpit.event_target(this); |
| 57 |
this.number = last_channel++; |
| 58 |
this.options = options; |
| 59 |
this.valid = true; |
| 60 |
|
| 61 |
const channel = this; |
| 62 |
|
| 63 |
function Transport() { |
| 64 |
this.close = function(problem) { console.assert(arguments.length == 1) }; |
| 65 |
} |
| 66 |
|
| 67 |
this.transport = new Transport(); |
| 68 |
|
| 69 |
this.send = function(payload) { |
| 70 |
console.assert(arguments.length == 1); |
| 71 |
console.assert(this.valid); |
| 72 |
peer.dispatchEvent("recv", channel, payload); |
| 73 |
}; |
| 74 |
|
| 75 |
this.close = function(options) { |
| 76 |
console.assert(arguments.length <= 1); |
| 77 |
this.valid = false; |
| 78 |
peer.dispatchEvent("close", channel, options || {}); |
| 79 |
}; |
| 80 |
|
| 81 |
QUnit.testDone(function() { |
| 82 |
channel.valid = false; |
| 83 |
}); |
| 84 |
|
| 85 |
peer.dispatchEvent("open", channel, options || {}); |
| 86 |
} |
| 87 |
|
| 88 |
cockpit.channel = function(options) { |
| 89 |
channel = new MockChannel(options); |
| 90 |
return channel; |
| 91 |
}; |
| 92 |
} |
| 93 |
|
| 94 |
function MockSink(expected, callback) { |
| 95 |
const self = this; |
| 96 |
|
| 97 |
self.samples = []; |
| 98 |
|
| 99 |
function input(beg, items, mapping) { |
| 100 |
for (let i = 0; i < items.length; i++) |
| 101 |
self.samples[beg + i] = items[i]; |
| 102 |
} |
| 103 |
|
| 104 |
self.series = { input }; |
| 105 |
return self; |
| 106 |
} |
| 107 |
|
| 108 |
let old_channel; |
| 109 |
QUnit.module("Mock tests", { |
| 110 |
beforeEach: () => { |
| 111 |
old_channel = cockpit.channel; |
| 112 |
}, |
| 113 |
afterEach: () => { |
| 114 |
cockpit.channel = old_channel; |
| 115 |
} |
| 116 |
}); |
| 117 |
|
| 118 |
QUnit.test("non-instanced decompression", assert => { |
| 119 |
assert.expect(1); |
| 120 |
|
| 121 |
const peer = new MockPeer(); |
| 122 |
const sink = new MockSink(); |
| 123 |
|
| 124 |
const metrics = cockpit.metrics(1000, { |
| 125 |
source: "source", |
| 126 |
metrics: [{ name: "m1" }], |
| 127 |
}); |
| 128 |
metrics.series = sink.series; |
| 129 |
|
| 130 |
metrics.follow(); |
| 131 |
peer.send_json({ |
| 132 |
timestamp: 0, |
| 133 |
now: 0, |
| 134 |
interval: 1000, |
| 135 |
metrics: [{ name: "m1" }] |
| 136 |
}); |
| 137 |
peer.send_json([[10]]); |
| 138 |
peer.send_json([[]]); |
| 139 |
|
| 140 |
assert.deepEqual(sink.samples, [[10], [10]], "got correct samples"); |
| 141 |
}); |
| 142 |
|
| 143 |
QUnit.test("interval validation", assert => { |
| 144 |
assert.expect(2); |
| 145 |
const done = assert.async(); |
| 146 |
const metrics_channel = cockpit.channel({ |
| 147 |
payload: "metrics1", |
| 148 |
source: "internal", |
| 149 |
interval: -1, |
| 150 |
}); |
| 151 |
|
| 152 |
metrics_channel.addEventListener("close", (_, error) => { |
| 153 |
assert.equal(error.problem, "protocol-error"); |
| 154 |
assert.equal(error.message, 'invalid "interval" value: -1'); |
| 155 |
done(); |
| 156 |
}); |
| 157 |
}); |
| 158 |
|
| 159 |
QUnit.test("metrics validation", assert => { |
| 160 |
assert.expect(2); |
| 161 |
const done = assert.async(); |
| 162 |
const metrics_channel = cockpit.channel({ |
| 163 |
payload: "metrics1", |
| 164 |
source: "internal", |
| 165 |
interval: 1000, |
| 166 |
metrics: { |
| 167 |
foo: 1 |
| 168 |
} |
| 169 |
}); |
| 170 |
|
| 171 |
metrics_channel.addEventListener("close", (_, error) => { |
| 172 |
assert.equal(error.problem, "protocol-error"); |
| 173 |
assert.equal(error.message, 'invalid "metrics" option was specified (not an array)'); |
| 174 |
done(); |
| 175 |
}); |
| 176 |
}); |
| 177 |
|
| 178 |
QUnit.test("metrics object validation", assert => { |
| 179 |
assert.expect(1); |
| 180 |
const done = assert.async(); |
| 181 |
const metrics_channel = cockpit.channel({ |
| 182 |
payload: "metrics1", |
| 183 |
source: "internal", |
| 184 |
interval: 1000, |
| 185 |
metrics: [ |
| 186 |
{ name: "nonexistant", derive: "rate" }, |
| 187 |
] |
| 188 |
}); |
| 189 |
|
| 190 |
metrics_channel.addEventListener("close", (_, error) => { |
| 191 |
assert.equal(error.problem, "not-supported"); |
| 192 |
done(); |
| 193 |
}); |
| 194 |
}); |
| 195 |
|
| 196 |
QUnit.start(); |