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 integration testsuite #1678

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ rand = "0.8.5"
ignore = "0.4.18"
postgres-types = { version = "0.2.4", features = ["derive"] }
cron = { version = "0.12.0" }
bytes = "1.1.0"

[dependencies.serde]
version = "1"
Expand Down
13 changes: 10 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ RUN apt-get update -y && \
pkg-config \
git \
cmake \
zlib1g-dev
zlib1g-dev \
postgresql

# postgres does not allow running as root
RUN groupadd -r builder && useradd -m -r -g builder builder
USER builder

RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- \
--default-toolchain stable --profile minimal -y

COPY . .
COPY --chown=builder . /triagebot
WORKDIR /triagebot

RUN bash -c 'source $HOME/.cargo/env && cargo test --release --all'
RUN bash -c 'source $HOME/.cargo/env && cargo build --release'

Expand All @@ -38,7 +45,7 @@ RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \

RUN mkdir -p /opt/triagebot

COPY --from=build /target/release/triagebot /usr/local/bin/
COPY --from=build /triagebot/target/release/triagebot /usr/local/bin/
COPY templates /opt/triagebot/templates
WORKDIR /opt/triagebot
ENV PORT=80
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ The general overview of what you will need to do:
Or, you can use a service such as https://ngrok.com/ to access on your local dev machine via localhost.
Installation is fairly simple, though requires setting up a (free) account.
Run the command `ngrok http 8000` to forward to port 8000 on localhost.

> Note: GitHub has a webhook forwarding service available in beta.
> See [cli/gh-webhook](https://docs.github.com/en/developers/webhooks-and-events/webhooks/receiving-webhooks-with-the-github-cli) for more information.
> This is super easy to use, and doesn't require manually configuring webhook settings.
> The command to run looks something like:
>
> ```sh
> gh webhook forward --repo=ehuss/triagebot-test --events=* \
> --url=http://127.0.0.1:8000/github-hook --secret somelongsekrit
> ```
>
> Where the value in `--secret` is the secret value you place in `GITHUB_WEBHOOK_SECRET` described below, and `--repo` is the repo you want to test against.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know what permissions are necessary for this? It seems like it might be a convenient way to test against actual GitHub from CI (too), but it's not clear whether the permissions model really makes that easy.

For CI at least we could probably set up our own forwarding if needed though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, the default token generated by gh auth login will have full permissions. gh webhook just needs the ability to alter webhook settings on the repo, which that token should have. At least in my testing, it "just worked" without me needing to do anything special (other than signing up for the beta).

I'm not sure it would really be feasible to set it up on CI. I think it would need to start by creating a bot account, and manually creating a token with a limited scope to a single "test" repo, and add that token to the workflow secrets. But then I think PR authors would then have full write access to that repo, which could be dangerous.

I think it would be challenging to do in a secure way, but I haven't thought about it much. I don't know when it will be out of beta, either.


4. Create a GitHub repo to run some tests on.
5. Configure the webhook in your GitHub repo.
I recommend at least skimming the [GitHub webhook documentation](https://docs.github.com/en/developers/webhooks-and-events/webhooks/about-webhooks) if you are not familiar with webhooks. In short:
Expand All @@ -67,6 +80,17 @@ The general overview of what you will need to do:
8. Add a `triagebot.toml` file to the main branch of your GitHub repo with whichever services you want to try out.
9. Try interacting with your repo, such as issuing `@rustbot` commands or interacting with PRs and issues (depending on which services you enabled in `triagebot.toml`). Watch the logs from the server to see what's going on.

## Tests

When possible, writing unittests is very helpful and one of the easiest ways to test.
For more advanced testing, there is an integration test called `testsuite` which provides an end-to-end service for testing triagebot.
There are two parts to it:

* [`github_client`](tests/github_client/mod.rs) — Tests specifically targeting `GithubClient`.
This sets up an HTTP server that mimics api.github.com and verifies the client's behavior.
* [`server_test`](tests/server_test/mod.rs) — This tests the `triagebot` server itself and its behavior when it receives a webhook.
This launches the `triagebot` server, sets up HTTP servers to intercept api.github.com requests, launches PostgreSQL in a sandbox, and then injects webhook events into the `triagebot` server and validates its response.

## License

Triagebot is distributed under the terms of both the MIT license and the
Expand Down
3 changes: 1 addition & 2 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::collections::HashMap;
use std::sync::Arc;

use async_trait::async_trait;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use tera::{Context, Tera};

Expand Down Expand Up @@ -87,7 +86,7 @@ pub fn to_human(d: DateTime<Utc>) -> String {
#[async_trait]
impl<'a> Action for Step<'a> {
async fn call(&self) -> anyhow::Result<String> {
let gh = GithubClient::new_with_default_token(Client::new());
let gh = GithubClient::new_from_env();

// retrieve all Rust compiler meetings
// from today for 7 days
Expand Down
9 changes: 6 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,13 @@ impl fmt::Display for ConfigurationError {
Add a `triagebot.toml` in the root of the default branch to enable it."
),
ConfigurationError::Toml(e) => {
write!(f, "Malformed `triagebot.toml` in default branch.\n{}", e)
write!(f, "Malformed `triagebot.toml` in default branch.\n{e}")
}
ConfigurationError::Http(_) => {
write!(f, "Failed to query configuration for this repository.")
ConfigurationError::Http(e) => {
write!(
f,
"Failed to query configuration for this repository.\n{e:?}"
)
}
}
}
Expand Down
Loading