/
xml-parser.js
64 lines (51 loc) · 1.27 KB
/
xml-parser.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
'use strict';
var common = require('./includes/common.js');
var fs = require('fs');
var util = require('util');
var assert = require('assert');
var config = require('../config/aws.js');
var have = {
'libxml-to-js': true,
'xml2js': true
};
var callbacks = {
'libxml-to-js': 0,
'xml2js': 0
};
try {
require('libxml-to-js');
} catch (e) {
have['libxml-to-js'] = false;
callbacks['libxml-to-js']++;
util.log('libxml-to-js is not installed, moving on without it');
}
try {
require('xml2js');
} catch (e) {
util.error('ERROR: xml2js is missing, can not test the installation');
process.exit(1);
}
var testXml = function (lib) {
util.log('testing the XML library ' + lib);
var parser = require(lib);
if (lib === 'xml2js') {
parser = new parser.Parser(config.xml2jsConfig).parseString;
}
fs.readFile('data/ec2-describeimages.xml', function (err, data) {
assert.ifError(err);
var xml = new Buffer(data).toString();
parser(xml, function (err, res) {
callbacks[lib]++;
assert.ifError(err);
assert.equal(res.imagesSet.item[0].imageId, 'ami-be3adfd7');
assert.equal(res.imagesSet.item[1].imageId, 'ami-be3adfd9');
});
});
};
var idx;
for (idx in have) {
if (have.hasOwnProperty(idx) && have[idx] === true) {
testXml(idx);
}
}
common.teardown(callbacks);