From 75f3b553590ac31a060f6a1b6d458a6ad4ddf791 Mon Sep 17 00:00:00 2001 From: Sagi Shnaidman Date: Mon, 24 May 2021 11:52:32 +0300 Subject: [PATCH] Change present state to be as created state For being more compliant with docker module. See #257 --- .../podman/podman_container_lib.py | 15 +++++- plugins/modules/podman_container.py | 2 +- .../targets/podman_container/tasks/main.yml | 51 ++++++++++++------- .../tasks/idem_all.yml | 2 +- .../tasks/idem_labels.yml | 2 +- .../tasks/idem_networks.yml | 2 +- .../tasks/idem_ports.yml | 2 +- .../tasks/idem_stopsignal.yml | 2 +- .../tasks/idem_users.yml | 2 +- .../tasks/idem_volumes.yml | 2 +- .../tasks/idem_workdir.yml | 2 +- .../tasks/root-podman.yml | 6 +-- .../tasks/rootless-podman-network.yml | 2 +- .../targets/podman_containers/tasks/main.yml | 18 +++---- 14 files changed, 69 insertions(+), 41 deletions(-) diff --git a/plugins/module_utils/podman/podman_container_lib.py b/plugins/module_utils/podman/podman_container_lib.py index 9b4004d6c..b6338cde7 100644 --- a/plugins/module_utils/podman/podman_container_lib.py +++ b/plugins/module_utils/podman/podman_container_lib.py @@ -1509,6 +1509,15 @@ def update_container_result(self, changed=True): def make_started(self): """Run actions if desired state is 'started'.""" + if not self.image: + if not self.container.exists: + self.module.fail_json(msg='Cannot start container when image' + ' is not specified!') + self.container.start() + self.results['actions'].append('started %s' % + self.container.name) + self.update_container_result() + return if self.container.exists and self.restart: if self.container.running: self.container.restart() @@ -1568,6 +1577,10 @@ def make_created(self): self.container.recreate() self.results['actions'].append('recreated %s' % self.container.name) + if self.container.running: + self.container.start() + self.results['actions'].append('started %s' % + self.container.name) self.update_container_result() return self.update_container_result(changed=False) @@ -1606,7 +1619,7 @@ def make_absent(self): def execute(self): """Execute the desired action according to map of actions & states.""" states_map = { - 'present': self.make_started, + 'present': self.make_created, 'started': self.make_started, 'absent': self.make_absent, 'stopped': self.make_stopped, diff --git a/plugins/modules/podman_container.py b/plugins/modules/podman_container.py index 1e8dbe8a4..d87f23096 100644 --- a/plugins/modules/podman_container.py +++ b/plugins/modules/podman_container.py @@ -921,7 +921,7 @@ def main(): ) # work on input vars - if (module.params['state'] in ['started', 'present', 'created'] + if (module.params['state'] in ['present', 'created'] and not module.params['force_restart'] and not module.params['image']): module.fail_json(msg="State '%s' required image to be configured!" % diff --git a/tests/integration/targets/podman_container/tasks/main.yml b/tests/integration/targets/podman_container/tasks/main.yml index 1d3800727..1b2fb6bf2 100644 --- a/tests/integration/targets/podman_container/tasks/main.yml +++ b/tests/integration/targets/podman_container/tasks/main.yml @@ -18,7 +18,7 @@ - name: Test no image with state 'started' containers.podman.podman_container: name: container - state: started + state: created ignore_errors: true register: no_image1 @@ -35,8 +35,8 @@ - no_image is failed - no_image1 is failed - no_image2 is failed - - no_image.msg == "State 'started' required image to be configured!" - - no_image1.msg == "State 'started' required image to be configured!" + - no_image.msg == "Cannot start container when image is not specified!" + - no_image1.msg == "State 'created' required image to be configured!" - no_image2.msg == "State 'present' required image to be configured!" fail_msg: No image test failed! success_msg: No image test passed! @@ -50,7 +50,7 @@ containers.podman.podman_container: name: container image: alpine:3.7 - state: present + state: started command: sleep 1d register: image @@ -58,7 +58,7 @@ containers.podman.podman_container: name: container2 image: alpine:3.7 - state: present + state: started command: sleep 1d register: image2 @@ -82,7 +82,7 @@ containers.podman.podman_container: name: container image: ineverneverneverexist - state: present + state: started command: sleep 1d register: imagefail ignore_errors: true @@ -93,12 +93,11 @@ - imagefail is failed - imagefail.msg == "Can't pull image ineverneverneverexist" - - name: Force container recreate containers.podman.podman_container: name: container image: alpine - state: present + state: started command: sleep 1d recreate: true register: recreated @@ -108,11 +107,27 @@ that: - recreated is changed - recreated.container is defined - - recreated.container['State']['Running'] + - recreated.container['State']['Running']|bool - "'recreated container' in recreated.actions" fail_msg: Force recreate test failed! success_msg: Force recreate test passed! + - name: Start container + containers.podman.podman_container: + name: container + state: started + + - name: Present container + containers.podman.podman_container: + name: container + state: present + register: start_present + + - name: Check output is correct + assert: + that: + - start_present.container['State']['Running'] + - name: Stop container containers.podman.podman_container: name: container @@ -394,14 +409,14 @@ containers.podman.podman_container: name: testidem image: docker.io/alpine - state: present + state: started command: sleep 20m - name: Check basic idempotency of running container - run it again containers.podman.podman_container: name: testidem image: alpine:latest - state: present + state: started command: sleep 20m register: idem @@ -414,7 +429,7 @@ containers.podman.podman_container: name: testidem image: alpine:latest - state: present + state: started command: sleep 20m force_restart: true register: idem_r @@ -428,7 +443,7 @@ containers.podman.podman_container: name: testidem image: alpine:latest - state: present + state: started command: sleep 20m register: idem_r1 @@ -441,7 +456,7 @@ containers.podman.podman_container: name: testidem image: alpine - state: present + state: started command: sleep 20m tty: true register: idem1 @@ -455,7 +470,7 @@ containers.podman.podman_container: name: testidem image: alpine - state: present + state: started command: sleep 20m register: idem2 @@ -482,7 +497,7 @@ containers.podman.podman_container: name: testidem-pod image: docker.io/alpine - state: present + state: started command: sleep 20m pod: "new:testidempod" @@ -490,7 +505,7 @@ containers.podman.podman_container: name: testidem-pod image: alpine:latest - state: present + state: started command: sleep 20m pod: testidempod register: idem3 @@ -504,7 +519,7 @@ containers.podman.podman_container: name: testidem-pod image: alpine - state: present + state: started command: sleep 20m tty: true pod: testidempod diff --git a/tests/integration/targets/podman_container_idempotency/tasks/idem_all.yml b/tests/integration/targets/podman_container_idempotency/tasks/idem_all.yml index 08873f67a..9a6bee7b6 100644 --- a/tests/integration/targets/podman_container_idempotency/tasks/idem_all.yml +++ b/tests/integration/targets/podman_container_idempotency/tasks/idem_all.yml @@ -8,7 +8,7 @@ containers.podman.podman_container: image: "{{ idem_image }}" name: idempotency - state: present + state: started command: 1h - name: Run container again diff --git a/tests/integration/targets/podman_container_idempotency/tasks/idem_labels.yml b/tests/integration/targets/podman_container_idempotency/tasks/idem_labels.yml index 4d4f1fb69..d49221062 100644 --- a/tests/integration/targets/podman_container_idempotency/tasks/idem_labels.yml +++ b/tests/integration/targets/podman_container_idempotency/tasks/idem_labels.yml @@ -6,7 +6,7 @@ - containers.podman.podman_container: image: "{{ idem_image }}" name: idempotency - state: present + state: started command: 1h - containers.podman.podman_container: diff --git a/tests/integration/targets/podman_container_idempotency/tasks/idem_networks.yml b/tests/integration/targets/podman_container_idempotency/tasks/idem_networks.yml index 5a21aac4f..4357924aa 100644 --- a/tests/integration/targets/podman_container_idempotency/tasks/idem_networks.yml +++ b/tests/integration/targets/podman_container_idempotency/tasks/idem_networks.yml @@ -8,7 +8,7 @@ name: netcontainer image: "{{ idem_image }}" command: 1h - state: present + state: started network: "{{ item.first_net }}" - name: Run container again with {{ item.first_net }} diff --git a/tests/integration/targets/podman_container_idempotency/tasks/idem_ports.yml b/tests/integration/targets/podman_container_idempotency/tasks/idem_ports.yml index 3083a7232..a2c5e41fc 100644 --- a/tests/integration/targets/podman_container_idempotency/tasks/idem_ports.yml +++ b/tests/integration/targets/podman_container_idempotency/tasks/idem_ports.yml @@ -6,7 +6,7 @@ - containers.podman.podman_container: image: "{{ idem_image }}" name: idempotency - state: present + state: started command: 1h - containers.podman.podman_container: diff --git a/tests/integration/targets/podman_container_idempotency/tasks/idem_stopsignal.yml b/tests/integration/targets/podman_container_idempotency/tasks/idem_stopsignal.yml index c4b2ded55..59546ce00 100644 --- a/tests/integration/targets/podman_container_idempotency/tasks/idem_stopsignal.yml +++ b/tests/integration/targets/podman_container_idempotency/tasks/idem_stopsignal.yml @@ -6,7 +6,7 @@ - containers.podman.podman_container: image: "{{ idem_image }}" name: idempotency - state: present + state: started command: 1h - containers.podman.podman_container: diff --git a/tests/integration/targets/podman_container_idempotency/tasks/idem_users.yml b/tests/integration/targets/podman_container_idempotency/tasks/idem_users.yml index ad1f103a4..1262b5ef3 100644 --- a/tests/integration/targets/podman_container_idempotency/tasks/idem_users.yml +++ b/tests/integration/targets/podman_container_idempotency/tasks/idem_users.yml @@ -6,7 +6,7 @@ - containers.podman.podman_container: image: "{{ idem_image }}" name: idempotency - state: present + state: started command: 1h - containers.podman.podman_container: diff --git a/tests/integration/targets/podman_container_idempotency/tasks/idem_volumes.yml b/tests/integration/targets/podman_container_idempotency/tasks/idem_volumes.yml index 475e44948..5924a358c 100644 --- a/tests/integration/targets/podman_container_idempotency/tasks/idem_volumes.yml +++ b/tests/integration/targets/podman_container_idempotency/tasks/idem_volumes.yml @@ -6,7 +6,7 @@ - containers.podman.podman_container: image: "{{ idem_image }}" name: idempotency - state: present + state: started command: 1h - containers.podman.podman_container: diff --git a/tests/integration/targets/podman_container_idempotency/tasks/idem_workdir.yml b/tests/integration/targets/podman_container_idempotency/tasks/idem_workdir.yml index cda0399f1..ddd19b830 100644 --- a/tests/integration/targets/podman_container_idempotency/tasks/idem_workdir.yml +++ b/tests/integration/targets/podman_container_idempotency/tasks/idem_workdir.yml @@ -6,7 +6,7 @@ - containers.podman.podman_container: image: "{{ idem_image }}" name: idempotency - state: present + state: started command: 1h - containers.podman.podman_container: diff --git a/tests/integration/targets/podman_container_idempotency/tasks/root-podman.yml b/tests/integration/targets/podman_container_idempotency/tasks/root-podman.yml index b3939d5bd..6b9ae85b7 100644 --- a/tests/integration/targets/podman_container_idempotency/tasks/root-podman.yml +++ b/tests/integration/targets/podman_container_idempotency/tasks/root-podman.yml @@ -9,7 +9,7 @@ containers.podman.podman_container: image: "{{ idem_image }}" name: root-idempotency - state: present + state: started command: 1h - name: Run container as is again @@ -93,14 +93,14 @@ containers.podman.podman_container: image: "{{ idem_image }}" name: root-idempotency - state: present + state: started command: 1h - name: Run containers with MAC address containers.podman.podman_container: image: "{{ idem_image }}" name: root-idempotency - state: present + state: started command: 1h mac_address: 44:55:66:77:88:99 register: info4 diff --git a/tests/integration/targets/podman_container_idempotency/tasks/rootless-podman-network.yml b/tests/integration/targets/podman_container_idempotency/tasks/rootless-podman-network.yml index f8518abc2..da1352e3b 100644 --- a/tests/integration/targets/podman_container_idempotency/tasks/rootless-podman-network.yml +++ b/tests/integration/targets/podman_container_idempotency/tasks/rootless-podman-network.yml @@ -11,7 +11,7 @@ name: rootlessnet image: "{{ idem_image }}" command: 1h - state: present + state: started - name: Run container again with no specified networks containers.podman.podman_container: diff --git a/tests/integration/targets/podman_containers/tasks/main.yml b/tests/integration/targets/podman_containers/tasks/main.yml index c7988b5e2..302c54ded 100644 --- a/tests/integration/targets/podman_containers/tasks/main.yml +++ b/tests/integration/targets/podman_containers/tasks/main.yml @@ -83,11 +83,11 @@ containers: - name: container image: alpine:3.7 - state: present + state: started command: sleep 1d - name: container1 image: alpine:3.7 - state: present + state: started command: sleep 1d register: image @@ -96,11 +96,11 @@ containers: - name: container1 image: alpine:3.7 - state: present + state: started command: sleep 1d - name: container3 image: alpine:3.7 - state: present + state: started command: sleep 1d register: image2 @@ -129,11 +129,11 @@ containers: - name: container1 image: alpine:3.7 - state: present + state: started command: sleep 1d - name: container image: ineverneverneverexist - state: present + state: started command: sleep 1d register: imagefail ignore_errors: true @@ -460,15 +460,15 @@ containers: - name: testidem image: docker.io/alpine - state: present + state: started command: sleep 20m - name: testidem2 image: docker.io/alpine - state: present + state: started command: sleep 21m - name: testidem3 image: docker.io/alpine - state: present + state: started command: sleep 22m - name: Check basic idempotency of running container - run it again