File: test_upgrade.py

package info (click to toggle)
python-cassandra-driver 3.29.2-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,144 kB
  • sloc: python: 51,532; ansic: 768; makefile: 138; sh: 13
file content (285 lines) | stat: -rw-r--r-- 11,206 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# Copyright DataStax, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import time
from itertools import count

from cassandra.auth import PlainTextAuthProvider, SaslAuthProvider
from cassandra.cluster import ConsistencyLevel, Cluster, DriverException, ExecutionProfile
from cassandra.policies import ConstantSpeculativeExecutionPolicy
from tests.integration.upgrade import UpgradeBase, UpgradeBaseAuth, UpgradePath, upgrade_paths

import unittest


# Previous Cassandra upgrade
two_to_three_path = upgrade_paths([
    UpgradePath("2.2.9-3.11", {"version": "2.2.9"}, {"version": "3.11.4"}, {}),
])

# Previous DSE upgrade
five_upgrade_path = upgrade_paths([
    UpgradePath("5.0.11-5.1.4", {"version": "5.0.11"}, {"version": "5.1.4"}, {}),
])


class UpgradeTests(UpgradeBase):
    @two_to_three_path
    def test_can_write(self):
        """
        Verify that the driver will keep querying C* even if there is a host down while being
        upgraded and that all the writes will eventually succeed
        @since 3.12
        @jira_ticket PYTHON-546
        @expected_result all the writes succeed

        @test_category upgrade
        """
        self.start_upgrade(0)

        self.cluster_driver.add_execution_profile("all", ExecutionProfile(consistency_level=ConsistencyLevel.ALL))
        self.cluster_driver.add_execution_profile("one", ExecutionProfile(consistency_level=ConsistencyLevel.LOCAL_ONE))

        c = count()
        while not self.is_upgraded():
            self.session.execute("INSERT INTO test3rf.test(k, v) VALUES (%s, 0)", (next(c), ), execution_profile="one")
            time.sleep(0.0001)

        total_number_of_inserted = self.session.execute("SELECT COUNT(*) from test3rf.test", execution_profile="all")[0][0]
        self.assertEqual(total_number_of_inserted, next(c))

        self.assertEqual(self.logger_handler.get_message_count("error", ""), 0)

    @two_to_three_path
    def test_can_connect(self):
        """
        Verify that the driver can connect to all the nodes
        despite some nodes being in different versions
        @since 3.12
        @jira_ticket PYTHON-546
        @expected_result the driver connects successfully and can execute queries against
        all the hosts

        @test_category upgrade
        """
        def connect_and_shutdown():
            cluster = Cluster()
            session = cluster.connect(wait_for_all_pools=True)
            queried_hosts = set()
            for _ in range(10):
                results = session.execute("SELECT * from system.local")
                self.assertGreater(len(results.current_rows), 0)
                self.assertEqual(len(results.response_future.attempted_hosts), 1)
                queried_hosts.add(results.response_future.attempted_hosts[0])
            self.assertEqual(len(queried_hosts), 3)
            cluster.shutdown()

        connect_and_shutdown()
        for node in self.nodes:
            self.upgrade_node(node)
            connect_and_shutdown()

        connect_and_shutdown()


class UpgradeTestsMetadata(UpgradeBase):
    @two_to_three_path
    def test_can_write(self):
        """
        Verify that the driver will keep querying C* even if there is a host down while being
        upgraded and that all the writes will eventually succeed
        @since 3.12
        @jira_ticket PYTHON-546
        @expected_result all the writes succeed

        @test_category upgrade
        """
        self.start_upgrade(0)

        self.cluster_driver.add_execution_profile("all", ExecutionProfile(consistency_level=ConsistencyLevel.ALL))
        self.cluster_driver.add_execution_profile("one", ExecutionProfile(consistency_level=ConsistencyLevel.LOCAL_ONE))

        c = count()
        while not self.is_upgraded():
            self.session.execute("INSERT INTO test3rf.test(k, v) VALUES (%s, 0)", (next(c),), execution_profile="one")
            time.sleep(0.0001)

        total_number_of_inserted = self.session.execute("SELECT COUNT(*) from test3rf.test", execution_profile="all")[0][0]
        self.assertEqual(total_number_of_inserted, next(c))

        self.assertEqual(self.logger_handler.get_message_count("error", ""), 0)

    @two_to_three_path
    def test_schema_metadata_gets_refreshed(self):
        """
        Verify that the driver fails to update the metadata while connected against
        different versions of nodes. This won't succeed because each node will report a
        different schema version

        @since 3.12
        @jira_ticket PYTHON-546
        @expected_result the driver raises DriverException when updating the schema
        metadata while upgrading
        all the hosts

        @test_category metadata
        """
        original_meta = self.cluster_driver.metadata.keyspaces
        number_of_nodes = len(self.cluster.nodelist())
        nodes = self.nodes
        for node in nodes[1:]:
            self.upgrade_node(node)
            # Wait for the control connection to reconnect
            time.sleep(20)

            with self.assertRaises(DriverException):
                self.cluster_driver.refresh_schema_metadata(max_schema_agreement_wait=10)

        self.upgrade_node(nodes[0])
        # Wait for the control connection to reconnect
        time.sleep(20)
        self.cluster_driver.refresh_schema_metadata(max_schema_agreement_wait=40)
        self.assertNotEqual(original_meta, self.cluster_driver.metadata.keyspaces)

    @two_to_three_path
    def test_schema_nodes_gets_refreshed(self):
        """
        Verify that the driver token map and node list gets rebuild correctly while upgrading.
        The token map and the node list should be the same after each node upgrade

        @since 3.12
        @jira_ticket PYTHON-546
        @expected_result the token map and the node list stays consistent with each node upgrade
        metadata while upgrading
        all the hosts

        @test_category metadata
        """
        for node in self.nodes:
            token_map = self.cluster_driver.metadata.token_map
            self.upgrade_node(node)
            # Wait for the control connection to reconnect
            time.sleep(20)

            self.cluster_driver.refresh_nodes(force_token_rebuild=True)
            self._assert_same_token_map(token_map, self.cluster_driver.metadata.token_map)

    def _assert_same_token_map(self, original, new):
        self.assertIsNot(original, new)
        self.assertEqual(original.tokens_to_hosts_by_ks, new.tokens_to_hosts_by_ks)
        self.assertEqual(original.token_to_host_owner, new.token_to_host_owner)
        self.assertEqual(original.ring, new.ring)


two_to_three_with_auth_path = upgrade_paths([
    UpgradePath("2.2.9-3.11-auth", {"version": "2.2.9"}, {"version": "3.11.4"},
                {'authenticator': 'PasswordAuthenticator',
                 'authorizer': 'CassandraAuthorizer'}),
])
class UpgradeTestsAuthentication(UpgradeBaseAuth):
    @two_to_three_with_auth_path
    def test_can_connect_auth_plain(self):
        """
        Verify that the driver can connect despite some nodes being in different versions
        with plain authentication
        @since 3.12
        @jira_ticket PYTHON-546
        @expected_result the driver connects successfully and can execute queries against
        all the hosts

        @test_category upgrade
        """
        auth_provider = PlainTextAuthProvider(
            username="cassandra",
            password="cassandra"
        )
        self.connect_and_shutdown(auth_provider)
        for node in self.nodes:
            self.upgrade_node(node)
            self.connect_and_shutdown(auth_provider)

        self.connect_and_shutdown(auth_provider)

    @two_to_three_with_auth_path
    def test_can_connect_auth_sasl(self):
        """
        Verify that the driver can connect despite some nodes being in different versions
        with ssl authentication
        @since 3.12
        @jira_ticket PYTHON-546
        @expected_result the driver connects successfully and can execute queries against
        all the hosts

        @test_category upgrade
        """
        sasl_kwargs = {'service': 'cassandra',
                       'mechanism': 'PLAIN',
                       'qops': ['auth'],
                       'username': 'cassandra',
                       'password': 'cassandra'}
        auth_provider = SaslAuthProvider(**sasl_kwargs)
        self.connect_and_shutdown(auth_provider)
        for node in self.nodes:
            self.upgrade_node(node)
            self.connect_and_shutdown(auth_provider)

        self.connect_and_shutdown(auth_provider)

    def connect_and_shutdown(self, auth_provider):
        cluster = Cluster(idle_heartbeat_interval=0,
                          auth_provider=auth_provider)
        session = cluster.connect(wait_for_all_pools=True)
        queried_hosts = set()
        for _ in range(10):
            results = session.execute("SELECT * from system.local")
            self.assertGreater(len(results.current_rows), 0)
            self.assertEqual(len(results.response_future.attempted_hosts), 1)
            queried_hosts.add(results.response_future.attempted_hosts[0])
        self.assertEqual(len(queried_hosts), 3)
        cluster.shutdown()


class UpgradeTestsPolicies(UpgradeBase):
    @two_to_three_path
    def test_can_write_speculative(self):
        """
        Verify that the driver will keep querying C* even if there is a host down while being
        upgraded and that all the writes will eventually succeed using the ConstantSpeculativeExecutionPolicy
        policy
        @since 3.12
        @jira_ticket PYTHON-546
        @expected_result all the writes succeed

        @test_category upgrade
        """
        spec_ep_rr = ExecutionProfile(speculative_execution_policy=ConstantSpeculativeExecutionPolicy(.5, 10),
                                      request_timeout=12)
        cluster = Cluster()
        self.addCleanup(cluster.shutdown)
        cluster.add_execution_profile("spec_ep_rr", spec_ep_rr)
        cluster.add_execution_profile("all", ExecutionProfile(consistency_level=ConsistencyLevel.ALL))
        session = cluster.connect()

        self.start_upgrade(0)

        c = count()
        while not self.is_upgraded():
            session.execute("INSERT INTO test3rf.test(k, v) VALUES (%s, 0)", (next(c),),
                                 execution_profile='spec_ep_rr')
            time.sleep(0.0001)

        total_number_of_inserted = session.execute("SELECT COUNT(*) from test3rf.test", execution_profile="all")[0][0]
        self.assertEqual(total_number_of_inserted, next(c))

        self.assertEqual(self.logger_handler.get_message_count("error", ""), 0)