diff --git a/collections/ansible_collections/theliberatededge/test_linux_containers/extensions/molecule/default/converge.yml b/collections/ansible_collections/theliberatededge/test_linux_containers/extensions/molecule/default/converge.yml index c7f4c47..959acf9 100644 --- a/collections/ansible_collections/theliberatededge/test_linux_containers/extensions/molecule/default/converge.yml +++ b/collections/ansible_collections/theliberatededge/test_linux_containers/extensions/molecule/default/converge.yml @@ -5,3 +5,5 @@ - name: imports and executes the playbook under test ansible.builtin.import_playbook: ../../../launch_linux_container.yml + vars: + lte_container_name: "{{ molecule_yml.platforms[0].name }}" diff --git a/collections/ansible_collections/theliberatededge/test_linux_containers/extensions/molecule/default/create.yml b/collections/ansible_collections/theliberatededge/test_linux_containers/extensions/molecule/default/create.yml deleted file mode 100644 index 371214e..0000000 --- a/collections/ansible_collections/theliberatededge/test_linux_containers/extensions/molecule/default/create.yml +++ /dev/null @@ -1,36 +0,0 @@ ---- -- name: Create - hosts: localhost - connection: local - gather_facts: false - # no_log: "{{ molecule_no_log }}" - tasks: - - # TODO: Developer must implement and populate 'server' variable - - - name: Create instance config - when: server.changed | default(false) | bool # noqa no-handler - block: - - name: Populate instance config dict # noqa jinja - ansible.builtin.set_fact: - instance_conf_dict: {} - # instance': "{{ }}", - # address': "{{ }}", - # user': "{{ }}", - # port': "{{ }}", - # 'identity_file': "{{ }}", } - with_items: "{{ server.results }}" - register: instance_config_dict - - - name: Convert instance config dict to a list - ansible.builtin.set_fact: - instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}" - - - name: Dump instance config - ansible.builtin.copy: - content: | - # Molecule managed - - {{ instance_conf | to_json | from_json | to_yaml }} - dest: "{{ molecule_instance_config }}" - mode: 0600 diff --git a/collections/ansible_collections/theliberatededge/test_linux_containers/extensions/molecule/default/destroy.yml b/collections/ansible_collections/theliberatededge/test_linux_containers/extensions/molecule/default/destroy.yml index 5ba2d23..1f2ddfc 100644 --- a/collections/ansible_collections/theliberatededge/test_linux_containers/extensions/molecule/default/destroy.yml +++ b/collections/ansible_collections/theliberatededge/test_linux_containers/extensions/molecule/default/destroy.yml @@ -1,24 +1,21 @@ --- -- name: Destroy +- name: Create hosts: localhost connection: local gather_facts: false - # no_log: "{{ molecule_no_log }}" tasks: - # Developer must implement. + - name: find current container state + ansible.builtin.command: "lxc ls {{ item.name }}" + register: container_state + changed_when: false + with_items: "{{ molecule_yml.platforms }}" - # Mandatory configuration for Molecule to function. - - - name: Populate instance config - ansible.builtin.set_fact: - instance_conf: {} - - - name: Dump instance config - ansible.builtin.copy: - content: | - # Molecule managed - - {{ instance_conf | to_json | from_json | to_yaml }} - dest: "{{ molecule_instance_config }}" - mode: 0600 - when: server.changed | default(false) | bool # noqa no-handler + - name: stop running containers + ansible.builtin.shell: + cmd: | + lxc stop {{ item.name }} && \ + lxc rm {{ item.name }} + when: + (container_state.results[0].rc == 0) and + ("RUNNING" in container_state.results[0].stdout) + with_items: "{{ molecule_yml.platforms }}" diff --git a/collections/ansible_collections/theliberatededge/test_linux_containers/extensions/molecule/default/molecule.yml b/collections/ansible_collections/theliberatededge/test_linux_containers/extensions/molecule/default/molecule.yml index f291e2b..9a40fa3 100644 --- a/collections/ansible_collections/theliberatededge/test_linux_containers/extensions/molecule/default/molecule.yml +++ b/collections/ansible_collections/theliberatededge/test_linux_containers/extensions/molecule/default/molecule.yml @@ -2,6 +2,11 @@ dependency: name: galaxy platforms: - - name: instance + - name: ${LTE_CONTAINER_NAME} +provisioner: + name: ansible + playbooks: + create: destroy.yml + destroy: destroy.yml verifier: name: ansible diff --git a/collections/ansible_collections/theliberatededge/test_linux_containers/extensions/molecule/default/verify.yml b/collections/ansible_collections/theliberatededge/test_linux_containers/extensions/molecule/default/verify.yml index f91205f..d50d0c2 100644 --- a/collections/ansible_collections/theliberatededge/test_linux_containers/extensions/molecule/default/verify.yml +++ b/collections/ansible_collections/theliberatededge/test_linux_containers/extensions/molecule/default/verify.yml @@ -3,10 +3,13 @@ hosts: localhost gather_facts: false tasks: - - name: stat file - ansible.builtin.stat: - path: ~/tst.log - register: stat_result - - name: verify file exists - assert: - that: stat_result.stat.exists + - name: find current container state + ansible.builtin.command: "lxc ls {{ item.name }}" + register: container_state + with_items: "{{ molecule_yml.platforms }}" + + - name: verify launched container is running + assert: + that: + (container_state.results[0].rc == 0) and + ("RUNNING" in container_state.results[0].stdout) diff --git a/collections/ansible_collections/theliberatededge/test_linux_containers/roles/linux_container/tasks/main.yml b/collections/ansible_collections/theliberatededge/test_linux_containers/roles/linux_container/tasks/main.yml index 8490985..4476738 100644 --- a/collections/ansible_collections/theliberatededge/test_linux_containers/roles/linux_container/tasks/main.yml +++ b/collections/ansible_collections/theliberatededge/test_linux_containers/roles/linux_container/tasks/main.yml @@ -1,6 +1,15 @@ --- -- name: Task is running from within the role - ansible.builtin.file: - path: ~/tst.log - state: touch +- name: find current container state + ansible.builtin.command: "lxc ls {{ lte_container_name }}" + register: container_state changed_when: false + +- name: launch debian bookworm linux container + ansible.builtin.shell: + cmd: | + lxc launch \ + images:debian/bookworm/cloud \ + {{ lte_container_name }} + when: + (container_state.rc == 1) or + ("RUNNING" not in container_state.stdout)