Skip to content
This repository has been archived by the owner on Jul 9, 2019. It is now read-only.

Commit

Permalink
Ignoring empty responses from MongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Jun 29, 2018
1 parent cda006d commit 6a8960f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
5 changes: 4 additions & 1 deletion mongoOperator/helpers/MongoResources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import logging
from json import JSONDecodeError

import re
Expand Down Expand Up @@ -119,7 +120,9 @@ def parseMongoResponse(cls, exec_response: str) -> Dict[str, any]:
if error_search:
raise ValueError(error_search.group(1).strip(": "))

raise ValueError("Cannot parse MongoDB status response: {}".format(repr(exec_response)))
# MongoDB often returns an empty status when it's starting up.
logging.info("Cannot find any JSON or error in the MongoDB response: %s", repr(exec_response))
return {}

@classmethod
def _createReplicaConfig(cls, cluster_object: V1MongoClusterConfiguration) -> Dict[str, any]:
Expand Down
5 changes: 2 additions & 3 deletions mongoOperator/services/MongoService.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ def _execInPod(self, pod_index: int, name: str, namespace: str, mongo_command: s
try:
exec_response = self.kubernetes_service.execInPod(self.CONTAINER, pod_name, namespace, exec_command)
response = MongoResources.parseMongoResponse(exec_response)
if response.get("ok") == 0 and response.get("codeName") == "NodeNotFound":
logging.info("Waiting for replica set members for %s @ ns/%s: %s", pod_name, namespace, response)
else:
if response and response.get("codeName") != "NodeNotFound":
return response
logging.info("Waiting for replica set members for %s @ ns/%s: %s", pod_name, namespace, response)

except ValueError as e:
if str(e) not in ("connection attempt failed", "connect failed"):
Expand Down
14 changes: 11 additions & 3 deletions tests/helpers/TestMongoResources.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,17 @@ def test_parseMongoResponse_error(self):
self.assertEqual("connect failed", str(context.exception))

def test_parseMongoResponse_empty(self):
with self.assertRaises(ValueError) as context:
MongoResources.parseMongoResponse("")
self.assertEqual("Cannot parse MongoDB status response: ''", str(context.exception))
self.assertEqual({}, MongoResources.parseMongoResponse(''))

def test_parseMongoResponse_only_version(self):
self.assertEqual({}, MongoResources.parseMongoResponse("MongoDB shell version v3.6.4\n"))

def test_parseMongoResponse_version_twice(self):
self.assertEqual({}, MongoResources.parseMongoResponse(
"MongoDB shell version v3.6.4\n"
"connecting to: mongodb://localhost:27017/admin\n"
"MongoDB server version: 3.6.4\n"
))

def test_parseMongoResponse_bad_json(self):
with open("tests/fixtures/mongo_responses/replica-status-ok.txt") as f:
Expand Down

0 comments on commit 6a8960f

Please sign in to comment.