Skip to content

Commit

Permalink
Change present state to be as created state
Browse files Browse the repository at this point in the history
For being more compliant with docker module.
See containers#257
  • Loading branch information
sshnaidm committed Jul 7, 2021
1 parent 8ada150 commit 75f3b55
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 41 deletions.
15 changes: 14 additions & 1 deletion plugins/module_utils/podman/podman_container_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/podman_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!" %
Expand Down
51 changes: 33 additions & 18 deletions tests/integration/targets/podman_container/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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!
Expand All @@ -50,15 +50,15 @@
containers.podman.podman_container:
name: container
image: alpine:3.7
state: present
state: started
command: sleep 1d
register: image

- name: Check using already pulled image
containers.podman.podman_container:
name: container2
image: alpine:3.7
state: present
state: started
command: sleep 1d
register: image2

Expand All @@ -82,7 +82,7 @@
containers.podman.podman_container:
name: container
image: ineverneverneverexist
state: present
state: started
command: sleep 1d
register: imagefail
ignore_errors: true
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -428,7 +443,7 @@
containers.podman.podman_container:
name: testidem
image: alpine:latest
state: present
state: started
command: sleep 20m
register: idem_r1

Expand All @@ -441,7 +456,7 @@
containers.podman.podman_container:
name: testidem
image: alpine
state: present
state: started
command: sleep 20m
tty: true
register: idem1
Expand All @@ -455,7 +470,7 @@
containers.podman.podman_container:
name: testidem
image: alpine
state: present
state: started
command: sleep 20m
register: idem2

Expand All @@ -482,15 +497,15 @@
containers.podman.podman_container:
name: testidem-pod
image: docker.io/alpine
state: present
state: started
command: sleep 20m
pod: "new:testidempod"

- name: Check basic idempotency of pod container - run it again
containers.podman.podman_container:
name: testidem-pod
image: alpine:latest
state: present
state: started
command: sleep 20m
pod: testidempod
register: idem3
Expand All @@ -504,7 +519,7 @@
containers.podman.podman_container:
name: testidem-pod
image: alpine
state: present
state: started
command: sleep 20m
tty: true
pod: testidempod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
state: started
command: 1h

- name: Run container again
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
state: started
command: 1h

- containers.podman.podman_container:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
state: started
command: 1h

- containers.podman.podman_container:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
state: started
command: 1h

- containers.podman.podman_container:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
state: started
command: 1h

- containers.podman.podman_container:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
state: started
command: 1h

- containers.podman.podman_container:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
state: started
command: 1h

- containers.podman.podman_container:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 9 additions & 9 deletions tests/integration/targets/podman_containers/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 75f3b55

Please sign in to comment.