| 1 |
/* |
| 2 |
* Copyright (C) 2014 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import QUnit from "qunit-tests"; |
| 7 |
import { journal } from "journal"; |
| 8 |
|
| 9 |
const debug = false; |
| 10 |
|
| 11 |
function pk(label, obj) { |
| 12 |
let str = label; |
| 13 |
if (obj) { |
| 14 |
str += ": "; |
| 15 |
str += obj.toSource(); |
| 16 |
} |
| 17 |
console.log(str); |
| 18 |
} |
| 19 |
|
| 20 |
function dbg(label, obj) { |
| 21 |
if (debug) |
| 22 |
pk(label, obj); |
| 23 |
} |
| 24 |
|
| 25 |
const time = 0; |
| 26 |
let bootid = 0; |
| 27 |
|
| 28 |
function make_entry(message) { |
| 29 |
return { |
| 30 |
__REALTIME_TIMESTAMP: time.toString(), |
| 31 |
__CURSOR: "fake", |
| 32 |
_BOOT_ID: bootid.toString(), |
| 33 |
_COMM: "fake", |
| 34 |
UNUSED_FIELD: "12", |
| 35 |
SYSLOG_IDENTIFIER: "fake", |
| 36 |
PRIORITY: "3", |
| 37 |
MESSAGE: message |
| 38 |
}; |
| 39 |
} |
| 40 |
|
| 41 |
function reboot() { |
| 42 |
bootid += 1; |
| 43 |
} |
| 44 |
|
| 45 |
let output; |
| 46 |
let renderer; |
| 47 |
let expected_day; |
| 48 |
|
| 49 |
const funcs = { |
| 50 |
render_line: (ident, prio, message, count, time, cursor) => ({ message, count }), |
| 51 |
render_day_header: day => ({ day }), |
| 52 |
render_reboot_separator: () => ({ reboot: true }), |
| 53 |
|
| 54 |
append: elt => { |
| 55 |
dbg('append', elt); |
| 56 |
output.push(elt); |
| 57 |
}, |
| 58 |
remove_last: () => { |
| 59 |
dbg('remove-last'); |
| 60 |
output = output.slice(0, output.length - 1); |
| 61 |
}, |
| 62 |
prepend: elt => { |
| 63 |
dbg('prepend', elt); |
| 64 |
output.unshift(elt); |
| 65 |
}, |
| 66 |
remove_first: () => { |
| 67 |
dbg('remove-first'); |
| 68 |
output = output.slice(1, output.length); |
| 69 |
} |
| 70 |
}; |
| 71 |
|
| 72 |
function append(message) { |
| 73 |
renderer.append(make_entry(message)); |
| 74 |
} |
| 75 |
|
| 76 |
function append_flush() { |
| 77 |
renderer.append_flush(); |
| 78 |
} |
| 79 |
|
| 80 |
function prepend(message) { |
| 81 |
renderer.prepend(make_entry(message)); |
| 82 |
} |
| 83 |
|
| 84 |
function prepend_flush() { |
| 85 |
renderer.prepend_flush(); |
| 86 |
} |
| 87 |
|
| 88 |
function jexpect(assert, label, expected) { |
| 89 |
function jequal(a, b) { |
| 90 |
if (a.day) |
| 91 |
return a.day == b.day; |
| 92 |
else if (a.message) |
| 93 |
return a.message == b.message && a.count == b.count; |
| 94 |
else if (a.reboot) |
| 95 |
return a.reboot == b.reboot; |
| 96 |
else |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
function check(expected) { |
| 101 |
if (output.length != expected.length) |
| 102 |
return false; |
| 103 |
for (let i = 0; i < output.length; i++) |
| 104 |
if (!jequal(output[i], expected[i])) |
| 105 |
return false; |
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
assert.ok(check(expected), label); |
| 110 |
} |
| 111 |
|
| 112 |
const month_names = [ |
| 113 |
'January', |
| 114 |
'February', |
| 115 |
'March', |
| 116 |
'April', |
| 117 |
'May', |
| 118 |
'June', |
| 119 |
'July', |
| 120 |
'August', |
| 121 |
'September', |
| 122 |
'October', |
| 123 |
'November', |
| 124 |
'December' |
| 125 |
]; |
| 126 |
|
| 127 |
QUnit.testStart(function() { |
| 128 |
output = []; |
| 129 |
renderer = journal.renderer(funcs); |
| 130 |
const d = new Date(time); |
| 131 |
expected_day = month_names[d.getMonth()] + ' ' + d.getDate().toFixed() + ', ' + d.getFullYear().toFixed(); |
| 132 |
}); |
| 133 |
|
| 134 |
QUnit.test("append", assert => { |
| 135 |
append('foo'); |
| 136 |
append('foo'); |
| 137 |
append_flush(); |
| 138 |
jexpect(assert, 'two repeated lines', |
| 139 |
[{ day: expected_day }, |
| 140 |
{ message: 'foo', count: 2 } |
| 141 |
]); |
| 142 |
append('foo'); |
| 143 |
append_flush(); |
| 144 |
jexpect(assert, 'three repeated lines after flush', |
| 145 |
[{ day: expected_day }, |
| 146 |
{ message: 'foo', count: 3 } |
| 147 |
]); |
| 148 |
}); |
| 149 |
|
| 150 |
QUnit.test('prepend', assert => { |
| 151 |
prepend('foo'); |
| 152 |
prepend('foo'); |
| 153 |
prepend_flush(); |
| 154 |
jexpect(assert, 'two repeated lines', |
| 155 |
[{ day: expected_day }, |
| 156 |
{ message: 'foo', count: 2 } |
| 157 |
]); |
| 158 |
prepend('foo'); |
| 159 |
prepend_flush(); |
| 160 |
jexpect(assert, 'three repeated lines after flush', |
| 161 |
[{ day: expected_day }, |
| 162 |
{ message: 'foo', count: 3 } |
| 163 |
]); |
| 164 |
}); |
| 165 |
|
| 166 |
QUnit.test('prepend after append', assert => { |
| 167 |
append('foo'); |
| 168 |
append_flush(); |
| 169 |
prepend('foo'); |
| 170 |
prepend_flush(); |
| 171 |
jexpect(assert, 'two repeated lines', |
| 172 |
[{ day: expected_day }, |
| 173 |
{ message: 'foo', count: 2 } |
| 174 |
]); |
| 175 |
}); |
| 176 |
|
| 177 |
QUnit.test('prepend after append', assert => { |
| 178 |
append('foo'); |
| 179 |
append_flush(); |
| 180 |
prepend('bar'); |
| 181 |
prepend_flush(); |
| 182 |
jexpect(assert, 'two different lines', |
| 183 |
[{ day: expected_day }, |
| 184 |
{ message: 'bar', count: 1 }, |
| 185 |
{ message: 'foo', count: 1 } |
| 186 |
]); |
| 187 |
}); |
| 188 |
|
| 189 |
QUnit.test('append after prepend', assert => { |
| 190 |
prepend('foo'); |
| 191 |
prepend_flush(); |
| 192 |
append('foo'); |
| 193 |
append_flush(); |
| 194 |
jexpect(assert, 'two repeated lines', |
| 195 |
[{ day: expected_day }, |
| 196 |
{ message: 'foo', count: 2 } |
| 197 |
]); |
| 198 |
}); |
| 199 |
|
| 200 |
QUnit.test('append after split', assert => { |
| 201 |
prepend('bar'); |
| 202 |
prepend('baz'); |
| 203 |
prepend_flush(); |
| 204 |
append('foo'); |
| 205 |
append_flush(); |
| 206 |
jexpect(assert, 'two different lines', |
| 207 |
[{ day: expected_day }, |
| 208 |
{ message: 'baz', count: 1 }, |
| 209 |
{ message: 'bar', count: 1 }, |
| 210 |
{ message: 'foo', count: 1 } |
| 211 |
]); |
| 212 |
}); |
| 213 |
|
| 214 |
QUnit.test('append after split', assert => { |
| 215 |
prepend('bar'); |
| 216 |
prepend('baz'); |
| 217 |
prepend_flush(); |
| 218 |
append('bar'); |
| 219 |
append_flush(); |
| 220 |
jexpect(assert, 'two repeated lines', |
| 221 |
[{ day: expected_day }, |
| 222 |
{ message: 'baz', count: 1 }, |
| 223 |
{ message: 'bar', count: 2 }, |
| 224 |
]); |
| 225 |
}); |
| 226 |
|
| 227 |
QUnit.test('prepend after split', assert => { |
| 228 |
append('foo'); |
| 229 |
append('bar'); |
| 230 |
append_flush(); |
| 231 |
prepend('baz'); |
| 232 |
prepend_flush(); |
| 233 |
jexpect(assert, 'two different lines', |
| 234 |
[{ day: expected_day }, |
| 235 |
{ message: 'baz', count: 1 }, |
| 236 |
{ message: 'foo', count: 1 }, |
| 237 |
{ message: 'bar', count: 1 }, |
| 238 |
]); |
| 239 |
}); |
| 240 |
|
| 241 |
QUnit.test('prepend after split', assert => { |
| 242 |
append('foo'); |
| 243 |
append('bar'); |
| 244 |
append_flush(); |
| 245 |
prepend('foo'); |
| 246 |
prepend_flush(); |
| 247 |
jexpect(assert, 'two repeated lines', |
| 248 |
[{ day: expected_day }, |
| 249 |
{ message: 'foo', count: 2 }, |
| 250 |
{ message: 'bar', count: 1 }, |
| 251 |
]); |
| 252 |
}); |
| 253 |
|
| 254 |
QUnit.test('reboot', assert => { |
| 255 |
append('foo'); |
| 256 |
append('foo'); |
| 257 |
reboot(); |
| 258 |
append('foo'); |
| 259 |
append_flush(); |
| 260 |
jexpect(assert, 'two repeated lines before reboot', |
| 261 |
[{ day: expected_day }, |
| 262 |
{ message: 'foo', count: 2 }, |
| 263 |
{ reboot: true }, |
| 264 |
{ message: 'foo', count: 1 }, |
| 265 |
]); |
| 266 |
}); |
| 267 |
|
| 268 |
QUnit.test('prepend to reboot same day', assert => { |
| 269 |
append('foo'); |
| 270 |
append('baz'); |
| 271 |
append_flush(); |
| 272 |
reboot(); |
| 273 |
prepend('bar'); |
| 274 |
prepend_flush(); |
| 275 |
jexpect(assert, 'different lines', |
| 276 |
[{ day: expected_day }, |
| 277 |
{ message: 'bar', count: 1 }, |
| 278 |
{ reboot: true }, |
| 279 |
{ message: 'foo', count: 1 }, |
| 280 |
{ message: 'baz', count: 1 }, |
| 281 |
]); |
| 282 |
}); |
| 283 |
|
| 284 |
QUnit.test('prepend to reboot same day', assert => { |
| 285 |
append('foo'); |
| 286 |
append('baz'); |
| 287 |
append_flush(); |
| 288 |
reboot(); |
| 289 |
prepend('foo'); |
| 290 |
prepend_flush(); |
| 291 |
jexpect(assert, 'repeated line', |
| 292 |
[{ day: expected_day }, |
| 293 |
{ message: 'foo', count: 1 }, |
| 294 |
{ reboot: true }, |
| 295 |
{ message: 'foo', count: 1 }, |
| 296 |
{ message: 'baz', count: 1 }, |
| 297 |
]); |
| 298 |
}); |
| 299 |
|
| 300 |
QUnit.start(); |