forked from nephics/noxmox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-noxmox.js
255 lines (225 loc) · 5.96 KB
/
test-noxmox.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
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
//
// Copyright(c) 2011-2012 Nephics AB
// MIT Licensed
//
var fs = require('fs');
var crypto = require('crypto');
var util = require('util');
var assert = require('assert');
var nox = require('./nox.js');
var mox = require('./mox.js');
// Read aws auth data from stdin or cmd line argument, and run the tests
(function readAuthData() {
var data = [];
var timeout;
if (process.argv.length >= 3) {
// read from filename given as argument
fs.readFile(process.argv[2], 'utf8', function(err, data) {
if (err) {
console.log('Failed to read file ' + process.argv[2] + ', error: ' +
err);
}
else runTests(data);
});
}
else {
// try to read from stdin, bail out if no data after 100ms
timeout = setTimeout(function() {
if (!data.length) failed();
}, 100);
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (chunk) {
data.push(chunk);
});
process.stdin.on('end', function () {
runTests(data.join(''));
});
process.stdin.on('error', failed);
}
function failed() {
console.log(['To run the tests you will need to supply JSON ',
'encoded object with the aws key, secret and bucketname.\n',
'Example calls:\n\n',
' cat aws_auth.json | ', process.argv[0], ' ', process.argv[1], '\n\n',
' ', process.argv[0], ' ', process.argv[1], ' aws_auth.json\n']
.join(''));
process.exit(1);
}
}());
function runTests(data) {
var options;
try {
options = JSON.parse(data);
}
catch (e) {
console.log('Failed to parse input as JSON data');
process.exit(1);
}
console.log('\nTesting mox client');
var moxclient = mox.createClient(options);
test(moxclient, function() {
var noxclient = nox.createClient(options);
console.log('\nTesting nox client');
test(noxclient, function(){
console.log('\nAll tests completed');
});
});
}
function test(client, callback) {
var name = 'test/noxmox.txt';
t1();
function t1() {
var buf = new Buffer('Testing the noxmox lib.');
upload(client, name, buf, t2);
}
function t2() {
stat(client, name, t3);
}
function t3() {
download(client, name, t4);
}
function t4() {
remove(client, name, t5);
}
function t5() {
statRemoved(client, name, t6);
}
function t6() {
downloadRemoved(client, name, t7);
}
function t7() {
removeRemoved(client, name, callback);
}
}
function logErrors(req) {
req.on('error', function(err) {
console.log(err.message || err);
});
}
function logResponse(res) {
console.log('status code: ' + res.statusCode);
console.log('headers: ' + util.inspect(res.headers));
}
function upload(client, name, buf, callback) {
console.log('\nFile upload');
var req = client.put(name, {
'Content-Type':'text/plain',
'Content-Length':buf.length,
'Content-MD5': crypto.createHash('md5').update(buf).digest('base64')
});
logErrors(req);
req.on('continue', function() {
req.end(buf);
});
req.on('response', function(res) {
logResponse(res);
res.on('data', function(chunk) {
console.log(chunk);
});
res.on('end', function() {
console.log('Response finished');
if (res.statusCode === 404) {
console.log('Failed to upload file, make sure the test bucket exists!');
process.exit(2);
}
if (res.statusCode === 307) {
console.log('Failed to upload file to bucket in non-standard region, make sure to include the endpoint in the bucket name: ' +
res.headers.location.match(/\/\/(.*\.amazonaws\.com).*/)[1]);
process.exit(3);
}
assert.equal(res.statusCode, 200);
callback();
});
});
}
function stat(client, name, callback) {
var req = client.head(name);
logErrors(req);
console.log('\nFile stat');
req.on('response', function(res) {
logResponse(res);
res.on('data', function(chunk) {
console.log(chunk);
});
res.on('end', function() {
console.log('Response finished');
assert.equal(res.statusCode, 200);
callback();
});
});
req.end();
}
function statRemoved(client, name, callback) {
var req = client.head(name);
logErrors(req);
console.log('\nNonexistent file stat');
req.on('response', function(res) {
logResponse(res);
res.on('data', function(chunk) {
console.log(chunk);
});
res.on('end', function() {
console.log('Response finished');
assert.equal(res.statusCode, 404);
callback();
});
});
req.end();
}
function download(client, name, callback) {
var req = client.get(name);
logErrors(req);
console.log('\nFile download');
req.on('response', function(res) {
logResponse(res);
var len = 0;
res.on('data', function(chunk) {
len += chunk.length;
});
res.on('end', function() {
console.log('Downloaded ' + len + ' bytes of file data');
assert.equal(res.statusCode, 200);
callback();
});
});
req.end();
}
function downloadRemoved(client, name, callback) {
var req = client.get(name);
logErrors(req);
console.log('\nNonexistent file download');
req.on('response', function(res) {
logResponse(res);
res.on('data', function(chunk) {
console.log(chunk.toString());
});
res.on('end', function() {
assert.equal(res.statusCode, 404);
callback();
});
});
req.end();
}
function remove(client, name, callback) {
var req = client.del(name);
logErrors(req);
console.log('\nFile delete');
req.on('response', function(res) {
logResponse(res);
res.on('data', function(chunk) {
console.log(chunk);
});
res.on('end', function() {
console.log('Response finished');
assert.equal(res.statusCode, 204);
callback();
});
});
req.end();
}
function removeRemoved(client, name, callback) {
// Trying to removing a nonexistent file, looks the same
// as removing an existent file (status code 204).
remove(client, name, callback);
}