From 49339c421a2bb5487ea01942cc0a7ce7f75645af Mon Sep 17 00:00:00 2001 From: Maja Massarini Date: Fri, 5 Jul 2024 14:58:07 +0200 Subject: [PATCH] Make test work in vagrant, local tmt vm and remote testing farm vm --- Makefile | 65 +++++++++++++++++++--------- containers/Vagrantfile | 19 ++++---- plans/deployment.fmf | 53 +++++++++++------------ playbooks/oc-cluster-run.yml | 12 ++--- playbooks/oc-cluster-setup.yml | 11 +++-- playbooks/oc-cluster-tests-setup.yml | 13 ------ playbooks/oc-cluster-user.yml | 11 +++-- playbooks/test_deploy.yml | 4 +- playbooks/test_deploy_setup.yml | 8 ++-- tests/deployment/main.fmf | 2 +- 10 files changed, 104 insertions(+), 94 deletions(-) diff --git a/Makefile b/Makefile index 59dd939..49cadec 100755 --- a/Makefile +++ b/Makefile @@ -11,12 +11,13 @@ VAGRANT_SSH_USER = "$(shell cd containers && vagrant ssh-config | awk '/User/{pr VAGRANT_SSH_GUEST = "$(shell cd containers && vagrant ssh-config | awk '/HostName/{print $$2}')" VAGRANT_SSH_IDENTITY_FILE = "$(shell cd containers && vagrant ssh-config | awk '/IdentityFile/{print $$2}')" VAGRANT_SSH_CONFIG = $(shell cd containers && vagrant ssh-config | awk 'NR>1 {print " -o "$$1"="$$2}') -#VAGRANT_SHARED_DIR = "/vagrant" -VAGRANT_SHARED_DIR = "/home/tmt/deployment" +VAGRANT_SHARED_DIR = "/vagrant" CENTOS_VAGRANT_BOX = CentOS-Stream-Vagrant-8-latest.x86_64.vagrant-libvirt.box CENTOS_VAGRANT_URL = https://cloud.centos.org/centos/8-stream/x86_64/images/$(CENTOS_VAGRANT_BOX) +CRC_PULL_SECRET ?= "$(shell cat secrets/openshift-local-pull-secret.yml)" + ifneq "$(shell whoami)" "root" ASK_PASS ?= --ask-become-pass endif @@ -72,7 +73,7 @@ oc-cluster-destroy: oc-cluster-up: cd containers && vagrant up - cd containers && vagrant ssh -c "cd $(VAGRANT_SHARED_DIR) && $(AP) playbooks/oc-cluster-run.yml" + cd containers && vagrant ssh -c "cd $(VAGRANT_SHARED_DIR) && $(AP) --extra-vars user=vagrant playbooks/oc-cluster-run.yml" oc-cluster-down: cd containers && vagrant halt @@ -81,23 +82,45 @@ oc-cluster-ssh: oc-cluster-up ssh $(VAGRANT_SSH_CONFIG) localhost test-deploy: -# to be run inside VM where the oc cluster is running! Call make tmt-vagrant-tests instead from outside the vagrant machine. +# to be run inside VM where the oc cluster && tmt is running! Call make tmt-vagrant-tests instead from outside the vagrant machine. +# SHARED_DIR could be /vagrant or /home/tmt/deployment, it depends on the VM where tmt is being run +# look inside deployment.fmf to find out the value of SHARED_DIR DEPLOYMENT=dev $(AP) playbooks/generate-local-secrets.yml - DEPLOYMENT=dev $(AP) -e '{"src_dir": $(VAGRANT_SHARED_DIR)}' playbooks/test_deploy_setup.yml - cd $(VAGRANT_SHARED_DIR); DEPLOYMENT=dev $(AP) -e '{"container_engine": "podman", "registry": "default-route-openshift-image-registry.apps-crc.testing", "registry_user": "kubeadmin", "src_dir": $(VAGRANT_SHARED_DIR)}' playbooks/test_deploy.yml - -tmt-vagrant-test: - tmt run --all provision --how connect --user vagrant --guest $(VAGRANT_SSH_GUEST) --port $(VAGRANT_SSH_PORT) --key $(VAGRANT_SSH_IDENTITY_FILE) - -tf-deploy: - testing-farm request --compose Fedora-Rawhide --git-url https://github.com/majamassarini/deployment --git-ref tf-openshift-tests --plan deployment - -# tmt run --id packit-service-deployment --until execute -# tmt run --id packit-service-deployment prepare --force -# tmt run --id packit-service-deployment login --step prepare:start -# tmt run --id packit-service-deployment execute --force -# tmt run --id packit-service-deployment login --step execute:start -# tmt run --id packit-service-deployment finish -# tmt run --id packit-service-deployment clean - + DEPLOYMENT=dev $(AP) -e '{"user": $(USER), "src_dir": $(SHARED_DIR)}' playbooks/test_deploy_setup.yml + cd $(SHARED_DIR); DEPLOYMENT=dev $(AP) -e '{"container_engine": "podman", "registry": "default-route-openshift-image-registry.apps-crc.testing", "registry_user": "kubeadmin", "user": $(USER), "src_dir": $(SHARED_DIR)}' playbooks/test_deploy.yml + +# Openshift Local pull_secret must exist locally +# or you can also define the CRC_PULL_SECRET var +check-pull-secret: + if [ ! -f secrets/openshift-local-pull-secret.yml ] && [ ! -n "$(CRC_PULL_SECRET)" ]; then echo "no pull secret available create secrets/openshift-local-pull-secret.yml file or set CRC_PULL_SECRET variable"; exit 1; else echo "pull secret found"; fi + +# Execute tmt deployment test on a vagrant virtual machine +# The virtual machine has to be already up and running, +# using the target oc-cluster-up +tmt-vagrant-test: check-pull-secret + tmt run --all provision --how connect --user vagrant --guest $(VAGRANT_SSH_GUEST) --port $(VAGRANT_SSH_PORT) --key $(VAGRANT_SSH_IDENTITY_FILE) plan --name deployment/vagrant + +# Execute tmt deployment test on a local virtual machine provisioned by tmt +# +# tmt local provisioned virtual machine have by default 2 cpu cores +# you need to change tmt defaults to be able to run this test locally +# change DEFAULT_CPU_COUNT in tmt/steps/provision/testcloud.py to 6 +# +# For running this same test remotely, using testing farm, we need the +# github action, there are no other ways (at the moment) to deal with +# the secrets (in our case the pull_request Openshift Local secret). +# For this reason the deployment/remote plan is not called by this file +# but is called from the testing farm github action configured in this PR +# +# Useful tmt/virsh commands to debug this test are listed below +# tmt run --id deployment --until execute +# tmt run --id deployment prepare --force +# tmt run --id deployment login --step prepare:start +# tmt run --id deployment execute --force +# tmt run --id deployment login --step execute:start +# tmt run --id deployment finish +# tmt clean runs +# tmt clean guests # virsh list --all +tmt-local-test: check-pull-secret + tmt run --id deployment plans --name deployment/local diff --git a/containers/Vagrantfile b/containers/Vagrantfile index 067d63d..4a50130 100644 --- a/containers/Vagrantfile +++ b/containers/Vagrantfile @@ -2,13 +2,11 @@ # vi: set ft=ruby : VAGRANTFILE_API_VERSION = "2" -# export VAGRANT_EXPERIMENTAL="1" -# export VAGRANT_EXPERIMENTAL="disks" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| - config.vm.box = "centos/stream8" - #config.vm.box_url = "https://cloud.centos.org/centos/8-stream/x86_64/images/CentOS-Stream-Vagrant-8-latest.x86_64.vagrant-libvirt.box" gives 404 - config.vm.box_url = "file:///$VagrantProjectHome/../CentOS-Stream-Vagrant-8-latest.x86_64.vagrant-libvirt.box" + config.vm.box = "centos/stream9" + config.vm.box_url = "https://cloud.centos.org/centos/9-stream/x86_64/images/CentOS-Stream-Vagrant-9-latest.x86_64.vagrant-libvirt.box" + #config.vm.box_url = "file:///$VagrantProjectHome/../CentOS-Stream-Vagrant-8-latest.x86_64.vagrant-libvirt.box" # Forward traffic on the host to the development server on the guest @@ -44,30 +42,33 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| # sshfs_opts_append: "-o nonempty" # Comment this line if you would like to disable the automatic update during provisioning - config.vm.provision "shell", inline: "sudo dnf -y --disablerepo '*' --enablerepo=extras swap centos-linux-repos centos-stream-repos" + # config.vm.provision "shell", inline: "sudo dnf -y --disablerepo '*' --enablerepo=extras swap centos-linux-repos centos-stream-repos" # !!!!!!! resize disk image !!!!!!!!! config.vm.provision "shell", inline: "sudo dnf install -y cloud-utils-growpart" config.vm.provision "shell", inline: "sudo growpart /dev/vda 1" - config.vm.provision "shell", inline: "sudo xfs_growfs /dev/vda1" + config.vm.provision "shell", inline: "sudo resize2fs /dev/vda1" + # config.vm.provision "shell", inline: "sudo xfs_growfs /dev/vda1" # this was for CentOS Stream 8 # bootstrap and run with ansible config.vm.provision "ansible" do |ansible| # ansible.verbose = "-vvv" ansible.verbose = true ansible.playbook = "../playbooks/oc-cluster-setup.yml" + ansible.extra_vars = {"user": "vagrant"} end config.vm.provision "ansible" do |ansible| # ansible.verbose = "-vvv" ansible.verbose = true ansible.playbook = "../playbooks/oc-cluster-run.yml" - ansible.extra_vars = "../secrets/openshift-local-pull-secret.yml" + ansible.raw_arguments = ['--extra-vars', 'user=vagrant', '--extra-vars', '@../secrets/openshift-local-pull-secret.yml'] end config.vm.provision "ansible" do |ansible| # ansible.verbose = "-vvv" + ansible.become = true + ansible.become_user = "root" ansible.verbose = true ansible.playbook = "../playbooks/oc-cluster-tests-setup.yml" - ansible.extra_vars = {"src_dir": "/vagrant"} end # Create the box diff --git a/plans/deployment.fmf b/plans/deployment.fmf index 83d9726..8be5d2a 100644 --- a/plans/deployment.fmf +++ b/plans/deployment.fmf @@ -5,6 +5,8 @@ execute: environment: DEPLOYMENT: dev + SHARED_DIR: /home/tmt/deployment + USER: tmt #ANSIBLE_DEBUG: 1 report: @@ -16,20 +18,20 @@ discover: - deployment /remote: - summary: Run packit-service deployment test on a remote testing-farm machine (details are defined in the GitHub action) + summary: Run packit-service deployment test on a remote testing-farm machine (provisioning details are defined in the GitHub action) prepare: - how: ansible playbook: playbooks/oc-cluster-user.yml - extra-args: '-vvv' + extra-args: "-vvv --extra-vars user=tmt" - how: ansible playbook: playbooks/oc-cluster-setup.yml - extra-args: '-vvv' + extra-args: "-vvv --extra-vars user=tmt" - how: ansible playbook: playbooks/oc-cluster-run.yml - extra-args: '-vvv' + extra-args: "-vvv --extra-vars user=tmt" - how: ansible playbook: playbooks/oc-cluster-tests-setup.yml - extra-args: '-vvv' + extra-args: "-vvv" /local: summary: Run packit-service deployment test on a local machine managed by tmt @@ -48,32 +50,29 @@ discover: prepare: - how: ansible playbook: playbooks/oc-cluster-user.yml - extra-args: '-vvv' + extra-args: "-vvv --extra-vars user=tmt" - how: ansible playbook: playbooks/oc-cluster-setup.yml - extra-args: '-vvv' + extra-args: "-vvv --extra-vars user=tmt" - how: ansible playbook: playbooks/oc-cluster-run.yml - extra-args: '-vvv --extra-vars @./secrets/openshift-local-pull-secret.yml' + extra-args: "-vvv --extra-vars @./secrets/openshift-local-pull-secret.yml --extra-vars user=tmt" - how: ansible playbook: playbooks/oc-cluster-tests-setup.yml - extra-args: '-vvv' - #- how: ansible - # playbook: playbooks/generate-local-secrets.yml - # extra-args: '-vvv --extra-vars generate_secrets_project_dir="./"' - #- how: ansible - # playbook: playbooks/test_deploy_setup.yml - # extra-args: '-vvv' + extra-args: "-vvv" -#/vagrant: -# summary: Run packit-service deployment test on a vagrant machine already up and running -# -# description: -# guest, port and key details are taken from the tmt-vagrant-test Makefile target. -# This provision is meant to be used just locally with the Vagrant machine already up -# and running. To start the Vagrant machine use the oc-cluster-up Makefile target. -# To access the Vagrant machine use the oc-cluster-ssh Makefile target. -# -# provision: -# how: connect -# user: vagrant +/vagrant: + summary: Run packit-service deployment test on a vagrant machine already up and running + environment+: + SHARED_DIR: /vagrant + USER: vagrant + + description: + guest, port and key details are taken from the tmt-vagrant-test Makefile target. + This provision is meant to be used just locally with the Vagrant machine already up + and running. To start the Vagrant machine use the oc-cluster-up Makefile target. + To access the Vagrant machine use the oc-cluster-ssh Makefile target. + + provision: + how: connect + user: vagrant diff --git a/playbooks/oc-cluster-run.yml b/playbooks/oc-cluster-run.yml index ac2e5c2..677960d 100644 --- a/playbooks/oc-cluster-run.yml +++ b/playbooks/oc-cluster-run.yml @@ -2,11 +2,11 @@ - name: Start Openshift Local cluster hosts: all become: true - become_user: tmt + become_user: "{{ user }}" gather_facts: False vars: dest_pull_secret_file: /tmp/openshift-local-pull-secret.txt - crc_path: /home/tmt/.local/bin + crc_path: "/home/{{ user }}/.local/bin" tasks: - name: Look for pull_secret in env vars if it is not already defined ansible.builtin.set_fact: @@ -20,10 +20,10 @@ content: "{{ pull_secret }}" dest: "{{ dest_pull_secret_file }}" mode: "0640" - owner: tmt + owner: "{{ user }}" - name: Find user uid - ansible.builtin.command: "id -u tmt" + ansible.builtin.command: "id -u {{ user }}" register: user_uid changed_when: false @@ -40,8 +40,8 @@ - name: Create symbolic link to oc ansible.builtin.file: # src: "/tmp/{{ unarchive.files[0] }}/crc" - src: /home/tmt/.crc/bin/oc/oc + src: "/home/{{ user }}/.crc/bin/oc/oc" # dest: "{{ crc_path }}/crc" - dest: /home/tmt/.local/bin/oc + dest: "/home/{{ user }}/.local/bin/oc" state: link force: True diff --git a/playbooks/oc-cluster-setup.yml b/playbooks/oc-cluster-setup.yml index 76a6cd3..46ff421 100644 --- a/playbooks/oc-cluster-setup.yml +++ b/playbooks/oc-cluster-setup.yml @@ -1,12 +1,11 @@ --- - name: Install an Openshift Local cluster become: true - become_user: tmt + become_user: "{{ user }}" hosts: all vars: crc_download_link: https://developers.redhat.com/content-gateway/rest/mirror/pub/openshift-v4/clients/crc/latest/crc-linux-amd64.tar.xz - crc_path: /home/tmt/.local/bin - user: tmt + crc_path: "/home/{{ user }}/.local/bin" tasks: - name: Download CRC ansible.builtin.get_url: @@ -20,7 +19,7 @@ dest: /tmp list_files: True remote_src: True - owner: tmt + owner: "{{ user }}" register: unarchive - name: Create ~/.local/bin if it does not exist ansible.builtin.file: @@ -30,7 +29,7 @@ - name: Show crc executable path ansible.builtin.debug: msg: "/tmp/{{ unarchive.files[0] }}crc" - - name: Copy crc to tmt usr + - name: Copy crc to user dir ansible.builtin.copy: src: "/tmp/{{ unarchive.files[0] }}/crc" remote_src: true @@ -55,7 +54,7 @@ changed_when: False - name: Find user uid - ansible.builtin.command: "id -u tmt" + ansible.builtin.command: "id -u {{ user }}" register: user_uid changed_when: false diff --git a/playbooks/oc-cluster-tests-setup.yml b/playbooks/oc-cluster-tests-setup.yml index b35536f..0048388 100644 --- a/playbooks/oc-cluster-tests-setup.yml +++ b/playbooks/oc-cluster-tests-setup.yml @@ -20,16 +20,3 @@ - python3-passlib # for using htpasswd ansible module - make - podman - - #- name: Download OC - # ansible.builtin.get_url: - # url: "{{ oc_download_link }}" - # dest: /tmp - # mode: "0644" - # register: download - #- name: Unarchive tarball in /usr/bin - # ansible.builtin.unarchive: - # src: "{{ download.dest }}" - # dest: /usr/bin - # list_files: True - # remote_src: True diff --git a/playbooks/oc-cluster-user.yml b/playbooks/oc-cluster-user.yml index b9042b0..a4045cf 100644 --- a/playbooks/oc-cluster-user.yml +++ b/playbooks/oc-cluster-user.yml @@ -2,18 +2,17 @@ - name: Configure a user able to run a Openshift Local Cluster hosts: all tasks: - - name: Add tmt user + - name: Add user ansible.builtin.user: - name: tmt + name: "{{ user }}" append: true groups: wheel - name: Add tmt user to sudoers ansible.builtin.lineinfile: path: /etc/sudoers state: present - #line: "{{ user }} ALL=(ALL) NOPASSWD: ALL" - line: "tmt ALL=(ALL) NOPASSWD: ALL" + line: "{{ user }} ALL=(ALL) NOPASSWD: ALL" validate: /usr/sbin/visudo -cf %s - - name: Enable lingering for tmt - ansible.builtin.command: loginctl enable-linger tmt + - name: Enable lingering for user + ansible.builtin.command: "loginctl enable-linger {{ user }}" changed_when: False diff --git a/playbooks/test_deploy.yml b/playbooks/test_deploy.yml index ece2eb8..d69aa6f 100644 --- a/playbooks/test_deploy.yml +++ b/playbooks/test_deploy.yml @@ -2,10 +2,10 @@ # SPDX-License-Identifier: MIT --- -- name: Deploy as tmt user +- name: Deploy as tmt/vagrant user hosts: all become: true - become_user: tmt + become_user: "{{ user }}" become_flags: -i vars: test_project_name: myproject diff --git a/playbooks/test_deploy_setup.yml b/playbooks/test_deploy_setup.yml index 8fe35f6..d4d2070 100644 --- a/playbooks/test_deploy_setup.yml +++ b/playbooks/test_deploy_setup.yml @@ -5,7 +5,7 @@ - name: Ensure crc is started, create openshift project and vars/packit/dev.yml hosts: all become: true - become_user: tmt + become_user: "{{ user }}" become_flags: -i vars: dest_pull_secret_file: /tmp/openshift-local-pull-secret.txt @@ -19,7 +19,7 @@ tmt_tree: "{{ lookup('env', 'TMT_TREE') }}" tasks: - name: Find user uid - ansible.builtin.command: "id -u tmt" + ansible.builtin.command: "id -u {{ user }}" register: user_uid changed_when: false @@ -74,6 +74,7 @@ dest: /home/tmt/deployment owner: tmt directory_mode: "0755" + when: src_dir == "/home/tmt/deployment" - name: Copy secrets that are already been generated in tmt home # noqa risky-file-permissions # this is a recursive copy ansible.builtin.copy: @@ -81,6 +82,7 @@ dest: /home/tmt/deployment owner: tmt directory_mode: "0755" + when: src_dir == "/home/tmt/deployment" - name: Create packit/dev.yml ansible.builtin.copy: @@ -107,5 +109,5 @@ image_fedmsg: {{ image_fedmsg }} image_dashboard: {{ image_dashboard }} image_tokman: {{ image_tokman }} - dest: "/home/tmt/deployment/vars/packit/dev.yml" + dest: "{{ src_dir }}/vars/packit/dev.yml" mode: 0644 diff --git a/tests/deployment/main.fmf b/tests/deployment/main.fmf index ea2becf..6912688 100644 --- a/tests/deployment/main.fmf +++ b/tests/deployment/main.fmf @@ -1,6 +1,6 @@ duration: 60m -environment: +environment+: DEPLOYMENT: dev require: