Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
feat(test/): convert server_tests, undefined_tests, replset_tests, re…
Browse files Browse the repository at this point in the history
…plset_state_tests, and repleset_server_selection_tests to run with mongodb-test-runner

NODE-1096
  • Loading branch information
malexandert authored and mbroadst committed Aug 11, 2017
1 parent 34f704c commit 3a7c5fd
Show file tree
Hide file tree
Showing 6 changed files with 2,073 additions and 0 deletions.
161 changes: 161 additions & 0 deletions test/mocha_tests/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
{
"root": true,
"env": { // http://eslint.org/docs/user-guide/configuring.html#specifying-environments
"node": true, // Node.js global variables and Node.js-specific rules
"es6": true,
"mocha": true
},
"parserOptions": {
"ecmaVersion": 8, // or 2017
"ecmaFeatures": {
"arrowFunctions": true,
"blockBindings": true,
"classes": true,
"defaultParams": true,
"destructuring": true,
"forOf": true,
"generators": false,
"modules": false,
"objectLiteralComputedProperties": true,
"objectLiteralDuplicateProperties": false,
"objectLiteralShorthandMethods": true,
"objectLiteralShorthandProperties": true,
"spread": true,
"superInFunctions": true,
"templateStrings": true,
"jsx": false
}
},
"rules": {
/**
* Variables
*/
"no-shadow": 2, // http://eslint.org/docs/rules/no-shadow
"no-shadow-restricted-names": 2, // http://eslint.org/docs/rules/no-shadow-restricted-names
"no-unused-vars": [2, { // http://eslint.org/docs/rules/no-unused-vars
"vars": "local",
"args": "none"
}],
"no-use-before-define": 0, // http://eslint.org/docs/rules/no-use-before-define

/**
* Possible errors
*/
"comma-dangle": [2, "never"], // http://eslint.org/docs/rules/comma-dangle
"no-cond-assign": [2, "always"], // http://eslint.org/docs/rules/no-cond-assign
"no-debugger": 1, // http://eslint.org/docs/rules/no-debugger
"no-alert": 1, // http://eslint.org/docs/rules/no-alert
"no-dupe-keys": 2, // http://eslint.org/docs/rules/no-dupe-keys
"no-duplicate-case": 2, // http://eslint.org/docs/rules/no-duplicate-case
"no-empty": 2, // http://eslint.org/docs/rules/no-empty
"no-ex-assign": 2, // http://eslint.org/docs/rules/no-ex-assign
"no-extra-boolean-cast": 0, // http://eslint.org/docs/rules/no-extra-boolean-cast
"no-extra-semi": 2, // http://eslint.org/docs/rules/no-extra-semi
"no-func-assign": 2, // http://eslint.org/docs/rules/no-func-assign
"no-inner-declarations": 2, // http://eslint.org/docs/rules/no-inner-declarations
"no-invalid-regexp": 2, // http://eslint.org/docs/rules/no-invalid-regexp
"no-irregular-whitespace": 2, // http://eslint.org/docs/rules/no-irregular-whitespace
"no-obj-calls": 2, // http://eslint.org/docs/rules/no-obj-calls
"no-sparse-arrays": 2, // http://eslint.org/docs/rules/no-sparse-arrays
"no-unreachable": 2, // http://eslint.org/docs/rules/no-unreachable
"use-isnan": 2, // http://eslint.org/docs/rules/use-isnan
"block-scoped-var": 0, // http://eslint.org/docs/rules/block-scoped-var
"no-undef": 2, // http://eslint.org/docs/rules/no-undef
/**
* Best practices
*/
// "consistent-return": 2, // http://eslint.org/docs/rules/consistent-return
"curly": [2, "multi-line"], // http://eslint.org/docs/rules/curly
"default-case": 2, // http://eslint.org/docs/rules/default-case
"dot-notation": [2, { // http://eslint.org/docs/rules/dot-notation
"allowKeywords": true
}],
"eqeqeq": 2, // http://eslint.org/docs/rules/eqeqeq
"guard-for-in": 0, // http://eslint.org/docs/rules/guard-for-in
"no-caller": 2, // http://eslint.org/docs/rules/no-caller
"no-else-return": 2, // http://eslint.org/docs/rules/no-else-return
"no-eq-null": 2, // http://eslint.org/docs/rules/no-eq-null
"no-eval": 2, // http://eslint.org/docs/rules/no-eval
"no-extend-native": 2, // http://eslint.org/docs/rules/no-extend-native
"no-extra-bind": 2, // http://eslint.org/docs/rules/no-extra-bind
"no-fallthrough": 2, // http://eslint.org/docs/rules/no-fallthrough
"no-floating-decimal": 2, // http://eslint.org/docs/rules/no-floating-decimal
"no-implied-eval": 2, // http://eslint.org/docs/rules/no-implied-eval
"no-lone-blocks": 2, // http://eslint.org/docs/rules/no-lone-blocks
"no-loop-func": 2, // http://eslint.org/docs/rules/no-loop-func
"no-multi-str": 2, // http://eslint.org/docs/rules/no-multi-str
"no-native-reassign": 2, // http://eslint.org/docs/rules/no-native-reassign
"no-new": 2, // http://eslint.org/docs/rules/no-new
"no-new-func": 2, // http://eslint.org/docs/rules/no-new-func
"no-new-wrappers": 2, // http://eslint.org/docs/rules/no-new-wrappers
"no-octal": 2, // http://eslint.org/docs/rules/no-octal
"no-octal-escape": 2, // http://eslint.org/docs/rules/no-octal-escape
"no-param-reassign": 0, // http://eslint.org/docs/rules/no-param-reassign
"no-proto": 2, // http://eslint.org/docs/rules/no-proto
"no-redeclare": 2, // http://eslint.org/docs/rules/no-redeclare
"no-return-assign": 2, // http://eslint.org/docs/rules/no-return-assign
"no-script-url": 2, // http://eslint.org/docs/rules/no-script-url
"no-self-compare": 2, // http://eslint.org/docs/rules/no-self-compare
"no-sequences": 2, // http://eslint.org/docs/rules/no-sequences
"no-throw-literal": 2, // http://eslint.org/docs/rules/no-throw-literal
"no-with": 2, // http://eslint.org/docs/rules/no-with
"radix": 2, // http://eslint.org/docs/rules/radix
"strict": [2, "global"], // http://eslint.org/docs/rules/strict
"wrap-iife": [2, "any"], // http://eslint.org/docs/rules/wrap-iife
"yoda": 2, // http://eslint.org/docs/rules/yoda

/**
* Style
*/
"indent": [2, 2, {
"VariableDeclarator": {"var": 2, "let": 2, "const": 3}
}], // http://eslint.org/docs/rules/indent
"brace-style": [2, // http://eslint.org/docs/rules/brace-style
"1tbs", {
"allowSingleLine": true
}],
"quotes": [
2, "single", "avoid-escape" // http://eslint.org/docs/rules/quotes
],
"camelcase": [2, { // http://eslint.org/docs/rules/camelcase
"properties": "never"
}],
"comma-spacing": [2, { // http://eslint.org/docs/rules/comma-spacing
"before": false,
"after": true
}],
"comma-style": [2, "last"], // http://eslint.org/docs/rules/comma-style
"eol-last": 2, // http://eslint.org/docs/rules/eol-last
"func-names": 0, // http://eslint.org/docs/rules/func-names
"key-spacing": [2, { // http://eslint.org/docs/rules/key-spacing
"beforeColon": false,
"afterColon": true
}],
"new-cap": [2, { // http://eslint.org/docs/rules/new-cap
"newIsCap": true
}],
"no-multiple-empty-lines": [2, { // http://eslint.org/docs/rules/no-multiple-empty-lines
"max": 2
}],
"no-nested-ternary": 2, // http://eslint.org/docs/rules/no-nested-ternary
"no-new-object": 2, // http://eslint.org/docs/rules/no-new-object
"no-spaced-func": 2, // http://eslint.org/docs/rules/no-spaced-func
"no-trailing-spaces": 2, // http://eslint.org/docs/rules/no-trailing-spaces
"no-extra-parens": [2, "functions"], // http://eslint.org/docs/rules/no-extra-parens
"no-underscore-dangle": 0, // http://eslint.org/docs/rules/no-underscore-dangle
"padded-blocks": [2, "never"], // http://eslint.org/docs/rules/padded-blocks
"semi": [2, "always"], // http://eslint.org/docs/rules/semi
"semi-spacing": [2, { // http://eslint.org/docs/rules/semi-spacing
"before": false,
"after": true
}],
"keyword-spacing": 2, // http://eslint.org/docs/rules/keyword-spacing
"space-before-blocks": 2, // http://eslint.org/docs/rules/space-before-blocks
"space-before-function-paren": [2, "never"], // http://eslint.org/docs/rules/space-before-function-paren
"space-infix-ops": 2, // http://eslint.org/docs/rules/space-infix-ops
"spaced-comment": [0, "always", { // http://eslint.org/docs/rules/spaced-comment
"exceptions": ["*"],
"markers": ["*"]
}]
}
}
123 changes: 123 additions & 0 deletions test/mocha_tests/mocha_replset_server_selection_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
'use strict';

var expect = require('chai').expect,
f = require('util').format,
fs = require('fs'),
ReplSetState = require('../../lib/topologies/replset_state'),
MongoError = require('../../lib/error').MongoError,
ReadPreference = require('../../lib/topologies/read_preference'),
Server = require('../../lib/topologies/server');

describe('A replicaset with no primary', function() {
it('should correctly execute server selection tests', {
metadata: { requires: { topology: 'single' } },

test: function(done) {
var path = f('%s/../tests/server-selection/tests/server_selection/ReplicaSetNoPrimary/read', __dirname);
var entries = fs.readdirSync(path).filter(function(x) {
return x.indexOf('.json') !== -1;
});

// Execute each of the entries
entries.forEach(function(x) {
executeEntry(x, f('%s/%s', path, x));
});

done();
}
});
});

describe('A replicaset with a primary', function() {
it('should correctly execute server selection tests', {
metadata: { requires: { topology: 'single' } },

test: function(done) {
var path = f('%s/../tests/server-selection/tests/server_selection/ReplicaSetWithPrimary/read', __dirname);
var entries = fs.readdirSync(path).filter(function(x) {
return x.indexOf('.json') !== -1;
});

// Execute each of the entries
entries.forEach(function(x) {
executeEntry(x, f('%s/%s', path, x));
});

done();
}
});
});

function convert(mode) {
if (mode.toLowerCase() === 'primarypreferred') return 'primaryPreferred';
if (mode.toLowerCase() === 'secondarypreferred') return 'secondaryPreferred';
return mode.toLowerCase();
}

function executeEntry(file, path) {
// Read and parse the json file
file = require(path);

// Let's pick out the parts of the selection specification
var topologyDescription = file.topology_description;
var inLatencyWindow = file.in_latency_window;
var readPreference = file.read_preference;

try {
// Create a Replset and populate it with dummy topology servers
var replset = new ReplSetState();
replset.topologyType = topologyDescription.type;
// For each server add them to the state
topologyDescription.servers.forEach(function(s) {
var server = new Server({
host: s.address.split(':')[0],
port: parseInt(s.address.split(':')[1], 10)
});

// Add additional information
if (s.avg_rtt_ms) server.lastIsMasterMS = s.avg_rtt_ms;
if (s.tags) server.ismaster = {tags: s.tags};
// Ensure the server looks connected
server.isConnected = function() {return true;};

if (s.type === 'RSSecondary') {
replset.secondaries.push(server);
} else if (s.type === 'RSPrimary') {
replset.primary = server;
} else if (s.type === 'RSArbiter') {
replset.arbiters.push(server);
}
});

// Create read preference
var rp = new ReadPreference(convert(readPreference.mode), readPreference.tag_sets);

// Perform a pickServer
var server = replset.pickServer(rp);
var foundWindow = null;

// server should be in the latency window
for (var i = 0; i < inLatencyWindow.length; i++) {
var w = inLatencyWindow[i];

if (server.name === w.address) {
foundWindow = w;
break;
}
}

if (['ReplicaSetNoPrimary', 'Primary', 'ReplicaSetWithPrimary'].indexOf(topologyDescription.type) !== -1
&& inLatencyWindow.length === 0) {
if (server instanceof MongoError) {
expect(server.message).to.equal('no primary server available');
} else {
expect(server).to.be.null;
}
} else {
expect(foundWindow).to.not.be.null;
}
} catch (err) {
console.log(err.stack);
process.exit(0);
}
}
Loading

0 comments on commit 3a7c5fd

Please sign in to comment.