From 1b44bebed3c60e1dd0e707f6f15518aca84ec76c Mon Sep 17 00:00:00 2001 From: Seth Foster Date: Tue, 23 Jan 2024 17:42:33 -0500 Subject: [PATCH] Peers_from_control_nodes requires listener port Adds validation and a unit test to ensure: - peers_from_control_nodes=True should fail if listener_port is not set - peers_from_control_nodes=False should be NOOP if listener_port is not set Signed-off-by: Seth Foster --- awx/api/serializers.py | 5 +++++ .../tests/functional/api/test_instance_peers.py | 15 +++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index c852e1b5a7..5526d9741f 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -5708,6 +5708,11 @@ class InstanceSerializer(BaseSerializer): if len(set(peers_instances)) != len(peers_instances): raise serializers.ValidationError(_("Cannot peer to the same instance more than once.")) + # cannot enable peers_from_control_nodes if listener_port is not set + if attrs.get('peers_from_control_nodes'): + if not attrs.get('listener_port') and self.instance and self.instance.canonical_address_port is None: + raise serializers.ValidationError(_("Cannot enable peers_from_control_nodes if listener_port is not set.")) + return super().validate(attrs) def validate_node_type(self, value): diff --git a/awx/main/tests/functional/api/test_instance_peers.py b/awx/main/tests/functional/api/test_instance_peers.py index 712a3902c0..471598827b 100644 --- a/awx/main/tests/functional/api/test_instance_peers.py +++ b/awx/main/tests/functional/api/test_instance_peers.py @@ -136,17 +136,24 @@ class TestPeers: def test_peers_from_control_nodes_without_listener_port(self, admin_user, patch): """ - patching with peers_from_control_nodes should not create a receptor address - if port is not defined + patching with peers_from_control_nodes=True should fail if listener_port is not set + patching with peers_from_control_nodes=False should be NOOP if listener_port is not set """ hop = Instance.objects.create(hostname='abc', node_type="hop") - patch( + resp = patch( url=reverse('api:instance_detail', kwargs={'pk': hop.pk}), data={"peers_from_control_nodes": True}, user=admin_user, + expect=400, + ) + assert 'Cannot enable peers_from_control_nodes if listener_port is not set' in str(resp.data) + patch( + url=reverse('api:instance_detail', kwargs={'pk': hop.pk}), + data={"peers_from_control_nodes": False}, + user=admin_user, expect=200, ) - assert not ReceptorAddress.objects.filter(instance=hop).exists() + assert not ReceptorAddress.objects.filter(instance=hop, peers_from_control_nodes=False).exists() def test_bidirectional_peering(self, admin_user, patch): """