Esempio n. 1
0
    def test_handle_status_change(self):
        event = {'change_type': 'UP', 'address': ('1.2.3.4', 9000)}
        self.cluster.scheduler.reset_mock()
        self.control_connection._handle_status_change(event)
        self.cluster.scheduler.schedule_unique.assert_called_once_with(
            ANY, self.control_connection.refresh_node_list_and_token_map)

        # do the same with a known Host
        event = {'change_type': 'UP', 'address': ('192.168.1.0', 9000)}
        self.cluster.scheduler.reset_mock()
        self.control_connection._handle_status_change(event)
        host = self.cluster.metadata.hosts[DefaultEndPoint('192.168.1.0')]
        self.cluster.scheduler.schedule_unique.assert_called_once_with(
            ANY, self.cluster.on_up, host)

        self.cluster.scheduler.schedule.reset_mock()
        event = {'change_type': 'DOWN', 'address': ('1.2.3.4', 9000)}
        self.control_connection._handle_status_change(event)
        self.assertFalse(self.cluster.scheduler.schedule.called)

        # do the same with a known Host
        event = {'change_type': 'DOWN', 'address': ('192.168.1.0', 9000)}
        self.control_connection._handle_status_change(event)
        host = self.cluster.metadata.hosts[DefaultEndPoint('192.168.1.0')]
        self.assertIs(host, self.cluster.down_host)
Esempio n. 2
0
    def test_default_endpoint_properties(self):
        endpoint = DefaultEndPoint('10.0.0.1')
        self.assertEqual(endpoint.address, '10.0.0.1')
        self.assertEqual(endpoint.port, 9042)
        self.assertEqual(str(endpoint), '10.0.0.1:9042')

        endpoint = DefaultEndPoint('10.0.0.1', 8888)
        self.assertEqual(endpoint.address, '10.0.0.1')
        self.assertEqual(endpoint.port, 8888)
        self.assertEqual(str(endpoint), '10.0.0.1:8888')
Esempio n. 3
0
    def test_connection_endpoint(self):
        endpoint = DefaultEndPoint('1.2.3.4')
        c = Connection(endpoint)
        self.assertEqual(c.endpoint, endpoint)
        self.assertEqual(c.endpoint.address, endpoint.address)

        c = Connection(host=endpoint)  # kwarg
        self.assertEqual(c.endpoint, endpoint)
        self.assertEqual(c.endpoint.address, endpoint.address)

        c = Connection('10.0.0.1')
        endpoint = DefaultEndPoint('10.0.0.1')
        self.assertEqual(c.endpoint, endpoint)
        self.assertEqual(c.endpoint.address, endpoint.address)
Esempio n. 4
0
    def __init__(self):
        self.endpoint = DefaultEndPoint("192.168.1.0")
        self.local_results = [[
            "schema_version", "cluster_name", "data_center", "rack",
            "partitioner", "release_version", "tokens"
        ],
                              [[
                                  "a", "foocluster", "dc1", "rack1",
                                  "Murmur3Partitioner", "2.2.0",
                                  ["0", "100", "200"]
                              ]]]

        self.peer_results = [[
            "rpc_address", "peer", "schema_version", "data_center", "rack",
            "tokens"
        ],
                             [[
                                 "192.168.1.1", "10.0.0.1", "a", "dc1",
                                 "rack1", ["1", "101", "201"]
                             ],
                              [
                                  "192.168.1.2", "10.0.0.2", "a", "dc1",
                                  "rack1", ["2", "102", "202"]
                              ]]]
        self.wait_for_responses = Mock(return_value=_node_meta_results(
            self.local_results, self.peer_results))
Esempio n. 5
0
    def test_predicate_changes(self):
        """
        Test to validate host filter reacts correctly when the predicate return
        a different subset of the hosts
        HostFilterPolicy
        @since 3.8
        @jira_ticket PYTHON-961
        @expected_result the excluded hosts are ignored

        @test_category policy
        """
        external_event = True
        contact_point = DefaultEndPoint("127.0.0.1")

        single_host = {Host(contact_point, SimpleConvictionPolicy)}
        all_hosts = {
            Host(DefaultEndPoint("127.0.0.{}".format(i)),
                 SimpleConvictionPolicy)
            for i in (1, 2, 3)
        }

        predicate = lambda host: host.endpoint == contact_point if external_event else True
        hfp = ExecutionProfile(load_balancing_policy=HostFilterPolicy(
            RoundRobinPolicy(), predicate=predicate))
        cluster = Cluster((contact_point, ),
                          execution_profiles={EXEC_PROFILE_DEFAULT: hfp},
                          protocol_version=PROTOCOL_VERSION,
                          topology_event_refresh_window=0,
                          status_event_refresh_window=0)
        session = cluster.connect(wait_for_all_pools=True)

        queried_hosts = set()
        for _ in range(10):
            response = session.execute("SELECT * from system.local")
            queried_hosts.update(response.response_future.attempted_hosts)

        self.assertEqual(queried_hosts, single_host)

        external_event = False
        futures = session.update_created_pools()
        wait_futures(futures, timeout=cluster.connect_timeout)

        queried_hosts = set()
        for _ in range(10):
            response = session.execute("SELECT * from system.local")
            queried_hosts.update(response.response_future.attempted_hosts)
        self.assertEqual(queried_hosts, all_hosts)
Esempio n. 6
0
 def make_connection(self):
     c = self.connection_class(DefaultEndPoint('1.2.3.4'),
                               cql_version='3.0.1',
                               connect_timeout=5)
     mocket = Mock()
     mocket.send.side_effect = lambda x: len(x)
     self.set_socket(c, mocket)
     return c
Esempio n. 7
0
    def test_endpoint_equality(self):
        self.assertEqual(DefaultEndPoint('10.0.0.1'),
                         DefaultEndPoint('10.0.0.1'))

        self.assertEqual(DefaultEndPoint('10.0.0.1'),
                         DefaultEndPoint('10.0.0.1', 9042))

        self.assertNotEqual(DefaultEndPoint('10.0.0.1'),
                            DefaultEndPoint('10.0.0.2'))

        self.assertNotEqual(DefaultEndPoint('10.0.0.1'),
                            DefaultEndPoint('10.0.0.1', 0000))
Esempio n. 8
0
    def __init__(self, endpoint, conviction_policy_factory, datacenter=None, rack=None, host_id=None):
        if endpoint is None:
            raise ValueError("endpoint may not be None")
        if conviction_policy_factory is None:
            raise ValueError("conviction_policy_factory may not be None")

        self.endpoint = endpoint if isinstance(endpoint, EndPoint) else DefaultEndPoint(endpoint)
        self.conviction_policy = conviction_policy_factory(self)
        self.host_id = host_id
        self.set_location_info(datacenter, rack)
        self.lock = RLock()
 def setUp(self):
     if twistedreactor is None:
         raise unittest.SkipTest("Twisted libraries not available")
     twistedreactor.TwistedConnection.initialize_reactor()
     self.reactor_cft_patcher = patch(
         'twisted.internet.reactor.callFromThread')
     self.reactor_run_patcher = patch('twisted.internet.reactor.run')
     self.mock_reactor_cft = self.reactor_cft_patcher.start()
     self.mock_reactor_run = self.reactor_run_patcher.start()
     self.obj_ut = twistedreactor.TwistedConnection(
         DefaultEndPoint('1.2.3.4'), cql_version='3.0.1')
Esempio n. 10
0
    def test_wait_for_schema_agreement_rpc_lookup(self):
        """
        If the rpc_address is 0.0.0.0, the "peer" column should be used instead.
        """
        self.connection.peer_results[1].append(
            ["0.0.0.0", PEER_IP, "b", "dc1", "rack1", ["3", "103", "203"]])
        host = Host(DefaultEndPoint("0.0.0.0"), SimpleConvictionPolicy)
        self.cluster.metadata.hosts[DefaultEndPoint("foobar")] = host
        host.is_up = False

        # even though the new host has a different schema version, it's
        # marked as down, so the control connection shouldn't care
        self.assertTrue(self.control_connection.wait_for_schema_agreement())
        self.assertEqual(self.time.clock, 0)

        # but once we mark it up, the control connection will care
        host.is_up = True
        self.assertFalse(self.control_connection.wait_for_schema_agreement())
        self.assertGreaterEqual(self.time.clock,
                                self.cluster.max_schema_agreement_wait)
Esempio n. 11
0
    def test_timeout(self, *args):
        request_id = 999

        get_holders = self.make_get_holders(1)

        def send_msg(msg, req_id, msg_callback):
            pass

        # we used endpoint=X here because it's a mock and we need connection.endpoint to be set
        connection = Mock(spec=Connection,
                          endpoint=DefaultEndPoint('localhost'),
                          max_request_id=127,
                          lock=Lock(),
                          in_flight=0,
                          is_idle=True,
                          is_defunct=False,
                          is_closed=False,
                          get_request_id=lambda: request_id,
                          send_msg=Mock(side_effect=send_msg))
        holder = get_holders.return_value[0]
        holder.get_connections.return_value.append(connection)

        self.run_heartbeat(get_holders)

        self.assertEqual(connection.in_flight, get_holders.call_count)
        connection.send_msg.assert_has_calls([call(ANY, request_id, ANY)] *
                                             get_holders.call_count)
        connection.defunct.assert_has_calls([call(ANY)] *
                                            get_holders.call_count)
        exc = connection.defunct.call_args_list[0][0][0]
        self.assertIsInstance(exc, OperationTimedOut)
        self.assertEqual(exc.errors,
                         'Connection heartbeat timeout after 0.05 seconds')
        self.assertEqual(exc.last_host, DefaultEndPoint('localhost'))
        holder.return_connection.assert_has_calls([call(connection)] *
                                                  get_holders.call_count)
Esempio n. 12
0
    def test_wait_for_schema_agreement_skipping(self):
        """
        If rpc_address or schema_version isn't set, the host should be skipped
        """
        # an entry with no schema_version
        self.connection.peer_results[1].append([
            "192.168.1.3", "10.0.0.3", None, "dc1", "rack1",
            ["3", "103", "203"]
        ])
        # an entry with a different schema_version and no rpc_address
        self.connection.peer_results[1].append(
            [None, None, "b", "dc1", "rack1", ["4", "104", "204"]])

        # change the schema version on one of the existing entries
        self.connection.peer_results[1][1][3] = 'c'
        self.cluster.metadata.get_host(
            DefaultEndPoint('192.168.1.1')).is_up = False

        self.assertTrue(self.control_connection.wait_for_schema_agreement())
        self.assertEqual(self.time.clock, 0)
    def __init__(self):
        self.hosts = {
            DefaultEndPoint("192.168.1.0"):
            Host(DefaultEndPoint("192.168.1.0"), SimpleConvictionPolicy),
            DefaultEndPoint("192.168.1.1"):
            Host(DefaultEndPoint("192.168.1.1"), SimpleConvictionPolicy),
            DefaultEndPoint("192.168.1.2"):
            Host(DefaultEndPoint("192.168.1.2"), SimpleConvictionPolicy)
        }
        for host in self.hosts.values():
            host.set_up()

        self.cluster_name = None
        self.partitioner = None
        self.token_map = {}
Esempio n. 14
0
def validate_ssl_options(**kwargs):
    ssl_options = kwargs.get('ssl_options', None)
    ssl_context = kwargs.get('ssl_context', None)
    hostname = kwargs.get('hostname', '127.0.0.1')

    # find absolute path to client CA_CERTS
    tries = 0
    while True:
        if tries > 5:
            raise RuntimeError(
                "Failed to connect to SSL cluster after 5 attempts")
        try:
            cluster = Cluster(contact_points=[DefaultEndPoint(hostname)],
                              protocol_version=PROTOCOL_VERSION,
                              ssl_options=ssl_options,
                              ssl_context=ssl_context)
            session = cluster.connect(wait_for_all_pools=True)
            break
        except Exception:
            ex_type, ex, tb = sys.exc_info()
            log.warning("{0}: {1} Backtrace: {2}".format(
                ex_type.__name__, ex, traceback.extract_tb(tb)))
            del tb
            tries += 1

    # attempt a few simple commands.
    insert_keyspace = """CREATE KEYSPACE ssltest
            WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3'}
            """
    statement = SimpleStatement(insert_keyspace)
    statement.consistency_level = 3
    session.execute(statement)

    drop_keyspace = "DROP KEYSPACE ssltest"
    statement = SimpleStatement(drop_keyspace)
    statement.consistency_level = ConsistencyLevel.ANY
    session.execute(statement)

    cluster.shutdown()
Esempio n. 15
0
    def test_endpoint_resolve(self):
        self.assertEqual(
            DefaultEndPoint('10.0.0.1').resolve(), ('10.0.0.1', 9042))

        self.assertEqual(
            DefaultEndPoint('10.0.0.1', 3232).resolve(), ('10.0.0.1', 3232))
Esempio n. 16
0
 def setUp(self):
     self.connection = self.connection_class(DefaultEndPoint("127.0.0.1"),
                                             connect_timeout=5)
Esempio n. 17
0
 def make_connection(self):
     c = Connection(DefaultEndPoint('1.2.3.4'))
     c._socket = Mock()
     c._socket.send.side_effect = lambda x: len(x)
     return c