Skip to content

Commit

Permalink
Add smoke test
Browse files Browse the repository at this point in the history
  • Loading branch information
w4 committed Nov 14, 2023
1 parent 6a50d80 commit f769041
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 0 deletions.
77 changes: 77 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
on: [push, pull_request]

name: Test

jobs:
smoke:
name: Smoke Test
runs-on: ubuntu-latest
services:
gitlab:
image: gitlab/gitlab-ee:latest
options: --shm-size 256m
ports:
- 80:80
- 443:443
steps:
- uses: actions/checkout@v4
- name: Start gitlab-cargo-shim
run: |
docker build . -t gitlab-cargo-shim
docker run --detach \
--name gitlab-cargo-shim \
--mount type=bind,source=$(pwd)/test/config.toml,target=/app/config.toml \
--network host \
gitlab-cargo-shim \
-c /app/config.toml
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y python3-pip jq openssh-client
pip3 install requests beautifulsoup4 html5lib
- name: Wait for GitLab to boot
run: timeout 20m bash -c 'until curl -s http://127.0.0.1/users/sign_in | grep csrf-token; do sleep 5; done'
- name: Create GitLab package
run: |
export GITLAB_CONTAINER=$(docker ps --format "{{.ID}}" --no-trunc --filter "ancestor=gitlab/gitlab-ee:latest")
export ROOT_PASSWORD=$(docker exec $GITLAB_CONTAINER grep 'Password:' /etc/gitlab/initial_root_password | sed 's/Password: //')
export ROOT_PAT=$(python3 ./test/create_pat.py)
curl -s --request POST --header "PRIVATE-TOKEN: $ROOT_PAT" --header "Content-Type: application/json" \
--data '{"name": "example-lib"}' \
--url 'http://127.0.0.1/api/v4/projects/'
- name: Packaging example-lib
run: |
cd test/example-lib
cargo package
cargo metadata --format-version 1 > metadata.json
- name: Uploading example-lib to GitLab
run: |
curl --header "PRIVATE-TOKEN: $ROOT_PAT" --upload-file test/example-lib/target/package/example-lib-0.1.0.crate http://127.0.0.1/api/v4/projects/root%2Fexample-lib/packages/generic/example-lib/0.1.0/example-lib-0.1.0.crate
curl --header "PRIVATE-TOKEN: $ROOT_PAT" --upload-file test/example-lib/metadata.json http://127.0.0.1/api/v4/projects/root%2Fexample-lib/packages/generic/example-lib/0.1.0/metadata.json
- name: Creating SSH key to identify with gitlab-cargo-shim
run: |
ssh-keygen -t ed25519 -C testkey -N '' -f ~/.ssh/id_ed25519
- name: Fetching public keys from gitlab-cargo-shim and storing in known_hosts
run: |
ssh-keyscan -p 2233 127.0.0.1 > ~/.ssh/known_hosts
- name: Write PAT to .config
run: |
echo -e "Host *\n User personal-token:$ROOT_PAT" > ~/.ssh/config
- name: Building example-bin using example-lib from registry
run: |
cd test/example-bin
CARGO_NET_GIT_FETCH_WITH_CLI=true cargo check
- name: Collect docker logs on failure
if: failure()
uses: jwalton/gh-docker-logs@v2
with:
dest: './logs'
- name: Tar logs
if: failure()
run: tar cvzf ./logs.tgz ./logs
- name: Upload logs as artifacts
if: failure()
uses: actions/upload-artifact@master
with:
name: logs.tgz
path: ./logs.tgz
3 changes: 3 additions & 0 deletions src/providers/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ impl super::UserProvider for Gitlab {
return Ok(None);
};

eprintln!("username={username}");
eprintln!("password={password}");

if username == "gitlab-ci-token" || username == "personal-token" {
// we're purposely not using `self.client` here as we don't
// want to use our admin token for this request but still want to use any ssl cert provided.
Expand Down
5 changes: 5 additions & 0 deletions test/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
listen-address = "0.0.0.0:2233"
state-directory = "/tmp/state"

[gitlab]
uri = "http://127.0.0.1"
65 changes: 65 additions & 0 deletions test/create_pat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Creates a personal access token using the GitLab UI, given only a username + password combination. This is required
# when using the spawned instance of GitLab in smoke tests, as we only get back a password from the logs, and there's
# no way for us to authenticate with the GitLab API using just a password.
#
# Adapted from https://gist.github.com/gpocentek/bd4c3fbf8a6ce226ebddc4aad6b46c0a

import re
import sys
import os
import requests
import bs4

BASE_URL = "http://127.0.0.1"
SIGN_IN_URL = BASE_URL + "/users/sign_in"
PAT_URL = BASE_URL + "/-/profile/personal_access_tokens"

session = requests.Session()

# fetch CSRF for sign-in page
print('Fetching CSRF token from sign in page', file=sys.stderr)
sign_in_page = session.get(SIGN_IN_URL)
root = bs4.BeautifulSoup(sign_in_page.text, "html5lib")
csrf = root.find_all("meta", attrs={'name': 'csrf-token'})[0]['content']

if not csrf:
print('Unable to find csrf token on sign in page', file=sys.stderr)
sys.exit(1)

# login to gitlab using the ROOT_PASSWORD env var, storing the session token in our session object
sign_in_res = session.post(SIGN_IN_URL, data={
'user[login]': 'root',
'user[password]': os.environ['ROOT_PASSWORD'].strip(),
'authenticity_token': csrf,
})

if sign_in_res.status_code != 200:
print('Failed to login to GitLab instance', file=sys.stderr)
sys.exit(1)

print('Successfully logged into GitLab, fetching CSRF token for PAT page', file=sys.stderr)

# fetch the csrf token for PAT creation
pat_page = session.get(PAT_URL)
root = bs4.BeautifulSoup(pat_page.text, "html5lib")
csrf = root.find_all("meta", attrs={'name': 'csrf-token'})[0]['content']

if not csrf:
print('Unable to find csrf token on PAT creation page', file=sys.stderr)
sys.exit(1)

print('Found CSRF token, creating PAT', file=sys.stderr)

# create the personal access token
response = session.post(PAT_URL, data={
"personal_access_token[name]": "apitoken",
"personal_access_token[scopes][]": "api",
"authenticity_token": csrf,
})

personal_access_token = response.json()['new_token']
if not personal_access_token:
print('Failed to find personal access token in response', file=sys.stderr)
sys.exit(1)

print(personal_access_token)
2 changes: 2 additions & 0 deletions test/example-bin/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[registries]
my-group-example-lib = { index = "ssh://127.0.0.1:2233/root/example-lib" }
2 changes: 2 additions & 0 deletions test/example-bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
Cargo.lock
9 changes: 9 additions & 0 deletions test/example-bin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "example-bin"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
example-lib = { version = "0.1", registry = "my-group-example-lib" }
1 change: 1 addition & 0 deletions test/example-bin/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This crate is built in CI to ensure example-lib can be fetched via gitlab-cargo-shim
3 changes: 3 additions & 0 deletions test/example-bin/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
eprintln!("{}", example_lib::add(2, 3));
}
2 changes: 2 additions & 0 deletions test/example-lib/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
Cargo.lock
8 changes: 8 additions & 0 deletions test/example-lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "example-lib"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
2 changes: 2 additions & 0 deletions test/example-lib/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This crate is published to the created GitLab instance, and fetched
by example-bin.
14 changes: 14 additions & 0 deletions test/example-lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}

0 comments on commit f769041

Please sign in to comment.