oneacct-export 0.2.7 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +1 -21
  5. data/bin/oneacct-export +7 -6
  6. data/config/conf.yml +25 -7
  7. data/lib/data_validators/apel_data_validator.rb +99 -0
  8. data/lib/data_validators/data_compute.rb +57 -0
  9. data/lib/data_validators/data_validator.rb +12 -0
  10. data/lib/data_validators/data_validator_helper.rb +15 -0
  11. data/lib/data_validators/logstash_data_validator.rb +82 -0
  12. data/lib/data_validators/pbs_data_validator.rb +86 -0
  13. data/lib/errors/not_implemented_error.rb +3 -0
  14. data/lib/errors/validation_error.rb +3 -0
  15. data/lib/errors.rb +2 -0
  16. data/lib/input_validator.rb +12 -2
  17. data/lib/one_data_accessor.rb +11 -10
  18. data/lib/one_worker.rb +109 -137
  19. data/lib/oneacct_exporter/version.rb +1 -1
  20. data/lib/oneacct_exporter.rb +9 -7
  21. data/lib/oneacct_opts.rb +36 -13
  22. data/lib/output_types.rb +5 -0
  23. data/lib/redis_conf.rb +2 -2
  24. data/lib/settings.rb +3 -3
  25. data/lib/templates/apel-0.2.erb +6 -6
  26. data/lib/templates/logstash-0.1.erb +3 -0
  27. data/lib/templates/pbs-0.1.erb +6 -0
  28. data/mock/{one_worker_vm8.xml → one_worker_vm_dn01.xml} +76 -74
  29. data/mock/one_worker_vm_dn02.xml +174 -0
  30. data/mock/{one_worker_DISK_missing.xml → one_worker_vm_empty_disk_records.xml} +10 -6
  31. data/mock/one_worker_vm_empty_history_records.xml +131 -0
  32. data/mock/one_worker_vm_image_name01.xml +175 -0
  33. data/mock/{one_worker_valid_machine.xml → one_worker_vm_image_name02.xml} +35 -7
  34. data/mock/one_worker_vm_image_name03.xml +167 -0
  35. data/mock/{one_worker_vm2.xml → one_worker_vm_image_name04.xml} +38 -9
  36. data/mock/{one_worker_vm1.xml → one_worker_vm_image_name05.xml} +36 -8
  37. data/mock/{one_worker_vm9.xml → one_worker_vm_image_name06.xml} +8 -5
  38. data/oneacct-export.gemspec +1 -0
  39. data/spec/data_validators/apel_data_validator_spec.rb +497 -0
  40. data/spec/data_validators/data_compute_spec.rb +193 -0
  41. data/spec/data_validators/data_validator_helper_spec.rb +66 -0
  42. data/spec/data_validators/data_validator_spec.rb +14 -0
  43. data/spec/data_validators/logstash_data_validator_spec.rb +469 -0
  44. data/spec/data_validators/pbs_data_validator_spec.rb +353 -0
  45. data/spec/one_worker_spec.rb +234 -542
  46. data/spec/oneacct_exporter_spec.rb +1 -41
  47. data/spec/oneacct_opts_spec.rb +135 -32
  48. data/spec/spec_helper.rb +18 -1
  49. metadata +51 -52
  50. data/mock/one_worker_DEPLOY_ID_missing.xml +0 -136
  51. data/mock/one_worker_DISK_SIZE_nan.xml +0 -147
  52. data/mock/one_worker_ETIME_0.xml +0 -137
  53. data/mock/one_worker_ETIME_missing.xml +0 -136
  54. data/mock/one_worker_ETIME_nan.xml +0 -137
  55. data/mock/one_worker_GID_missing.xml +0 -136
  56. data/mock/one_worker_GNAME_missing.xml +0 -136
  57. data/mock/one_worker_HISTORY_RECORDS_missing.xml +0 -91
  58. data/mock/one_worker_HISTORY_many.xml +0 -137
  59. data/mock/one_worker_HISTORY_missing.xml +0 -93
  60. data/mock/one_worker_HISTORY_one.xml +0 -115
  61. data/mock/one_worker_IMAGE_ID_missing.xml +0 -136
  62. data/mock/one_worker_MEMORY_0.xml +0 -137
  63. data/mock/one_worker_MEMORY_missing.xml +0 -135
  64. data/mock/one_worker_MEMORY_nan.xml +0 -137
  65. data/mock/one_worker_NET_RX_0.xml +0 -137
  66. data/mock/one_worker_NET_RX_missing.xml +0 -136
  67. data/mock/one_worker_NET_RX_nan.xml +0 -137
  68. data/mock/one_worker_NET_TX_0.xml +0 -137
  69. data/mock/one_worker_NET_TX_missing.xml +0 -136
  70. data/mock/one_worker_NET_TX_nan.xml +0 -137
  71. data/mock/one_worker_RETIME_0_RUNNING.xml +0 -115
  72. data/mock/one_worker_RETIME_0_STOPPED.xml +0 -115
  73. data/mock/one_worker_RETIME_missing.xml +0 -114
  74. data/mock/one_worker_RSTIME_0.xml +0 -115
  75. data/mock/one_worker_RSTIME_>_RETIME.xml +0 -115
  76. data/mock/one_worker_RSTIME_missing.xml +0 -114
  77. data/mock/one_worker_STATE_missing.xml +0 -136
  78. data/mock/one_worker_STATE_out_of_range.xml +0 -137
  79. data/mock/one_worker_STIME_>_ETIME.xml +0 -137
  80. data/mock/one_worker_STIME_missing.xml +0 -136
  81. data/mock/one_worker_STIME_nan.xml +0 -137
  82. data/mock/one_worker_TEMPLATE_missing.xml +0 -79
  83. data/mock/one_worker_UID_missing.xml +0 -136
  84. data/mock/one_worker_VCPU_0.xml +0 -137
  85. data/mock/one_worker_VCPU_missing.xml +0 -136
  86. data/mock/one_worker_VCPU_nan.xml +0 -137
  87. data/mock/one_worker_malformed_vm.xml +0 -136
  88. data/mock/one_worker_vm3.xml +0 -137
  89. data/mock/one_worker_vm4.xml +0 -106
  90. data/mock/one_worker_vm5.xml +0 -106
  91. data/mock/one_worker_vm6.xml +0 -107
  92. data/mock/one_worker_vm7.xml +0 -147
@@ -0,0 +1,497 @@
1
+ require 'spec_helper'
2
+
3
+ module DataValidators
4
+ describe ApelDataValidator do
5
+
6
+ subject { ApelDataValidator.new(Logger.new('/dev/null')) }
7
+
8
+ describe '.validate_data' do
9
+ let(:data) do
10
+ data = {}
11
+
12
+ data['endpoint'] = 'machine.hogwarts.co.uk'
13
+ data['site_name'] = 'Hogwarts'
14
+ data['cloud_type'] = 'OpenNebula'
15
+
16
+ data['vm_uuid'] = '36551'
17
+ data['start_time'] = '1383741160'
18
+ data['end_time'] = '1383742270'
19
+ data['machine_name'] = 'one-36551'
20
+ data['user_id'] = '120'
21
+ data['group_id'] = '0'
22
+ data['user_name'] = 'uname'
23
+ data['group_name'] = 'gname'
24
+ data['status_code'] = '3'
25
+ data['status'] = 'STATUS'
26
+ data['cpu_count'] = '1'
27
+ data['network_inbound'] = '4154845418'
28
+ data['network_outbound'] = '6326418701'
29
+ data['memory'] = '1736960'
30
+ history = []
31
+ rec = {}
32
+ rec['start_time'] = '1383741169'
33
+ rec['end_time'] = '1383741259'
34
+ rec['rstart_time'] = '1383741278'
35
+ rec['rend_time'] = '1383741367'
36
+ rec['seq'] = '0'
37
+ rec['hostname'] = 'supermachine1.somewhere.com'
38
+ history << rec
39
+ rec = {}
40
+ rec['start_time'] = '1383741589'
41
+ rec['end_time'] = '1383742270'
42
+ rec['rstart_time'] = '1383741674'
43
+ rec['rend_time'] = '1383742270'
44
+ rec['seq'] = '1'
45
+ rec['hostname'] = 'supermachine2.somewhere.com'
46
+ history << rec
47
+ data['history'] = history
48
+ data['disks'] = [{'size' => '10240'}, {'size' => '42368'}]
49
+
50
+ data['user_dn'] = '/MY=STuPID/CN=DN/CN=HERE'
51
+ data['image_name'] = 'https://appdb.egi.eu/store/vo/image/blablablabla/'
52
+
53
+ data
54
+ end
55
+
56
+ let(:valid_data) do
57
+ valid_data = {}
58
+
59
+ valid_data['endpoint'] = 'machine.hogwarts.co.uk'
60
+ valid_data['site_name'] = 'Hogwarts'
61
+ valid_data['cloud_type'] = 'OpenNebula'
62
+
63
+ valid_data['vm_uuid'] = '36551'
64
+ valid_data['start_time'] = Time.at(1383741160)
65
+ valid_data['end_time'] = Time.at(1383742270)
66
+ valid_data['machine_name'] = 'one-36551'
67
+ valid_data['user_id'] = '120'
68
+ valid_data['group_id'] = '0'
69
+ valid_data['user_name'] = 'uname'
70
+ valid_data['group_name'] = 'gname'
71
+ valid_data['status_code'] = '3'
72
+ valid_data['status'] = 'started'
73
+ valid_data['cpu_count'] = '1'
74
+ valid_data['network_inbound'] = 3
75
+ valid_data['network_outbound'] = 5
76
+ valid_data['memory'] = '1736960'
77
+ history = []
78
+ rec = {}
79
+ rec['start_time'] = '1383741169'
80
+ rec['end_time'] = '1383741259'
81
+ rec['rstart_time'] = '1383741278'
82
+ rec['rend_time'] = '1383741367'
83
+ rec['seq'] = '0'
84
+ rec['hostname'] = 'supermachine1.somewhere.com'
85
+ history << rec
86
+ rec = {}
87
+ rec['start_time'] = '1383741589'
88
+ rec['end_time'] = '1383742270'
89
+ rec['rstart_time'] = '1383741674'
90
+ rec['rend_time'] = '1383742270'
91
+ rec['seq'] = '1'
92
+ rec['hostname'] = 'supermachine2.somewhere.com'
93
+ history << rec
94
+ valid_data['history'] = history
95
+ valid_data['disks'] = [{'size' => '10240'}, {'size' => '42368'}]
96
+
97
+ valid_data['user_dn'] = '/MY=STuPID/CN=DN/CN=HERE'
98
+ valid_data['image_name'] = 'https://appdb.egi.eu/store/vo/image/blablablabla/'
99
+
100
+ valid_data['duration'] = Time.at(685)
101
+ valid_data['suspend'] = 425
102
+ valid_data['disk_size'] = 52608
103
+
104
+ valid_data
105
+ end
106
+
107
+ context 'with all data valid' do
108
+ it 'returns the same data transformed if needed' do
109
+ expect(subject.validate_data(data)).to eq(valid_data)
110
+ end
111
+ end
112
+
113
+ context 'without any data' do
114
+ let(:data) { nil }
115
+
116
+ it 'fails with ValidationError' do
117
+ expect { subject.validate_data(data) }.to raise_error(Errors::ValidationError)
118
+ end
119
+ end
120
+
121
+ context 'with empty data' do
122
+ let(:data) { {} }
123
+
124
+ it 'fails with ValidationError' do
125
+ expect { subject.validate_data(data) }.to raise_error(Errors::ValidationError)
126
+ end
127
+ end
128
+
129
+ context 'without endpoint' do
130
+ before :example do
131
+ data['endpoint'] = nil
132
+ end
133
+
134
+ it 'fails with ValidationError' do
135
+ expect { subject.validate_data(data) }.to raise_error(Errors::ValidationError)
136
+ end
137
+ end
138
+
139
+ context 'without site name' do
140
+ before :example do
141
+ data['site_name'] = nil
142
+ end
143
+
144
+ it 'fails with ValidationError' do
145
+ expect { subject.validate_data(data) }.to raise_error(Errors::ValidationError)
146
+ end
147
+ end
148
+
149
+ context 'without cloud type' do
150
+ before :example do
151
+ data['cloud_type'] = nil
152
+ end
153
+
154
+ it 'fails with ValidationError' do
155
+ expect { subject.validate_data(data) }.to raise_error(Errors::ValidationError)
156
+ end
157
+ end
158
+
159
+ context 'without vm uuid' do
160
+ before :example do
161
+ data['vm_uuid'] = nil
162
+ end
163
+
164
+ it 'fails with ValidationError' do
165
+ expect { subject.validate_data(data) }.to raise_error(Errors::ValidationError)
166
+ end
167
+ end
168
+
169
+ context 'without start time' do
170
+ before :example do
171
+ data['start_time'] = nil
172
+ end
173
+
174
+ it 'it fails with ValidationError' do
175
+ expect { subject.validate_data(data) }.to raise_error(Errors::ValidationError)
176
+ end
177
+ end
178
+
179
+ context 'with zero start time' do
180
+ before :example do
181
+ data['start_time'] = '0'
182
+ end
183
+
184
+ it 'it fails with ValidationError' do
185
+ expect { subject.validate_data(data) }.to raise_error(Errors::ValidationError)
186
+ end
187
+ end
188
+
189
+ context 'with start time that is not a number' do
190
+ before :example do
191
+ data['start_time'] = 'string'
192
+ end
193
+
194
+ it 'it fails with ValidationError' do
195
+ expect { subject.validate_data(data) }.to raise_error(Errors::ValidationError)
196
+ end
197
+ end
198
+
199
+ context 'without end time' do
200
+ before :example do
201
+ data['end_time'] = nil
202
+ end
203
+
204
+ it 'it fails with ValidationError' do
205
+ expect { subject.validate_data(data) }.to raise_error(Errors::ValidationError)
206
+ end
207
+ end
208
+
209
+ context 'with end time that is not a number' do
210
+ before :example do
211
+ data['end_time'] = 'string'
212
+ end
213
+
214
+ it 'it fails with ValidationError' do
215
+ expect { subject.validate_data(data) }.to raise_error(Errors::ValidationError)
216
+ end
217
+ end
218
+
219
+ context 'with zero end time' do
220
+ before :example do
221
+ data['end_time'] = '0'
222
+ valid_data['end_time'] = 'NULL'
223
+ valid_data['suspend'] = 'NULL'
224
+ end
225
+
226
+ it 'it replaces end_time and suspend with "NULL"' do
227
+ expect(subject.validate_data(data)).to eq(valid_data)
228
+ end
229
+ end
230
+
231
+ context 'with start time bigger than end time' do
232
+ before :example do
233
+ time = data['end_time']
234
+ data['end_time'] = data['start_time']
235
+ data['start_time'] = time
236
+ end
237
+
238
+ it 'it fails with ValidationError' do
239
+ expect { subject.validate_data(data) }.to raise_error(Errors::ValidationError)
240
+ end
241
+ end
242
+
243
+ context 'without machine name' do
244
+ before :example do
245
+ data['machine_name'] = nil
246
+ end
247
+
248
+ it 'replaces machine name with string created from id and prefix "one-"' do
249
+ expect(subject.validate_data(data)).to eq(valid_data)
250
+ end
251
+ end
252
+
253
+ context 'without user id' do
254
+ before :example do
255
+ data['user_id'] = nil
256
+ valid_data['user_id'] = 'NULL'
257
+ end
258
+
259
+ it 'replaces user id with "NULL"' do
260
+ expect(subject.validate_data(data)).to eq(valid_data)
261
+ end
262
+ end
263
+
264
+ context 'without group id' do
265
+ before :example do
266
+ data['group_id'] = nil
267
+ valid_data['group_id'] = 'NULL'
268
+ end
269
+
270
+ it 'replaces group id with "NULL"' do
271
+ expect(subject.validate_data(data)).to eq(valid_data)
272
+ end
273
+ end
274
+
275
+ context 'without user dn' do
276
+ before :example do
277
+ data['user_dn'] = nil
278
+ valid_data['user_dn'] = 'NULL'
279
+ end
280
+
281
+ it 'replaces user dn with "NULL"' do
282
+ expect(subject.validate_data(data)).to eq(valid_data)
283
+ end
284
+ end
285
+
286
+ context 'without user name' do
287
+ before :example do
288
+ data['user_name'] = nil
289
+ valid_data['user_name'] = 'NULL'
290
+ end
291
+
292
+ it 'replaces user name with "NULL"' do
293
+ expect(subject.validate_data(data)).to eq(valid_data)
294
+ end
295
+ end
296
+
297
+ context 'without group name' do
298
+ before :example do
299
+ data['group_name'] = nil
300
+ valid_data['group_name'] = nil
301
+ end
302
+
303
+ it 'replaces group name with nil' do
304
+ expect(subject.validate_data(data)).to eq(valid_data)
305
+ end
306
+ end
307
+
308
+ context 'without status code' do
309
+ before :example do
310
+ data['status_code'] = nil
311
+ valid_data['status'] = 'NULL'
312
+ valid_data['status_code'] = nil
313
+ end
314
+
315
+ it 'replaces status with "NULL"' do
316
+ expect(subject.validate_data(data)).to eq(valid_data)
317
+ end
318
+ end
319
+
320
+ context 'with status coce out of range' do
321
+ before :example do
322
+ data['status_code'] = '42'
323
+ end
324
+
325
+ it 'it fails with ValidationError' do
326
+ expect { subject.validate_data(data) }.to raise_error(Errors::ValidationError)
327
+ end
328
+ end
329
+
330
+ context 'with status code that is not a number' do
331
+ before :example do
332
+ data['status_code'] = 'string'
333
+ valid_data['status'] = 'NULL'
334
+ valid_data['status_code'] = 'string'
335
+ end
336
+
337
+ it 'replaces status with "NULL"' do
338
+ expect(subject.validate_data(data)).to eq(valid_data)
339
+ end
340
+ end
341
+
342
+ context 'without history records' do
343
+ before :example do
344
+ data['history'] = nil
345
+ end
346
+
347
+ it 'it fails with ValidationError' do
348
+ expect { subject.validate_data(data) }.to raise_error(Errors::ValidationError)
349
+ end
350
+ end
351
+
352
+ context 'with empty history records' do
353
+ before :example do
354
+ data['history'] = []
355
+ end
356
+
357
+ it 'it fails with ValidationError' do
358
+ expect { subject.validate_data(data) }.to raise_error(Errors::ValidationError)
359
+ end
360
+ end
361
+
362
+ context 'without cpu count' do
363
+ before :example do
364
+ data['cpu_count'] = nil
365
+ valid_data['cpu_count'] = '1'
366
+ end
367
+
368
+ it 'replaces cpu count with 1' do
369
+ expect(subject.validate_data(data)).to eq(valid_data)
370
+ end
371
+ end
372
+
373
+ context 'with cpu count that is not a number' do
374
+ before :example do
375
+ data['cpu_count'] = 'string'
376
+ valid_data['cpu_count'] = '1'
377
+ end
378
+
379
+ it 'replaces cpu count with 1' do
380
+ expect(subject.validate_data(data)).to eq(valid_data)
381
+ end
382
+ end
383
+
384
+ context 'with zero cpu count' do
385
+ before :example do
386
+ data['cpu_count'] = '0'
387
+ valid_data['cpu_count'] = '1'
388
+ end
389
+
390
+ it 'replaces cpu count with 1' do
391
+ expect(subject.validate_data(data)).to eq(valid_data)
392
+ end
393
+ end
394
+
395
+ context 'without network inbound' do
396
+ before :example do
397
+ data['network_inbound'] = nil
398
+ valid_data['network_inbound'] = 0
399
+ end
400
+
401
+ it 'replaces network_inbound with 0' do
402
+ expect(subject.validate_data(data)).to eq(valid_data)
403
+ end
404
+ end
405
+
406
+ context 'with network inbound that is not a number' do
407
+ before :example do
408
+ data['network_inbound'] = 'string'
409
+ valid_data['network_inbound'] = 0
410
+ end
411
+
412
+ it 'replaces network_inbound with 0' do
413
+ expect(subject.validate_data(data)).to eq(valid_data)
414
+ end
415
+ end
416
+
417
+ context 'without network outbound' do
418
+ before :example do
419
+ data['network_outbound'] = nil
420
+ valid_data['network_outbound'] = 0
421
+ end
422
+
423
+ it 'replaces network outbound with 0' do
424
+ expect(subject.validate_data(data)).to eq(valid_data)
425
+ end
426
+ end
427
+
428
+ context 'with network outbound that is not a number' do
429
+ before :example do
430
+ data['network_outbound'] = 'string'
431
+ valid_data['network_outbound'] = 0
432
+ end
433
+
434
+ it 'replaces network outbound with 0' do
435
+ expect(subject.validate_data(data)).to eq(valid_data)
436
+ end
437
+ end
438
+
439
+ context 'without memory' do
440
+ before :example do
441
+ data['memory'] = nil
442
+ valid_data['memory'] = '0'
443
+ end
444
+
445
+ it 'replaces memory with 0' do
446
+ expect(subject.validate_data(data)).to eq(valid_data)
447
+ end
448
+ end
449
+
450
+ context 'with memory that is not a number' do
451
+ before :example do
452
+ data['memory'] = 'string'
453
+ valid_data['memory'] = '0'
454
+ end
455
+
456
+ it 'replaces memory with 0' do
457
+ expect(subject.validate_data(data)).to eq(valid_data)
458
+ end
459
+ end
460
+
461
+ context 'without image name' do
462
+ before :example do
463
+ data['image_name'] = nil
464
+ valid_data['image_name'] = 'NULL'
465
+ end
466
+
467
+ it 'replaces image name with "NULL"' do
468
+ expect(subject.validate_data(data)).to eq(valid_data)
469
+ end
470
+ end
471
+
472
+ context 'without disks' do
473
+ before :example do
474
+ data['disks'] = nil
475
+ valid_data['disks'] = nil
476
+ valid_data['disk_size'] = 'NULL'
477
+ end
478
+
479
+ it 'replaces disk size with "NULL"' do
480
+ expect(subject.validate_data(data)).to eq(valid_data)
481
+ end
482
+ end
483
+
484
+ context 'with empty disks' do
485
+ before :example do
486
+ data['disks'] = []
487
+ valid_data['disks'] = []
488
+ valid_data['disk_size'] = 0
489
+ end
490
+
491
+ it 'replaces disk size with 0' do
492
+ expect(subject.validate_data(data)).to eq(valid_data)
493
+ end
494
+ end
495
+ end
496
+ end
497
+ end
@@ -0,0 +1,193 @@
1
+ require 'spec_helper'
2
+
3
+ module DataValidators
4
+ describe DataCompute do
5
+ subject { data_compute }
6
+
7
+ let(:data_compute) do
8
+ data_compute = Object.new
9
+ data_compute.extend(Errors)
10
+ data_compute.extend(InputValidator)
11
+ data_compute.extend(DataValidatorHelper)
12
+ data_compute.extend(DataCompute)
13
+
14
+ data_compute
15
+ end
16
+
17
+ let(:log) { double('log') }
18
+
19
+ before :example do
20
+ allow(subject).to receive(:log).and_return(log)
21
+ allow(log).to receive(:warn)
22
+ end
23
+
24
+ describe '.sum_disk_size' do
25
+ let(:disks) do
26
+ disks = []
27
+ disk = {'size' => '10240'}
28
+ disks << disk
29
+ disk = {'size' => '42368'}
30
+ disks << disk
31
+
32
+ disks
33
+ end
34
+
35
+ let(:vm_id) { 42 }
36
+
37
+ context 'vm without disks' do
38
+ it 'returns nil' do
39
+ expect(subject.sum_disk_size(nil, vm_id)).to be_nil
40
+ end
41
+ end
42
+
43
+ context 'vm empty disks' do
44
+ it 'returns 0' do
45
+ expect(subject.sum_disk_size([], vm_id)).to eq(0)
46
+ end
47
+ end
48
+
49
+ context 'vm with disk without size' do
50
+ before :example do
51
+ disks[0] = {}
52
+ end
53
+
54
+ it 'returns nil' do
55
+ expect(subject.sum_disk_size(disks, vm_id)).to be_nil
56
+ end
57
+ end
58
+
59
+ context 'vm with disk with invalid size' do
60
+ before :example do
61
+ disks[0] = {'size' => 'not_a_number'}
62
+ end
63
+
64
+ it 'returns nil' do
65
+ expect(subject.sum_disk_size(disks, vm_id)).to be_nil
66
+ end
67
+ end
68
+
69
+ context 'vm with single disk and valid size' do
70
+ it 'return correct disk size' do
71
+ expect(subject.sum_disk_size(disks[1..1], vm_id)).to eq(42368)
72
+ end
73
+ end
74
+
75
+ context 'vm with multiple disks and valid size' do
76
+ it 'return correct disk size' do
77
+ expect(subject.sum_disk_size(disks, vm_id)).to eq(52608)
78
+ end
79
+ end
80
+ end
81
+
82
+ describe '.sum_rstime' do
83
+
84
+ let(:history) do
85
+ history = []
86
+ rec = {}
87
+ rec['start_time'] = '1383741169'
88
+ rec['end_time'] = '1383741259'
89
+ rec['rstart_time'] = '1383741278'
90
+ rec['rend_time'] = '1383741367'
91
+ rec['seq'] = '0'
92
+ rec['hostname'] = 'supermachine1.somewhere.com'
93
+ history << rec
94
+ rec = {}
95
+ rec['start_time'] = '1383741589'
96
+ rec['end_time'] = '1383742270'
97
+ rec['rstart_time'] = '1383741674'
98
+ rec['rend_time'] = '1383742270'
99
+ rec['seq'] = '1'
100
+ rec['hostname'] = 'supermachine2.somewhere.com'
101
+ history << rec
102
+
103
+ history
104
+ end
105
+
106
+ let(:vm_id) { 42 }
107
+ let(:completed) { true }
108
+
109
+ context 'vm without history records' do
110
+ it 'returns nil' do
111
+ expect(subject.sum_rstime(nil, completed, vm_id)).to be_nil
112
+ end
113
+ end
114
+
115
+ context 'vm with empty history records' do
116
+ it 'returns 0' do
117
+ expect(subject.sum_rstime([], completed, vm_id)).to eq(0)
118
+ end
119
+ end
120
+
121
+ context 'vm without rstart_time' do
122
+ before :example do
123
+ history[0]['rstart_time'] = nil
124
+ end
125
+
126
+ it 'skips invalid history record and count the rest' do
127
+ expect(subject.sum_rstime(history, completed, vm_id)).to eq(596)
128
+ end
129
+ end
130
+
131
+ context 'vm with rstart_time that is 0' do
132
+ before :example do
133
+ history[0]['rstart_time'] = '0'
134
+ end
135
+
136
+ it 'skips invalid history record and count the rest' do
137
+ expect(subject.sum_rstime(history, completed, vm_id)).to eq(596)
138
+ end
139
+ end
140
+
141
+ context 'vm without rend_time' do
142
+ before :example do
143
+ history[0]['rend_time'] = nil
144
+ end
145
+
146
+ it 'skips invalid history record and count the rest' do
147
+ expect(subject.sum_rstime(history, completed, vm_id)).to eq(596)
148
+ end
149
+ end
150
+
151
+ context 'vm with rend_time that is 0' do
152
+ before :example do
153
+ history[0]['rend_time'] = '0'
154
+ end
155
+
156
+ context 'and running' do
157
+ before :example do
158
+ allow(Time).to receive(:now) { 1383741716 }
159
+ end
160
+
161
+ let(:completed) { false }
162
+
163
+ it 'returns difference between current time and start of the virtual machine and count the rest' do
164
+ expect(subject.sum_rstime(history, completed, vm_id)).to eq(1034)
165
+ end
166
+ end
167
+
168
+ context 'and stopped' do
169
+ it 'fails with ValidationError' do
170
+ expect { subject.sum_rstime(history, completed, vm_id) }.to raise_error(Errors::ValidationError)
171
+ end
172
+ end
173
+ end
174
+
175
+ context 'vm with rstart_time bigger than rend_time' do
176
+ before :example do
177
+ history[0]['rstart_time'] = '1383741259'
178
+ history[0]['rend_time'] = '1383741169'
179
+ end
180
+
181
+ it 'fails with ValidationError' do
182
+ expect { subject.sum_rstime(history, completed, vm_id) }.to raise_error(Errors::ValidationError)
183
+ end
184
+ end
185
+
186
+ context 'with all data valid' do
187
+ it 'count the correct value' do
188
+ expect(subject.sum_rstime(history, completed, vm_id)).to eq(685)
189
+ end
190
+ end
191
+ end
192
+ end
193
+ end