Skip to content

Commit f6bfccd

Browse files
authored
Merge pull request #342 from sjanuary/headless
Add a test for headless mode
2 parents 667b184 + 0c4e4ba commit f6bfccd

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"tap": "7.x"
1919
},
2020
"scripts": {
21-
"test": "tap --reporter tap tests/api_tests.js",
21+
"test": "tap --reporter tap --timeout=120 tests/api_tests.js tests/headless_test.js",
2222
"install": "node-gyp rebuild"
2323
},
2424
"directories": {

tests/api_tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*******************************************************************************/
1616

1717
var app = require('./test_app');
18+
app.start();
1819
var monitor = app.appmetrics.monitor();
1920
app.appmetrics.enable("profiling");
2021

tests/headless_test.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*******************************************************************************
2+
* Copyright 2016 IBM Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*******************************************************************************/
16+
17+
var app = require('./test_app');
18+
var fs = require('fs');
19+
var path = require('path');
20+
var appmetrics = app.appmetrics;
21+
var outputDir = path.join(process.cwd(), "headlesstestoutput" + Date.now());
22+
23+
// Run in headless mode for 1 minute, producing output in 'outputDir'
24+
appmetrics.configure({'com.ibm.diagnostics.healthcenter.headless':'on',
25+
'com.ibm.diagnostics.healthcenter.headless.run.duration':'1',
26+
'com.ibm.diagnostics.healthcenter.headless.run.number.of.runs':'1',
27+
'com.ibm.diagnostics.healthcenter.headless.output.directory': outputDir });
28+
app.start();
29+
30+
31+
var tap = require('tap');
32+
tap.plan(1); // NOTE: This needs to be updated when tests are added/removed
33+
34+
tap.test('Headless mode should produce a .hcd file', function(t) {
35+
36+
setTimeout(function(){
37+
fs.readdir(outputDir, function(error, files) {
38+
if (error) {
39+
t.fail("An error occurred: " + error)
40+
t.end();
41+
cleanUp();
42+
return
43+
}
44+
for (var i = 0, len = files.length; i < len; i++) {
45+
if(files[i].endsWith('.hcd')) {
46+
t.pass(".hcd file found");
47+
t.end();
48+
cleanUp();
49+
return;
50+
}
51+
}
52+
t.fail("No .hcd file found");
53+
t.end();
54+
cleanUp();
55+
});
56+
}, 70000);
57+
});
58+
59+
function cleanUp() {
60+
app.endRun();
61+
deleteDir(outputDir);
62+
}
63+
64+
function deleteDir(directory) {
65+
// Delete temporary directory
66+
if(fs.existsSync(directory)) {
67+
fs.readdirSync(directory).forEach(function(file,index){
68+
var fileName = path.join(directory, file)
69+
fs.unlinkSync(fileName)
70+
})
71+
fs.rmdirSync(directory);
72+
}
73+
}
74+

tests/test_app.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,15 @@ if (timeout) {
4646
//If being run from other test, start the agent and make available
4747
if (agent) {
4848
appmetrics = require('../');
49-
appmetrics.start();
5049

5150
// Make agent visible for other script files.
5251
module.exports.appmetrics = appmetrics;
5352
}
5453

54+
module.exports.start = function start() {
55+
appmetrics.start();
56+
}
57+
5558
//Write a string to memory on timer
5659
var test = null;
5760
var ih = setInterval(function() {

0 commit comments

Comments
 (0)