summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/src/github.com/mongodb/mongo-tools/test/qa-tests/jstests/libs/mongostat.js
blob: 91b5c6f36fb18cee35d4088874cb2025d22af18a (plain)
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
var exitCodeSuccess = 0;
var exitCodeErr = 1;
// Go reserves exit code 2 for its own use.
var exitCodeBadOptions = 3;
var exitCodeStopped = 4;

// NOTE: On Windows, stopMongoProgramByPid doesn't terminiate a process in a
// way that it can control its exit code.
if (_isWindows()) {
  exitCodeStopped = exitCodeErr;
}

var rowRegex = /^sh\d+\|\s/;
// portRegex finds the port on a line which has enough whitespace-delimited
// values to be considered a stat line and not an error message
var portRegex = /^sh\d+\|\s+\S+:(\d+)(\s+\S+){16}/;

function statRows() {
  return rawMongoProgramOutput()
    .split("\n")
    .filter(function(r) {
      return r.match(rowRegex);
    })
    .map(function(r) {
      return r.replace(/^sh\d+\| /, "");
    });
}

function statFields(row) {
  return row.split(/\s/).filter(function(s) {
    return s !== "";
  });
}

function getLatestChunk() {
  var output = rawMongoProgramOutput();
  // mongostat outputs a blank line between each set of stats when there are
  // multiple hosts; we want just one chunk of stat lines
  var lineChunks = output.split("| \n");
  if (lineChunks.length === 1) {
    return lineChunks[0];
  }
  return lineChunks[lineChunks.length - 2];
}

function latestPortCounts() {
  var portCounts = {};
  getLatestChunk().split("\n").forEach(function(r) {
    var matches = r.match(portRegex);
    if (matches === null) {
      return;
    }
    var port = matches[1];
    if (!portCounts[port]) {
      portCounts[port] = 0;
    }
    portCounts[port]++;
  });
  return portCounts;
}

function hasPort(port) {
  port = String(port);
  return function() {
    return latestPortCounts()[port] >= 1;
  };
}

function lacksPort(port) {
  port = String(port);
  return function() {
    return latestPortCounts()[port] === undefined;
  };
}

function hasOnlyPorts(expectedPorts) {
  expectedPorts = expectedPorts.map(String);
  return function() {
    var portCounts = latestPortCounts();
    for (var port in portCounts) {
      if (expectedPorts.indexOf(port) === -1) {
        return false;
      }
    }
    for (var i in expectedPorts) {
      if (portCounts[expectedPorts[i]] !== 1) {
        return false;
      }
    }
    return true;
  };
}

function statCheck(args, checker) {
  clearRawMongoProgramOutput();
  pid = startMongoProgramNoConnect.apply(null, args);
  try {
    assert.soon(checker, "discoverTest wait timed out");
    return true;
  } catch (e) {
    return false;
  } finally {
    stopMongoProgramByPid(pid);
  }
}

function discoverTest(ports, connectHost) {
  return statCheck(["mongostat",
    "--host", connectHost,
    "--noheaders",
    "--discover"],
  hasOnlyPorts(ports));
}