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 8b0e1e4
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 51 deletions.
48 changes: 38 additions & 10 deletions plugins/module_utils/podman/podman_container_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class PodmanModuleParams:
Arguments:
action {str} -- action type from 'run', 'stop', 'create', 'delete',
'start'
'start', 'restart'
params {dict} -- dictionary of module parameters
"""
Expand All @@ -195,7 +195,7 @@ def construct_command_from_params(self):
Returns:
list -- list of byte strings for Popen command
"""
if self.action in ['start', 'stop', 'delete']:
if self.action in ['start', 'stop', 'delete', 'restart']:
return self.start_stop_delete()
if self.action in ['create', 'run']:
cmd = [self.action, '--name', self.params['name']]
Expand All @@ -217,7 +217,7 @@ def construct_command_from_params(self):

def start_stop_delete(self):

if self.action in ['stop', 'start']:
if self.action in ['stop', 'start', 'restart']:
cmd = [self.action, self.params['name']]
return [to_bytes(i, errors='surrogate_or_strict') for i in cmd]

Expand Down Expand Up @@ -1385,7 +1385,7 @@ def _perform_action(self, action):
Arguments:
action {str} -- action to perform - start, create, stop, run,
delete
delete, restart
"""
b_command = PodmanModuleParams(action,
self.module_params,
Expand Down Expand Up @@ -1432,6 +1432,10 @@ def start(self):
"""Start the container."""
self._perform_action('start')

def restart(self):
"""Restart the container."""
self._perform_action('restart')

def create(self):
"""Create the container."""
self._perform_action('create')
Expand All @@ -1450,11 +1454,6 @@ def recreate_run(self):
self.delete()
self.run()

def restart(self):
"""Restart the container."""
self.stop()
self.start()


class PodmanManager:
"""Module manager class.
Expand Down Expand Up @@ -1509,6 +1508,20 @@ 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!')
if self.restart:
self.container.restart()
self.results['actions'].append('restarted %s' %
self.container.name)
else:
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 +1581,21 @@ 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
elif self.restart:
if self.container.running:
self.container.restart()
self.results['actions'].append('restarted %s' %
self.container.name)
else:
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 +1634,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
55 changes: 36 additions & 19 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,29 @@
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
image: alpine
state: present
command: sleep 1d
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 @@ -238,7 +255,7 @@
- restarted is changed
- restarted.container is defined
- restarted.container['State']['Running']
- "'started container' in restarted.actions"
- "'restarted container' in restarted.actions"
fail_msg: Restart container test failed!

- name: Restart running container
Expand Down Expand Up @@ -394,14 +411,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 +431,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 +445,7 @@
containers.podman.podman_container:
name: testidem
image: alpine:latest
state: present
state: started
command: sleep 20m
register: idem_r1

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

Expand All @@ -482,15 +499,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 +521,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
Loading

0 comments on commit 8b0e1e4

Please sign in to comment.