Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add end-to-end test #67

Merged
merged 2 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ jobs:
with:
command: test

end-to-end:
name: End-to-end upload test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Start containers
working-directory: ./tests
run: docker-compose up -d
jameswestman marked this conversation as resolved.
Show resolved Hide resolved

- name: Wait for the containers to finish loading
run: sleep 10

- name: Run test
run: docker exec flat-manager ./tests/run-test.py
jameswestman marked this conversation as resolved.
Show resolved Hide resolved

fmt:
name: Rustfmt
runs-on: ubuntu-latest
Expand Down
29 changes: 29 additions & 0 deletions tests/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM rust:latest
jameswestman marked this conversation as resolved.
Show resolved Hide resolved

# Install run-tests.py's dependencies
RUN apt-get update
RUN apt-get install -y flatpak flatpak-builder python3-pip python3-gi libostree-dev
RUN flatpak remote-add flathub https://flathub.org/repo/flathub.flatpakrepo
RUN flatpak install -y flathub org.freedesktop.Platform//21.08 org.freedesktop.Sdk//21.08
RUN pip install asyncio aiohttp tenacity pygobject

# Use the test config.json and configure it with a GPG key
COPY ./tests/config.json ./test-config.json
COPY ./tests/gen-key.sh ./gen-key.sh
RUN ./gen-key.sh

# Initialize the OSTree repo
RUN ostree --repo=repo init --mode=archive-z2
RUN mkdir build-repo

# Copy the files
COPY ./ ./

# Make sure our test config.json is used, not one that was in the source directory
RUN cp --force ./test-config.json ./config.json

# Build flat-manager
RUN cargo build

# Wait 5 seconds for the database to start, then run flat-manager
CMD ["sh", "-c", "sleep 5 ; ./target/debug/flat-manager"]
4 changes: 4 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# flat-manager CI

This test suite uses docker-compose to bring up a real flat-manager instance, running with a real database, to run
tests against.
30 changes: 30 additions & 0 deletions tests/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"repos": {
"stable": {
"path": "repo",
"collection-id": "org.test.Stable",
"suggested-repo-name": "flat-manager-test",
"runtime-repo-url": "https://dl.flathub.org/repo/flathub.flatpakrepo",
"gpg-key": "@GPG_KEY@",
"base-url": null,
"subsets": {
"all": {
"collection-id": "org.test.Stable",
"base-url": null
},
"free": {
"collection-id": "org.test.Stable.test",
"base-url": null
}
}
}
},
"port": 8080,
"delay-update-secs": 3,
"database-url": "postgres://postgres:postgres@db/test_db",
"build-repo-base": "build-repo",
"build-gpg-key": null,
"gpg-homedir": null,
"secret": "c2VjcmV0Cg==",
"repo-secret": "c2VjcmV0Cg=="
}
22 changes: 22 additions & 0 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: "3"

services:
flat-manager:
privileged: true # needed for flatpak-builder to work
container_name: flat-manager
build:
dockerfile: tests/Dockerfile
context: ..
ports:
- "8080:8080"
depends_on:
- db

db:
image: docker.io/library/postgres:12
ports:
- "5432:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=test_db
13 changes: 13 additions & 0 deletions tests/gen-key.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

# Generate a GPG key
gpg --quick-gen-key --batch --yes --passphrase="" "flat-manager CI"

# Find the key
KEY=$(gpg --list-signatures --with-colons | grep "sig" | grep "flat-manager CI" | head -n 1 | awk -F":" '{print $5}')

# Export the key so we can import it into flatpak
gpg --export --armor > key.gpg

# Set the key in the config file
sed --in-place "s/@GPG_KEY@/$KEY/g" test-config.json
2 changes: 2 additions & 0 deletions tests/hello.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
echo "Hello, world!"
19 changes: 19 additions & 0 deletions tests/org.flatpak.FlatManagerCI.metainfo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<component type="desktop-application">
<id>org.flatpak.FlatManagerCI</id>
<name>flat-manager CI</name>
<metadata_license>CC0-1.0</metadata_license>
<project_license>CC-BY-4.0</project_license>
<url type="homepage">https://flatpak.org</url>
<summary>Test app for flat-manager's CI</summary>
<description><p>Test app for flat-manager's CI</p></description>
<screenshots>
<screenshot type="default">
<image type="source" width="800" height="600">https://placekitten.com/800/600</image>
</screenshot>
</screenshots>
<releases>
<release timestamp="2022-03-01" version="0.0.1"/>
</releases>
<content_rating></content_rating>
</component>
16 changes: 16 additions & 0 deletions tests/org.flatpak.FlatManagerCI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
app-id: org.flatpak.FlatManagerCI
runtime: org.freedesktop.Platform
runtime-version: '21.08'
sdk: org.freedesktop.Sdk
command: hello.sh
modules:
- name: hello
buildsystem: simple
build-commands:
- install -D hello.sh /app/bin/hello.sh
- install -D org.flatpak.FlatManagerCI.metainfo.xml /app/share/metainfo/org.flatpak.FlatManagerCI.metainfo.xml
sources:
- type: file
path: hello.sh
- type: file
path: org.flatpak.FlatManagerCI.metainfo.xml
48 changes: 48 additions & 0 deletions tests/run-test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3

import os, subprocess, sys, time
from subprocess import PIPE

def sleep(seconds):
print(f"Waiting {seconds} seconds")
time.sleep(seconds)

def exec(cmd):
print("Executing", cmd)

p = subprocess.run(cmd, stdout=PIPE, stderr=sys.stderr)

if p.returncode != 0:
raise AssertionError(f"Command `{cmd}` failed with exit code {p.returncode}")

return p.stdout.decode().strip()

REPO_DIR = "_repo"

# Build the flatpak app
exec(["flatpak-builder", "--force-clean", "--repo", REPO_DIR, "_flatpak", "tests/org.flatpak.FlatManagerCI.yml"])

# Generate a flat-manager token
os.environ["REPO_TOKEN"] = exec(["cargo", "run", "--bin=gentoken", "--", "--secret=secret", "--repo=stable"])

# Create a new build and save the repo URL
build_repo = exec(["./flat-manager-client", "create", "http://127.0.0.1:8080", "stable"])

# Push to the upload repo
exec(["./flat-manager-client", "push", build_repo, REPO_DIR])

# Commit to the build repo
exec(["./flat-manager-client", "commit", build_repo])

# Wait for that job to finish
sleep(10)

# Publish to the main repo
exec(["./flat-manager-client", "publish", build_repo])

# Wait for the repository to be updated
sleep(15)

# Make sure the app installs successfully
exec(["flatpak", "remote-add", "flat-manager", "http://127.0.0.1:8080/repo/stable", "--gpg-import=key.gpg"])
exec(["flatpak", "install", "-y", "flat-manager", "org.flatpak.FlatManagerCI"])