From 702eadf21bfb0ef341febd6abf5d8cd85cc494fa Mon Sep 17 00:00:00 2001 From: Sloane Hertel <19572925+s-hertel@users.noreply.github.com> Date: Wed, 31 Aug 2022 13:22:47 -0400 Subject: [PATCH] Fix apt making changes in check mode (#78496) (#78530) * Don't actually update the cache in check mode Add tests for updating the cache in check mode * Don't mark packages as manually installed in check mode and add a test for it * changelog * Update test/integration/targets/apt/tasks/apt.yml * fix test (cherry picked from commit 3add5e932178799f8409f3255f148260efa7b4ac) --- .../fragments/78496-fix-apt-check-mode.yml | 3 ++ lib/ansible/modules/apt.py | 7 ++-- test/integration/targets/apt/tasks/apt.yml | 16 +++++++++ test/integration/targets/apt/tasks/repo.yml | 36 +++++++++++++++++++ 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/78496-fix-apt-check-mode.yml diff --git a/changelogs/fragments/78496-fix-apt-check-mode.yml b/changelogs/fragments/78496-fix-apt-check-mode.yml new file mode 100644 index 00000000000..25d6ae84666 --- /dev/null +++ b/changelogs/fragments/78496-fix-apt-check-mode.yml @@ -0,0 +1,3 @@ +bugfixes: + - apt - don't actually update the cache in check mode with update_cache=true. + - apt - don't mark existing packages as manually installed in check mode (https://github.com/ansible/ansible/issues/66413). diff --git a/lib/ansible/modules/apt.py b/lib/ansible/modules/apt.py index 0d6c3352926..b254fb031e1 100644 --- a/lib/ansible/modules/apt.py +++ b/lib/ansible/modules/apt.py @@ -809,7 +809,7 @@ def install(m, pkgspec, cache, upgrade=False, default_release=None, status = True data = dict(changed=False) - if not build_dep: + if not build_dep and not m.check_mode: mark_installed_manually(m, package_names) return (status, data) @@ -1332,7 +1332,8 @@ def main(): for retry in range(update_cache_retries): try: - cache.update() + if not module.check_mode: + cache.update() break except apt.cache.FetchFailedException as e: err = to_native(e) @@ -1347,7 +1348,7 @@ def main(): cache.open(progress=None) mtimestamp, post_cache_update_time = get_updated_cache_time() - if updated_cache_time != post_cache_update_time: + if module.check_mode or updated_cache_time != post_cache_update_time: updated_cache = True updated_cache_time = post_cache_update_time diff --git a/test/integration/targets/apt/tasks/apt.yml b/test/integration/targets/apt/tasks/apt.yml index 5b1a24a3b4f..d273eda71e2 100644 --- a/test/integration/targets/apt/tasks/apt.yml +++ b/test/integration/targets/apt/tasks/apt.yml @@ -83,12 +83,26 @@ - apt_install_fnmatch is changed - apt_uninstall_fnmatch is changed +- name: Test update_cache 1 (check mode) + apt: + update_cache: true + cache_valid_time: 10 + register: apt_update_cache_1_check_mode + check_mode: true + - name: Test update_cache 1 apt: update_cache: true cache_valid_time: 10 register: apt_update_cache_1 +- name: Test update_cache 2 (check mode) + apt: + update_cache: true + cache_valid_time: 10 + register: apt_update_cache_2_check_mode + check_mode: true + - name: Test update_cache 2 apt: update_cache: true @@ -98,7 +112,9 @@ - name: verify update_cache assert: that: + - apt_update_cache_1_check_mode is changed - apt_update_cache_1 is changed + - apt_update_cache_2_check_mode is not changed - apt_update_cache_2 is not changed - name: uninstall apt bindings with apt again diff --git a/test/integration/targets/apt/tasks/repo.yml b/test/integration/targets/apt/tasks/repo.yml index aa90109b69c..e4e39aa3570 100644 --- a/test/integration/targets/apt/tasks/repo.yml +++ b/test/integration/targets/apt/tasks/repo.yml @@ -196,6 +196,42 @@ name: foobar=1.0.0 allow_unauthenticated: yes + - name: mark foobar as auto for next test + shell: apt-mark auto foobar + + - name: Install foobar (marked as manual) (check mode) + apt: + name: foobar=1.0.1 + allow_unauthenticated: yes + check_mode: yes + register: manual_foobar_install_check_mode + + - name: check foobar was not marked as manually installed by check mode + shell: apt-mark showmanual | grep foobar + ignore_errors: yes + register: showmanual + + - assert: + that: + - manual_foobar_install_check_mode.changed + - "'foobar' not in showmanual.stdout" + + - name: Install foobar (marked as manual) + apt: + name: foobar=1.0.1 + allow_unauthenticated: yes + register: manual_foobar_install + + - name: check foobar was marked as manually installed + shell: apt-mark showmanual | grep foobar + ignore_errors: yes + register: showmanual + + - assert: + that: + - manual_foobar_install.changed + - "'foobar' in showmanual.stdout" + - name: Upgrade foobar to a version which does not depend on foo, autoremove should remove foo apt: upgrade: dist