Skip to content

Commit

Permalink
Peers_from_control_nodes requires listener port
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
fosterseth committed Feb 2, 2024
1 parent a4cf55b commit 1b44beb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
5 changes: 5 additions & 0 deletions awx/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5708,6 +5708,11 @@ def check_peers_changed():
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):
Expand Down
15 changes: 11 additions & 4 deletions awx/main/tests/functional/api/test_instance_peers.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,24 @@ def test_peers_from_control_nodes(self, admin_user, patch):

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):
"""
Expand Down

0 comments on commit 1b44beb

Please sign in to comment.