This repository was archived by the owner on Oct 26, 2021. It is now read-only.
generated from server-state/template-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path020-module.test.js
191 lines (164 loc) · 6.44 KB
/
020-module.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
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
require('./_gen-pegjs-parser');
jest.doMock('fs');
const serverModule = require('../src');
describe('Test simple module behaviour', () => {
const MOCKED_NO_FILE = {};
const MOCKED_EMPTY_FILE = {
'/proc/mdstat': '\n'
};
const MOCKED_NOPARSE_FILE = {
'/proc/mdstat': 'I am not parsable :D\n'
};
const MOCKED_CORRECT = {
files: {
'/proc/mdstat': 'Personalities : [raid1] [raid0] \nmd127 : active raid0 sdb3[2] sdb2[1] sdb1[0]\n 62862336 blocks super 1.2 512k chunks\n \nmd126 : active raid0 sdb6[2] sdb5[1] sdb4[0]\n 62862336 blocks super 1.2 512k chunks\n\nunused devices: <none>\n'
},
dirs: {
'/dev/md': ['myRaid', 'mySecondRaid']
},
links: {
'/dev/md/myRaid': '../md127',
'/dev/md/mySecondRaid': '../md126'
}
};
const MOCKED_MISSING = {
files: {
'/proc/mdstat': 'Personalities : [raid1] [raid0] \nmd127 : active raid0 sdb3[2] sdb2[1] sdb1[0]\n 62862336 blocks super 1.2 512k chunks\n \nmd126 : active raid0 sdb6[2] sdb5[1] sdb4[0]\n 62862336 blocks super 1.2 512k chunks\n\nunused devices: <none>\n'
},
dirs: {
'/dev/md': ['myRaid']
},
links: {
'/dev/md/myRaid': '../md127'
}
};
const MOCKED_WRONG_LINK = {
files: {
'/proc/mdstat': 'Personalities : [raid1] [raid0] \nmd127 : active raid0 sdb3[2] sdb2[1] sdb1[0]\n 62862336 blocks super 1.2 512k chunks\n \nmd126 : active raid0 sdb6[2] sdb5[1] sdb4[0]\n 62862336 blocks super 1.2 512k chunks\n\nunused devices: <none>\n'
},
dirs: {
'/dev/md': ['myRaid', 'mySecondRaid']
},
links: {
'/dev/md/myRaid': '../md127',
'/dev/md/mySecondRaid': '../other/location'
}
};
it('should return an error object when file /proc/mdstat do not exist', async () => {
require('fs').__setMockFiles(MOCKED_NO_FILE);
expect(serverModule()).rejects.toThrow();
});
it('should return an error object when file /proc/mdstat is empty', async () => {
require('fs').__setMockFiles(MOCKED_EMPTY_FILE);
expect(serverModule()).rejects.toThrow();
});
it('should return an error object when file /proc/mdstat is not parsable', async () => {
require('fs').__setMockFiles(MOCKED_NOPARSE_FILE);
expect(serverModule()).rejects.toThrow();
});
it('should return an result with correct unique names', () => {
require('fs').__setMockFS(MOCKED_CORRECT);
expect(serverModule()).resolves.toMatchSnapshot();
});
it('should return an correct result with one missing unique name', () => {
require('fs').__setMockFS(MOCKED_MISSING);
expect(serverModule()).resolves.toMatchSnapshot();
});
it('should return an correct result with one wrong link destination', () => {
require('fs').__setMockFS(MOCKED_WRONG_LINK);
expect(serverModule()).resolves.toMatchSnapshot();
});
});
describe('Test module options', () => {
const types = ['null', 'undefined', 'boolean', 'number', 'string', 'object', 'array'];
const typeExamples = {
null: null,
undefined: undefined,
boolean: false,
number: 3.141,
string: 'I am a string',
object: { prop1: 'prop1', prop2: 'prop2' },
array: ['I', 'am', 'an', 'array']
};
const MOCKED_STANDARD = {
files: {
'/proc/mdstat': 'Personalities : [raid1] [raid0] \nmd127 : active raid0 sdb3[2] sdb2[1] sdb1[0]\n 62862336 blocks super 1.2 512k chunks\n \nmd126 : active raid0 sdb6[2] sdb5[1] sdb4[0]\n 62862336 blocks super 1.2 512k chunks\n\nmd125 : active raid0 sdb9[2] sdb8[1] sdb7[0]\n 62862336 blocks super 1.2 512k chunks\n\nunused devices: <none>\n'
},
dirs: {
'/dev/md': ['myRaid', 'mySecondRaid']
},
links: {
'/dev/md/myRaid': '../md127',
'/dev/md/mySecondRaid': '../md126'
}
};
const OPTIONS_TESTS = [
[
'filter',
'array',
[
[
'not filter any raids',
{ filter: [], invert: false }
],
[
'filter everything',
{ filter: [], invert: true }
],
[
'filter out \'myRaid\'',
{ filter: ['myRaid'], invert: false }
],
[
'filter everything except \'myRaid\'',
{ filter: ['myRaid'], invert: true }
],
[
'filter out \'myRaid\' and \'mySecondRaid\'',
{ filter: ['myRaid', 'mySecondRaid'], invert: false }
],
[
'filter everything except \'myRaid\' and \'mySecondRaid\'',
{ filter: ['myRaid', 'mySecondRaid'], invert: true }
],
[
'filter block device without unique name',
{ filter: ['md125'], invert: false }
]
]
],
[
'invert',
'boolean',
[
[
'do nothing',
{}
]
]
]
];
beforeAll(() => {
require('fs').__setMockFS(MOCKED_STANDARD);
});
it.each(types)('should pass with given options, type: %s', (type) => {
expect(serverModule(typeExamples[type])).resolves.toBeTruthy();
});
describe.each(OPTIONS_TESTS)('Test option \'%s\'', (name, type, tests) => {
// basic member tests
it.each(types.filter(testType => testType !== type))('should reject when \'' + name + '\' is given with type: %s', (testType) => {
let obj = {};
obj[name] = typeExamples[testType];
expect(serverModule(obj)).rejects.toThrow();
});
it('should pass when \'' + name + '\' is given with type: ' + type, () => {
let obj = {};
obj[name] = typeExamples[type];
expect(serverModule(obj)).resolves.toBeTruthy();
});
// special member cases (defined in OPTIONS_TESTS)
it.each(tests)('should %s', (desc, options) => {
expect(serverModule(options)).resolves.toMatchSnapshot();
});
});
});