def test_mongo_is_ready_when_server_info_is_available(self, server_info):
        config = MONGO_CONFIG.copy()
        mongo = MongoDB(config)

        server_info.return_value = {"info": "some info"}
        ready = mongo.is_ready()
        self.assertEqual(ready, True)
 def test_replica_set_uri_has_correct_root_credentials(self):
     config = MONGO_CONFIG.copy()
     mongo = MongoDB(config)
     uri = mongo.replica_set_uri()
     prefix, _ = uri.split('@')
     _, user, password = prefix.split(':')
     user = user.lstrip("/")
     self.assertEqual("root", user)
     self.assertEqual(password, config['root_password'])
    def test_replica_set_uri_sets_correct_credentials(self):
        config = MONGO_CONFIG.copy()
        mongo = MongoDB(config)

        credentials = {"username": "******", "password": "******"}
        uri = mongo.replica_set_uri(credentials)
        prefix, _ = uri.split('@')
        _, user, password = prefix.split(':')
        user = user.lstrip("/")
        self.assertEqual(credentials["username"], user)
        self.assertEqual(password, credentials["password"])
    def test_drop_database_requests_database_removal(self, client):
        config = MONGO_CONFIG.copy()
        mongo = MongoDB(config)

        mock_client = MagicMock()
        client.return_value = mock_client
        mock_db = Mock()
        mock_client.__getitem__.return_value = mock_db

        databases = ["mydb"]
        mongo.drop_databases(databases)
        client.assert_called()
        mock_db.command.assert_called_once_with("dropDatabase")
    def test_drop_user_requests_user_removal(self, client):
        config = MONGO_CONFIG.copy()
        mongo = MongoDB(config)

        mock_client = MagicMock()
        client.return_value = mock_client
        mock_db = Mock()
        mock_client.__getitem__.return_value = mock_db

        username = "******"
        mongo.drop_user(username)

        client.assert_called()
        mock_db.command.assert_called_once_with("dropUser", username)
    def test_initializing_replica_invokes_admin_command(
            self, mock_client, client):
        config = MONGO_CONFIG.copy()
        mongo = MongoDB(config)

        client.return_value = mock_client

        hosts = {}
        for i in range(config['num_peers']):
            hosts['i'] = "host{}".format(i)

        mongo.initialize_replica_set(hosts)
        mock_client.admin.command.assert_called()
        command, _ = mock_client.admin.command.call_args
        self.assertEqual("replSetInitiate", command[0])
    def test_new_user_requests_user_creation(self, client):
        config = MONGO_CONFIG.copy()
        mongo = MongoDB(config)

        mock_client = MagicMock()
        client.return_value = mock_client
        mock_db = Mock()
        mock_client.__getitem__.return_value = mock_db

        credentials = {"username": "******", "password": "******"}
        mongo.new_user(credentials)

        client.assert_called()
        mock_db.command.assert_called_once_with("createUser",
                                                credentials["username"],
                                                pwd=credentials["password"],
                                                roles=[])
Esempio n. 8
0
 def mongo(self):
     config = {
         'app_name': self.model.app.name,
         'replica_set_name': self.replica_set_name,
         'num_peers': self.num_peers,
         'port': self.port
     }
     return MongoDB(config)
    def test_mongoserver_returns_correct_version(self, client):
        config = MONGO_CONFIG.copy()
        mongo = MongoDB(config)

        version = "1.0.0"
        mock_client = MagicMock()
        client.return_value = mock_client
        mock_client.server_info.return_value = {"version": version}
        self.assertEqual(mongo.version, version)
    def test_new_databases_creation_updates_user_privileges(self, client):
        config = MONGO_CONFIG.copy()
        mongo = MongoDB(config)

        mock_client = MagicMock()
        client.return_value = mock_client
        mock_db = Mock()
        mock_client.__getitem__.return_value = mock_db

        credentials = {"username": "******", "password": "******"}
        databases = ["mydb"]
        roles = [{"role": "readWrite", "db": databases[0]}]
        mongo.new_databases(credentials, databases)

        client.assert_called()
        mock_db.command.assert_called_once_with("updateUser",
                                                credentials["username"],
                                                roles=roles)
Esempio n. 11
0
    def root_password(self):
        """MongoDB root password.
        """
        root_password = None
        peers = self.peers

        data = self.peers.data[peers.app]
        root_password = data.get('root_password', None)

        if root_password is None:
            root_password = MongoDB.new_password()

        return root_password
Esempio n. 12
0
    def credentials(self, rel_id):
        """Fetch credentials associated with a relation.

        Args:
           rel_id: integer id of relation

        Returns:
           A dictionary with keys "username" and "password".
        """
        consumers = self.consumers()

        if self.is_new_relation(rel_id):
            creds = {
                'username': self.new_username(rel_id),
                'password': MongoDB.new_password()
            }
            consumers[rel_id] = creds

            self.charm.peers.data[self.charm.app]['consumers'] = json.dumps(
                consumers)
        else:
            creds = consumers[rel_id]
        return creds
Esempio n. 13
0
 def mongo(self):
     """Fetch the MongoDB server interface object.
     """
     return MongoDB(self.config)
 def test_replica_set_uri_contains_correct_number_of_hosts(self):
     config = MONGO_CONFIG.copy()
     mongo = MongoDB(config)
     uri = mongo.replica_set_uri()
     host_list = uri.split(',')
     self.assertEqual(len(host_list), config['num_peers'])
    def test_mongo_is_not_ready_when_server_info_is_not_available(self):
        config = MONGO_CONFIG.copy()
        mongo = MongoDB(config)

        ready = mongo.is_ready()
        self.assertEqual(ready, False)
    def test_client_returns_mongo_client_instance(self):
        config = MONGO_CONFIG.copy()
        mongo = MongoDB(config)

        client = mongo.client()
        self.assertIsInstance(client, MongoClient)