-
Notifications
You must be signed in to change notification settings - Fork 85
/
launchApplicationInRunningStateWithPayload.js
117 lines (108 loc) · 4.58 KB
/
launchApplicationInRunningStateWithPayload.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
"use strict";
var dial = require("../libs/dialClient.js"),
utils = require("../libs/utils.js"),
Q = require("q");
const argv = require("yargs")
.usage("\nUsage: node " + __filename.slice(__dirname.length + 1) + "[options]")
.option("host", {
describe: "IP address of host on which DIAL server under test is running",
type: "string",
demand: true
})
.option("application", {
alias: "app",
describe: "Application to test",
type: "string",
demand: true
})
.option("timeToWaitForStateChange", {
alias: "ttw",
describe: "Time(ms) to wait between state changes before querying application status",
type: "string",
default: 5000
})
.help("help").alias("help", "h").argv;
function test() {
var host = argv.host;
var app = argv.application;
var timeToWaitForStateChange = argv.timeToWaitForStateChange || 5000;
return new Q()
.then(function () {
utils.printTestInfo(__filename.slice(__dirname.length + 1), "Launch " + app + " with payload using DIAL server when application is already running and check for response code 201");
})
.then(function () {
utils.printDebug("Querying application state");
})
.then(dial.getApplicationStatus.bind(null, host, app))
.then(function getCurrentAppState(result) {
if(!result || !result.state) {
return Q.reject(new Error("Error retrieving current " + app + " application state"));
}
return result.state;
})
.then(function startAppIfNotRunning(state) {
utils.printDebug("Application is in " + state + " state");
if(state !== "running") {
return new Q()
.then(function () {
utils.printDebug("Launching application ..");
return dial.launchApplication(host, app);
})
.then(function (response) {
if(response.statusCode !== 201) {
return Q.reject(new Error("Error launching " + app + " application. Expected status code 201 from DIAL server but got " + response.statusCode));
}
})
.then(function () {
utils.printDebug("Wait for " + timeToWaitForStateChange + " ms for state change to happen");
})
.delay(timeToWaitForStateChange)
.then(function () {
utils.printDebug("Querying application state");
return dial.getApplicationStatus(host, app)
})
.then(function getCurrentAppState(result) {
if(!result || !result.state) {
return Q.reject(new Error("Error retrieving current " + app + " application state"));
}
utils.printDebug("Application is in " + result.state + " state");
if(result.state !== "running") {
return Q.reject(new Error("Expected " + app + " application to be in running state, but querying state returned state as" + result.state));
}
});
}
})
.then(function () {
utils.printDebug("Launching application with payload..");
})
.then(dial.launchApplication.bind(null, host, app, "key1=val1&key2=val2"))
.then(function (response) {
if(response.statusCode !== 201) {
return Q.reject(new Error("Error launching " + app + " application when it was already running. Expected status code 201 from DIAL server but got " + response.statusCode));
}
})
.then(function () {
utils.printDebug("Querying application state");
return dial.getApplicationStatus(host, app)
})
.then(function getCurrentAppState(result) {
if(!result || !result.state) {
return Q.reject(new Error("Error retrieving current " + app + " application state"));
}
utils.printDebug("Application is in " + result.state + " state");
if(result.state !== "running") {
return Q.reject(new Error("Expected " + app + " application to be in running state, but querying state returned state as" + result.state));
}
})
.then(function () {
utils.printTestSuccess()
})
.fail(function handleError(err) {
utils.printTestFailure(err);
});
}
module.exports.test = test;
if (require.main === module) {
test()
.done();
}