-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.js
102 lines (80 loc) · 2.57 KB
/
test.js
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
var dotest = require ('dotest');
var app = require ('./');
// Setup
// $ NODE_APP_IFACE=eth0 npm test
var config = {
bin: process.env.NODE_APP_BIN || null
};
var iface = process.env.NODE_APP_IFACE || 'eth0';
var vnstat = app (config);
dotest.add ('Module', function (test) {
test ()
.isFunction ('fail', 'exports', app)
.isObject ('fail', 'interface', vnstat)
.isFunction ('fail', '.getConfig', vnstat && vnstat.getConfig)
.isFunction ('fail', '.getStats', vnstat && vnstat.getStats)
.done ();
});
dotest.add ('Method .getConfig', function (test) {
vnstat.getConfig (function (err, data) {
test (err)
.isObject ('fail', 'data', data)
.isNotEmpty ('fail', 'data.DatabaseDir', data && data.DatabaseDir)
.isNotEmpty ('fail', 'data.Interface', data && data.Interface)
.done ();
});
});
dotest.add ('Method .getStats - iface', function (test) {
vnstat.getStats (iface, function (err, data) {
var days = data && data.traffic && data.traffic.days;
var rx = days && days [0] && days [0] .rx;
test (err)
.isObject ('fail', 'data', data)
.isString ('fail', 'data.id', data && data.id)
.isObject ('fail', 'data.traffic', data && data.traffic)
.isArray ('fail', 'data.traffic.days', days)
.isObject ('fail', 'data.traffic.days[0]', days && days [0])
.isNumber ('fail', 'data.traffic.days[0].rx', rx)
.done ();
});
});
dotest.add ('Method .getStats - all', function (test) {
vnstat.getStats (function (err, data) {
test (err)
.isArray ('fail', 'data', data)
.done ();
});
});
dotest.add ('Error: invalid interface', function (test) {
vnstat.getStats ('unreal-iface', function (err, data) {
test ()
.isError ('fail', 'err', err)
.isExactly ('fail', 'err.message', err && err.message, 'invalid interface')
.isUndefined ('fail', 'data', data)
.done ();
});
});
dotest.add ('Error: no config', function (test) {
config.bin = '-';
vnstat = app (config);
vnstat.getConfig (function (err, data) {
test ()
.isError ('fail', 'err', err)
.isExactly ('fail', 'err.message', err && err.message, 'no config')
.isUndefined ('fail', 'data', data)
.done ();
});
});
dotest.add ('Error: command failed', function (test) {
config.bin = '-';
vnstat = app (config);
vnstat.getStats (function (err, data) {
test ()
.isError ('fail', 'err', err)
.isExactly ('fail', 'err.message', err && err.message, 'command failed')
.isUndefined ('fail', 'data', data)
.done ();
});
});
// Start the tests
dotest.run ();