Skip to content

Commit

Permalink
docker_swarm_service: fix crash during idempotence check if published…
Browse files Browse the repository at this point in the history
…_port is not specified (#136)

* Avoid crash during idempotence check if published_port is not specified.

* Add tests.

* Convert lambda to function.
  • Loading branch information
felixfontein authored May 5, 2021
1 parent 859bc29 commit 74475d1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- "docker_swarm_services - avoid crash during idempotence check if ``published_port`` is not specified (https://github.com/ansible-collections/community.docker/issues/107, https://github.com/ansible-collections/community.docker/pull/136)."
6 changes: 4 additions & 2 deletions plugins/modules/docker_swarm_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,6 @@

import shlex
import time
import operator
import traceback

from distutils.version import LooseVersion
Expand Down Expand Up @@ -1792,7 +1791,10 @@ def has_publish_changed(self, old_publish):
old_publish = old_publish or []
if len(self.publish) != len(old_publish):
return True
publish_sorter = operator.itemgetter('published_port', 'target_port', 'protocol')

def publish_sorter(item):
return (item.get('published_port') or 0, item.get('target_port') or 0, item.get('protocol') or '')

publish = sorted(self.publish, key=publish_sorter)
old_publish = sorted(old_publish, key=publish_sorter)
for publish_item, old_publish_item in zip(publish, old_publish):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,40 @@
name: "{{ service_name }}"
register: publish_8_info

- name: publish (without published_port)
docker_swarm_service:
name: "{{ service_name }}"
image: "{{ docker_test_image_alpine }}"
resolve_image: no
command: '/bin/sh -v -c "sleep 10m"'
publish:
- protocol: udp
target_port: 60001
mode: host
register: publish_9
ignore_errors: yes
- name: gather service info
docker_swarm_service_info:
name: "{{ service_name }}"
register: publish_9_info

- name: publish (without published_port, idempotence)
docker_swarm_service:
name: "{{ service_name }}"
image: "{{ docker_test_image_alpine }}"
resolve_image: no
command: '/bin/sh -v -c "sleep 10m"'
publish:
- protocol: udp
target_port: 60001
mode: host
register: publish_10
ignore_errors: yes
- name: gather service info
docker_swarm_service_info:
name: "{{ service_name }}"
register: publish_10_info

- name: cleanup
docker_swarm_service:
name: "{{ service_name }}"
Expand All @@ -1543,6 +1577,8 @@
- publish_7 is not changed
- publish_8 is changed
- (publish_8_info.service.Endpoint.Ports | length) == 2
- publish_9 is changed
- publish_10 is not changed
when: docker_api_version is version('1.25', '>=') and docker_py_version is version('3.0.0', '>=')
- assert:
that:
Expand Down

0 comments on commit 74475d1

Please sign in to comment.