Skip to content

Commit

Permalink
Make test work in vagrant, local tmt vm and remote testing farm vm
Browse files Browse the repository at this point in the history
  • Loading branch information
majamassarini committed Jul 10, 2024
1 parent 4cab4d4 commit 49339c4
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 94 deletions.
65 changes: 44 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
19 changes: 10 additions & 9 deletions containers/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
53 changes: 26 additions & 27 deletions plans/deployment.fmf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ execute:

environment:
DEPLOYMENT: dev
SHARED_DIR: /home/tmt/deployment
USER: tmt
#ANSIBLE_DEBUG: 1

report:
Expand All @@ -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
Expand All @@ -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
12 changes: 6 additions & 6 deletions playbooks/oc-cluster-run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand All @@ -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
11 changes: 5 additions & 6 deletions playbooks/oc-cluster-setup.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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

Expand Down
13 changes: 0 additions & 13 deletions playbooks/oc-cluster-tests-setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 5 additions & 6 deletions playbooks/oc-cluster-user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions playbooks/test_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 49339c4

Please sign in to comment.