2014-02-20 03:13:47 +01:00
|
|
|
# test code for the git module
|
|
|
|
# (c) 2014, James Tanner <tanner.jc@gmail.com>
|
|
|
|
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-03-12 19:28:48 +01:00
|
|
|
- name: set role facts
|
|
|
|
set_fact:
|
|
|
|
checkout_dir: '{{ output_dir }}/git'
|
|
|
|
repo_format1: 'https://github.com/jimi-c/test_role'
|
|
|
|
repo_format2: 'git@github.com:jimi-c/test_role.git'
|
|
|
|
repo_format3: 'ssh://git@github.com/jimi-c/test_role.git'
|
2014-10-28 22:58:25 +01:00
|
|
|
repo_submodules: 'https://github.com/abadger/test_submodules.git'
|
|
|
|
repo_submodules_newer: 'https://github.com/abadger/test_submodules_newer.git'
|
|
|
|
repo_submodule1: 'https://github.com/abadger/test_submodules_subm1.git'
|
|
|
|
repo_submodule1_newer: 'https://github.com/abadger/test_submodules_subm1_newer.git'
|
|
|
|
repo_submodule2: 'https://github.com/abadger/test_submodules_subm2.git'
|
2014-03-12 19:28:48 +01:00
|
|
|
known_host_files:
|
|
|
|
- "{{ lookup('env','HOME') }}/.ssh/known_hosts"
|
|
|
|
- '/etc/ssh/ssh_known_hosts'
|
2014-02-20 03:13:47 +01:00
|
|
|
|
|
|
|
- name: clean out the output_dir
|
|
|
|
shell: rm -rf {{ output_dir }}/*
|
|
|
|
|
|
|
|
- name: verify that git is installed so this test can continue
|
|
|
|
shell: which git
|
|
|
|
|
2014-03-12 19:28:48 +01:00
|
|
|
#
|
|
|
|
# Test repo=https://github.com/...
|
|
|
|
#
|
|
|
|
|
2014-02-20 03:13:47 +01:00
|
|
|
- name: initial checkout
|
2014-03-12 19:28:48 +01:00
|
|
|
git: repo={{ repo_format1 }} dest={{ checkout_dir }}
|
2014-02-20 03:13:47 +01:00
|
|
|
register: git_result
|
|
|
|
|
|
|
|
- name: verify information about the initial clone
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "'before' in git_result"
|
|
|
|
- "'after' in git_result"
|
|
|
|
- "not git_result.before"
|
2014-03-12 19:28:48 +01:00
|
|
|
- "git_result.changed"
|
2014-02-20 03:13:47 +01:00
|
|
|
|
|
|
|
- name: repeated checkout
|
2014-03-12 19:28:48 +01:00
|
|
|
git: repo={{ repo_format1 }} dest={{ checkout_dir }}
|
2014-02-20 03:13:47 +01:00
|
|
|
register: git_result2
|
|
|
|
|
|
|
|
- name: check for tags
|
|
|
|
stat: path={{ checkout_dir }}/.git/refs/tags
|
|
|
|
register: tags
|
|
|
|
|
|
|
|
- name: check for HEAD
|
|
|
|
stat: path={{ checkout_dir }}/.git/HEAD
|
|
|
|
register: head
|
|
|
|
|
|
|
|
- name: check for remotes
|
|
|
|
stat: path={{ checkout_dir }}/.git/branches
|
|
|
|
register: branches
|
|
|
|
|
2014-04-29 16:41:05 +02:00
|
|
|
- name: assert presence of tags/trunk/branches
|
2014-02-20 03:13:47 +01:00
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "tags.stat.isdir"
|
|
|
|
- "head.stat.isreg"
|
|
|
|
- "branches.stat.isdir"
|
|
|
|
|
|
|
|
- name: verify on a reclone things are marked unchanged
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "not git_result2.changed"
|
|
|
|
|
2014-03-12 19:28:48 +01:00
|
|
|
#
|
|
|
|
# Test repo=git@github.com:/...
|
|
|
|
# Requires variable: github_ssh_private_key
|
|
|
|
#
|
|
|
|
|
|
|
|
- name: clear checkout_dir
|
|
|
|
file: state=absent path={{ checkout_dir }}
|
2014-02-20 03:13:47 +01:00
|
|
|
|
2014-03-12 19:28:48 +01:00
|
|
|
- name: remove known_host files
|
|
|
|
file: state=absent path={{ item }}
|
|
|
|
with_items: known_host_files
|
2014-02-20 03:13:47 +01:00
|
|
|
|
2014-03-12 19:28:48 +01:00
|
|
|
- name: checkout ssh://git@github.com repo without accept_hostkey (expected fail)
|
|
|
|
git: repo={{ repo_format2 }} dest={{ checkout_dir }}
|
|
|
|
register: git_result
|
|
|
|
ignore_errors: true
|
2014-02-20 03:13:47 +01:00
|
|
|
|
2014-03-12 19:28:48 +01:00
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- 'git_result.failed'
|
|
|
|
- 'git_result.msg == "github.com has an unknown hostkey. Set accept_hostkey to True or manually add the hostkey prior to running the git module"'
|
|
|
|
|
|
|
|
- name: checkout git@github.com repo with accept_hostkey (expected pass)
|
|
|
|
git:
|
|
|
|
repo: '{{ repo_format2 }}'
|
|
|
|
dest: '{{ checkout_dir }}'
|
|
|
|
accept_hostkey: true
|
|
|
|
key_file: '{{ github_ssh_private_key }}'
|
|
|
|
register: git_result
|
|
|
|
when: github_ssh_private_key is defined
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- 'git_result.changed'
|
|
|
|
when: not git_result|skipped
|
|
|
|
|
|
|
|
#
|
|
|
|
# Test repo=ssh://git@github.com/...
|
|
|
|
# Requires variable: github_ssh_private_key
|
|
|
|
#
|
|
|
|
|
|
|
|
- name: clear checkout_dir
|
|
|
|
file: state=absent path={{ checkout_dir }}
|
|
|
|
|
|
|
|
- name: checkout ssh://git@github.com repo with accept_hostkey (expected pass)
|
|
|
|
git:
|
|
|
|
repo: '{{ repo_format3 }}'
|
|
|
|
dest: '{{ checkout_dir }}'
|
|
|
|
version: 'master'
|
|
|
|
accept_hostkey: false # should already have been accepted
|
|
|
|
key_file: '{{ github_ssh_private_key }}'
|
|
|
|
register: git_result
|
|
|
|
when: github_ssh_private_key is defined
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- 'git_result.changed'
|
|
|
|
when: not git_result|skipped
|
2014-09-17 09:01:27 +02:00
|
|
|
|
|
|
|
# Test a non-updating repo query with no destination specified
|
|
|
|
|
|
|
|
- name: get info on a repo without updating and with no destination specified
|
|
|
|
git:
|
|
|
|
repo: '{{ repo_format1 }}'
|
|
|
|
update: no
|
|
|
|
accept_hostkey: yes
|
|
|
|
register: git_result
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- 'git_result.changed'
|
2014-10-28 22:58:25 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# Submodule tests
|
|
|
|
#
|
|
|
|
|
|
|
|
# Repository A with submodules defined (repo_submodules)
|
|
|
|
# .gitmodules file points to Repository I
|
|
|
|
# Repository B forked from A that has newer commits (repo_submodules_newer)
|
|
|
|
# .gitmodules file points to Repository II instead of I
|
|
|
|
# .gitmodules file also points to Repository III
|
|
|
|
# Repository I for submodule1 (repo_submodule1)
|
|
|
|
# Has 1 file checked in
|
|
|
|
# Repository II forked from I that has newer commits (repo_submodule1_newer)
|
|
|
|
# Has 2 files checked in
|
|
|
|
# Repository III for a second submodule (repo_submodule2)
|
|
|
|
# Has 1 file checked in
|
|
|
|
|
|
|
|
- name: clear checkout_dir
|
|
|
|
file: state=absent path={{ checkout_dir }}
|
|
|
|
|
|
|
|
- name: Test that clone without recursive does not retrieve submodules
|
|
|
|
git:
|
|
|
|
repo: '{{ repo_submodules }}'
|
|
|
|
dest: '{{ checkout_dir }}'
|
|
|
|
recursive: no
|
|
|
|
|
|
|
|
- command: 'ls -1a {{ checkout_dir }}/submodule1'
|
|
|
|
register: submodule1
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that: '{{ submodule1.stdout_lines|length }} == 2'
|
|
|
|
|
|
|
|
- name: clear checkout_dir
|
|
|
|
file: state=absent path={{ checkout_dir }}
|
|
|
|
|
|
|
|
|
|
|
|
- name: Test that clone with recursive retrieves submodules
|
|
|
|
git:
|
|
|
|
repo: '{{ repo_submodules }}'
|
|
|
|
dest: '{{ checkout_dir }}'
|
|
|
|
recursive: yes
|
|
|
|
|
|
|
|
- command: 'ls -1a {{ checkout_dir }}/submodule1'
|
|
|
|
register: submodule1
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that: '{{ submodule1.stdout_lines|length }} == 4'
|
|
|
|
|
|
|
|
- name: Copy the checkout so we can run several different tests on it
|
|
|
|
command: 'cp -pr {{ checkout_dir }} {{ checkout_dir }}.bak'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- name: Test that update without recursive does not change submodules
|
|
|
|
command: 'git config --replace-all remote.origin.url {{ repo_submodules_newer }}'
|
|
|
|
args:
|
|
|
|
chdir: '{{ checkout_dir }}'
|
|
|
|
|
|
|
|
- git:
|
|
|
|
repo: '{{ repo_submodules_newer }}'
|
|
|
|
dest: '{{ checkout_dir }}'
|
|
|
|
recursive: no
|
|
|
|
update: yes
|
|
|
|
track_submodules: yes
|
|
|
|
|
|
|
|
- command: 'ls -1a {{ checkout_dir }}/submodule1'
|
|
|
|
register: submodule1
|
|
|
|
|
|
|
|
- stat:
|
|
|
|
path: '{{ checkout_dir }}/submodule2'
|
|
|
|
register: submodule2
|
|
|
|
|
|
|
|
- command: 'ls -1a {{ checkout_dir }}/submodule2'
|
|
|
|
register: submodule2
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that: '{{ submodule1.stdout_lines|length }} == 4'
|
|
|
|
- assert:
|
|
|
|
that: '{{ submodule2.stdout_lines|length }} == 2'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- name: Restore checkout to prior state
|
|
|
|
file: state=absent path={{ checkout_dir }}
|
|
|
|
- command: 'cp -pr {{ checkout_dir }}.bak {{ checkout_dir }}'
|
|
|
|
|
|
|
|
- name: Test that update with recursive updated existing submodules
|
|
|
|
command: 'git config --replace-all remote.origin.url {{ repo_submodules_newer }}'
|
|
|
|
args:
|
|
|
|
chdir: '{{ checkout_dir }}'
|
|
|
|
|
|
|
|
- git:
|
|
|
|
repo: '{{ repo_submodules_newer }}'
|
|
|
|
dest: '{{ checkout_dir }}'
|
|
|
|
update: yes
|
|
|
|
recursive: yes
|
|
|
|
track_submodules: yes
|
|
|
|
|
|
|
|
- command: 'ls -1a {{ checkout_dir }}/submodule1'
|
|
|
|
register: submodule1
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that: '{{ submodule1.stdout_lines|length }} == 5'
|
|
|
|
|
|
|
|
|
|
|
|
- name: Test that update with recursive found new submodules
|
|
|
|
command: 'ls -1a {{ checkout_dir }}/submodule2'
|
|
|
|
register: submodule2
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that: '{{ submodule2.stdout_lines|length }} == 4'
|