From 9a82ac0f601dc552c5d3b3d4fd94b6dbb825df6b Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Thu, 27 Jun 2019 14:00:17 -0700 Subject: [PATCH] Move upload-git-mirror test job in-repo This was previously defined in openstack-zuul-jobs, but we're working out a way of keeping these in-repo, so move it here. This was entirely authored by David Simard. Change-Id: I743b8e9011f717569589d9e16ac0228ad3d112d1 Co-Authored-By: David Moreau Simard --- doc/source/test-jobs.rst | 1 + test-playbooks/upload-git-mirror.yaml | 91 +++++++++++++++++++++++++++ zuul.d/test-general-roles-jobs.yaml | 10 +++ 3 files changed, 102 insertions(+) create mode 100644 test-playbooks/upload-git-mirror.yaml create mode 100644 zuul.d/test-general-roles-jobs.yaml diff --git a/doc/source/test-jobs.rst b/doc/source/test-jobs.rst index cb2c7c874..5e62443b9 100644 --- a/doc/source/test-jobs.rst +++ b/doc/source/test-jobs.rst @@ -8,3 +8,4 @@ these unless you are making changes to the roles in this repository. .. zuul:autojob:: zuul-jobs-test-install-nodejs .. zuul:autojob:: zuul-jobs-test-registry +.. zuul:autojob:: zuul-jobs-test-upload-git-mirror diff --git a/test-playbooks/upload-git-mirror.yaml b/test-playbooks/upload-git-mirror.yaml new file mode 100644 index 000000000..6510a57df --- /dev/null +++ b/test-playbooks/upload-git-mirror.yaml @@ -0,0 +1,91 @@ +- name: Run tests for the upload-git-mirror role + hosts: all + vars: + test_repo: https://opendev.org/zuul/zuul-jobs + test_repo_path: "{{ ansible_user_dir }}/tests/zuul-jobs" + pre_tasks: + - name: Create tests directory + file: + path: "{{ ansible_user_dir }}/tests" + state: directory + + # For pushing to localhost over ssh + - name: Create a test ssh keypair + command: "ssh-keygen -t rsa -b 2048 -N '' -f {{ ansible_user_dir }}/tests/id_rsa" + + - name: Get test private key + command: "cat {{ ansible_user_dir }}/tests/id_rsa" + changed_when: false + register: private_key_contents + + - name: Get test public key + command: "cat {{ ansible_user_dir }}/tests/id_rsa.pub" + changed_when: false + register: public_key_contents + + - name: Add public key to authorized_keys + authorized_key: + user: "{{ ansible_user }}" + state: present + key: "{{ public_key_contents.stdout }}" + + - name: Get localhost ssh host public key + shell: ssh-keyscan -t rsa localhost + register: host_key + tasks: + - name: Get git commit hash for current patch of zuul-jobs + command: git rev-parse HEAD + changed_when: false + args: + chdir: "{{ ansible_user_dir }}/{{ zuul.project.src_dir }}" + register: current_commit + + - name: Clone upstream zuul-jobs as a bare repository + git: + repo: "{{ test_repo }}" + dest: "{{ test_repo_path }}" + bare: yes + register: test_repo_clone + + - name: Assert that the git repositories have different commits for HEAD + assert: + that: + - test_repo_clone.after != current_commit.stdout + + - name: Run upload-git-mirror with specified credentials + vars: + git_mirror_credentials: + user: "{{ ansible_user }}" + host: localhost + ssh_key: "{{ private_key_contents.stdout }}" + host_key: "{{ host_key.stdout }}" + # TODO: key is renamed to ssh_key to prevent ambiguity with host_key. + # Remove key when the rename has landed in the role. + key: "{{ private_key_contents.stdout }}" + git_mirror_repository: "{{ test_repo_path }}" + include_role: + name: upload-git-mirror + + - name: Get new repository HEAD commit + command: git rev-parse HEAD + changed_when: false + args: + chdir: "{{ test_repo_path }}" + register: after_mirror + + - name: Assert that the git repositories have the same commit for HEAD + assert: + that: + - after_mirror.stdout == current_commit.stdout + + - name: Check that the private key has been removed + stat: + path: "{{ ssh_private_key_tmp.path }}" + register: private_key_stat + + - name: Assert that the private key and the config was removed + assert: + that: + - git_mirror_key_removed is changed + - not private_key_stat.stat.exists + - git_mirror_ssh_config_removed is changed diff --git a/zuul.d/test-general-roles-jobs.yaml b/zuul.d/test-general-roles-jobs.yaml new file mode 100644 index 000000000..1fee96a32 --- /dev/null +++ b/zuul.d/test-general-roles-jobs.yaml @@ -0,0 +1,10 @@ +# Jobs which test roles listed in general-roles.rst + +- job: + name: zuul-jobs-test-upload-git-mirror + description: | + Tests for the upload-git-mirror role from zuul-jobs + run: test-playbooks/upload-git-mirror.yaml + files: + - ^roles/upload-git-mirror/.* + - ^test-playbooks/upload-git-mirror.yaml