From 1da6a67c5079df50cfe2f0908e22bf290b3bea20 Mon Sep 17 00:00:00 2001 From: Rob Cutmore Date: Thu, 22 Dec 2016 16:46:47 -0500 Subject: [PATCH] Add tests for Git remote URL changes (#16893) * Update Git tests for set remote URL changes * Git: report changed when needed in check mode --- lib/ansible/modules/source_control/git.py | 2 +- test/integration/targets/git/tasks/main.yml | 50 +++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/source_control/git.py b/lib/ansible/modules/source_control/git.py index ad505ed7b31..95675cbd434 100644 --- a/lib/ansible/modules/source_control/git.py +++ b/lib/ansible/modules/source_control/git.py @@ -993,7 +993,7 @@ def main(): if module.check_mode: remote_head = get_remote_head(git_path, module, dest, version, remote, bare) - result.update(changed=(result['before'] != remote_head), after=remote_head) + result.update(changed=(result['before'] != remote_head or remote_url_changed), after=remote_head) # FIXME: This diff should fail since the new remote_head is not fetched yet?! if module._diff: diff = get_diff(module, git_path, dest, repo, remote, depth, bare, result['before'], result['after']) diff --git a/test/integration/targets/git/tasks/main.yml b/test/integration/targets/git/tasks/main.yml index 2f5373028d4..80ee9171906 100644 --- a/test/integration/targets/git/tasks/main.yml +++ b/test/integration/targets/git/tasks/main.yml @@ -36,6 +36,7 @@ - "{{ lookup('env','HOME') }}/.ssh/known_hosts" - '/etc/ssh/ssh_known_hosts' git_version_supporting_depth: 1.9.1 + git_version_supporting_ls_remote: 1.7.5 - name: clean out the output_dir shell: rm -rf {{ output_dir }}/* @@ -442,6 +443,55 @@ assert: that: "repo_content.stat.exists" +# Make sure 'changed' result is accurate in check mode. +# See https://github.com/ansible/ansible-modules-core/pull/4243 + +- name: clear checkout_dir + file: state=absent path={{ checkout_dir }} + +- name: clone repo + git: repo={{ repo_update_url_1 }} dest={{ checkout_dir }} + +- name: clone repo with same url to same destination + git: repo={{ repo_update_url_1 }} dest={{ checkout_dir }} + register: checkout_same_url + +- name: check repo not changed + assert: + that: + - not checkout_same_url|changed + + +- name: clone repo with new url to same destination + git: repo={{ repo_update_url_2 }} dest={{ checkout_dir }} + register: checkout_new_url + +- name: check repo changed + assert: + that: + - checkout_new_url|changed + + +- name: clone repo with new url in check mode + git: repo={{ repo_update_url_1 }} dest={{ checkout_dir }} + register: checkout_new_url_check_mode + check_mode: True + +- name: check repo reported changed in check mode + assert: + that: + - checkout_new_url_check_mode|changed + when: git_version.stdout | version_compare("{{git_version_supporting_ls_remote}}", '>=') + +- name: clone repo with new url after check mode + git: repo={{ repo_update_url_1 }} dest={{ checkout_dir }} + register: checkout_new_url_after_check_mode + +- name: check repo still changed after check mode + assert: + that: + - checkout_new_url_after_check_mode|changed + # Test that checkout by branch works when the branch is not in our current repo but the sha is - name: clear checkout_dir