-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
117 lines (101 loc) · 3.64 KB
/
index.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
/**
* Performs checks against a GeoServer instance.
* Provide your username and password as
* environment variables when running the script, i.e:
* `GS_CHECKER_USER=myuser GS_CHECKER_PWD=mypassword npm start` or inject the
* URL to the GeoServer to test
* `GS_CHECKER_BASEURL=http://localhost:9999/geoserver/ npm start`
*
*/
const puppeteer = require('puppeteer');
const screenshot = 'geoserver.png';
const geoserverBaseUrl =
process.env.GS_CHECKER_BASEURL || 'http://localhost:8080/geoserver';
console.info('---------------------------------------------------------------');
console.info('Checking GeoServer at: ', geoserverBaseUrl);
console.info('---------------------------------------------------------------');
const user = process.env.GS_CHECKER_USER || 'admin';
const pwd = process.env.GS_CHECKER_PWD || 'geoserver';
// flag to skip testing parts, which need authentication
const noLogin = process.env.GS_CHECKER_NO_LOGIN === 'true';
// force other executable for chromium or chrome browser
const chromeExecPath = process.env.GS_CHECKER_CHROME_EXEC;
(async () => {
const browser = await puppeteer.launch({
headless: 'new',
executablePath: chromeExecPath,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const page = await browser.newPage();
// check for failed page.goto calls, e.g. HTTP 404
page.on('response', response => {
if (response.status() >= 400) {
console.error('✘ Given URL', response.url(),
'returned non 200 response code:', response.status());
process.exit(1);
}
});
try {
await page.goto(geoserverBaseUrl + 'web');
} catch (error) {
console.error('✘ Given URL could not be opened:', geoserverBaseUrl);
process.exit(1);
}
if (noLogin) {
console.info('Done... skip parts needing authentication due to ' +
'flag GS_CHECKER_NO_LOGIN=true');
process.exit(0);
}
await page.type('#username', user);
await page.type('#password', pwd);
await page.keyboard.press('Enter');
await page.waitForNavigation();
await page.screenshot({
path: screenshot
});
// check if login was successful by non existing error element in the UI
if (await page.$('.feedbackPanelERROR') !== null) {
console.error('✘ Login failed to GeoServer web interface.');
process.exit(1);
} else {
console.info('✔ Successfully logged in to GeoServer web interface.');
console.info(' Check screenshot after login: ', screenshot);
}
// check if expected workspaces are present (via GeoServer REST API)
if (process.env.GS_CHECKER_WS) {
const expectedWs = process.env.GS_CHECKER_WS.split(',');
const fetch = require('node-fetch');
const credentialsBase64 = Buffer.from(user + ':' + pwd).toString('base64');
const authHeader = 'Basic ' + credentialsBase64;
const headers = {
Authorization: authHeader
};
const reqOptions = {
method: 'GET',
headers: headers
};
const wsUrl = geoserverBaseUrl + 'rest/workspaces.json';
const res = await fetch(wsUrl, reqOptions);
const json = await res.json();
let wsNames = [];
if (json.workspaces.workspace) {
wsNames = json.workspaces.workspace.map(ws => ws.name)
}
// check if all expected workspaces are existing
const wsNotFound = [];
if (wsNames) {
expectedWs.forEach((wsToCheck) => {
if (!wsNames.includes(wsToCheck)) {
wsNotFound.push(wsToCheck);
}
});
}
if (wsNotFound.length > 0) {
console.error('✘ Missing workspace(s):', wsNotFound.join(', '));
process.exit(1);
} else {
console.info('✔ Found all expected workspaces in GeoServer');
}
}
browser.close();
})();