Skip to content

Commit

Permalink
Merge pull request #238 from canonical/integration-tests
Browse files Browse the repository at this point in the history
Setup environment for running integration tests
  • Loading branch information
nadzyah authored Nov 20, 2024
2 parents 5554e06 + cc0d735 commit 7e4157f
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 19 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/run_integration_tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Run integration tests
on:
push:
branches: [ main ]
paths:
- server/**
- client/**
- tests/**
- ".github/workflows/run_integration_tests.yaml"
pull_request:
branches: [ main ]
paths:
- server/**
- client/**
- tests/**
- ".github/workflows/run_integration_tests.yaml"
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
runs-on: [self-hosted, linux, large, jammy, x64]
defaults:
run:
working-directory: tests
steps:
- uses: actions/checkout@v4
- name: Set up docker buildx
uses: docker/setup-buildx-action@v3
with:
config-inline: |
[registry."docker.io"]
mirrors = ["https://github-runner-dockerhub-cache.canonical.com:5000"]
- name: Install docker compose
run: |
sudo apt update
sudo apt install -y docker-compose-v2
- name: Build and run integration tests
run: |
docker compose up --build --abort-on-container-exit
2 changes: 2 additions & 0 deletions .github/workflows/test_client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ on:
paths:
- client/**
- Cargo.*
- ".github/workflows/test_client.yaml"
pull_request:
branches: [ main ]
paths:
- client/**
- Cargo.*
- ".github/workflows/test_client.yaml"
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test_server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ on:
branches: [ main ]
paths:
- server/**
- ".github/workflows/test_server.yaml"
pull_request:
branches: [ main ]
paths:
- server/**
- ".github/workflows/test_server.yaml"
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
Expand Down
32 changes: 16 additions & 16 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion client/hwlib/debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Build-Depends: cargo:native,
Maintainer: Canonical Hardware Certification <[email protected]>
Uploaders: Nadzeya Hutsko <[email protected]>
Standards-Version: 4.6.2
XS-Vendored-Sources-Rust: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]+wasi-snapshot-preview1, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
XS-Vendored-Sources-Rust: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]+wasi-snapshot-preview1, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
Vcs-Git: https://github.com/canonical/hardware-api.git [client/hwlib/src]
Vcs-Browser: https://github.com/canonical/hardware-api/tree/main/client/hwlib/src
Homepage: https://github.com/canonical/hardware-api
Expand Down
4 changes: 2 additions & 2 deletions client/hwlib/src/py_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ fn send_certification_request(py: Python, url: String) -> PyResult<PyObject> {
match response {
Ok(response_value) => {
let json_str = serde_json::json!(response_value).to_string();
let json: PyObject = PyString::new_bound(py, &json_str).into();
let json_module = py.import_bound("json")?;
let json: PyObject = PyString::new(py, &json_str).into();
let json_module = py.import("json")?;
let json_object: PyObject = json_module.call_method1("loads", (json,))?.into();

Ok(json_object)
Expand Down
14 changes: 14 additions & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "integration-tests"
version = "0.1.0"
edition = "2021"

[dependencies]
hwlib = { path = "client/hwlib" }
tokio = { version = "1.0", features = ["full", "test-util"] }
anyhow = "1.0"
simple_test_case = "1.2.0"

[[test]]
name = "integration"
path = "src/integration_test.rs"
22 changes: 22 additions & 0 deletions tests/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM ubuntu:24.04

# Install Rust and required packages
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
curl \
build-essential \
pkgconf \
libssl-dev \
lsb-release \
kmod \
python3 \
python3-pip \
rustup

RUN rustup default stable

WORKDIR /app

COPY Cargo.toml .
COPY src src/

CMD ["cargo", "test", "--test", "integration", "--", "--nocapture"]
14 changes: 14 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Integration tests for the project

This folder contains the tests for the hardware-api project that check
how client and server communicate.

To run them, first install
[docker](https://docs.docker.com/engine/install/ubuntu/) and [setup
permissions](https://docs.docker.com/engine/install/linux-postinstall/).

Then, run the tests with `docker compose`:

```sh
docker compose up --build --abort-on-container-exit
```
22 changes: 22 additions & 0 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
services:
hwapi-server-integration:
build:
context: ../server
dockerfile: Dockerfile
args:
IMPORT_TOOL_PATH: ""
C3_URL: https://c3_url
DB_URL: sqlite:////home/server/test-integration.db
ports:
- "8080:8080"

integration-tests:
build:
context: .
dockerfile: Dockerfile
depends_on:
- hwapi-server-integration
volumes:
- ../client:/app/client
environment:
- API_URL=http://hwapi-server-integration:8080
88 changes: 88 additions & 0 deletions tests/src/integration_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* Copyright 2024 Canonical Ltd.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Written by:
* Nadzeya Hutsko <[email protected]>
*/

use anyhow::Result;
use std::path::PathBuf;
use simple_test_case::test_case;
use hwlib::{
models::request_validators::{CertificationStatusRequest, Paths},
models::response_validators::CertificationStatusResponse,
send_certification_status_request,
};

fn get_test_device_paths(device_type: &str) -> Paths {
let base_path = PathBuf::from("/app/client/hwlib/test_data/amd64").join(device_type);
Paths {
smbios_entry_filepath: base_path.join("smbios_entry_point"),
smbios_table_filepath: base_path.join("DMI"),
cpuinfo_filepath: PathBuf::from("./none"),
max_cpu_frequency_filepath: base_path.join("cpuinfo_max_freq"),
device_tree_dirpath: PathBuf::from("./none"),
proc_version_filepath: base_path.join("version"),
}
}

#[test_case(
"dgx_station";
"dgx_station" // test name
)]
#[test_case(
"dell_xps13";
"dell_xps13"
)]
#[test_case(
"thinkstation_p620";
"thinkstation_p620"
)]
#[tokio::test]
async fn test_certification_request(dir_path: &str) -> Result<()> {
let api_url = match std::env::var("API_URL") {
Ok(url) => url,
Err(..) => panic!("API_URL environment variable must be specified"),
};
let cert_request = CertificationStatusRequest::new(get_test_device_paths(dir_path))?;
let response = send_certification_status_request(api_url, &cert_request).await?;

// Currently all the responses should match the "Not Seen" status, it'll be updated
// once snapshot tests are defined
match response {
CertificationStatusResponse::NotSeen => {
// Here, we don't need any further assertions.
// We simply want to ensure this case is hit.
},
_ => {
panic!("Expected response to be NotSeen, but it was {:?}", response);
},
}

Ok(())
}


#[tokio::test]
async fn test_server_connection_error() -> Result<()> {
let result = send_certification_status_request(
"http://non-existent-server:8080".to_string(),
&CertificationStatusRequest::new(get_test_device_paths("dell_xps13"))?,
).await;

assert!(result.is_err());
Ok(())
}

0 comments on commit 7e4157f

Please sign in to comment.