|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +TencentBlueKing is pleased to support the open source community by making |
| 4 | +蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available. |
| 5 | +Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. |
| 6 | +Licensed under the MIT License (the "License"); you may not use this file except |
| 7 | +in compliance with the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://opensource.org/licenses/MIT |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software distributed under |
| 12 | +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 13 | +either express or implied. See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +
|
| 16 | +We undertake not to change the open source license (MIT license) applicable |
| 17 | +to the current version of the project delivered to anyone in the future. |
| 18 | +""" |
| 19 | + |
| 20 | +import pytest |
| 21 | +from django.test import override_settings |
| 22 | +from vendor.provider import DeadLetterRoutingProviderPlugin, HAProviderPlugin |
| 23 | + |
| 24 | +from .conftest import make_cluster |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture() |
| 28 | +def cluster(): |
| 29 | + """Cluster with version 3.9.0 (>= 3.8, supports quorum).""" |
| 30 | + return make_cluster("3.9.0") |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture() |
| 34 | +def old_cluster(): |
| 35 | + """Cluster with version 3.7.0 (< 3.8, classic mirror only).""" |
| 36 | + return make_cluster("3.7.0") |
| 37 | + |
| 38 | + |
| 39 | +class TestHAProviderPlugin: |
| 40 | + """HAProviderPlugin: version-based branching between quorum and classic mirror.""" |
| 41 | + |
| 42 | + def test_quorum_path_for_version_gte_3_8(self, cluster, mock_client): |
| 43 | + """For RabbitMQ >= 3.8, HAProviderPlugin should use quorum queues.""" |
| 44 | + plugin = HAProviderPlugin(context={}, client=mock_client, cluster=cluster, virtual_host="vh") |
| 45 | + plugin.on_create() |
| 46 | + |
| 47 | + mock_client.virtual_host.create.assert_called_once_with("vh", default_queue_type="quorum") |
| 48 | + mock_client.user_policy.create.assert_not_called() |
| 49 | + |
| 50 | + def test_classic_mirror_path_for_version_lt_3_8(self, old_cluster, mock_client): |
| 51 | + """For RabbitMQ < 3.8, HAProviderPlugin should use classic mirrored queues.""" |
| 52 | + plugin = HAProviderPlugin(context={}, client=mock_client, cluster=old_cluster, virtual_host="vh") |
| 53 | + plugin.on_create() |
| 54 | + |
| 55 | + mock_client.virtual_host.create.assert_not_called() |
| 56 | + mock_client.user_policy.create.assert_called_once() |
| 57 | + _, args, _ = mock_client.user_policy.create.mock_calls[0] |
| 58 | + assert args[0] == "vh" |
| 59 | + assert args[1] == HAProviderPlugin.HA_POLICY_NAME |
| 60 | + assert args[2]["definition"]["ha-mode"] == "all" |
| 61 | + |
| 62 | + @override_settings(RABBITMQ_HA_POLICY_ENABLED=False) |
| 63 | + def test_noop_when_disabled(self, cluster, mock_client): |
| 64 | + """Setting disabled → no calls at all.""" |
| 65 | + plugin = HAProviderPlugin(context={}, client=mock_client, cluster=cluster, virtual_host="vh") |
| 66 | + plugin.on_create() |
| 67 | + |
| 68 | + mock_client.virtual_host.create.assert_not_called() |
| 69 | + mock_client.user_policy.create.assert_not_called() |
| 70 | + |
| 71 | + |
| 72 | +class TestDeadLetterRoutingProviderPlugin: |
| 73 | + """DeadLetterRoutingProviderPlugin: dead letter exchange/queue setup with quorum awareness.""" |
| 74 | + |
| 75 | + DLX_SETTINGS = { |
| 76 | + "RABBITMQ_DEFAULT_DEAD_LETTER_ROUTING_KEY": "", |
| 77 | + "RABBITMQ_DEFAULT_DEAD_LETTER_EXCHANGE": "dlx.exchange", |
| 78 | + "RABBITMQ_DEFAULT_DEAD_LETTER_EXCHANGE_TYPE": "direct", |
| 79 | + "RABBITMQ_DEFAULT_DEAD_LETTER_EXCHANGE_DURABLE": False, |
| 80 | + "RABBITMQ_DEFAULT_DEAD_LETTER_QUEUE": "dlx.queue", |
| 81 | + "RABBITMQ_DEFAULT_DEAD_LETTER_QUEUE_DURABLE": False, |
| 82 | + } |
| 83 | + |
| 84 | + @override_settings(**DLX_SETTINGS) |
| 85 | + def test_quorum_queue_for_version_gte_3_8(self, cluster, mock_client): |
| 86 | + """DLQ declared as quorum type with durable=True for >= 3.8.""" |
| 87 | + ctx = {} |
| 88 | + plugin = DeadLetterRoutingProviderPlugin(context=ctx, client=mock_client, cluster=cluster, virtual_host="vh") |
| 89 | + plugin.on_create() |
| 90 | + |
| 91 | + _, kwargs = mock_client.queue.declare.call_args |
| 92 | + assert kwargs["arguments"] == {"x-queue-type": "quorum"} |
| 93 | + assert kwargs["durable"] is True |
| 94 | + |
| 95 | + override_settings(**DLX_SETTINGS) |
| 96 | + |
| 97 | + def test_classic_queue_for_version_lt_3_8(self, old_cluster, mock_client): |
| 98 | + """DLQ declared without quorum arguments for < 3.8.""" |
| 99 | + plugin = DeadLetterRoutingProviderPlugin( |
| 100 | + context={}, client=mock_client, cluster=old_cluster, virtual_host="vh" |
| 101 | + ) |
| 102 | + plugin.on_create() |
| 103 | + |
| 104 | + _, kwargs = mock_client.queue.declare.call_args |
| 105 | + assert kwargs["arguments"] == {} |
| 106 | + assert kwargs["durable"] is False |
| 107 | + |
| 108 | + @override_settings(**DLX_SETTINGS) |
| 109 | + def test_skipped_for_version_lt_2_8(self, mock_client): |
| 110 | + """Versions below 2.8 → on_create is a no-op.""" |
| 111 | + c = make_cluster("2.7.0") |
| 112 | + plugin = DeadLetterRoutingProviderPlugin(context={}, client=mock_client, cluster=c, virtual_host="vh") |
| 113 | + plugin.on_create() |
| 114 | + |
| 115 | + mock_client.exchange.declare.assert_not_called() |
| 116 | + mock_client.queue.declare.assert_not_called() |
| 117 | + |
| 118 | + @override_settings(**DLX_SETTINGS) |
| 119 | + def test_context_populated(self, cluster, mock_client): |
| 120 | + """After on_create, context should contain dlx-* keys.""" |
| 121 | + ctx = {} |
| 122 | + plugin = DeadLetterRoutingProviderPlugin(context=ctx, client=mock_client, cluster=cluster, virtual_host="vh") |
| 123 | + plugin.on_create() |
| 124 | + |
| 125 | + assert ctx["dlx-exchange"] == "dlx.exchange" |
| 126 | + assert ctx["dlx-queue"] == "dlx.queue" |
| 127 | + assert ctx["dlx-routing-key"] == "" |
0 commit comments