Skip to content

Commit

Permalink
behave: Add tests for osc-git commands, update test suite as needed
Browse files Browse the repository at this point in the history
  • Loading branch information
dmach committed Sep 20, 2024
1 parent 61d57bc commit a9b0b16
Show file tree
Hide file tree
Showing 43 changed files with 864 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,4 @@ jobs:
- name: "Run tests"
run: |
cd behave
behave -Dosc=../osc-wrapper.py -Dmax_podman_containers=2
behave -Dosc=../osc-wrapper.py -Dpodman_max_containers=2
11 changes: 9 additions & 2 deletions behave/Containerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
FROM opensuse/leap:15.6

RUN zypper ar --repo http://download.opensuse.org/repositories/OBS:/Server:/Unstable/15.6/OBS:Server:Unstable.repo
RUN zypper -n --gpg-auto-import-keys refresh
RUN zypper -n modifyrepo --disable repo-openh264 || : \
&& zypper ar --repo http://download.opensuse.org/repositories/OBS:/Server:/Unstable/15.6/OBS:Server:Unstable.repo \
&& zypper ar --repo http://download.opensuse.org/repositories/devel:/tools:/scm/15.6/devel:tools:scm.repo \
&& zypper -n --gpg-auto-import-keys refresh

RUN zypper -n install \
bash \
bash-completion \
git \
gitea \
less \
obs-api \
obs-gitea-bridge \
obs-server \
obs-signd \
obs-worker \
Expand All @@ -16,6 +21,7 @@ RUN zypper -n install \
openssl \
perl-XML-SAX \
rpm-build \
sqlite3 \
systemd \
vim \
&& rm -rf /var/cache/zypp/*
Expand All @@ -25,6 +31,7 @@ COPY container-files/ /
RUN /bin/bash /opt/setup/setup.sh \
&& /bin/bash /opt/setup/initial-data.sh \
&& /bin/bash /opt/setup/prebuilt-rpms.sh \
&& /bin/bash /opt/setup/gitea.sh \
&& rm -rf /var/log/apache2/* \
&& rm -rf /srv/obs/log/* \
&& rm -rf /srv/obs/service/log/* \
Expand Down
36 changes: 36 additions & 0 deletions behave/container-files/etc/gitea/conf/app.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
WORK_PATH = /var/lib/gitea

[server]
CERT_FILE = /etc/gitea/https/cert.pem
KEY_FILE = /etc/gitea/https/key.pem
STATIC_ROOT_PATH = /usr/share/gitea
APP_DATA_PATH = /var/lib/gitea/data
PPROF_DATA_PATH = /var/lib/gitea/data/tmp/pprof
PROTOCOL = http
HTTP_PORT = 3000
DISABLE_SSH = false
START_SSH_SERVER = true
SSH_PORT = 3022
LFS_START_SERVER = true

[lfs]
PATH = /var/lib/gitea/data/lfs

[database]
DB_TYPE = sqlite3
PATH = /var/lib/gitea/data/gitea.db

[security]
INSTALL_LOCK = true

[oauth2]
ENABLED = false

[log]
ROOT_PATH = /var/log/gitea
MODE = console, file
; Either "Trace", "Debug", "Info", "Warn", "Error" or "None", default is "Info"
LEVEL = Debug

[service]
ENABLE_BASIC_AUTHENTICATION = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=Configure /etc/gitea/conf/app.ini from environmental variables

[Service]
Type=oneshot
User=root
Group=root
PassEnvironment=GITEA_SERVER_HTTP_PORT
PassEnvironment=GITEA_SERVER_SSH_PORT
ExecStart=bash -c /usr/bin/gitea-configure-from-env

[Install]
RequiredBy=gitea.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=Fix /var/lib/gitea/data by copying it back and forth to the same overlayfs layer

[Service]
Type=oneshot
User=gitea
Group=gitea
ExecStart=/bin/bash -c 'mv /var/lib/gitea/data /var/lib/gitea/data.backup && mkdir -p /var/lib/gitea/data && cp -a /var/lib/gitea/data.backup/* /var/lib/gitea/data/'

[Install]
RequiredBy=gitea.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Unit]
Description=Configure OBS from environmental variables

[Service]
Type=oneshot
User=root
Group=root
PassEnvironment=OBS_PROXY_AUTH
ExecStart=bash -c /usr/bin/obs-configure-from-env

[Install]
RequiredBy=apache2.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<package name="test-GitPkgA" project="test:factory">
<title/>
<description/>
<scmsync>http://localhost:3000/pool/test-GitPkgA#factory</scmsync>
</package>
143 changes: 143 additions & 0 deletions behave/container-files/opt/setup/gitea.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
set -x
set -e


TOPDIR=$(dirname $(readlink -f "$0"))
source "$TOPDIR/common.sh"


OSC="osc -A https://localhost"


chown gitea:gitea /etc/gitea/conf/app.ini

DB_PATH=/var/lib/gitea/data/gitea.db

# create the database
su - gitea -c 'gitea migrate'


# to generate an access token for testing, use the following Python code:
# from hashlib import pbkdf2_hmac
# char = b"1"
# print(pbkdf2_hmac(hash_name="sha256", password=40*char, salt=10*char, iterations=10000, dklen=50).hex())


# user #1: Admin, password=opensuse
# gitea refuses to create user 'admin'; let's create 'admin1' and rename it in the database
su - gitea -c 'gitea admin user create --username Admin1 --password opensuse --email [email protected] --must-change-password=false --admin'
su - gitea -c "echo \"update user set lower_name='admin', name='Admin' where lower_name = 'admin1';\" | sqlite3 $DB_PATH"
su - gitea -c "echo \"INSERT INTO access_token (uid, name, token_hash, token_salt, token_last_eight, scope, created_unix, updated_unix) VALUES (1, 'admin', '2da98f9cae724ae30563e3ba9663afb24af91019d04736523f1762eed291c449aebbbb749571958e1811588b33e64ae86bd7', '1111111111', '11111111', 'all', 0, 0);\" | sqlite3 $DB_PATH"
export TOKEN_ADMIN='1111111111111111111111111111111111111111'


# user #2: Alice, password=opensuse
su - gitea -c 'gitea admin user create --username Alice --password opensuse --email [email protected] --must-change-password=false'
#su - gitea -c "echo \"update user set must_change_password=0 where lower_name = 'alice';\" | sqlite3 $DB_PATH"
su - gitea -c "echo \"INSERT INTO access_token (uid, name, token_hash, token_salt, token_last_eight, scope, created_unix, updated_unix) VALUES (2, 'alice', '5aeaf57e2c156673a566815b5a5739f9aa25bc3ac0a3c9e942f31361230e1f26983f6b2abfd009358202fc2e02c8137693ee', 'aaaaaaaaaa', 'aaaaaaaa', 'all', 0, 0);\" | sqlite3 $DB_PATH"
export TOKEN_ALICE='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'

#sqlite> update access_token set scope='read:repository,write:repository,read:user' where id in (2,3);
#read:issue

# user #3 Bob, password=opensuse
su - gitea -c 'gitea admin user create --username Bob --password opensuse --email [email protected] --must-change-password=false'
#su - gitea -c "echo \"update user set must_change_password=0 where lower_name = 'bob';\" | sqlite3 $DB_PATH"
su - gitea -c "echo \"INSERT INTO access_token (uid, name, token_hash, token_salt, token_last_eight, scope, created_unix, updated_unix) VALUES (3, 'bob', 'b97a745cff7dabb6a767c4e993609ef41c54b8f722f9ff88b4232430e087751d54436fec1240f056585b270f432efb02d188', 'bbbbbbbbbb', 'bbbbbbbb', 'all', 0, 0);\" | sqlite3 $DB_PATH"
export TOKEN_BOB='bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'


systemctl enable gitea
systemctl enable gitea-configure-from-env
systemctl enable gitea-fix-var-lib-gitea-data


su - gitea -c 'gitea' 2>&1 >/dev/null &
sleep 15


function create_org {
org="$1"
curl \
-X POST \
-H "Authorization: token $TOKEN_ADMIN" \
-H "Content-type: application/json" \
--data "{\"username\": \"$org\"}" \
"http://localhost:3000/api/v1/orgs"
}


function create_org_repo {
org="$1"
repo="$2"
curl \
-X POST \
-H "Authorization: token $TOKEN_ADMIN" \
-H "Content-type: application/json" \
--data "{\"name\": \"$repo\", \"default_branch\": \"factory\"}" \
"http://localhost:3000/api/v1/orgs/$org/repos"
}


function add_ssh_key {
user="$1"
token="$2"
ssh_key_path="$3"

key="$(cat $ssh_key_path)"
curl \
-X POST \
-H "Authorization: token $token" \
-H "Content-type: application/json" \
--data "{\"key\": \"$key\", \"title\": \"$(echo $key | cut -d ' ' -f 3-)\"}" \
"http://localhost:3000/api/v1/user/keys"
}


create_org pool
create_org_repo pool test-GitPkgA
add_ssh_key admin $TOKEN_ADMIN /root/.ssh/admin.pub
add_ssh_key alice $TOKEN_ALICE /root/.ssh/alice.pub
add_ssh_key bob $TOKEN_BOB /root/.ssh/bob.pub


# create test-GitPkgA package based on test-PkgA
# * change the package name
# * use changelog dates as commit/commiter dates for reproducibility

GITDIR="$(mktemp -d)"
pushd "$GITDIR"

git init --initial-branch factory
# git commiter equals to the configured user
git config user.name "Geeko Packager"
git config user.email "[email protected]"

cp -a "$TOPDIR"/fixtures/pac/test-pkgA-1.spec test-GitPkgA.spec
cp -a "$TOPDIR"/fixtures/pac/test-pkgA-1.changes test-GitPkgA.changes
sed 's@test-pkgA@test-GitPkgA@' -i *
git add *
DATE="2022-01-03 11:22:33 UTC"
GIT_COMMITTER_DATE="$DATE" git commit -a -m "Initial commit" --date "$DATE"

cp -a "$TOPDIR"/fixtures/pac/test-pkgA-2.spec test-GitPkgA.spec
cp -a "$TOPDIR"/fixtures/pac/test-pkgA-2.changes test-GitPkgA.changes
sed 's@test-pkgA@test-GitPkgA@' -i *
git add *
DATE="2022-01-04 11:22:33 UTC"
GIT_COMMITTER_DATE="$DATE" git commit -a -m "Version 2" --date "$DATE"

cp -a "$TOPDIR"/fixtures/pac/test-pkgA-3.spec test-GitPkgA.spec
cp -a "$TOPDIR"/fixtures/pac/test-pkgA-3.changes test-GitPkgA.changes
sed 's@test-pkgA@test-GitPkgA@' -i *
git add *
DATE="2022-01-05 11:22:33 UTC"
GIT_COMMITTER_DATE="$DATE" git commit -a -m "Version 3" --date "$DATE"

git remote add origin http://admin:opensuse@localhost:3000/pool/test-GitPkgA.git
git push --set-upstream origin factory

popd

# create test-GitPkgA package in test:factory that has scmsync set to gitea
$OSC api -X PUT '/source/test:factory/test-GitPkgA/_meta' --file "$TOPDIR"/fixtures/pac/test-GitPkgA.xml
5 changes: 5 additions & 0 deletions behave/container-files/opt/setup/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ sed -i 's!^<VirtualHost \*:82>!<VirtualHost *:82 *:1082>!' /etc/apache2/vhosts.d
sed -i 's!^<VirtualHost \*:443>!<VirtualHost *:443 *:1443>!' /etc/apache2/vhosts.d/obs.conf
sed -i 's!^Listen 82$!Listen 82\nListen 1082\nListen 1443!' /etc/apache2/vhosts.d/obs.conf

# forward X-Username HTTP header to HTTP_X_USERNAME variable that is needed for OBS proxy auth
sed -i 's@^\(.*SSLEngine.*\)@ # the variable is used when OBS proxy auth is on\n SetEnvIf X-Username "(.*)" HTTP_X_USERNAME=$1\n\n\1@' /etc/apache2/vhosts.d/obs.conf


# enable apache mods
APACHE_MODS="passenger rewrite proxy proxy_http xforward headers ssl socache_shmcb"
Expand Down Expand Up @@ -144,3 +147,5 @@ sed -i '/^BindsTo *=.*/d; s/^WantedBy *=.*/WantedBy = default.target/' /usr/lib/

# OBS worker
# systemctl enable obsworker

systemctl enable obs-configure-from-env
7 changes: 7 additions & 0 deletions behave/container-files/root/.ssh/admin
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACBqN71PEkPcdLHUG+oMKpAmnDAgdcuS4V3hEyRWaVf+iQAAAJh1HuOIdR7j
iAAAAAtzc2gtZWQyNTUxOQAAACBqN71PEkPcdLHUG+oMKpAmnDAgdcuS4V3hEyRWaVf+iQ
AAAEAqkTwb9syNHFuOFoy/UJgZGoHHX7zSMx7X10GmhgeIBGo3vU8SQ9x0sdQb6gwqkCac
MCB1y5LhXeETJFZpV/6JAAAAEGFkbWluQGdpdGVhLXRlc3QBAgMEBQ==
-----END OPENSSH PRIVATE KEY-----
1 change: 1 addition & 0 deletions behave/container-files/root/.ssh/admin.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGo3vU8SQ9x0sdQb6gwqkCacMCB1y5LhXeETJFZpV/6J admin@gitea-test
7 changes: 7 additions & 0 deletions behave/container-files/root/.ssh/alice
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACBjDWj0eVxs8MzCGGrlEVekrafN+SaLymKSRHkeHefmFAAAAJhrDGRKawxk
SgAAAAtzc2gtZWQyNTUxOQAAACBjDWj0eVxs8MzCGGrlEVekrafN+SaLymKSRHkeHefmFA
AAAEDAwkWtXW0L35H8L4Can51HQh8JReoINka/SbeHflG2+mMNaPR5XGzwzMIYauURV6St
p835JovKYpJEeR4d5+YUAAAAEGFsaWNlQGdpdGVhLXRlc3QBAgMEBQ==
-----END OPENSSH PRIVATE KEY-----
1 change: 1 addition & 0 deletions behave/container-files/root/.ssh/alice.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGMNaPR5XGzwzMIYauURV6Stp835JovKYpJEeR4d5+YU alice@gitea-test
7 changes: 7 additions & 0 deletions behave/container-files/root/.ssh/bob
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACCB3UPX7odgSnmMTKiD3SFI7U6DF7QU5RXoLAwDz0zPKAAAAJhCzJXDQsyV
wwAAAAtzc2gtZWQyNTUxOQAAACCB3UPX7odgSnmMTKiD3SFI7U6DF7QU5RXoLAwDz0zPKA
AAAECYDWlFAzIcdQsOV07CJXRYWoynJSNUsPzvGZDk4/tX+4HdQ9fuh2BKeYxMqIPdIUjt
ToMXtBTlFegsDAPPTM8oAAAADmJvYkBnaXRlYS10ZXN0AQIDBAUGBw==
-----END OPENSSH PRIVATE KEY-----
1 change: 1 addition & 0 deletions behave/container-files/root/.ssh/bob.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIHdQ9fuh2BKeYxMqIPdIUjtToMXtBTlFegsDAPPTM8o bob@gitea-test
11 changes: 11 additions & 0 deletions behave/container-files/usr/bin/gitea-configure-from-env
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

CONF='/etc/gitea/conf/app.ini'

if [ -n "${GITEA_SERVER_HTTP_PORT}" ]; then
sed -i "s@^HTTP_PORT =.*@HTTP_PORT = ${GITEA_SERVER_HTTP_PORT}@" "${CONF}"
fi

if [ -n "${GITEA_SERVER_SSH_PORT}" ]; then
sed -i "s@^SSH_PORT =.*@SSH_PORT = ${GITEA_SERVER_SSH_PORT}@" "${CONF}"
fi
13 changes: 13 additions & 0 deletions behave/container-files/usr/bin/obs-configure-from-env
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

CONF='/srv/www/obs/api/config/options.yml'

if [ "${OBS_PROXY_AUTH}" -eq 1 ]; then
# enable proxy auth in OBS if $OBS_PROXY_AUTH is set to 1
sed -i 's@^[ #]*proxy_auth_mode: .*@ proxy_auth_mode: :on@' "${CONF}"
sed -i 's@^[ #]*proxy_auth_login_page: .*@ proxy_auth_login_page: https://my-idp.example.com/login@' "${CONF}"
sed -i 's@^[ #]*proxy_auth_logout_page: .*@ proxy_auth_logout_page: https://my-idp.example.com/logout@' "${CONF}"

# if this is not set, any changes to the tokens fail
sed -i 's@^[ #]*proxy_auth_account_page: .*@ proxy_auth_account_page: https://my-idp.example.com/myaccount@' "${CONF}"
fi
3 changes: 3 additions & 0 deletions behave/container-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ podman run \
--cap-add SYS_PTRACE \
-p 1443:443 \
-p 1082:82 \
-p 3000:3000 \
-p 3001:3000 \
-p 3022:3022 \
obs-server

sleep 0.5
Expand Down
4 changes: 3 additions & 1 deletion behave/features/add.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Scenario: Run `osc add` on a new file in a package
Given I set working directory to "{context.osc.temp}"
And I execute osc with args "checkout test:factory test-pkgA"
And I set working directory to "{context.osc.temp}/test:factory/test-pkgA"
And I copy file "{context.fixtures}/pac/test-pkgA-1.spec" to "{context.osc.temp}/test:factory/test-pkgA/new_file"
And I create file "{context.osc.temp}/test:factory/test-pkgA/new_file" with perms "0644"
"""
"""
And I execute osc with args "status --verbose"
And stdout is
"""
Expand Down
12 changes: 9 additions & 3 deletions behave/features/createrequest.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ Background:

@destructive
Scenario: Run `osc createrequest`
When I copy file "{context.fixtures}/pac/test-pkgA-1.spec" to "{context.osc.temp}/home:Admin:branches:test:devel/test-pkgA/new_file"
When I create file "{context.osc.temp}/home:Admin:branches:test:devel/test-pkgA/new_file" with perms "0644"
"""
"""
And I execute osc with args "add new_file"
And I execute osc with args "ci -m 'commit description'"
And I execute osc with args "createrequest -a submit -m 'request description'"
Expand All @@ -21,14 +23,18 @@ Scenario: Run `osc createrequest`

@destructive
Scenario: Run `osc createrequest --supersede`
Given I copy file "{context.fixtures}/pac/test-pkgA-1.spec" to "{context.osc.temp}/home:Admin:branches:test:devel/test-pkgA/new_file"
Given I create file "{context.osc.temp}/home:Admin:branches:test:devel/test-pkgA/new_file" with perms "0644"
"""
"""
And I execute osc with args "add new_file"
And I execute osc with args "ci -m 'commit description'"
And I execute osc with args "createrequest -a submit -m 'request description'"
And the exit code is 0
And I execute osc with args "api /request/1"
And stdout doesn't contain "<state name=\"superseded\">"
When I copy file "{context.fixtures}/pac/test-pkgA-1.spec" to "{context.osc.temp}/home:Admin:branches:test:devel/test-pkgA/another_file"
When I create file "{context.osc.temp}/home:Admin:branches:test:devel/test-pkgA/another_file" with perms "0644"
"""
"""
And I execute osc with args "add new_file"
And I execute osc with args "ci -m 'commit description'"
And I execute osc with args "createrequest -a submit -m 'request description' --supersede 1"
Expand Down
Loading

0 comments on commit a9b0b16

Please sign in to comment.