8000 Add test cases for _admin/metrics/v2 endpoint in cluster mode by naushniki · Pull Request #14446 · arangodb/arangodb · GitHub
[go: up one dir, main page]

Skip to content

Add test cases for _admin/metrics/v2 endpoint in cluster mode #14446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add test cases for _admin/metrics/v2 endpoint in cluster mode
  • Loading branch information
Vitaly Pryakhin committed Jul 1, 2021
commit eea02c8e341d55a8bc3e262307539be627a00c97
8 changes: 6 additions & 2 deletions README_maintainers.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Depending on the platform, ArangoDB tries to locate the temporary directory:
### Local Cluster Startup

The scripts `scripts/startLocalCluster` helps you to quickly fire up a testing
cluster on your local machine. `scripts/stopLocalCluster` stops it again.
cluster on your local machine. `scripts/shutdownLocalCluster` stops it again.

`scripts/startLocalCluster [numDBServers numCoordinators [mode]]`

Expand Down Expand Up @@ -692,10 +692,14 @@ To locate the suite(s) associated with a specific test file use:

./scripts/unittest find --test tests/js/common/shell/shell-aqlfunctions.js

Run all suite(s) associated with a specific test file:
Run all suite(s) associated with a specific test file in single server mode:

./scripts/unittest auto --test tests/js/common/shell/shell-aqlfunctions.js

Run all suite(s) associated with a specific test file in cluster mode:

./scripts/unittest auto --cluster true --test tests/js/common/shell/shell-aqlfunctions.js

Run all C++ based Google Test (gtest) tests using the `arangodbtests` binary:

./scripts/unittest gtest
Expand Down
202 changes: 202 additions & 0 deletions tests/js/client/shell/shell-promtool-cluster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/* jshint globalstrict:false, strict:false, maxlen: 200 */
/* global assertTrue, assertEqual, fail, arango */

////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2014-2021 ArangoDB GmbH, Cologne, Germany
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Jan Steemann
////////////////////////////////////////////////////////////////////////////////

const jsunity = require('jsunity');
const internal = require('internal');
const fs = require('fs');
const pu = require('@arangodb/testutils/process-utils');
const console = require('console');
const request = require("@arangodb/request");
const expect = require('chai').expect;
const errors = require('@arangodb').errors;
const suspendExternal = internal.suspendExternal;
const continueExternal = internal.continueExternal;

// name of environment variable
const PATH = 'PROMTOOL_PATH';

// detect the path to promtool
let promtoolPath = internal.env[PATH];
if (!promtoolPath) {
promtoolPath = '.';
}
promtoolPath = fs.join(promtoolPath, 'promtool' + pu.executableExt);

const metricsUrlPath = "/_admin/metrics/v2";
const serverIdPath = "/_admin/server/id";

function validateMetrics(metrics) {
let toRemove = [];
try {
// store output of /_admin/metrics/v2 into a temp file
let input = fs.getTempFile();
fs.writeFileSync(input, metrics);
toRemove.push(input);

// this is where we will capture the output from promtool
let output = fs.getTempFile();
toRemove.push(output);

// build command string. this will be unsafe if input/output
// contain quotation marks and such. but as these parameters are
// under our control this is very unlikely
let command = promtoolPath + ' check metrics < "' + input + '" > "' + output + '" 2>&1';
// pipe contents of temp file into promtool
let actualRc = internal.executeExternalAndWait('sh', ['-c', command]);
assertTrue(actualRc.hasOwnProperty('exit'));
assertEqual(0, actualRc.exit);

let promtoolResult = fs.readFileSync(output).toString();
// no errors found means an empty result file
assertEqual('', promtoolResult);
} finally {
// remove temp files
toRemove.forEach((f) => {
fs.remove(f);
});
}
}

function validateMetricsOnServer(server) {
print("Querying server ", server.name);
let res = request.get({
url: server.url + metricsUrlPath
});
expect(res).to.be.an.instanceof(request.Response);
expect(res).to.have.property('statusCode', 200);
let body = String(res.body);
validateMetrics(body);
}

function getServerId(server) {
let res = request.get({
url: server.url + serverIdPath
});
return res.json.id;
}

function validateMetricsViaCoordinator(coordinator, server) {
let serverId = getServerId(server);
let metricsUrl = coordinator.url + metricsUrlPath + "?serverId=" + serverId;
let res = request.get({ url: metricsUrl });
expect(res).to.be.an.instanceof(request.Response);
expect(res).to.have.property('statusCode', 200);
let body = String(res.body);
validateMetrics(body);
}

function promtoolClusterSuite() {
'use strict';

if (!internal.env.hasOwnProperty('INSTANCEINFO')) {
throw new Error('env.INSTANCEINFO was not set by caller!');
}
const instanceinfo = JSON.parse(internal.env.INSTANCEINFO);
let dbServers = instanceinfo.arangods.filter(arangod => arangod.role === "dbserver");
let agents = instanceinfo.arangods.filter(arangod => arangod.role === "agent");
let coordinators = instanceinfo.arangods.filter(arangod => arangod.role === "coordinator");

return {
testMetricsOnDbServers: function () {
print("Validating metrics from db servers...");
for (let i = 0; i < dbServers.length; i++) {
validateMetricsOnServer(dbServers[i]);
}
},
testMetricsOnAgents: function () {
print("Validating metrics from agents...");
for (let i = 0; i < agents.length; i++) {
validateMetricsOnServer(agents[i]);
}
},
testMetricsOnCoordinators: function () {
print("Validating metrics coordinators...");
for (let i = 0; i < coordinators.length; i++) {
validateMetricsOnServer(coordinators[i]);
}
},
testMetricsViaCoordinator: function () {
//exclude agents, because agents cannot be queried via coordinator
let servers = dbServers.concat(coordinators);
for (let i = 0; i < servers.length; i++) {
validateMetricsViaCoordinator(coordinators[0], servers[i]);
}
},
testInvalidServerId: function () {
//query metrics from coordinator, supplying invalid server id
let coordinator = coordinators[0];
let metricsUrl = coordinator.url + metricsUrlPath + "?serverId=" + "invalid-server-id";
let res = request.get({ url: metricsUrl });
expect(res).to.be.an.instanceof(request.Response);
expect(res).to.have.property('statusCode', 404);
expect(res.json.errorNum).to.equal(errors.ERROR_HTTP_BAD_PARAMETER.code);
},
testServerDoesntResponse: function () {
//query metrics from coordinator, supplying id of a server, that is shut down
let dbServer = dbServers[0];
let serverId = getServerId(dbServer);
assertTrue(suspendExternal(dbServer.pid));
dbServer.suspended = true;
try {
let metricsUrl = metricsUrlPath + "?serverId=" + serverId;
let res = arango.GET_RAW(metricsUrl);
assertEqual(503, res.code);
//Do not validate response body because errorNum and errorMessage can differ depending on whether there was an open connection
//between coordinator and db server when the later stopped responding. This cannot be reproduced consistently in the test.
// let body = res.parsedBody;
// expect(body.errorNum).to.equal(errors.ERROR_CLUSTER_CONNECTION_LOST.code);
} finally {
assertTrue(continueExternal(dbServer.pid));
delete dbServer.suspended;
}
}
};
}

if (internal.platform === 'linux') {
// this test intentionally only executes on Linux, and only if PROMTOOL_PATH
// is set to the path containing the `promtool` executable. if the PROMTOOL_PATH
// is set, but the executable cannot be found, the test will error out.
// the test also requires `sh` to be a shell that supports input/output redirection,
// and `true` to be an executable that returns exit code 0 (we use sh -c true` as a
// test to check the shell functionality).
if (fs.exists(promtoolPath)) {
let actualRc = internal.executeExternalAndWait('sh', ['-c', 'true']);
if (actualRc.hasOwnProperty('exit') && actualRc.exit === 0) {
jsunity.run(promtoolClusterSuite);
} else {
console.warn('skipping test because no working sh can be found');
}
} else if (!internal.env.hasOwnProperty(PATH)) {
console.warn('skipping test because promtool is not found. you can set ' + PATH + ' accordingly');
} else {
fail('promtool not found in PROMTOOL_PATH (' + internal.env[PATH] + ')');
}
} else {
console.warn('skipping test because we are not on Linux');
}

return jsunity.done();
0