Skip to content

Commit

Permalink
🐛 Make integration tests compatible w/ modern Git (ansible#80122)
Browse files Browse the repository at this point in the history
* 🐛 Make integration tests compatible w/ modern Git

This patch makes use of the `init.defaultBranch` setting to unify
the test across new and old Git versions since one defaults to
`master` and the other uses `main` for the default branch.

Where possible, it uses the `HEAD` committish to avoid having to
normalize the branch name.

The change fixes the following integration tests:

  * `ansible-galaxy`

  * `ansible-galaxy-collection-scm` (recursive collection)

  * `git`

* 🐛Replace `git-symbolic-ref` with a repo template

This custom Git repository template emulates the `init.defaultBranch` setting
on Git versions below 2.28. Ref: https://superuser.com/a/1559582.
Other workarounds mentioned there, like invoking
`git symbolic-ref HEAD refs/heads/main` after each `git init` turned
out to have mysterious side effects that break the tests in surprising ways.

* 🎨 Make Git integration test non-destructive

This patch makes use of the `$HOME` environment variable to trick Git
into using a user-global config generated in the temporary directory.
  • Loading branch information
webknjaz authored Mar 3, 2023
1 parent cc8e6d0 commit c0e0550
Show file tree
Hide file tree
Showing 15 changed files with 176 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
lineinfile:
path: '{{ scm_path }}/namespace_2/collection_2/galaxy.yml'
regexp: '^dependencies'
line: "dependencies: {'git+file://{{ scm_path }}/namespace_1/.git#collection_1/': 'master'}"
# NOTE: The committish is set to `HEAD` here because Git's default has
# NOTE: changed to `main` and it behaves differently in
# NOTE: different envs with different Git versions.
line: >-
dependencies:
{'git+file://{{ scm_path }}/namespace_1/.git#collection_1/': 'HEAD'}
- name: Commit the changes
shell: git add ./; git commit -m 'add collection'
Expand Down
7 changes: 5 additions & 2 deletions test/integration/targets/ansible-galaxy/runme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ f_ansible_galaxy_create_role_repo_post()
git add .
git commit -m "local testing ansible galaxy role"

# NOTE: `HEAD` is used because the newer Git versions create
# NOTE: `main` by default and the older ones differ. We
# NOTE: want to avoid hardcoding them.
git archive \
--format=tar \
--prefix="${repo_name}/" \
master > "${repo_tar}"
HEAD > "${repo_tar}"
# Configure basic (insecure) HTTPS-accessible repository
galaxy_local_test_role_http_repo="${galaxy_webserver_root}/${galaxy_local_test_role}.git"
if [[ ! -d "${galaxy_local_test_role_http_repo}" ]]; then
Expand Down Expand Up @@ -354,7 +357,7 @@ pushd "${galaxy_testdir}"
popd # ${galaxy_testdir}

f_ansible_galaxy_status \
"role info non-existant role"
"role info non-existent role"

mkdir -p "${role_testdir}"
pushd "${role_testdir}"
Expand Down
9 changes: 6 additions & 3 deletions test/integration/targets/git/tasks/depth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,16 @@
repo: 'file://{{ repo_dir|expanduser }}/shallow'
dest: '{{ checkout_dir }}'
depth: 1
version: master
version: >-
{{ git_default_branch }}
- name: DEPTH | run a second time (now fetch, not clone)
git:
repo: 'file://{{ repo_dir|expanduser }}/shallow'
dest: '{{ checkout_dir }}'
depth: 1
version: master
version: >-
{{ git_default_branch }}
register: git_fetch

- name: DEPTH | ensure the fetch succeeded
Expand All @@ -120,7 +122,8 @@
repo: 'file://{{ repo_dir|expanduser }}/shallow'
dest: '{{ checkout_dir }}'
depth: 1
version: master
version: >-
{{ git_default_branch }}
- name: DEPTH | switch to older branch with depth=1 (uses fetch)
git:
Expand Down
4 changes: 2 additions & 2 deletions test/integration/targets/git/tasks/forcefully-fetch-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
git add leet;
git commit -m uh-oh;
git tag -f herewego;
git push --tags origin master
git push --tags origin '{{ git_default_branch }}'
args:
chdir: "{{ repo_dir }}/tag_force_push_clone1"

Expand All @@ -26,7 +26,7 @@
git add leet;
git commit -m uh-oh;
git tag -f herewego;
git push -f --tags origin master
git push -f --tags origin '{{ git_default_branch }}'
args:
chdir: "{{ repo_dir }}/tag_force_push_clone1"

Expand Down
10 changes: 6 additions & 4 deletions test/integration/targets/git/tasks/gpg-verification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
environment:
- GNUPGHOME: "{{ git_gpg_gpghome }}"
shell: |
set -e
set -eEu
git init
touch an_empty_file
git add an_empty_file
git commit --no-gpg-sign --message "Commit, and don't sign"
Expand All @@ -48,11 +50,11 @@
git tag --annotate --message "This is not a signed tag" unsigned_annotated_tag HEAD
git commit --allow-empty --gpg-sign --message "Commit, and sign"
git tag --sign --message "This is a signed tag" signed_annotated_tag HEAD
git checkout -b some_branch/signed_tip master
git checkout -b some_branch/signed_tip '{{ git_default_branch }}'
git commit --allow-empty --gpg-sign --message "Commit, and sign"
git checkout -b another_branch/unsigned_tip master
git checkout -b another_branch/unsigned_tip '{{ git_default_branch }}'
git commit --allow-empty --no-gpg-sign --message "Commit, and don't sign"
git checkout master
git checkout '{{ git_default_branch }}'
args:
chdir: "{{ git_gpg_source }}"

Expand Down
26 changes: 24 additions & 2 deletions test/integration/targets/git/tasks/localmods.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# test for https://github.com/ansible/ansible-modules-core/pull/5505
- name: LOCALMODS | prepare old git repo
shell: rm -rf localmods; mkdir localmods; cd localmods; git init; echo "1" > a; git add a; git commit -m "1"
shell: |
set -eEu
rm -rf localmods
mkdir localmods
cd localmods
git init
echo "1" > a
git add a
git commit -m "1"
args:
chdir: "{{repo_dir}}"

Expand Down Expand Up @@ -55,7 +66,18 @@

# localmods and shallow clone
- name: LOCALMODS | prepare old git repo
shell: rm -rf localmods; mkdir localmods; cd localmods; git init; echo "1" > a; git add a; git commit -m "1"
shell: |
set -eEu
rm -rf localmods
mkdir localmods
cd localmods
git init
echo "1" > a
git add a
git commit -m "1"
args:
chdir: "{{repo_dir}}"

Expand Down
50 changes: 30 additions & 20 deletions test/integration/targets/git/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,37 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

- import_tasks: setup.yml
- import_tasks: setup-local-repos.yml
# NOTE: Moving `$HOME` to tmp dir allows this integration test be
# NOTE: non-destructive. There is no other way to instruct Git use a custom
# NOTE: config path. There are new `$GIT_CONFIG_KEY_{COUNT,KEY,VALUE}` vars
# NOTE: for setting specific configuration values but those are only available
# NOTE: since Git v2.31 which is why we cannot rely on them yet.

- import_tasks: formats.yml
- import_tasks: missing_hostkey.yml
- import_tasks: missing_hostkey_acceptnew.yml
- import_tasks: no-destination.yml
- import_tasks: specific-revision.yml
- import_tasks: submodules.yml
- import_tasks: change-repo-url.yml
- import_tasks: depth.yml
- import_tasks: single-branch.yml
- import_tasks: checkout-new-tag.yml
- include_tasks: gpg-verification.yml
when:
- block:
- import_tasks: setup.yml
- import_tasks: setup-local-repos.yml

- import_tasks: formats.yml
- import_tasks: missing_hostkey.yml
- import_tasks: missing_hostkey_acceptnew.yml
- import_tasks: no-destination.yml
- import_tasks: specific-revision.yml
- import_tasks: submodules.yml
- import_tasks: change-repo-url.yml
- import_tasks: depth.yml
- import_tasks: single-branch.yml
- import_tasks: checkout-new-tag.yml
- include_tasks: gpg-verification.yml
when:
- not gpg_version.stderr
- gpg_version.stdout
- not (ansible_os_family == 'RedHat' and ansible_distribution_major_version is version('7', '<'))
- import_tasks: localmods.yml
- import_tasks: reset-origin.yml
- import_tasks: ambiguous-ref.yml
- import_tasks: archive.yml
- import_tasks: separate-git-dir.yml
- import_tasks: forcefully-fetch-tag.yml
- import_tasks: localmods.yml
- import_tasks: reset-origin.yml
- import_tasks: ambiguous-ref.yml
- import_tasks: archive.yml
- import_tasks: separate-git-dir.yml
- import_tasks: forcefully-fetch-tag.yml
environment:
HOME: >-
{{ remote_tmp_dir }}
3 changes: 2 additions & 1 deletion test/integration/targets/git/tasks/missing_hostkey.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
git:
repo: '{{ repo_format3 }}'
dest: '{{ checkout_dir }}'
version: 'master'
version: >-
{{ git_default_branch }}
accept_hostkey: false # should already have been accepted
key_file: '{{ github_ssh_private_key }}'
ssh_opts: '-o UserKnownHostsFile={{ remote_tmp_dir }}/known_hosts'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
git:
repo: '{{ repo_format3 }}'
dest: '{{ checkout_dir }}'
version: 'master'
version: >-
{{ git_default_branch }}
accept_newhostkey: false # should already have been accepted
key_file: '{{ github_ssh_private_key }}'
ssh_opts: '-o UserKnownHostsFile={{ remote_tmp_dir }}/known_hosts'
Expand Down
9 changes: 8 additions & 1 deletion test/integration/targets/git/tasks/reset-origin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
state: directory

- name: RESET-ORIGIN | Initialise the repo with a file named origin,see github.com/ansible/ansible/pull/22502
shell: git init; echo "PR 22502" > origin; git add origin; git commit -m "PR 22502"
shell: |
set -eEu
git init
echo "PR 22502" > origin
git add origin
git commit -m "PR 22502"
args:
chdir: "{{ repo_dir }}/origin"

Expand Down
33 changes: 28 additions & 5 deletions test/integration/targets/git/tasks/setup-local-repos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,32 @@
- "{{ repo_dir }}/tag_force_push"

- name: SETUP-LOCAL-REPOS | prepare minimal git repo
shell: git init; echo "1" > a; git add a; git commit -m "1"
shell: |
set -eEu
git init
echo "1" > a
git add a
git commit -m "1"
args:
chdir: "{{ repo_dir }}/minimal"

- name: SETUP-LOCAL-REPOS | prepare git repo for shallow clone
shell: |
git init;
echo "1" > a; git add a; git commit -m "1"; git tag earlytag; git branch earlybranch;
echo "2" > a; git add a; git commit -m "2";
set -eEu
git init
echo "1" > a
git add a
git commit -m "1"
git tag earlytag
git branch earlybranch
echo "2" > a
git add a
git commit -m "2"
args:
chdir: "{{ repo_dir }}/shallow"

Expand All @@ -29,7 +46,10 @@

- name: SETUP-LOCAL-REPOS | prepare tmp git repo with two branches
shell: |
set -eEu
git init
echo "1" > a; git add a; git commit -m "1"
git checkout -b test_branch; echo "2" > a; git commit -m "2 on branch" a
git checkout -b new_branch; echo "3" > a; git commit -m "3 on new branch" a
Expand All @@ -40,6 +60,9 @@
# We make the repo here for consistency with the other repos,
# but we finish setting it up in forcefully-fetch-tag.yml.
- name: SETUP-LOCAL-REPOS | prepare tag_force_push git repo
shell: git init --bare
shell: |
set -eEu
git init --bare
args:
chdir: "{{ repo_dir }}/tag_force_push"
38 changes: 36 additions & 2 deletions test/integration/targets/git/tasks/setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,44 @@
register: gpg_version

- name: SETUP | set git global user.email if not already set
shell: git config --global user.email || git config --global user.email "[email protected]"
shell: git config --global user.email '[email protected]'

- name: SETUP | set git global user.name if not already set
shell: git config --global user.name || git config --global user.name "Ansible Test Runner"
shell: git config --global user.name 'Ansible Test Runner'

- name: SETUP | set git global init.defaultBranch
shell: >-
git config --global init.defaultBranch '{{ git_default_branch }}'
- name: SETUP | set git global init.templateDir
# NOTE: This custom Git repository template emulates the `init.defaultBranch`
# NOTE: setting on Git versions below 2.28.
# NOTE: Ref: https://superuser.com/a/1559582.
# NOTE: Other workarounds mentioned there, like invoking
# NOTE: `git symbolic-ref HEAD refs/heads/main` after each `git init` turned
# NOTE: out to have mysterious side effects that break the tests in surprising
# NOTE: ways.
shell: |
set -eEu
git config --global \
init.templateDir '{{ remote_tmp_dir }}/git-templates/git.git'
mkdir -pv '{{ remote_tmp_dir }}/git-templates'
set +e
GIT_TEMPLATES_DIR=$(\
2>/dev/null \
ls -1d \
'/Library/Developer/CommandLineTools/usr/share/git-core/templates' \
'/usr/local/share/git-core/templates' \
'/usr/share/git-core/templates' \
)
set -e
>&2 echo "Found Git's default templates directory: ${GIT_TEMPLATES_DIR}"
cp -r "${GIT_TEMPLATES_DIR}" '{{ remote_tmp_dir }}/git-templates/git.git'
echo 'ref: refs/heads/{{ git_default_branch }}' \
> '{{ remote_tmp_dir }}/git-templates/git.git/HEAD'
- name: SETUP | create repo_dir
file:
Expand Down
6 changes: 4 additions & 2 deletions test/integration/targets/git/tasks/single-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@
repo: 'file://{{ repo_dir|expanduser }}/shallow_branches'
dest: '{{ checkout_dir }}'
single_branch: yes
version: master
version: >-
{{ git_default_branch }}
register: single_branch_3

- name: SINGLE_BRANCH | Clone example git repo using single_branch with version again
git:
repo: 'file://{{ repo_dir|expanduser }}/shallow_branches'
dest: '{{ checkout_dir }}'
single_branch: yes
version: master
version: >-
{{ git_default_branch }}
register: single_branch_4

- name: SINGLE_BRANCH | List revisions
Expand Down
Loading

0 comments on commit c0e0550

Please sign in to comment.