Ejemplo n.º 1
0
 def test_wait_while_reachable(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     servers = [member['host'] for member in self.repl.members()]
     self.assertTrue(self.repl.wait_while_reachable(servers, timeout=10))
     self.repl.member_command(1, 'stop')
     self.assertFalse(self.repl.wait_while_reachable(servers, timeout=10))
Ejemplo n.º 2
0
    def test_scram_with_ssl(self):
        member_params = {'procParams': {'clusterAuthMode': 'x509'}}
        self.repl_cfg = {
            'login': '******',
            'password': '******',
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        # Should not raise an Exception.
        self.repl = ReplicaSet(self.repl_cfg)

        # Should create the user we requested. No raise on authenticate.
        client = pymongo.MongoClient(self.repl.primary(),
                                     ssl_certfile=certificate('client.pem'),
                                     ssl_cert_reqs=ssl.CERT_NONE)
        client.admin.authenticate('luke', 'ekul')
        # This should be the only user.
        self.assertEqual(len(client.admin.command('usersInfo')['users']), 1)
        self.assertFalse(client['$external'].command('usersInfo')['users'])
Ejemplo n.º 3
0
 def test_member_info_auth_uri(self):
     member_params = {
         'procParams': {
             'clusterAuthMode': 'x509',
             'setParameter': {
                 'authenticationMechanisms': 'MONGODB-X509'
             }
         }
     }
     self.repl_cfg = {
         'login': TEST_SUBJECT,
         'authSource': '$external',
         'members': [member_params, member_params],
         'sslParams': {
             'sslCAFile': certificate('ca.pem'),
             'sslPEMKeyFile': certificate('server.pem'),
             'sslMode': 'requireSSL',
             'sslClusterFile': certificate('cluster_cert.pem'),
             'sslAllowInvalidCertificates': True
         }
     }
     self.repl = ReplicaSet(self.repl_cfg)
     for i in range(len(self.repl)):
         member = self.repl.member_info(i)
         self.assertIn('mongodb_auth_uri', member)
         uri = member['mongodb_auth_uri']
         host = Servers().hostname(member['server_id'])
         self.assertIn(host, uri)
         self.assertIn(TEST_SUBJECT, uri)
         self.assertIn('authSource=$external', uri)
         self.assertIn('authMechanism=MONGODB-X509', uri)
Ejemplo n.º 4
0
    def test_mongodb_auth_uri(self):
        if SERVER_VERSION < (2, 4):
            raise SkipTest("Need to be able to set 'authenticationMechanisms' "
                           "parameter to test.")

        member_params = {
            'procParams': {
                'clusterAuthMode': 'x509',
                'setParameter': {
                    'authenticationMechanisms': 'MONGODB-X509'
                }
            }
        }
        self.repl_cfg = {
            'login': TEST_SUBJECT,
            'authSource': '$external',
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        self.repl = ReplicaSet(self.repl_cfg)

        self.assertIn('mongodb_auth_uri', self.repl.info())
        repl_auth_uri = self.repl.info()['mongodb_auth_uri']
        hosts = ','.join(m['host'] for m in self.repl.members())
        self.assertIn(hosts, repl_auth_uri)
        self.assertIn(TEST_SUBJECT, repl_auth_uri)
        self.assertIn('authSource=$external', repl_auth_uri)
        self.assertIn('authMechanism=MONGODB-X509', repl_auth_uri)
Ejemplo n.º 5
0
 def test_servers(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     self.repl.repl_member_add({"rsParams": {"priority": 0}})
     for member in self.repl.servers():
         self.assertTrue(member['host'] in self.repl.run_command(
             'isMaster', is_eval=False).get('hosts'))
Ejemplo n.º 6
0
 def test_member_del_no_reconfig(self):
     self.repl_cfg = {'members': [{}, {}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     m_count = len(self.repl.config['members'])
     self.assertTrue(self.repl.member_del(2, reconfig=False))
     self.assertEqual(len(self.repl.config['members']), m_count)
     self.repl.server_map.pop(2)
Ejemplo n.º 7
0
 def test_member_id_to_host(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     members = self.repl.config['members']
     for member in members:
         host = self.repl.member_id_to_host(member['_id'])
         self.assertEqual(member['host'], host)
Ejemplo n.º 8
0
 def test_len(self):
     self.repl = ReplicaSet(self.repl_cfg)
     self.assertTrue(len(self.repl) == len(self.repl_cfg['members']))
     self.repl.member_del(3)
     self.assertTrue(len(self.repl) == len(self.repl_cfg['members']) - 1)
     self.repl.repl_member_add({'rsParams': {'arbiterOnly': True}})
     self.assertTrue(len(self.repl) == len(self.repl_cfg['members']))
Ejemplo n.º 9
0
 def test_hidden(self):
     self.repl = ReplicaSet(self.repl_cfg)
     for _ in self.repl.hidden():
         self.assertTrue(
             self.repl.run_command('serverStatus',
                                   arg=None,
                                   is_eval=False,
                                   member_id=2)['repl']['hidden'])
Ejemplo n.º 10
0
 def test_members(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     members1 = sorted(self.repl.config['members'],
                       key=lambda item: item['_id'])
     members2 = sorted(self.repl.members(), key=lambda item: item['_id'])
     self.assertEqual(len(members1), len(members2))
     for i in range(len(members1)):
         self.assertEqual(members1[i]['host'], members2[i]['host'])
         self.assertEqual(members1[i]['_id'], members2[i]['_id'])
Ejemplo n.º 11
0
 def test_member_info(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     member = [
         item for item in self.repl.config['members'] if item['_id'] == 1
     ][0]
     result = self.repl.member_info(1)
     self.assertTrue(result['procInfo']['alive'])
     self.assertIn(member['host'], result['mongodb_uri'])
     self.assertTrue(len(result['rsInfo']) > 0)
Ejemplo n.º 12
0
 def test_member_command(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     _id = 1
     self.assertTrue(self.repl.member_info(_id)['procInfo']['alive'])
     self.repl.member_command(_id, 'stop')
     self.assertFalse(self.repl.member_info(_id)['procInfo']['alive'])
     self.repl.member_command(_id, 'start')
     self.assertTrue(self.repl.member_info(_id)['procInfo']['alive'])
     self.repl.member_command(_id, 'restart')
     self.assertTrue(self.repl.member_info(_id)['procInfo']['alive'])
Ejemplo n.º 13
0
 def test_connection(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     _id = 1
     hostname = self.repl.member_id_to_host(_id)
     self.assertTrue(self.repl.connection(timeout=5))
     self.assertTrue(self.repl.connection(hostname=hostname, timeout=5))
     self.repl.member_command(_id, 'stop')
     self.assertRaises(
         pymongo.errors.AutoReconnect,
         lambda: self.repl.connection(hostname=hostname, timeout=5))
Ejemplo n.º 14
0
 def setUp(self):
     PortPool().change_range()
     self.servers = Servers()
     self.servers.set_settings(os.environ.get('MONGOBIN', None))
     self.repl_cfg = {
         'auth_key': 'secret',
         'login': '******',
         'password': '******',
         'members': [{}, {}]
     }
     self.repl = ReplicaSet(self.repl_cfg)
Ejemplo n.º 15
0
 def test_member_create(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     result = self.repl.member_create({}, 13)
     self.assertTrue('host' in result)
     self.assertTrue('_id' in result)
     h_id = Servers().host_to_server_id(result['host'])
     h_info = Servers().info(h_id)
     self.assertIn(result['host'], h_info['mongodb_uri'])
     self.assertTrue(h_info['procInfo']['alive'])
     Servers().remove(h_id)
Ejemplo n.º 16
0
 def setUp(self):
     PortPool().change_range()
     self.servers = Servers()
     self.servers.set_settings(*TEST_RELEASES)
     self.repl_cfg = {
         'auth_key': 'secret',
         'login': '******',
         'password': '******',
         'members': [{}, {}]
     }
     self.repl = ReplicaSet(self.repl_cfg)
Ejemplo n.º 17
0
    def test_compare_servers_passives(self):
        self.repl = ReplicaSet(self.repl_cfg)
        self.repl.repl_member_add({"rsParams": {"priority": 0}})
        self.repl.repl_member_add({})
        servers = self.repl.servers()
        passives = self.repl.passives()
        for item in servers:
            self.assertTrue(item not in passives)

        for item in passives:
            self.assertTrue(item not in servers)
Ejemplo n.º 18
0
 def test_info(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     cfg = self.repl.config
     info = self.repl.info()
     self.assertEqual(info['auth_key'], self.repl.auth_key)
     self.assertEqual(info['id'], self.repl.repl_id)
     self.assertEqual(len(info['members']), len(cfg['members']))
     members1 = sorted(cfg['members'], key=lambda item: item['_id'])
     members2 = sorted(info['members'], key=lambda item: item['_id'])
     for i in range(len(members1)):
         self.assertEqual(members1[i]['_id'], members2[i]['_id'])
         self.assertEqual(members1[i]['host'], members2[i]['host'])
Ejemplo n.º 19
0
 def test_run_command(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     self.assertEqual(
         self.repl.run_command("rs.status()", is_eval=True)['ok'], 1)
     result = self.repl.run_command('serverStatus',
                                    arg=None,
                                    is_eval=False,
                                    member_id=0)['repl']
     for key in ('me', 'ismaster', 'setName', 'primary', 'hosts'):
         self.assertTrue(key in result)
     self.assertEqual(
         self.repl.run_command(command="replSetGetStatus",
                               is_eval=False)['ok'], 1)
Ejemplo n.º 20
0
 def test_repl_member_add(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     member_id = self.repl.repl_member_add(
         {"rsParams": {
             "priority": 0,
             "hidden": True
         }})
     self.assertTrue(member_id >= 0)
     member = [
         item for item in self.repl.config['members']
         if item['_id'] == member_id
     ][0]
     self.assertTrue(member['hidden'])
Ejemplo n.º 21
0
 def test_repl_update(self):
     self.repl_cfg = {
         'members': [{}, {}, {
             'rsParams': {
                 'priority': 0,
                 'hidden': True
             }
         }]
     }
     self.repl = ReplicaSet(self.repl_cfg)
     config = self.repl.config
     config['members'][1]['priority'] = 0
     config['members'][1]['hidden'] = True
     self.assertTrue(self.repl.repl_update(config))
     self.assertTrue(self.repl.config['members'][1]['hidden'])
Ejemplo n.º 22
0
 def test_repl_member_add(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     member_id = self.repl.repl_member_add({"rsParams": {"priority": 0, "hidden": True}})
     self.assertTrue(member_id >= 0)
     member = [item for item in self.repl.config['members'] if item['_id'] == member_id][0]
     self.assertTrue(member['hidden'])
Ejemplo n.º 23
0
 def test_member_id_to_host(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     members = self.repl.config['members']
     for member in members:
         host = self.repl.member_id_to_host(member['_id'])
         self.assertEqual(member['host'], host)
Ejemplo n.º 24
0
 def test_member_update(self):
     self.repl = ReplicaSet(self.repl_cfg)
     member = [
         item for item in self.repl.config['members'] if item['_id'] == 2
     ][0]
     self.assertTrue(member.get('hidden', False))
     self.assertTrue(
         self.repl.member_update(
             2, {"rsParams": {
                 "priority": 1,
                 "hidden": False
             }}))
     member = [
         item for item in self.repl.config['members'] if item['_id'] == 2
     ][0]
     self.assertFalse(member.get('hidden', False))
Ejemplo n.º 25
0
 def test_host2id(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     members = self.repl.config['members']
     for member in members:
         self.assertEqual(member['_id'],
                          self.repl.host2id(member['host']))
Ejemplo n.º 26
0
 def test_member_update(self):
     self.repl = ReplicaSet(self.repl_cfg)
     member = [item for item in self.repl.config['members'] if item['_id'] == 2][0]
     self.assertTrue(member.get('hidden', False))
     self.assertTrue(self.repl.member_update(2, {"rsParams": {"priority": 1, "hidden": False}}))
     member = [item for item in self.repl.config['members'] if item['_id'] == 2][0]
     self.assertFalse(member.get('hidden', False))
Ejemplo n.º 27
0
 def test_member_del_no_reconfig(self):
     self.repl_cfg = {'members': [{}, {}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     m_count = len(self.repl.config['members'])
     self.assertTrue(self.repl.member_del(2, reconfig=False))
     self.assertEqual(len(self.repl.config['members']), m_count)
     self.repl.server_map.pop(2)
Ejemplo n.º 28
0
 def test_wait_while_reachable(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     servers = [member['host'] for member in self.repl.members()]
     self.assertTrue(self.repl.wait_while_reachable(servers, timeout=10))
     self.repl.member_command(1, 'stop')
     self.assertFalse(self.repl.wait_while_reachable(servers, timeout=10))
Ejemplo n.º 29
0
 def test_len(self):
     self.repl = ReplicaSet(self.repl_cfg)
     self.assertTrue(len(self.repl) == len(self.repl_cfg['members']))
     self.repl.member_del(3)
     self.assertTrue(len(self.repl) == len(self.repl_cfg['members']) - 1)
     self.repl.repl_member_add({'rsParams': {'arbiterOnly': True}})
     self.assertTrue(len(self.repl) == len(self.repl_cfg['members']))
Ejemplo n.º 30
0
 def test_auth_arbiter_member_info(self):
     self.repl = ReplicaSet(
         {'members': [{}, {
             'rsParams': {
                 'arbiterOnly': True
             }
         }]})
     info = self.repl.member_info(1)
     for key in ('procInfo', 'mongodb_uri', 'statuses', 'rsInfo'):
         self.assertIn(key, info)
     rs_info = info['rsInfo']
     for key in ('primary', 'secondary', 'arbiterOnly'):
         self.assertIn(key, rs_info)
     self.assertFalse(rs_info['primary'])
     self.assertFalse(rs_info['secondary'])
     self.assertTrue(rs_info['arbiterOnly'])
Ejemplo n.º 31
0
 def test_member_info_auth_uri(self):
     member_params = {
         'procParams': {
             'clusterAuthMode': 'x509',
             'setParameter': {'authenticationMechanisms': 'MONGODB-X509'}
         }
     }
     self.repl_cfg = {
         'login': TEST_SUBJECT,
         'authSource': '$external',
         'members': [member_params, member_params],
         'sslParams': {
             'sslCAFile': certificate('ca.pem'),
             'sslPEMKeyFile': certificate('server.pem'),
             'sslMode': 'requireSSL',
             'sslClusterFile': certificate('cluster_cert.pem'),
             'sslAllowInvalidCertificates': True
         }
     }
     self.repl = ReplicaSet(self.repl_cfg)
     for i in range(len(self.repl)):
         member = self.repl.member_info(i)
         self.assertIn('mongodb_auth_uri', member)
         uri = member['mongodb_auth_uri']
         host = Servers().hostname(member['server_id'])
         self.assertIn(host, uri)
         self.assertIn(TEST_SUBJECT, uri)
         self.assertIn('authSource=$external', uri)
         self.assertIn('authMechanism=MONGODB-X509', uri)
Ejemplo n.º 32
0
    def test_mongodb_auth_uri(self):
        if SERVER_VERSION < (2, 4):
            raise SkipTest("Need to be able to set 'authenticationMechanisms' "
                           "parameter to test.")

        member_params = {
            'procParams': {
                'clusterAuthMode': 'x509',
                'setParameter': {'authenticationMechanisms': 'MONGODB-X509'}
            }
        }
        self.repl_cfg = {
            'login': TEST_SUBJECT,
            'authSource': '$external',
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        self.repl = ReplicaSet(self.repl_cfg)

        self.assertIn('mongodb_auth_uri', self.repl.info())
        repl_auth_uri = self.repl.info()['mongodb_auth_uri']
        hosts = ','.join(m['host'] for m in self.repl.members())
        self.assertIn(hosts, repl_auth_uri)
        self.assertIn(TEST_SUBJECT, repl_auth_uri)
        self.assertIn('authSource=$external', repl_auth_uri)
        self.assertIn('authMechanism=MONGODB-X509', repl_auth_uri)
        replset_param = 'replicaSet=' + self.repl.repl_id
        self.assertIn(replset_param, repl_auth_uri)
Ejemplo n.º 33
0
    def test_scram_with_ssl(self):
        member_params = {'procParams': {'clusterAuthMode': 'x509'}}
        self.repl_cfg = {
            'login': '******',
            'password': '******',
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        # Should not raise an Exception.
        self.repl = ReplicaSet(self.repl_cfg)

        # Should create the user we requested. No raise on authenticate.
        client = pymongo.MongoClient(
            self.repl.primary(), ssl_certfile=certificate('client.pem'),
            ssl_cert_reqs=ssl.CERT_NONE)
        client.admin.authenticate('luke', 'ekul')
        # This should be the only user.
        self.assertEqual(len(client.admin.command('usersInfo')['users']), 1)
        self.assertFalse(client['$external'].command('usersInfo')['users'])
Ejemplo n.º 34
0
 def test_run_command(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     self.assertEqual(self.repl.run_command("rs.status()", is_eval=True)['ok'], 1)
     result = self.repl.run_command('serverStatus', arg=None, is_eval=False, member_id=0)['repl']
     for key in ('me', 'ismaster', 'setName', 'primary', 'hosts'):
         self.assertTrue(key in result)
     self.assertEqual(self.repl.run_command(command="replSetGetStatus", is_eval=False)['ok'], 1)
Ejemplo n.º 35
0
 def test_member_info(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     member = [item for item in self.repl.config['members'] if item['_id'] == 1][0]
     result = self.repl.member_info(1)
     self.assertTrue(result['procInfo']['alive'])
     self.assertIn(member['host'], result['mongodb_uri'])
     self.assertTrue(len(result['rsInfo']) > 0)
Ejemplo n.º 36
0
 def test_repl_update(self):
     self.repl_cfg = {'members': [{}, {}, {'rsParams': {'priority': 0, 'hidden': True}}]}
     self.repl = ReplicaSet(self.repl_cfg)
     config = self.repl.config
     config['members'][1]['priority'] = 0
     config['members'][1]['hidden'] = True
     self.assertTrue(self.repl.repl_update(config))
     self.assertTrue(self.repl.config['members'][1]['hidden'])
Ejemplo n.º 37
0
 def test_rs_settings(self):
     if SERVER_VERSION < (2, 4):
         raise SkipTest(
             "Need at least MongoDB >= 2.4 to test replica set settings.")
     self.repl_cfg = {
         'rsSettings': {
             'heartbeatTimeoutSecs': 20
         },
         'members': [{}]
     }
     self.repl = ReplicaSet(self.repl_cfg)
     conn = self.repl.connection()
     if SERVER_VERSION >= (2, 8):
         config = conn.admin.command('replSetGetConfig')['config']
     else:
         config = conn.local.system.replset.find_one()
     self.assertEqual(config['settings']['heartbeatTimeoutSecs'], 20)
Ejemplo n.º 38
0
    def test_reset(self):
        self.repl_cfg = {'members': [{}, {}, {}]}
        self.repl = ReplicaSet(self.repl_cfg)

        server_ids = [m['server_id'] for m in self.repl.members()]
        all_hosts = map(Servers().hostname, server_ids)

        # Shut down all members of the ReplicaSet.
        for server_id in server_ids:
            Servers().command(server_id, 'stop')

        # Reset the ReplicaSet. --- We should be able to connect to all members.
        self.repl.reset()

        for host in all_hosts:
            # No ConnectionFailure/AutoReconnect.
            connected(pymongo.MongoClient(host))
Ejemplo n.º 39
0
 def test_members(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     members1 = sorted(self.repl.config['members'], key=lambda item: item['_id'])
     members2 = sorted(self.repl.members(), key=lambda item: item['_id'])
     self.assertEqual(len(members1), len(members2))
     for i in range(len(members1)):
         self.assertEqual(members1[i]['host'], members2[i]['host'])
         self.assertEqual(members1[i]['_id'], members2[i]['_id'])
Ejemplo n.º 40
0
 def test_connection(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     _id = 1
     hostname = self.repl.member_id_to_host(_id)
     self.assertTrue(self.repl.connection(timeout=5))
     self.assertTrue(self.repl.connection(hostname=hostname, timeout=5))
     self.repl.member_command(_id, 'stop')
     self.assertRaises(pymongo.errors.AutoReconnect, lambda: self.repl.connection(hostname=hostname, timeout=5))
Ejemplo n.º 41
0
 def test_member_create(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     result = self.repl.member_create({}, 13)
     self.assertTrue('host' in result)
     self.assertTrue('_id' in result)
     h_id = Servers().host_to_server_id(result['host'])
     h_info = Servers().info(h_id)
     self.assertIn(result['host'], h_info['mongodb_uri'])
     self.assertTrue(h_info['procInfo']['alive'])
     Servers().remove(h_id)
Ejemplo n.º 42
0
 def test_member_command(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     _id = 1
     self.assertTrue(self.repl.member_info(_id)['procInfo']['alive'])
     self.repl.member_command(_id, 'stop')
     self.assertFalse(self.repl.member_info(_id)['procInfo']['alive'])
     self.repl.member_command(_id, 'start')
     self.assertTrue(self.repl.member_info(_id)['procInfo']['alive'])
     self.repl.member_command(_id, 'restart')
     self.assertTrue(self.repl.member_info(_id)['procInfo']['alive'])
Ejemplo n.º 43
0
    def test_compare_servers_passives(self):
        self.repl = ReplicaSet(self.repl_cfg)
        self.repl.repl_member_add({"rsParams": {"priority": 0}})
        self.repl.repl_member_add({})
        servers = self.repl.servers()
        passives = self.repl.passives()
        for item in servers:
            self.assertTrue(item not in passives)

        for item in passives:
            self.assertTrue(item not in servers)
Ejemplo n.º 44
0
    def test_operations(self):
        repl_cfg = {'members': [{}, {}]}
        repl = ReplicaSet(repl_cfg)

        self.assertEqual(len(self.rs), 0)
        operator.setitem(self.rs, 1, repl)
        self.assertEqual(len(self.rs), 1)
        self.assertEqual(operator.getitem(self.rs, 1).repl_id, repl.repl_id)
        operator.delitem(self.rs, 1)
        self.assertEqual(len(self.rs), 0)
        self.assertRaises(KeyError, operator.getitem, self.rs, 1)
Ejemplo n.º 45
0
    def test_ssl_auth(self):
        if SERVER_VERSION < (2, 4):
            raise SkipTest("Need to be able to set 'authenticationMechanisms' "
                           "parameter to test.")

        member_params = {
            'procParams': {
                'clusterAuthMode': 'x509',
                'setParameter': {
                    'authenticationMechanisms': 'MONGODB-X509'
                }
            }
        }
        self.repl_cfg = {
            'login': TEST_SUBJECT,
            'authSource': '$external',
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        # Should not raise an Exception.
        self.repl = ReplicaSet(self.repl_cfg)

        # Should create an extra user. No raise on authenticate.
        client = pymongo.MongoClient(self.repl.primary(),
                                     ssl_certfile=DEFAULT_CLIENT_CERT,
                                     ssl_cert_reqs=ssl.CERT_NONE)
        client['$external'].authenticate(DEFAULT_SUBJECT,
                                         mechanism='MONGODB-X509')

        # Should create the user we requested. No raise on authenticate.
        client = pymongo.MongoClient(self.repl.primary(),
                                     ssl_certfile=certificate('client.pem'),
                                     ssl_cert_reqs=ssl.CERT_NONE)
        client['$external'].authenticate(TEST_SUBJECT,
                                         mechanism='MONGODB-X509')
 def test_auth_arbiter_member_info(self):
     self.repl = ReplicaSet({'members': [
         {}, {'rsParams': {'arbiterOnly': True}}]})
     info = self.repl.member_info(1)
     for key in ('procInfo', 'mongodb_uri', 'statuses', 'rsInfo'):
         self.assertIn(key, info)
     rs_info = info['rsInfo']
     for key in ('primary', 'secondary', 'arbiterOnly'):
         self.assertIn(key, rs_info)
     self.assertFalse(rs_info['primary'])
     self.assertFalse(rs_info['secondary'])
     self.assertTrue(rs_info['arbiterOnly'])
Ejemplo n.º 47
0
 def test_info(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     cfg = self.repl.config
     info = self.repl.info()
     self.assertEqual(info['auth_key'], self.repl.auth_key)
     self.assertEqual(info['id'], self.repl.repl_id)
     self.assertEqual(len(info['members']), len(cfg['members']))
     members1 = sorted(cfg['members'], key=lambda item: item['_id'])
     members2 = sorted(info['members'], key=lambda item: item['_id'])
     for i in range(len(members1)):
         self.assertEqual(members1[i]['_id'], members2[i]['_id'])
         self.assertEqual(members1[i]['host'], members2[i]['host'])
Ejemplo n.º 48
0
    def test_ssl(self):
        member_params = {}
        self.repl_cfg = {
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        # Should not raise an Exception.
        self.repl = ReplicaSet(self.repl_cfg)

        # Server should require SSL.
        with self.assertRaises(pymongo.errors.ConnectionFailure):
            connected(pymongo.MongoClient, self.repl.primary())

        # This shouldn't raise.
        connected(
            pymongo.MongoClient(self.repl.primary(),
                                ssl_certfile=certificate('client.pem')))
Ejemplo n.º 49
0
 def test_rs_settings(self):
     if SERVER_VERSION < (2, 4):
         raise SkipTest(
             "Need at least MongoDB >= 2.4 to test replica set settings.")
     self.repl_cfg = {
         'rsSettings': {'heartbeatTimeoutSecs': 20},
         'members': [{}]
     }
     self.repl = ReplicaSet(self.repl_cfg)
     conn = self.repl.connection()
     if SERVER_VERSION >= (2, 8):
         config = conn.admin.command('replSetGetConfig')['config']
     else:
         config = conn.local.system.replset.find_one()
     self.assertEqual(config['settings']['heartbeatTimeoutSecs'], 20)
class ReplicaSetAuthTestCase(unittest.TestCase):
    def setUp(self):
        PortPool().change_range()
        self.servers = Servers()
        self.servers.set_settings(os.environ.get('MONGOBIN', None))
        self.repl_cfg = {'auth_key': 'secret', 'login': '******', 'password': '******', 'members': [{}, {}]}
        self.repl = ReplicaSet(self.repl_cfg)

    def tearDown(self):
        if len(self.repl) > 0:
            self.repl.cleanup()

    def test_auth_connection(self):
        self.assertTrue(isinstance(self.repl.connection().admin.collection_names(), list))
        c = pymongo.MongoReplicaSetClient(self.repl.primary(), replicaSet=self.repl.repl_id)
        self.assertRaises(pymongo.errors.OperationFailure, c.admin.collection_names)

    def test_auth_admin(self):
        c = pymongo.MongoReplicaSetClient(self.repl.primary(), replicaSet=self.repl.repl_id)
        self.assertRaises(pymongo.errors.OperationFailure, c.admin.collection_names)
        self.assertTrue(c.admin.authenticate('admin', 'admin'))
        self.assertTrue(isinstance(c.admin.collection_names(), list))
        self.assertTrue(c.admin.logout() is None)
        self.assertRaises(pymongo.errors.OperationFailure, c.admin.collection_names)

    def test_auth_collection(self):
        c = pymongo.MongoReplicaSetClient(self.repl.primary(), replicaSet=self.repl.repl_id)
        self.assertTrue(c.admin.authenticate('admin', 'admin'))
        db = c.test_auth
        db.add_user('user', 'userpass', roles=['readWrite'])
        c.admin.logout()

        self.assertTrue(db.authenticate('user', 'userpass'))
        self.assertTrue(db.foo.insert({'foo': 'bar'}, w=2, wtimeout=1000))
        self.assertTrue(isinstance(db.foo.find_one(), dict))
        db.logout()
        self.assertRaises(pymongo.errors.OperationFailure, db.foo.find_one)

    def test_auth_arbiter_member_info(self):
        self.repl = ReplicaSet({'members': [
            {}, {'rsParams': {'arbiterOnly': True}}]})
        info = self.repl.member_info(1)
        for key in ('procInfo', 'mongodb_uri', 'statuses', 'rsInfo'):
            self.assertIn(key, info)
        rs_info = info['rsInfo']
        for key in ('primary', 'secondary', 'arbiterOnly'):
            self.assertIn(key, rs_info)
        self.assertFalse(rs_info['primary'])
        self.assertFalse(rs_info['secondary'])
        self.assertTrue(rs_info['arbiterOnly'])
Ejemplo n.º 51
0
    def test_reset(self):
        self.repl_cfg = {'members': [{}, {}, {}]}
        self.repl = ReplicaSet(self.repl_cfg)

        server_ids = [m['server_id'] for m in self.repl.members()]
        all_hosts = [Servers().hostname(server_id) for server_id in server_ids]

        # Shut down all members of the ReplicaSet.
        for server_id in server_ids:
            Servers().command(server_id, 'stop')

        # Reset the ReplicaSet. --- We should be able to connect to all members.
        self.repl.reset()

        for host in all_hosts:
            # No ConnectionFailure/AutoReconnect.
            connected(pymongo.MongoClient(host))
Ejemplo n.º 52
0
    def test_ssl_auth(self):
        if SERVER_VERSION < (2, 4):
            raise SkipTest("Need to be able to set 'authenticationMechanisms' "
                           "parameter to test.")

        member_params = {
            'procParams': {
                'clusterAuthMode': 'x509',
                'setParameter': {'authenticationMechanisms': 'MONGODB-X509'}
            }
        }
        self.repl_cfg = {
            'login': TEST_SUBJECT,
            'authSource': '$external',
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        # Should not raise an Exception.
        self.repl = ReplicaSet(self.repl_cfg)

        # Should create an extra user. No raise on authenticate.
        client = pymongo.MongoClient(
            self.repl.primary(), ssl_certfile=DEFAULT_CLIENT_CERT,
            ssl_cert_reqs=ssl.CERT_NONE)
        client['$external'].authenticate(
            DEFAULT_SUBJECT, mechanism='MONGODB-X509')

        # Should create the user we requested. No raise on authenticate.
        client = pymongo.MongoClient(
            self.repl.primary(), ssl_certfile=certificate('client.pem'),
            ssl_cert_reqs=ssl.CERT_NONE)
        client['$external'].authenticate(
            TEST_SUBJECT, mechanism='MONGODB-X509')
Ejemplo n.º 53
0
    def test_ssl(self):
        member_params = {}
        self.repl_cfg = {
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        # Should not raise an Exception.
        self.repl = ReplicaSet(self.repl_cfg)

        # Server should require SSL.
        with self.assertRaises(pymongo.errors.ConnectionFailure):
            connected(pymongo.MongoClient(self.repl.primary()))

        # This shouldn't raise.
        connected(pymongo.MongoClient(
            self.repl.primary(), ssl_certfile=certificate('client.pem'),
            ssl_cert_reqs=ssl.CERT_NONE))
Ejemplo n.º 54
0
class ReplicaSetSSLTestCase(SSLTestCase):

    def tearDown(self):
        if hasattr(self, 'repl'):
            self.repl.cleanup()

    def test_ssl_auth(self):
        if SERVER_VERSION < (2, 4):
            raise SkipTest("Need to be able to set 'authenticationMechanisms' "
                           "parameter to test.")

        member_params = {
            'procParams': {
                'clusterAuthMode': 'x509',
                'setParameter': {'authenticationMechanisms': 'MONGODB-X509'}
            }
        }
        self.repl_cfg = {
            'login': TEST_SUBJECT,
            'authSource': '$external',
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        # Should not raise an Exception.
        self.repl = ReplicaSet(self.repl_cfg)

        # Should create an extra user. No raise on authenticate.
        client = pymongo.MongoClient(
            self.repl.primary(), ssl_certfile=DEFAULT_CLIENT_CERT,
            ssl_cert_reqs=ssl.CERT_NONE)
        client['$external'].authenticate(
            DEFAULT_SUBJECT, mechanism='MONGODB-X509')

        # Should create the user we requested. No raise on authenticate.
        client = pymongo.MongoClient(
            self.repl.primary(), ssl_certfile=certificate('client.pem'),
            ssl_cert_reqs=ssl.CERT_NONE)
        client['$external'].authenticate(
            TEST_SUBJECT, mechanism='MONGODB-X509')

    def test_scram_with_ssl(self):
        member_params = {'procParams': {'clusterAuthMode': 'x509'}}
        self.repl_cfg = {
            'login': '******',
            'password': '******',
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        # Should not raise an Exception.
        self.repl = ReplicaSet(self.repl_cfg)

        # Should create the user we requested. No raise on authenticate.
        client = pymongo.MongoClient(
            self.repl.primary(), ssl_certfile=certificate('client.pem'),
            ssl_cert_reqs=ssl.CERT_NONE)
        client.admin.authenticate('luke', 'ekul')
        # This should be the only user.
        self.assertEqual(len(client.admin.command('usersInfo')['users']), 1)
        self.assertFalse(client['$external'].command('usersInfo')['users'])

    def test_ssl(self):
        member_params = {}
        self.repl_cfg = {
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        # Should not raise an Exception.
        self.repl = ReplicaSet(self.repl_cfg)

        # Server should require SSL.
        with self.assertRaises(pymongo.errors.ConnectionFailure):
            connected(pymongo.MongoClient(self.repl.primary()))

        # This shouldn't raise.
        connected(pymongo.MongoClient(
            self.repl.primary(), ssl_certfile=certificate('client.pem'),
            ssl_cert_reqs=ssl.CERT_NONE))

    def test_mongodb_auth_uri(self):
        if SERVER_VERSION < (2, 4):
            raise SkipTest("Need to be able to set 'authenticationMechanisms' "
                           "parameter to test.")

        member_params = {
            'procParams': {
                'clusterAuthMode': 'x509',
                'setParameter': {'authenticationMechanisms': 'MONGODB-X509'}
            }
        }
        self.repl_cfg = {
            'login': TEST_SUBJECT,
            'authSource': '$external',
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        self.repl = ReplicaSet(self.repl_cfg)

        self.assertIn('mongodb_auth_uri', self.repl.info())
        repl_auth_uri = self.repl.info()['mongodb_auth_uri']
        hosts = ','.join(m['host'] for m in self.repl.members())
        self.assertIn(hosts, repl_auth_uri)
        self.assertIn(TEST_SUBJECT, repl_auth_uri)
        self.assertIn('authSource=$external', repl_auth_uri)
        self.assertIn('authMechanism=MONGODB-X509', repl_auth_uri)
        replset_param = 'replicaSet=' + self.repl.repl_id
        self.assertIn(replset_param, repl_auth_uri)

    def test_member_info_auth_uri(self):
        member_params = {
            'procParams': {
                'clusterAuthMode': 'x509',
                'setParameter': {'authenticationMechanisms': 'MONGODB-X509'}
            }
        }
        self.repl_cfg = {
            'login': TEST_SUBJECT,
            'authSource': '$external',
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        self.repl = ReplicaSet(self.repl_cfg)
        for i in range(len(self.repl)):
            member = self.repl.member_info(i)
            self.assertIn('mongodb_auth_uri', member)
            uri = member['mongodb_auth_uri']
            host = Servers().hostname(member['server_id'])
            self.assertIn(host, uri)
            self.assertIn(TEST_SUBJECT, uri)
            self.assertIn('authSource=$external', uri)
            self.assertIn('authMechanism=MONGODB-X509', uri)
Ejemplo n.º 55
0
 def setUp(self):
     PortPool().change_range()
     self.servers = Servers()
     self.servers.set_settings(*TEST_RELEASES)
     self.repl_cfg = {'auth_key': 'secret', 'login': '******', 'password': '******', 'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
Ejemplo n.º 56
0
class ReplicaSetSSLTestCase(SSLTestCase):
    def test_ssl_auth(self):
        if SERVER_VERSION < (2, 4):
            raise SkipTest("Need to be able to set 'authenticationMechanisms' "
                           "parameter to test.")

        member_params = {
            'procParams': {
                'clusterAuthMode': 'x509',
                'setParameter': {
                    'authenticationMechanisms': 'MONGODB-X509'
                }
            }
        }
        self.repl_cfg = {
            'login': TEST_SUBJECT,
            'authSource': '$external',
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        # Should not raise an Exception.
        self.repl = ReplicaSet(self.repl_cfg)

        # Should create an extra user. No raise on authenticate.
        client = pymongo.MongoClient(self.repl.primary(),
                                     ssl_certfile=DEFAULT_CLIENT_CERT)
        client['$external'].authenticate(DEFAULT_SUBJECT,
                                         mechanism='MONGODB-X509')

        # Should create the user we requested. No raise on authenticate.
        client = pymongo.MongoClient(self.repl.primary(),
                                     ssl_certfile=certificate('client.pem'))
        client['$external'].authenticate(TEST_SUBJECT,
                                         mechanism='MONGODB-X509')

    def test_scram_with_ssl(self):
        member_params = {'procParams': {'clusterAuthMode': 'x509'}}
        self.repl_cfg = {
            'login': '******',
            'password': '******',
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        # Should not raise an Exception.
        self.repl = ReplicaSet(self.repl_cfg)

        # Should create the user we requested. No raise on authenticate.
        client = pymongo.MongoClient(self.repl.primary(),
                                     ssl_certfile=certificate('client.pem'))
        client.admin.authenticate('luke', 'ekul')
        # This should be the only user.
        self.assertEqual(len(client.admin.command('usersInfo')['users']), 1)
        self.assertFalse(client['$external'].command('usersInfo')['users'])

    def test_ssl(self):
        member_params = {}
        self.repl_cfg = {
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        # Should not raise an Exception.
        self.repl = ReplicaSet(self.repl_cfg)

        # Server should require SSL.
        with self.assertRaises(pymongo.errors.ConnectionFailure):
            connected(pymongo.MongoClient, self.repl.primary())

        # This shouldn't raise.
        connected(
            pymongo.MongoClient(self.repl.primary(),
                                ssl_certfile=certificate('client.pem')))

    def test_mongodb_auth_uri(self):
        if SERVER_VERSION < (2, 4):
            raise SkipTest("Need to be able to set 'authenticationMechanisms' "
                           "parameter to test.")

        member_params = {
            'procParams': {
                'clusterAuthMode': 'x509',
                'setParameter': {
                    'authenticationMechanisms': 'MONGODB-X509'
                }
            }
        }
        self.repl_cfg = {
            'login': TEST_SUBJECT,
            'authSource': '$external',
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        self.repl = ReplicaSet(self.repl_cfg)

        self.assertIn('mongodb_auth_uri', self.repl.info())
        repl_auth_uri = self.repl.info()['mongodb_auth_uri']
        hosts = ','.join(m['host'] for m in self.repl.members())
        self.assertIn(hosts, repl_auth_uri)
        self.assertIn(TEST_SUBJECT, repl_auth_uri)
        self.assertIn('authSource=$external', repl_auth_uri)
        self.assertIn('authMechanism=MONGODB-X509', repl_auth_uri)

    def test_member_info_auth_uri(self):
        member_params = {
            'procParams': {
                'clusterAuthMode': 'x509',
                'setParameter': {
                    'authenticationMechanisms': 'MONGODB-X509'
                }
            }
        }
        self.repl_cfg = {
            'login': TEST_SUBJECT,
            'authSource': '$external',
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        self.repl = ReplicaSet(self.repl_cfg)
        for i in range(len(self.repl)):
            member = self.repl.member_info(i)
            self.assertIn('mongodb_auth_uri', member)
            uri = member['mongodb_auth_uri']
            host = Servers().hostname(member['server_id'])
            self.assertIn(host, uri)
            self.assertIn(TEST_SUBJECT, uri)
            self.assertIn('authSource=$external', uri)
            self.assertIn('authMechanism=MONGODB-X509', uri)
Ejemplo n.º 57
0
 def test_servers(self):
     self.repl_cfg = {'members': [{}, {}]}
     self.repl = ReplicaSet(self.repl_cfg)
     self.repl.repl_member_add({"rsParams": {"priority": 0}})
     for member in self.repl.servers():
         self.assertTrue(member['host'] in self.repl.run_command('isMaster', is_eval=False).get('hosts'))
Ejemplo n.º 58
0
 def test_passives(self):
     self.repl = ReplicaSet(self.repl_cfg)
     self.repl.repl_member_add({"rsParams": {"priority": 0}})
     for member in self.repl.passives():
         self.assertTrue(member['host'] in self.repl.run_command('isMaster', is_eval=False).get('passives'))
Ejemplo n.º 59
0
class ReplicaSetAuthTestCase(unittest.TestCase):
    def setUp(self):
        PortPool().change_range()
        self.servers = Servers()
        self.servers.set_settings(*TEST_RELEASES)
        self.repl_cfg = {'auth_key': 'secret', 'login': '******', 'password': '******', 'members': [{}, {}]}
        self.repl = ReplicaSet(self.repl_cfg)

    def tearDown(self):
        if len(self.repl) > 0:
            self.repl.cleanup()

    def test_auth_connection(self):
        self.assertTrue(isinstance(self.repl.connection().admin.collection_names(), list))
        c = pymongo.MongoReplicaSetClient(self.repl.primary(), replicaSet=self.repl.repl_id)
        self.assertRaises(pymongo.errors.OperationFailure, c.admin.collection_names)

    def test_auth_admin(self):
        c = pymongo.MongoReplicaSetClient(self.repl.primary(), replicaSet=self.repl.repl_id)
        self.assertRaises(pymongo.errors.OperationFailure, c.admin.collection_names)
        self.assertTrue(c.admin.authenticate('admin', 'admin'))
        self.assertTrue(isinstance(c.admin.collection_names(), list))
        self.assertTrue(c.admin.logout() is None)
        self.assertRaises(pymongo.errors.OperationFailure, c.admin.collection_names)

    def test_auth_collection(self):
        c = pymongo.MongoReplicaSetClient(self.repl.primary(), replicaSet=self.repl.repl_id)
        self.assertTrue(c.admin.authenticate('admin', 'admin'))
        db = c.test_auth
        db.add_user('user', 'userpass', roles=['readWrite'])
        c.admin.logout()

        self.assertTrue(db.authenticate('user', 'userpass'))
        self.assertTrue(db.foo.insert({'foo': 'bar'}, w=2, wtimeout=10000))
        self.assertTrue(isinstance(db.foo.find_one(), dict))
        db.logout()
        self.assertRaises(pymongo.errors.OperationFailure, db.foo.find_one)

    def test_auth_arbiter_member_info(self):
        self.repl.cleanup()
        self.repl = ReplicaSet({'members': [
            {}, {'rsParams': {'arbiterOnly': True}}]})
        info = self.repl.member_info(1)
        for key in ('procInfo', 'mongodb_uri', 'statuses', 'rsInfo'):
            self.assertIn(key, info)
        rs_info = info['rsInfo']
        for key in ('primary', 'secondary', 'arbiterOnly'):
            self.assertIn(key, rs_info)
        self.assertFalse(rs_info['primary'])
        self.assertFalse(rs_info['secondary'])
        self.assertTrue(rs_info['arbiterOnly'])

    def test_mongodb_auth_uri(self):
        self.assertIn('mongodb_auth_uri', self.repl.info())
        rs_auth_uri = self.repl.info()['mongodb_auth_uri']
        hosts = ','.join(m['host'] for m in self.repl.members())
        self.assertIn(hosts, rs_auth_uri)
        self.assertIn('admin:admin', rs_auth_uri)
        self.assertIn('authSource=admin', rs_auth_uri)
        replset_param = 'replicaSet=' + self.repl.repl_id
        self.assertIn(replset_param, rs_auth_uri)

    def test_member_info_auth_uri(self):
        for i in range(len(self.repl)):
            member = self.repl.member_info(i)
            self.assertIn('mongodb_auth_uri', member)
            uri = member['mongodb_auth_uri']
            host = Servers().hostname(member['server_id'])
            self.assertIn(host, uri)
            self.assertIn('admin:admin', uri)
            self.assertIn('authSource=admin', uri)
Ejemplo n.º 60
0
 def test_passives(self):
     self.repl = ReplicaSet(self.repl_cfg)
     self.repl.repl_member_add({"rsParams": {"priority": 0}})
     for member in self.repl.passives():
         self.assertTrue(member['host'] in self.repl.run_command(
             'isMaster', is_eval=False).get('passives'))