Skip to content

Commit d29ce15

Browse files
Test deployment locally with tmt (from packit branch) (#581)
Test deployment locally with tmt (from packit branch) To be able to access secrets and test ansible scripts using github secrets in the testing farm github action I need to create a PR from the packit repo itself (and not from a fork). This is the reason why the PR is duplicated at the moment. Should fix #562 #563 #564 #565 Reviewed-by: Laura Barcziová Reviewed-by: Matej Focko Reviewed-by: Maja Massarini
2 parents ac1dd10 + 9702af0 commit d29ce15

24 files changed

+1227
-10
lines changed

.ansible-lint

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mock_modules:
1414
# Ansible 2.9.27 in F35 still contains the k8s module so we can ignore the error until F36,
1515
# where we can switch to kubernetes.core.k8s as ansible-5.x in F36 contains it.
1616
- k8s
17+
- kubernetes.core.k8s
1718
# Ignore until F36, where these are in community.crypto collection (part of ansible-5.x rpm).
1819
- openssh_keypair
1920
- openssl_certificate

.fmf/version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1

.github/workflows/tf-tests.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Schedule tests on Testing Farm
2+
on:
3+
pull_request:
4+
5+
# The concurrency key is used to prevent multiple workflows from running at the same time
6+
concurrency:
7+
group: my-concurrency-group
8+
cancel-in-progress: true
9+
10+
jobs:
11+
tests:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Schedule tests on Testing Farm
15+
uses: sclorg/testing-farm-as-github-action@v2
16+
with:
17+
compose: CentOS-Stream-9
18+
api_key: ${{ secrets.TF_API_KEY }}
19+
git_url: "https://github.com/packit/deployment"
20+
git_ref: "tf-openshift-tests"
21+
tmt_plan_regex: "deployment/remote"
22+
tmt_hardware: '{"memory": ">= 13 GiB", "disk": [{"size": ">= 100 GB"}], "cpu": {"cores": ">= 6"}, "virtualization": {"is-supported": true}}'
23+
pull_request_status_name: "Deployment"
24+
timeout: 3600
25+
secrets: CRC_PULL_SECRET=${{ secrets.CRC_PULL_SECRET }}

.zuul.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
check:
44
jobs:
55
- pre-commit
6-
- deployment-tests
6+
# - deployment-tests
77
gate:
88
jobs:
99
- pre-commit

Makefile

+43
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ AP := ansible-playbook -vv -c local -i localhost, -e ansible_python_interpreter=
77
# https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html#special-tags
88
TAGS ?= all
99

10+
CRC_PULL_SECRET ?= "$(shell cat secrets/openshift-local-pull-secret.yml)"
11+
1012
ifneq "$(shell whoami)" "root"
1113
ASK_PASS ?= --ask-become-pass
1214
endif
1315

1416
# Only for Packit team members with access to Bitwarden vault
17+
# if not working prepend OPENSSL_CONF=/dev/null to script invocation
1518
download-secrets:
1619
./scripts/download_secrets.sh
1720

@@ -50,3 +53,43 @@ check:
5053
move-stable:
5154
[[ -d move_stable_repositories ]] || scripts/move_stable.py init
5255
scripts/move_stable.py move-all
56+
57+
# To be run inside VM where the oc cluster is running!
58+
# `cd /vagrant; SHARED_DIR=/vagrant make test-deploy` for using it inside the vagrant VM.
59+
# `SHARED_DIR=/home/tmt/deployment make test-deploy` for using it inside the tmt VM.
60+
# SHARED_DIR could be /vagrant or /home/tmt/deployment, it depends on the VM where tmt is being run
61+
# look inside deployment.fmf to find out the value of SHARED_DIR set through tmt
62+
test-deploy:
63+
DEPLOYMENT=dev $(AP) playbooks/generate-local-secrets.yml
64+
DEPLOYMENT=dev $(AP) -e '{"user": $(USER), "src_dir": $(SHARED_DIR)}' playbooks/test_deploy_setup.yml
65+
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
66+
67+
# Openshift Local pull_secret must exist locally
68+
# or you can also define the CRC_PULL_SECRET var
69+
check-pull-secret:
70+
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
71+
72+
# Execute tmt deployment test on a local virtual machine provisioned by tmt
73+
#
74+
# tmt local provisioned virtual machine have by default 2 cpu cores
75+
# you need to change tmt defaults to be able to run this test locally
76+
# change DEFAULT_CPU_COUNT in tmt/steps/provision/testcloud.py to 6
77+
#
78+
# For running this same test remotely, using testing farm, we need the
79+
# github action, there are no other ways (at the moment) to deal with
80+
# the secrets (in our case the pull_request Openshift Local secret).
81+
# For this reason the deployment/remote plan is not called by this file
82+
# instead it is called from the testing farm github action
83+
#
84+
# Useful tmt/virsh commands to debug this test are listed below
85+
# tmt run --id deployment --until execute
86+
# tmt run --id deployment prepare --force
87+
# tmt run --id deployment login --step prepare:start
88+
# tmt run --id deployment execute --force
89+
# tmt run --id deployment login --step execute:start
90+
# tmt run --id deployment finish
91+
# tmt clean runs
92+
# tmt clean guests
93+
# virsh list --all
94+
tmt-local-test: check-pull-secret
95+
tmt run --id deployment plans --name deployment/local

containers/Makefile

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
ANSIBLE_PYTHON ?= $(shell command -v /usr/bin/python3 2> /dev/null || echo /usr/bin/python2)
2+
AP := ansible-playbook -vv -c local -i localhost, -e ansible_python_interpreter=$(ANSIBLE_PYTHON)
3+
VAGRANT_SSH_PORT = "$(shell vagrant ssh-config | awk '/Port/{print $$2}')"
4+
VAGRANT_SSH_USER = "$(shell vagrant ssh-config | awk '/User/{print $$2}')"
5+
VAGRANT_SSH_GUEST = "$(shell vagrant ssh-config | awk '/HostName/{print $$2}')"
6+
VAGRANT_SSH_IDENTITY_FILE = "$(shell vagrant ssh-config | awk '/IdentityFile/{print $$2}')"
7+
VAGRANT_SSH_CONFIG = $(shell vagrant ssh-config | awk 'NR>1 {print " -o "$$1"="$$2}')
8+
VAGRANT_SHARED_DIR = "/vagrant"
9+
10+
# to be used when the vagrant box link is broken, should be kept in sync with the Vagrant file
11+
#CENTOS_VAGRANT_BOX = CentOS-Stream-Vagrant-8-latest.x86_64.vagrant-libvirt.box
12+
#CENTOS_VAGRANT_URL = https://cloud.centos.org/centos/8-stream/x86_64/images/$(CENTOS_VAGRANT_BOX)
13+
14+
CRC_PULL_SECRET ?= "$(shell cat secrets/openshift-local-pull-secret.yml)"
15+
16+
# for this command to work, you may need to:
17+
# sudo systemctl enable --now libvirtd
18+
# sudo systemctl enable --now virtnetworkd
19+
oc-cluster-create:
20+
if [ ! -z "$(CENTOS_VAGRANT_BOX)" ] && [ -f $(CENTOS_VAGRANT_BOX) ]; then wget $(CENTOS_VAGRANT_URL); fi;
21+
vagrant up
22+
23+
oc-cluster-destroy:
24+
vagrant destroy
25+
26+
oc-cluster-up:
27+
vagrant up
28+
vagrant ssh -c "cd $(VAGRANT_SHARED_DIR) && $(AP) --extra-vars user=vagrant playbooks/oc-cluster-run.yml"
29+
30+
oc-cluster-down:
31+
vagrant halt
32+
33+
oc-cluster-ssh: oc-cluster-up
34+
ssh $(VAGRANT_SSH_CONFIG) localhost
35+
36+
# Openshift Local pull_secret must exist locally
37+
# or you can also define the CRC_PULL_SECRET var
38+
check-pull-secret:
39+
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
40+
41+
# Execute tmt deployment test on a vagrant virtual machine
42+
# The virtual machine has to be already up and running,
43+
# use the target oc-cluster-up
44+
tmt-vagrant-test: check-pull-secret
45+
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

containers/Vagrantfile

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# -*- mode: ruby -*-
2+
# vi: set ft=ruby :
3+
4+
VAGRANTFILE_API_VERSION = "2"
5+
6+
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
7+
config.vm.box = "centos/stream9"
8+
config.vm.box_url = "https://cloud.centos.org/centos/9-stream/x86_64/images/CentOS-Stream-Vagrant-9-latest.x86_64.vagrant-libvirt.box"
9+
#config.vm.box_url = "file:///$VagrantProjectHome/../CentOS-Stream-Vagrant-8-latest.x86_64.vagrant-libvirt.box"
10+
11+
12+
# Forward traffic on the host to the development server on the guest
13+
config.vm.network "forwarded_port", guest: 5000, host: 5000
14+
# Forward traffic on the host to Redis on the guest
15+
config.vm.network "forwarded_port", guest: 6379, host: 6379
16+
# Forward traffic on the host to the SSE server on the guest
17+
config.vm.network "forwarded_port", guest: 8080, host: 8080
18+
19+
20+
if Vagrant.has_plugin?("vagrant-hostmanager")
21+
config.hostmanager.enabled = true
22+
config.hostmanager.manage_host = true
23+
end
24+
25+
# Vagrant can share the source directory using rsync, NFS, or SSHFS (with the vagrant-sshfs
26+
# plugin). By default it rsyncs the current working directory to /vagrant.
27+
#
28+
# If you would prefer to use NFS to share the directory uncomment this and configure NFS
29+
# config.vm.synced_folder ".", "/vagrant", type: "nfs", nfs_version: 4, nfs_udp: false
30+
config.vm.synced_folder "..", "/vagrant"
31+
# config.vm.synced_folder ".", "/vagrant", disabled: true
32+
# config.vm.synced_folder ".", "/srv/pagure",
33+
# ssh_opts_append: "-o IdentitiesOnly=yes",
34+
# type: "sshfs"
35+
36+
# To cache update packages (which is helpful if frequently doing `vagrant destroy && vagrant up`)
37+
# you can create a local directory and share it to the guest's DNF cache. The directory needs to
38+
# exist, so create it before you uncomment the line below.
39+
#Dir.mkdir('.dnf-cache') unless File.exists?('.dnf-cache')
40+
#config.vm.synced_folder ".dnf-cache", "/var/cache/dnf",
41+
# type: "sshfs",
42+
# sshfs_opts_append: "-o nonempty"
43+
44+
# Comment this line if you would like to disable the automatic update during provisioning
45+
# config.vm.provision "shell", inline: "sudo dnf -y --disablerepo '*' --enablerepo=extras swap centos-linux-repos centos-stream-repos"
46+
47+
# !!!!!!! resize disk image !!!!!!!!!
48+
config.vm.provision "shell", inline: "sudo dnf install -y cloud-utils-growpart"
49+
config.vm.provision "shell", inline: "sudo growpart /dev/vda 1"
50+
config.vm.provision "shell", inline: "sudo resize2fs /dev/vda1"
51+
# config.vm.provision "shell", inline: "sudo xfs_growfs /dev/vda1" # this was for CentOS Stream 8
52+
53+
# bootstrap and run with ansible
54+
config.vm.provision "ansible" do |ansible|
55+
# ansible.verbose = "-vvv"
56+
ansible.verbose = true
57+
ansible.playbook = "../playbooks/oc-cluster-setup.yml"
58+
ansible.extra_vars = {"user": "vagrant"}
59+
end
60+
config.vm.provision "ansible" do |ansible|
61+
# ansible.verbose = "-vvv"
62+
ansible.verbose = true
63+
ansible.playbook = "../playbooks/oc-cluster-run.yml"
64+
ansible.raw_arguments = ['--extra-vars', 'user=vagrant', '--extra-vars', '@../secrets/openshift-local-pull-secret.yml']
65+
end
66+
config.vm.provision "ansible" do |ansible|
67+
# ansible.verbose = "-vvv"
68+
ansible.become = true
69+
ansible.become_user = "root"
70+
ansible.verbose = true
71+
ansible.playbook = "../playbooks/oc-cluster-tests-setup.yml"
72+
end
73+
74+
# Create the box
75+
config.vm.define "packit-oc-cluster" do |oc|
76+
oc.vm.host_name = "packit-oc-cluster.example.com"
77+
78+
oc.vm.provider :libvirt do |domain|
79+
# Season to taste
80+
domain.cpus = 6
81+
domain.graphics_type = "spice"
82+
domain.memory = 14336
83+
domain.video_type = "qxl"
84+
domain.machine_virtual_size = 100
85+
86+
# Uncomment the following line if you would like to enable libvirt's unsafe cache
87+
# mode. It is called unsafe for a reason, as it causes the virtual host to ignore all
88+
# fsync() calls from the guest. Only do this if you are comfortable with the possibility of
89+
# your development guest becoming corrupted (in which case you should only need to do a
90+
# vagrant destroy and vagrant up to get a new one).
91+
#
92+
# domain.volume_cache = "unsafe"
93+
end
94+
end
95+
end

docs/deployment/testing-changes.md

+83
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,86 @@ This repository provides helpful playbook to do this with one command:
6868

6969
Zuul provides a public key for every project. The ansible playbook downloads Zuul repository and pass the project tenant and name as parameters to encryption script. This script then encrypts files with public key of the project.
7070
For more information please refer to [official docs](https://ansible.softwarefactory-project.io/docs/user/zuul_user.html#create-a-secret-to-be-used-in-jobs).
71+
72+
### Test Deployment locally with OpenShift Local
73+
74+
For using OpenShift Local you need a _pull secret_, download it here: https://console.redhat.com/openshift/create/local. Save it in a file called `secrets/openshift-local-pull-secret.yml` following this format:
75+
76+
```
77+
---
78+
pull_secret: <<< DOWNLOADED PULL SECRET CONTENT >>>
79+
```
80+
81+
[Populate the `secrets` dir (`secrets/{SERVICE}/dev/`) with the other secrets.](secrets#running-a-servicebot-locally)
82+
83+
You can choose if you want to use a Virtual Machine created by Vagrant or one created by tmt.
84+
85+
Calling a test multiple times, modifyng and debugging it is simpler in a Vagrant VM.
86+
87+
The tmt environment ensure a more reproducible test.
88+
89+
#### Using Vagrant
90+
91+
Create and start the OpenShift Local cluster in a Vagrant VM with (it takes as long as an hour in my X1 ThinkPad):
92+
93+
```
94+
cd containers; make oc-cluster-create
95+
```
96+
97+
Once OC is up and running you can test the `packit-service` deployment with the command:
98+
99+
```
100+
cd containers; make tmt-vagrant-test
101+
```
102+
103+
This command will connect tmt to the Vagrant virtual machine and run the deploy test there (`make test-deploy`).
104+
You can run the test as many times as you want as long as the virtual machine is up and running and the `crc cluster` is started (`make oc-cluster-up` after every `make oc-cluster-down`).
105+
You can skip the `tmt` environment and run the test directly inside the VM:
106+
107+
```
108+
cd containers;
109+
make oc-cluster-ssh
110+
```
111+
112+
Inside the Vagrant VM as vagrant user you do:
113+
114+
```
115+
cd /vagrant
116+
SHARED_DIR=/vagrant make test-deploy
117+
```
118+
119+
You can directly work on the cluster:
120+
121+
```
122+
oc login -u kubeadmin https://api.crc.testing:6443
123+
oc project myproject
124+
oc describe node
125+
oc describe pods
126+
oc describe pod packit-worker-0
127+
...
128+
```
129+
130+
You can destroy the `libvirt` machine with `cd containers; make oc-cluster-destroy` and re-create it again with `cd containers; make oc-cluster-create`.
131+
132+
#### Using tmt
133+
134+
You can test the packit-service deployment using a tmt created local VM with the command:
135+
136+
```
137+
make tmt-local-test
138+
```
139+
140+
It is quite hard to change a test inside a tmt created VM and debug it.
141+
But, in case you need it this is a list of commands that can be handy:
142+
143+
```
144+
tmt run --id deployment --until execute
145+
tmt run --id deployment prepare --force
146+
tmt run --id deployment login --step prepare:start
147+
tmt run --id deployment execute --force
148+
tmt run --id deployment login --step execute:start
149+
tmt run --id deployment finish
150+
tmt clean runs
151+
tmt clean guests
152+
virsh list --all
153+
```

openshift/redis.yml.j2

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ spec:
2020
spec:
2121
containers:
2222
- name: redis
23-
image: quay.io/sclorg/redis-7-c9s
23+
image: quay.io/sclorg/redis-7-c9s:c9s
2424
ports:
2525
- containerPort: 6379
2626
volumeMounts:

0 commit comments

Comments
 (0)