def run_scenario(self, spec):
        # maybe skip test manually
        self.maybe_skip_test(spec)

        # process test-level runOnRequirements
        run_on_spec = spec.get('runOnRequirements', [])
        if not self.should_run_on(run_on_spec):
            raise unittest.SkipTest('runOnRequirements not satisfied')

        # process skipReason
        skip_reason = spec.get('skipReason', None)
        if skip_reason is not None:
            raise unittest.SkipTest('%s' % (skip_reason, ))

        # process createEntities
        self.entity_map = EntityMapUtil(self)
        self.entity_map.create_entities_from_spec(
            self.TEST_SPEC.get('createEntities', []))

        # process initialData
        self.insert_initial_data(self.TEST_SPEC.get('initialData', []))

        # process operations
        self.run_operations(spec['operations'])

        # process expectEvents
        self.check_events(spec.get('expectEvents', []))

        # process outcome
        self.verify_outcome(spec.get('outcome', []))
 def maybe_skip_test(self, spec):
     # add any special-casing for skipping tests here
     if client_context.storage_engine == 'mmapv1':
         if 'Dirty explicit session is discarded' in spec['description']:
             raise unittest.SkipTest(
                 "MMAPv1 does not support retryWrites=True")
     elif 'Client side error in command starting transaction' in spec[
             'description']:
         raise unittest.SkipTest("Implement PYTHON-1894")
    def setUpClass(cls):
        # super call creates internal client cls.client
        super(UnifiedSpecTestMixinV1, cls).setUpClass()

        # process file-level runOnRequirements
        run_on_spec = cls.TEST_SPEC.get('runOnRequirements', [])
        if not cls.should_run_on(run_on_spec):
            raise unittest.SkipTest('%s runOnRequirements not satisfied' %
                                    (cls.__name__, ))

        # add any special-casing for skipping tests here
        if client_context.storage_engine == 'mmapv1':
            if 'retryable-writes' in cls.TEST_SPEC['description']:
                raise unittest.SkipTest(
                    "MMAPv1 does not support retryWrites=True")
def got_app_error(topology, app_error):
    server_address = common.partition_node(app_error['address'])
    server = topology.get_server_by_address(server_address)
    error_type = app_error['type']
    generation = app_error.get('generation', server.pool.generation)
    when = app_error['when']
    max_wire_version = app_error['maxWireVersion']
    # XXX: We could get better test coverage by mocking the errors on the
    # Pool/SocketInfo.
    try:
        if error_type == 'command':
            _check_command_response(app_error['response'])
        elif error_type == 'network':
            raise AutoReconnect('mock non-timeout network error')
        elif error_type == 'timeout':
            raise NetworkTimeout('mock network timeout error')
        else:
            raise AssertionError('unknown error type: %s' % (error_type, ))
        assert False
    except (AutoReconnect, NotMasterError, OperationFailure) as e:
        if when == 'beforeHandshakeCompletes' and error_type == 'timeout':
            raise unittest.SkipTest('PYTHON-2211')

        topology.handle_error(server_address,
                              _ErrorContext(e, max_wire_version, generation))
示例#5
0
    def run_test(self):
        if not _HAVE_DNSPYTHON:
            raise unittest.SkipTest("DNS tests require the dnspython module")
        uri = test_case['uri']
        seeds = test_case['seeds']
        hosts = test_case['hosts']
        options = test_case.get('options')
        if seeds:
            seeds = split_hosts(','.join(seeds))
        if hosts:
            hosts = frozenset(split_hosts(','.join(hosts)))
        if options:
            for key, value in options.items():
                # Convert numbers to strings for comparison
                options[key] = str(value)

        if seeds:
            result = parse_uri(uri, validate=False)
            self.assertEqual(sorted(result['nodelist']), sorted(seeds))
            if options:
                self.assertEqual(result['options'], options)

            hostname = next(iter(client_context.client.nodes))[0]
            # The replica set members must be configured as 'localhost'.
            if hostname == 'localhost':
                client = MongoClient(uri, **_SSL_OPTS)
                # Force server selection
                client.admin.command('ismaster')
                wait_until(
                    lambda: hosts == client.nodes,
                    'match test hosts to client nodes')
        else:
            self.assertRaises(
                ConfigurationError, parse_uri, uri, validate=False)
 def maybe_skip_scenario(self, test):
     """Override to skip threaded tests when cdecimal is installed on 2.7
     """
     super(TestIntegration, self).maybe_skip_scenario(test)
     # PYTHON-2332
     ops = [op['name'] for op in test['operations']]
     if cdecimal_patched() and 'startThread' in ops:
         raise unittest.SkipTest('PYTHON-2332 test fails with cdecimal')
示例#7
0
 def maybe_skip_scenario(self, test):
     super(TestSpec, self).maybe_skip_scenario(test)
     skip_names = [
         'listCollectionObjects', 'listIndexNames', 'listDatabaseObjects']
     for name in skip_names:
         if name.lower() in test['description'].lower():
             raise unittest.SkipTest(
                 'PyMongo does not support %s' % (name,))
示例#8
0
 def setUp(self):
     if not _HAVE_DNSPYTHON:
         raise unittest.SkipTest("SRV polling tests require the dnspython "
                                 "module")
     # Patch timeouts to ensure short rescan SRV interval.
     self.client_knobs = client_knobs(heartbeat_frequency=WAIT_TIME,
                                      min_heartbeat_interval=WAIT_TIME,
                                      events_queue_frequency=WAIT_TIME)
     self.client_knobs.enable()
    def setUp(self):
        if self.__class__ is _PerformanceTest:
            raise unittest.SkipTest()

        super(_PerformanceTest, self).setUp()
        download_test_data()
        env.sync_cx.drop_database('perftest')

        # In case we're killed mid-test, print its name before starting.
        sys.stdout.write("{:<30}".format(self.__class__.__name__))
        sys.stdout.flush()
示例#10
0
    def run_test(self):
        if not _HAVE_DNSPYTHON:
            raise unittest.SkipTest("DNS tests require the dnspython module")
        uri = test_case['uri']
        seeds = test_case['seeds']
        hosts = test_case['hosts']
        options = test_case.get('options')
        if seeds:
            seeds = split_hosts(','.join(seeds))
        if hosts:
            hosts = frozenset(split_hosts(','.join(hosts)))
        if options:
            for key, value in options.items():
                # Convert numbers / booleans to strings for comparison
                if isinstance(value, bool):
                    options[key] = 'true' if value else 'false'
                elif isinstance(value, (int, float)):
                    options[key] = str(value)

        if seeds:
            result = parse_uri(uri, validate=False)
            self.assertEqual(sorted(result['nodelist']), sorted(seeds))
            if options:
                opts = result['options']
                if 'readpreferencetags' in opts:
                    rpts = validate_read_preference_tags(
                        'readPreferenceTags', opts.pop('readpreferencetags'))
                    opts['readPreferenceTags'] = rpts
                self.assertEqual(result['options'], options)

            hostname = next(iter(client_context.client.nodes))[0]
            # The replica set members must be configured as 'localhost'.
            if hostname == 'localhost':
                copts = client_context.default_client_options.copy()
                if client_context.ssl is True:
                    # Our test certs don't support the SRV hosts used in these tests.
                    copts['ssl_match_hostname'] = False

                client = MongoClient(uri, **copts)
                # Force server selection
                client.admin.command('ismaster')
                wait_until(
                    lambda: hosts == client.nodes,
                    'match test hosts to client nodes')
        else:
            try:
                parse_uri(uri)
            except (ConfigurationError, ValueError):
                pass
            else:
                self.fail("failed to raise an exception")
    def test_round_trip(self):
        if not client_context.version.at_least(3, 3, 6):
            raise unittest.SkipTest(
                'Round trip test requires MongoDB >= 3.3.6')

        coll = client_context.client.pymongo_test.test
        coll.drop()

        dec128 = Decimal128.from_bid(
            b'\x00@cR\xbf\xc6\x01\x00\x00\x00\x00\x00\x00\x00\x1c0')
        coll.insert_one({'dec128': dec128})
        doc = coll.find_one({'dec128': dec128})
        self.assertIsNotNone(doc)
        self.assertEqual(doc['dec128'], dec128)
示例#12
0
 def maybe_skip_scenario(self, test):
     if test.get('skipReason'):
         raise unittest.SkipTest(test.get('skipReason'))
 def setUp(self):
     if not _HAVE_DNSPYTHON:
         raise unittest.SkipTest("SRV polling tests require the dnspython "
                                 "module")
示例#14
0
 def setUpClass(cls):
     super(TestSpec, cls).setUpClass()
     if client_context.is_mongos and client_context.version[:2] <= (4, 0):
         raise unittest.SkipTest("4.0 mongos does not support failCommand")
 def maybe_skip_scenario(self, test):
     super(TransactionsBase, self).maybe_skip_scenario(test)
     if ('secondary' in self.id() and not client_context.is_mongos
             and not client_context.has_secondaries):
         raise unittest.SkipTest('No secondaries')
示例#16
0
    def run_test(self):
        if not _HAVE_DNSPYTHON:
            raise unittest.SkipTest("DNS tests require the dnspython module")
        uri = test_case['uri']
        seeds = test_case['seeds']
        hosts = test_case['hosts']
        options = test_case.get('options', {})
        if 'ssl' in options:
            options['tls'] = options.pop('ssl')
        parsed_options = test_case.get('parsed_options')
        # See DRIVERS-1324, unless tls is explicitly set to False we need TLS.
        needs_tls = not (options and (options.get('ssl') == False
                                      or options.get('tls') == False))
        if needs_tls and not client_context.tls:
            self.skipTest('this test requires a TLS cluster')
        if not needs_tls and client_context.tls:
            self.skipTest('this test requires a non-TLS cluster')

        if seeds:
            seeds = split_hosts(','.join(seeds))
        if hosts:
            hosts = frozenset(split_hosts(','.join(hosts)))

        if seeds:
            result = parse_uri(uri, validate=True)
            self.assertEqual(sorted(result['nodelist']), sorted(seeds))
            if options:
                opts = result['options']
                if 'readpreferencetags' in opts:
                    rpts = validate_read_preference_tags(
                        'readPreferenceTags', opts.pop('readpreferencetags'))
                    opts['readPreferenceTags'] = rpts
                self.assertEqual(result['options'], options)
            if parsed_options:
                for opt, expected in parsed_options.items():
                    if opt == 'user':
                        self.assertEqual(result['username'], expected)
                    elif opt == 'password':
                        self.assertEqual(result['password'], expected)
                    elif opt == 'auth_database' or opt == 'db':
                        self.assertEqual(result['database'], expected)

            hostname = next(iter(client_context.client.nodes))[0]
            # The replica set members must be configured as 'localhost'.
            if hostname == 'localhost':
                copts = client_context.default_client_options.copy()
                # Remove tls since SRV parsing should add it automatically.
                copts.pop('tls', None)
                if client_context.tls:
                    # Our test certs don't support the SRV hosts used in these
                    # tests.
                    copts['tlsAllowInvalidHostnames'] = True

                # The SRV spec tests assume drivers auto discover replica set
                # members. This should be removed during PYTHON-2679.
                if not self.load_balanced and ('directconnection'
                                               not in result['options']):
                    copts['directConnection'] = False

                client = MongoClient(uri, **copts)
                wait_until(lambda: hosts == client.nodes,
                           'match test hosts to client nodes')
        else:
            try:
                parse_uri(uri)
            except (ConfigurationError, ValueError):
                pass
            else:
                self.fail("failed to raise an exception")
    def run_scenario(self):
        if test.get('skipReason'):
            raise unittest.SkipTest(test.get('skipReason'))

        listener = OvertCommandListener()
        # Create a new client, to avoid interference from pooled sessions.
        # Convert test['clientOptions'] to dict to avoid a Jython bug using
        # "**" with ScenarioDict.
        client_options = dict(test['clientOptions'])
        use_multi_mongos = test['useMultipleMongoses']
        if client_context.is_mongos and use_multi_mongos:
            client = rs_client(client_context.mongos_seeds(),
                               event_listeners=[listener],
                               **client_options)
        else:
            client = rs_client(event_listeners=[listener], **client_options)
        # Close the client explicitly to avoid having too many threads open.
        self.addCleanup(client.close)

        # Kill all sessions before and after each test to prevent an open
        # transaction (from a test failure) from blocking collection/database
        # operations during test set up and tear down.
        self.kill_all_sessions()
        self.addCleanup(self.kill_all_sessions)

        database_name = scenario_def['database_name']
        collection_name = scenario_def['collection_name']
        write_concern_db = client.get_database(
            database_name, write_concern=WriteConcern(w='majority'))
        write_concern_coll = write_concern_db[collection_name]
        write_concern_coll.drop()
        write_concern_db.create_collection(collection_name)
        if scenario_def['data']:
            # Load data.
            write_concern_coll.insert_many(scenario_def['data'])

        # Create session0 and session1.
        sessions = {}
        session_ids = {}
        for i in range(2):
            session_name = 'session%d' % i
            opts = camel_to_snake_args(test['sessionOptions'][session_name])
            if 'default_transaction_options' in opts:
                txn_opts = opts['default_transaction_options']
                if 'readConcern' in txn_opts:
                    read_concern = ReadConcern(**dict(txn_opts['readConcern']))
                else:
                    read_concern = None
                if 'writeConcern' in txn_opts:
                    write_concern = WriteConcern(
                        **dict(txn_opts['writeConcern']))
                else:
                    write_concern = None

                if 'readPreference' in txn_opts:
                    read_pref = parse_read_preference(
                        txn_opts['readPreference'])
                else:
                    read_pref = None

                txn_opts = client_session.TransactionOptions(
                    read_concern=read_concern,
                    write_concern=write_concern,
                    read_preference=read_pref,
                )
                opts['default_transaction_options'] = txn_opts

            s = client.start_session(**dict(opts))

            sessions[session_name] = s
            # Store lsid so we can access it after end_session, in check_events.
            session_ids[session_name] = s.session_id

        self.addCleanup(end_sessions, sessions)

        if 'failPoint' in test:
            self.set_fail_point(test['failPoint'])
            self.addCleanup(self.set_fail_point, {
                'configureFailPoint': 'failCommand',
                'mode': 'off'
            })

        listener.results.clear()
        collection = client[database_name][collection_name]

        for op in test['operations']:
            expected_result = op.get('result')
            if expect_error(expected_result):
                with self.assertRaises(PyMongoError,
                                       msg=op['name']) as context:
                    self.run_operation(sessions, collection, op.copy())

                if expect_error_message(expected_result):
                    self.assertIn(expected_result['errorContains'].lower(),
                                  str(context.exception).lower())
                if expect_error_code(expected_result):
                    self.assertEqual(expected_result['errorCodeName'],
                                     context.exception.details.get('codeName'))
                if expect_error_labels_contain(expected_result):
                    self.assertErrorLabelsContain(
                        context.exception,
                        expected_result['errorLabelsContain'])
                if expect_error_labels_omit(expected_result):
                    self.assertErrorLabelsOmit(
                        context.exception, expected_result['errorLabelsOmit'])
            else:
                result = self.run_operation(sessions, collection, op.copy())
                if 'result' in op:
                    if op['name'] == 'runCommand':
                        self.check_command_result(expected_result, result)
                    else:
                        self.check_result(expected_result, result)

        for s in sessions.values():
            s.end_session()

        self.check_events(test, listener, session_ids)

        # Disable fail points.
        self.set_fail_point({
            'configureFailPoint': 'failCommand',
            'mode': 'off'
        })

        # Assert final state is expected.
        expected_c = test['outcome'].get('collection')
        if expected_c is not None:
            # Read from the primary with local read concern to ensure causal
            # consistency.
            primary_coll = collection.with_options(
                read_preference=ReadPreference.PRIMARY,
                read_concern=ReadConcern('local'))
            self.assertEqual(list(primary_coll.find()), expected_c['data'])
示例#18
0
 def maybe_skip_scenario(self, test):
     super(TestSpec, self).maybe_skip_scenario(test)
     if 'type=symbol' in test['description'].lower():
         raise unittest.SkipTest('PyMongo does not support the symbol type')
 def maybe_skip_test(self, spec):
     # add any special-casing for skipping tests here
     if client_context.storage_engine == 'mmapv1':
         if 'Dirty explicit session is discarded' in spec['description']:
             raise unittest.SkipTest(
                 "MMAPv1 does not support retryWrites=True")