Skip to content

Commit

Permalink
podman: get the current user from whoami instead of the environment
Browse files Browse the repository at this point in the history
b5509a1 uses $USER to get the home for the current user, but
if the controller does not run login(1), like in containers, the variable is not set.
This commit uses whoami to get the current user.

- src/molecule_plugins/podman/playbooks: remove $USER logic and use whoami to get the current user
- tox.ini: remove USER from the pass_env list
- test/podman/test_func.py: added test with a different $HOME
- .github/workflow/tox.yml: bump PYTEST_REQPASS
  • Loading branch information
grcancelliere committed Aug 4, 2023
1 parent 10a502b commit a807707
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
runs-on: ubuntu-22.04
needs: pre
env:
PYTEST_REQPASS: 13
PYTEST_REQPASS: 14
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.pre.outputs.matrix) }}
Expand Down
15 changes: 10 additions & 5 deletions src/molecule_plugins/podman/playbooks/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,28 @@
ansible.builtin.set_fact:
podman_cmd: "{{ _podman_path.stdout }}"

- name: Get passwd entries for USER env
- name: Get the current user
ansible.builtin.command: "whoami"
changed_when: false
register: local_user

- name: Get passwd entries for the current user
ansible.builtin.getent:
database: passwd
key: "{{ lookup('env', 'USER') }}"
key: "{{ local_user.stdout }}"

- name: Get shell async_dir
ansible.builtin.set_fact:
_shell_async_dir: >-
{{ lookup('ansible.builtin.config', 'async_dir', plugin_type='shell', plugin_name='sh')
| regex_replace('^~', ansible_facts.getent_passwd[lookup('env', 'USER')][4]) }}
| regex_replace('^~', ansible_facts.getent_passwd[local_user.stdout][4]) }}
- name: Set async_dir for HOME env
ansible.builtin.set_fact:
ansible_async_dir: >-
{{ _shell_async_dir
| regex_replace('^' + ansible_facts.getent_passwd[lookup('env', 'USER')][4], lookup('env', 'HOME')) }}
when: lookup('env', 'HOME') != ansible_facts.getent_passwd[lookup('env', 'USER')][4]
| regex_replace('^' + ansible_facts.getent_passwd[local_user.stdout][4], lookup('env', 'HOME')) }}
when: lookup('env', 'HOME') != ansible_facts.getent_passwd[local_user.stdout][4]

- name: Log into a container registry
ansible.builtin.command: >
Expand Down
15 changes: 10 additions & 5 deletions src/molecule_plugins/podman/playbooks/destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@
vars:
podman_exec: "{{ lookup('env','MOLECULE_PODMAN_EXECUTABLE')|default('podman',true) }}"
tasks:
- name: Get passwd entries for USER env
- name: Get the current user
ansible.builtin.command: "whoami"
changed_when: false
register: local_user

- name: Get passwd entries for the current user
ansible.builtin.getent:
database: passwd
key: "{{ lookup('env', 'USER') }}"
key: "{{ local_user.stdout }}"

- name: Get shell async_dir
ansible.builtin.set_fact:
_shell_async_dir: >-
{{ lookup('ansible.builtin.config', 'async_dir', plugin_type='shell', plugin_name='sh')
| regex_replace('^~', ansible_facts.getent_passwd[lookup('env', 'USER')][4]) }}
| regex_replace('^~', ansible_facts.getent_passwd[local_user.stdout][4]) }}
- name: Set async_dir for HOME env
ansible.builtin.set_fact:
ansible_async_dir: >-
{{ _shell_async_dir
| regex_replace('^' + ansible_facts.getent_passwd[lookup('env', 'USER')][4], lookup('env', 'HOME')) }}
when: lookup('env', 'HOME') != ansible_facts.getent_passwd[lookup('env', 'USER')][4]
| regex_replace('^' + ansible_facts.getent_passwd[local_user.stdout][4], lookup('env', 'HOME')) }}
when: lookup('env', 'HOME') != ansible_facts.getent_passwd[local_user.stdout][4]

- name: Destroy molecule instance(s)
ansible.builtin.shell: "{{ podman_exec }} container exists {{ item.name }} && {{ podman_exec }} rm -f {{ item.name }} || true"
Expand Down
13 changes: 13 additions & 0 deletions test/podman/test_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,16 @@ def test_dockerfile():
)
assert result.returncode == 0, format_result(result)
# , result


def test_change_user_home(tmp_path: pathlib.Path) -> None:
"""Runs the sample scenario with a different $HOME."""
new_home = tmp_path / "test_change_user_home"
env = os.environ.copy()
env["HOME"] = str(new_home)
result = run_command(
["molecule", "test", "-s", "test-podman"],
env=env,
)
assert result.returncode == 0
assert new_home.exists()
1 change: 0 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ passenv =
SSL_CERT_FILE
TOXENV
TWINE_*
USER
allowlist_externals =
bash
twine
Expand Down

0 comments on commit a807707

Please sign in to comment.