summaryrefslogtreecommitdiff
path: root/jarmon/jarmon.test.js
blob: a2b36be3123d5c9561eec64c230c70262068b1d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/* Copyright (c) Richard Wall
 * See LICENSE for details.
 *
 * Unit tests for Jarmon
 **/

YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) {
    Y.Test.Runner.add(new Y.Test.Case({
        name: "jarmon.downloadBinary",

        test_urlNotFound: function () {
            /**
             * When url cannot be found, the deferred should errback with status
             * 404.
             **/
            var self = this;
            var d = new jarmon.downloadBinary('non-existent-file.html');
            d.always(
                function(ret) {
                    self.resume(function() {
                        Y.Assert.isInstanceOf(Error, ret);
                        Y.Assert.areEqual('error:404', ret.message);
                    });
                });

            this.wait();
        },

        test_urlFound: function () {
            /**
             * When url is found, the deferred should callback with an instance
             * of javascriptrrd.BinaryFile
             **/
            var self = this;
            var d = new jarmon.downloadBinary('testfile.bin');
            d.always(
                function(ret) {
                    self.resume(function() {
                        Y.Assert.isInstanceOf(jarmon.BinaryFile, ret);
                        Y.Assert.areEqual(
                            String.fromCharCode(0), ret.getCharAt(0));
                    });
                });

            this.wait();
        }

    }));

    var RRD_STEP = 10;
    var RRD_DSNAME = 'speed';
    var RRD_DSINDEX = 0;
    var RRD_RRACOUNT = 2;
    var RRD_RRAROWS = 12;
    var RRD_STARTTIME = new Date('1 jan 1980 00:00:00').getTime();
    var RRD_ENDTIME = new Date('1 jan 1980 00:02:00').getTime();

    Y.Test.Runner.add(new Y.Test.Case({
        name: "jarmon.RRDFile",

        setUp: function() {
            var self = this;
            this.d = new jarmon.downloadBinary('build/test.rrd').pipe(
                function(binary) {
                    return new RRDFile(binary);
                },
                function(ret) {
                    console.log(ret);
                });
        },

        test_getLastUpdate: function () {
            /**
             * The generated rrd file should have a lastupdate date of
             * 1980-01-01 00:50:01
             **/
            var self = this;
            this.d.done(
                function(rrd) {
                    self.resume(function() {
                        Y.Assert.areEqual(
                            RRD_ENDTIME/1000+1, rrd.getLastUpdate());
                    });
                });
            this.wait();
        },

        test_getDSIndex: function () {
            /**
             * The generated rrd file should have a single DS whose name is
             * 'speed'. A RangeError is thrown if the requested index or dsName
             * doesnt exist.
             **/
            var self = this;
            this.d.done(
                function(rrd) {
                    self.resume(function() {
                        Y.Assert.areEqual(RRD_DSNAME, rrd.getDS(0).getName());
                        Y.Assert.areEqual(
                            RRD_DSINDEX, rrd.getDS('speed').getIdx());
                        var error = null;
                        try {
                            rrd.getDS(RRD_DSINDEX+1);
                        } catch(e) {
                            error = e;
                        }
                        Y.assert(error instanceof RangeError);
                    });
                });
            this.wait();
        },

        test_getNrRRAs: function () {
            /**
             * The generated rrd file should have a single RRA
             **/
            var self = this;
            this.d.done(
                function(rrd) {
                    self.resume(function() {
                        Y.Assert.areEqual(RRD_RRACOUNT, rrd.getNrRRAs());
                    });
                });
            this.wait();
        },

        test_getRRA: function () {
            /**
             * The generated rrd file should have a single RRA using AVERAGE
             * consolidation, step=10, rows=6 and values 0-5
             * rra.getEl throws a RangeError if asked for row which doesn't
             * exist.
             **/
            var self = this;
            this.d.done(
                function(rrd) {
                    self.resume(function() {
                        var rra = rrd.getRRA(0);
                        Y.Assert.areEqual('AVERAGE', rra.getCFName());
                        Y.Assert.areEqual(RRD_STEP, rra.getStep());
                        Y.Assert.areEqual(RRD_RRAROWS, rra.getNrRows());
                        for(var i=0; i<RRD_RRAROWS; i++) {
                            Y.Assert.areEqual(i, rra.getEl(i, RRD_DSINDEX));
                        }
                        var error = null;
                        try {
                            rra.getEl(RRD_RRAROWS+1, 0);
                        } catch(e) {
                            error = e;
                        }
                        Y.assert(error instanceof RangeError);
                    });
                });
            this.wait();
        }
    }));

    Y.Test.Runner.add(new Y.Test.Case({
        name: "jarmon.RrdQuery",

        setUp: function() {
            this.d = new jarmon.downloadBinary('build/test.rrd').pipe(
                function(binary) {
                    return new RRDFile(binary);
                },
                function(ret) {
                    console.log(ret);
                });
        },

        test_getDataTimeRangeOverlapError: function () {
            /**
             * The starttime must be less than the endtime
             **/
            var self = this;
            this.d.done(
                function(rrd) {
                    self.resume(
                        function() {
                            var rq = new jarmon.RrdQuery(rrd, '');
                            var error;
                            try {
                                rq.getData(1, 0);
                            } catch(e) {
                                error = e;
                            }
                            Y.Assert.isInstanceOf(RangeError, error);
                        });
                });
            this.wait();
        },


        test_getDataUnknownCfError: function () {
            /**
             * Error is raised if the rrd file doesn't contain an RRA with the
             * requested consolidation function (CF)
             **/
            var self = this;
            this.d.done(
                function(rrd) {
                    self.resume(function() {
                        var rq = new jarmon.RrdQuery(rrd, '');
                        var error = null;
                        try {
                            rq.getData(RRD_STARTTIME, RRD_ENDTIME, 0, 'FOO');
                        } catch(e) {
                            error = e;
                        }
                        Y.Assert.isInstanceOf(TypeError, error);
                    });
                });
            this.wait();
        },


        test_getData: function () {
            /**
             * The generated rrd file should have values 0-9 at 300s intervals
             * starting at 1980-01-01 00:00:00
             * Result should include a data points with times > starttime and
             * <= endTime
             **/
            var self = this;
            this.d.done(
                function(rrd) {
                    self.resume(function() {
                        var rq = new jarmon.RrdQuery(rrd, '');

                        /* We request data starting 1 STEP +1s after
                        the RRD file first val and ending 1 STEP -1s
                        before the RRD last val ie one step within the
                        RRD file, but 1s away from the step boundary
                        to test the quantisation of the requested time
                        range.*/
                        var data = rq.getData(
                            RRD_STARTTIME + (RRD_STEP+1) * 1000,
                            RRD_ENDTIME - (RRD_STEP-1) * 1000);

                        // so we expect two less rows than the total rows in the
                        // file.
                        Y.Assert.areEqual(RRD_RRAROWS-2, data.data.length);

                        // The value of the first returned row should be the
                        // second value in the RRD file (starts at value 0)
                        Y.Assert.areEqual(1, data.data[0][1]);

                        // The value of the last returned row should be the
                        // 10 value in the RRD file (starts at value 0)
                        Y.Assert.areEqual(10, data.data[data.data.length-1][1]);

                        // The timestamp of the first returned row should be
                        // exactly one step after the start of the RRD file
                        Y.Assert.areEqual(
                            RRD_STARTTIME+RRD_STEP*1000, data.data[0][0]);

                        // RRD_ENDTIME is on a step boundary and is therfore
                        // actually the start time of a new row
                        // So when we ask for endTime = RRD_ENDTIME-STEP-1 we
                        // actually get data up to the 2nd to last RRD row.
                        Y.Assert.areEqual(
                            RRD_ENDTIME-RRD_STEP*1000*2,
                            data.data[data.data.length-1][0]);
                    });
                });
            this.wait();
        },

        test_getDataUnknownValues: function () {
            /**
             * If the requested time range is outside the range of the RRD file
             * we should not get any values back
             **/
            var self = this;
            this.d.done(
                function(rrd) {
                    self.resume(function() {
                        var rq = new jarmon.RrdQuery(rrd, '');
                        var data = rq.getData(RRD_ENDTIME, RRD_ENDTIME+1000);
                        Y.Assert.areEqual(0, data.data.length);
                    });
                });
            this.wait();
        }

    }));


    Y.Test.Runner.add(new Y.Test.Case({
        name: "jarmon.RrdQueryRemote",

        setUp: function() {
            this.rq = new jarmon.RrdQueryRemote('build/test.rrd', '');
        },

        test_getDataTimeRangeOverlapError: function () {
            /**
             * The starttime must be less than the endtime
             **/
            var self = this;
            this.rq.getData(1, 0).fail(
                function(res) {
                    self.resume(function() {
                        Y.Assert.isInstanceOf(RangeError, res);
                    });
                });
            this.wait();
        },


        test_getDataUnknownCfError: function () {
            /**
             * Error is raised if the rrd file doesn't contain an RRA with the
             * requested consolidation function (CF)
             **/
            var self = this;
            this.rq.getData(RRD_STARTTIME, RRD_ENDTIME, 0, 'FOO').always(
                function(res) {
                    self.resume(function() {
                        Y.Assert.isInstanceOf(TypeError, res);
                    });
                });
            this.wait();
        },


        test_getData: function () {
            /**
             * The generated rrd file should have values 0-9 at 300s intervals
             * starting at 1980-01-01 00:00:00
             * Result should include a data points with times > starttime and
             * <= endTime
             **/
            var self = this;
            this.rq.getData(RRD_STARTTIME + (RRD_STEP+1) * 1000,
                            RRD_ENDTIME - (RRD_STEP-1) * 1000).always(
                function(data) {
                    self.resume(function() {
                        /* We request data starting 1 STEP +1s after
                         the RRD file first val and ending 1 STEP -1s
                         before the RRD last val ie one step within
                         the RRD file, but 1s away from the step
                         boundary to test the quantisation of the
                         requested time range.
                         so we expect two less rows than the total
                         rows in the file. */
                        Y.Assert.areEqual(RRD_RRAROWS-2, data.data.length);

                        // The value of the first returned row should be the
                        // second value in the RRD file (starts at value 0)
                        Y.Assert.areEqual(1, data.data[0][1]);

                        // The value of the last returned row should be the
                        // 10 value in the RRD file (starts at value 0)
                        Y.Assert.areEqual(10, data.data[data.data.length-1][1]);

                        // The timestamp of the first returned row should be
                        // exactly one step after the start of the RRD file
                        Y.Assert.areEqual(
                            RRD_STARTTIME+RRD_STEP*1000, data.data[0][0]);

                        // RRD_ENDTIME is on a step boundary and is therfore
                        // actually the start time of a new row
                        // So when we ask for endTime = RRD_ENDTIME-STEP-1 we
                        // actually get data up to the 2nd to last RRD row.
                        Y.Assert.areEqual(
                            RRD_ENDTIME-RRD_STEP*1000*2,
                            data.data[data.data.length-1][0]);
                    });
                });
            this.wait();
        },

        test_getDataUnknownValues: function () {
            /**
             * If the requested time range is outside the range of the RRD file
             * we should not get any values back
             **/
            var self = this;
            this.rq.getData(RRD_ENDTIME, RRD_ENDTIME+1000).always(
                function(data) {
                    self.resume(function() {
                        Y.Assert.areEqual(0, data.data.length);
                    });
                });
            this.wait();
        }
    }));


    Y.Test.Runner.add(new Y.Test.Case({
        name: "jarmon.Chart",

        test_draw: function () {
            /**
             * Test that a rendered chart has the correct dimensions, legend,
             * axis, labels etc
             **/
            var self = this;
            var $tpl = $(
                '<div><div class="chart"></div></div>').appendTo($('body'));
            var c = new jarmon.Chart($tpl, jarmon.Chart.BASE_OPTIONS);
            //
            c.options.xaxis.tzoffset = 0;
            c.addData(
                'speed',
                new jarmon.RrdQueryRemote('build/test.rrd', 'm/s'),
                true);
            var d = c.setTimeRange(RRD_STARTTIME, RRD_ENDTIME);
            d.done(
                function() {
                    self.resume(function() {
                        // TODO: write useful tests
                    });
                });
            this.wait();
        }
    }));


    Y.Test.Runner.add(new Y.Test.Case({
        name: "jarmon.RrdChooser",

        setUp: function() {
            this.$tpl = $('<div/>').appendTo($('body'));
            var c = new jarmon.RrdChooser(this.$tpl);
            c.drawRrdUrlForm();
        },

        test_drawInitialForm: function () {
            /**
             * Test that the initial config form contains an rrd form field
             **/
            Y.Assert.areEqual(
                this.$tpl.find('form input[name=rrd_url]').size(), 1);
        },

        test_drawUrlErrorMessage: function () {
            /**
             * Test that submitting the form with an incorrect url results in
             * an error message
             **/
            var self = this;
            this.$tpl.find('form input[name=rrd_url]').val('Foo/Bar').submit();
            this.wait(
                function() {
                    Y.Assert.areEqual(self.$tpl.find('.error').size(), 1);
                }, 1000);
        },

        test_drawUrlListDatasources: function () {
            /**
             * Test that submitting the form with an correct rrd url results in
             * list of further DS  label fields
             **/
            var self = this;
            this.$tpl.find(
                'form input[name=rrd_url]').val('build/test.rrd').submit();
            this.wait(
                function() {
                    Y.Assert.areEqual(
                        self.$tpl.find('input[name=rrd_ds_label]').size(), 1);
                }, 1000
            );
        }
    }));


    Y.Test.Runner.add(new Y.Test.Case({
        name: "jarmon.ChartEditor",

        setUp: function() {
            this.$tpl = $('<div/>').appendTo($('body'));
            var c = new jarmon.ChartEditor(
                this.$tpl,
                {
                    title: 'Foo',
                    datasources: [
                        ['data/cpu-0/cpu-wait.rrd', 0, 'CPU-0 Wait', '%'],
                        ['data/cpu-1/cpu-wait.rrd', 0, 'CPU-1 Wait', '%'],
                        ['data/cpu-0/cpu-system.rrd', 0, 'CPU-0 System', '%'],
                        ['data/cpu-1/cpu-system.rrd', 0, 'CPU-1 System', '%'],
                        ['data/cpu-0/cpu-user.rrd', 0, 'CPU-0 User', '%'],
                        ['data/cpu-1/cpu-user.rrd', 0, 'CPU-1 User', '%']
                    ]
                }
            );
            c.draw();
        },

        test_drawInitialForm: function () {
            /**
             * Test that the initial config form contains an rrd form field
             **/
            Y.Assert.areEqual(
                this.$tpl.find('form input[name=rrd_url]').size(), 1);
        }
    }));


    //initialize the console
    var yconsole = new Y.Console({
        newestOnTop: false,
        width:'600px',
        height: '400px'
    });
    yconsole.render('#log');
    Y.Test.Runner.run();
});