def test_checkReplicaSetOrInitialize_reconfigure(self): self.cluster_object.spec.mongodb.replicas = 4 self.kubernetes_service.execInPod.return_value = self._getFixture( "replica-status-ok") self.service.checkReplicaSetOrInitialize(self.cluster_object) cluster_config = json.loads(self.expected_cluster_config) cluster_config["members"].append({ "_id": 3, "host": "mongo-cluster-3.mongo-cluster.default.svc.cluster.local" }) self.expected_cluster_config = json.dumps(cluster_config) expected_calls = [ call.execInPod( 'mongodb', 'mongo-cluster-0', 'default', ['mongo', 'localhost:27017/admin', '--eval', 'rs.status()']), call.execInPod( 'mongodb', 'mongo-cluster-0', 'default', [ 'mongo', 'localhost:27017/admin', '--eval', 'rs.reconfig({})'.format(self.expected_cluster_config) ], ) ] self.assertEquals(expected_calls, self.kubernetes_service.mock_calls)
def test_checkReplicaSetOrInitialize_initialize(self): self.kubernetes_service.execInPod.side_effect = ( self._getFixture("replica-status-not-initialized"), self._getFixture("initiate-ok")) self.service.checkReplicaSetOrInitialize(self.cluster_object) expected_calls = [ call.execInPod( 'mongodb', 'mongo-cluster-0', 'default', ['mongo', 'localhost:27017/admin', '--eval', 'rs.status()']), call.execInPod('mongodb', 'mongo-cluster-0', 'default', [ 'mongo', 'localhost:27017/admin', '--eval', 'rs.initiate({})'.format(self.expected_cluster_config) ]) ] self.assertEquals(expected_calls, self.kubernetes_service.mock_calls)
def test_createUsers_not_master_then_already_exists(self): self.kubernetes_service.execInPod.side_effect = ( self._getFixture("createUser-notMaster"), self._getFixture("createUser-exists")) self.service.createUsers(self.cluster_object) expected_calls = [ call.getSecret('mongo-cluster-admin-credentials', 'default'), call.execInPod('mongodb', 'mongo-cluster-0', 'default', [ 'mongo', 'localhost:27017/admin', '--eval', self.expected_user_create ]), call.execInPod('mongodb', 'mongo-cluster-1', 'default', [ 'mongo', 'localhost:27017/admin', '--eval', self.expected_user_create ]), ] self.assertEquals(expected_calls, self.kubernetes_service.mock_calls)
def test_checkReplicaSetOrInitialize_ok(self): self.kubernetes_service.execInPod.return_value = self._getFixture( "replica-status-ok") self.service.checkReplicaSetOrInitialize(self.cluster_object) expected_calls = [ call.execInPod( 'mongodb', 'mongo-cluster-0', 'default', ['mongo', 'localhost:27017/admin', '--eval', 'rs.status()']) ] self.assertEquals(expected_calls, self.kubernetes_service.mock_calls)
def test_reconfigureReplicaSet(self): self.kubernetes_service.execInPod.return_value = self._getFixture( "initiate-ok") self.service.reconfigureReplicaSet(self.cluster_object) expected_calls = [ call.execInPod('mongodb', 'mongo-cluster-0', 'default', [ 'mongo', 'localhost:27017/admin', '--eval', 'rs.reconfig({})'.format(self.expected_cluster_config) ]) ] self.assertEquals(expected_calls, self.kubernetes_service.mock_calls)
def test__execInPod_connect_failed(self): self.kubernetes_service.execInPod.side_effect = ValueError( "connect failed"), self._getFixture("initiate-ok") result = self.service._execInPod(1, "cluster", "default", "rs.test()") self.assertEquals(self.initiate_ok_response, result) expected_calls = 2 * [ call.execInPod( 'mongodb', 'cluster-1', 'default', ['mongo', 'localhost:27017/admin', '--eval', 'rs.test()']) ] self.assertEquals(expected_calls, self.kubernetes_service.mock_calls)
def test__execInPod_ValueError(self): self.kubernetes_service.execInPod.side_effect = ValueError( "Value error.") with self.assertRaises(ValueError) as context: self.service._execInPod(1, "cluster", "default", "rs.test()") self.assertEquals("Value error.", str(context.exception)) expected_calls = [ call.execInPod( 'mongodb', 'cluster-1', 'default', ['mongo', 'localhost:27017/admin', '--eval', 'rs.test()']) ] self.assertEquals(expected_calls, self.kubernetes_service.mock_calls)
def test__execInPod_handshake_status(self): self.kubernetes_service.execInPod.side_effect = ( ApiException(500, reason="Handshake status: Failed!"), self._getFixture("initiate-ok")) result = self.service._execInPod(1, "cluster", "default", "rs.test()") self.assertEquals(self.initiate_ok_response, result) expected_calls = 2 * [ call.execInPod( 'mongodb', 'cluster-1', 'default', ['mongo', 'localhost:27017/admin', '--eval', 'rs.test()']) ] self.assertEquals(expected_calls, self.kubernetes_service.mock_calls)
def test__execInPod(self): self.kubernetes_service.execInPod.return_value = self._getFixture( "replica-status-not-initialized") result = self.service._execInPod(0, "cluster", "default", "rs.status()") self.assertEquals(self.not_initialized_response, result) expected_calls = [ call.execInPod( 'mongodb', 'cluster-0', 'default', ['mongo', 'localhost:27017/admin', '--eval', 'rs.status()']) ] self.assertEquals(expected_calls, self.kubernetes_service.mock_calls)
def test__execInPod_ApiException(self): self.kubernetes_service.execInPod.side_effect = ApiException( 400, reason="A reason.") with self.assertRaises(ApiException) as context: self.service._execInPod(5, "mongo-cluster", "ns", "rs.test()") self.assertEquals("(400)\nReason: A reason.\n", str(context.exception)) expected_calls = [ call.execInPod( 'mongodb', 'mongo-cluster-5', 'ns', ['mongo', 'localhost:27017/admin', '--eval', 'rs.test()']) ] self.assertEquals(expected_calls, self.kubernetes_service.mock_calls)
def test_createUsers_ok(self): self.kubernetes_service.execInPod.return_value = self._getFixture( "createUser-ok") self.service.createUsers(self.cluster_object) expected_calls = [ call.getSecret('mongo-cluster-admin-credentials', 'default'), call.execInPod('mongodb', 'mongo-cluster-0', 'default', [ 'mongo', 'localhost:27017/admin', '--eval', self.expected_user_create ]) ] self.assertEquals(expected_calls, self.kubernetes_service.mock_calls)
def test_checkCluster_new_version(self, check_mock, admin_mock, backup_mock): self.checker.cluster_versions[("mongo-cluster", "default")] = "50" self.kubernetes_service.execInPod.side_effect = ( self._getMongoFixture("replica-status-ok"), self._getMongoFixture("createUser-exists")) self.checker.checkCluster(self.cluster_object) self.assertEqual({("mongo-cluster", "default"): "100"}, self.checker.cluster_versions) expected = [ call.execInPod( 'mongodb', 'mongo-cluster-0', 'default', ['mongo', 'localhost:27017/admin', '--eval', 'rs.status()']), call.getSecret('mongo-cluster-admin-credentials', 'default'), call.execInPod('mongodb', 'mongo-cluster-0', 'default', [ 'mongo', 'localhost:27017/admin', '--eval', admin_mock.return_value ]) ] self.assertEqual(expected, self.kubernetes_service.mock_calls) self.assertEqual([call(self.cluster_object)] * 3, check_mock.mock_calls) backup_mock.assert_called_once_with(self.cluster_object)
def test__execInPod_NodeNotFound(self): self.kubernetes_service.execInPod.side_effect = ( self._getFixture("initiate-not-found"), self._getFixture("initiate-not-found"), self._getFixture("initiate-ok")) result = self.service._execInPod(1, "cluster", "default", "rs.initiate({})") self.assertEquals(self.initiate_ok_response, result) expected_calls = 3 * [ call.execInPod('mongodb', 'cluster-1', 'default', [ 'mongo', 'localhost:27017/admin', '--eval', 'rs.initiate({})' ]) ] self.assertEquals(expected_calls, self.kubernetes_service.mock_calls)
def test_checkCluster_same_version(self, backup_mock): self.checker.cluster_versions[( "mongo-cluster", "default")] = "100" # checkCluster will assume cached version self.kubernetes_service.execInPod.return_value = self._getMongoFixture( "replica-status-ok") self.checker.checkCluster(self.cluster_object) self.assertEqual({("mongo-cluster", "default"): "100"}, self.checker.cluster_versions) expected = [ call.execInPod( 'mongodb', 'mongo-cluster-0', 'default', ['mongo', 'localhost:27017/admin', '--eval', 'rs.status()']) ] self.assertEqual(expected, self.kubernetes_service.mock_calls) backup_mock.assert_called_once_with(self.cluster_object)
def test_checkReplicaSetOrInitialize_ValueError(self): response = self._getFixture("replica-status-ok").replace( '"ok" : 1', '"ok" : 2') self.kubernetes_service.execInPod.return_value = response with self.assertRaises(ValueError) as context: self.service.checkReplicaSetOrInitialize(self.cluster_object) expected_calls = [ call.execInPod( 'mongodb', 'mongo-cluster-0', 'default', ['mongo', 'localhost:27017/admin', '--eval', 'rs.status()']) ] self.assertEquals(expected_calls, self.kubernetes_service.mock_calls) self.assertIn("Unexpected response trying to check replicas: ", str(context.exception))
def test__execInPod_TimeoutError(self): self.kubernetes_service.execInPod.side_effect = ( ValueError("connection attempt failed"), ApiException(500, reason="Handshake status: Failed!"), self._getFixture("initiate-not-found"), ApiException(404, reason="Handshake status: error")) with self.assertRaises(TimeoutError) as context: self.service._execInPod(5, "mongo-cluster", "ns", "rs.test()") self.assertEquals("Could not check the replica set after 4 retries!", str(context.exception)) expected_calls = 4 * [ call.execInPod( 'mongodb', 'mongo-cluster-5', 'ns', ['mongo', 'localhost:27017/admin', '--eval', 'rs.test()']) ] self.assertEquals(expected_calls, self.kubernetes_service.mock_calls)
def test_createUsers_TimeoutError(self): self.kubernetes_service.execInPod.return_value = self._getFixture( "createUser-notMaster") with self.assertRaises(TimeoutError) as context: self.service.createUsers(self.cluster_object) expected_calls = [ call.getSecret('mongo-cluster-admin-credentials', 'default') ] + [ call.execInPod('mongodb', 'mongo-cluster-' + str(pod), 'default', [ 'mongo', 'localhost:27017/admin', '--eval', self.expected_user_create ]) for _ in range(4) for pod in range(3) ] self.assertEquals(expected_calls, self.kubernetes_service.mock_calls) self.assertEquals( "Could not create users in any of the 3 pods of cluster mongo-cluster @ ns/default.", str(context.exception))
def test_createUsers_ValueError(self): self.kubernetes_service.execInPod.return_value = self._getFixture( "createUser-ok").replace('"user"', '"error"') with self.assertRaises(ValueError) as context: self.service.createUsers(self.cluster_object) expected_calls = [ call.getSecret('mongo-cluster-admin-credentials', 'default'), call.execInPod('mongodb', 'mongo-cluster-0', 'default', [ 'mongo', 'localhost:27017/admin', '--eval', self.expected_user_create ]) ] self.assertEquals(expected_calls, self.kubernetes_service.mock_calls) self.assertEquals( "Unexpected response creating users for pod mongo-cluster-0 @ ns/default:\n" "{'error': 'root', 'roles': [{'role': 'root', 'db': 'admin'}]}", str(context.exception))