Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason0729 committed Jan 6, 2024
1 parent 34d8cec commit fc58808
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 166 deletions.
149 changes: 93 additions & 56 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion backend/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ entity-codegen:

release-docker:
just prepare
cp ../Cargo.toml ws-Cargo.toml
cp ../proto/*.proto .
docker build . --build-arg ARCH=$(uname -m) -t mdoj-backend
sudo docker build . --build-arg ARCH=$(uname -m) -t mdoj-backend

run:
cargo run
Expand Down
10 changes: 5 additions & 5 deletions judger/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ release-docker:
cd plugins/rlua-54 && sh ./build.sh
cp ../proto/judger.proto .
cp ../Cargo.toml ws-Cargo.toml
docker build . --build-arg ARCH=$(uname -m) -t mdoj-judger
sudo docker build . --build-arg ARCH=$(uname -m) -t mdoj-judger

build-plugin:
cd plugins && sh build-all.sh
Expand All @@ -27,10 +27,10 @@ feodra-deps:
clean:
sudo rm -rf .temp
cargo clean
docker images rm nsjail-3.1-$(uname -m)-linux-musl
docker images rm protobuf-3.21.1-$(uname -m)-linux-musl
docker images rm libnl-3.2.25-$(uname -m)-linux-musl
docker images rm musl-cross-make-$(uname -m)-linux-musl
sudo docker images rm nsjail-3.1-$(uname -m)-linux-musl
sudo docker images rm protobuf-3.21.1-$(uname -m)-linux-musl
sudo docker images rm libnl-3.2.25-$(uname -m)-linux-musl
sudo docker images rm musl-cross-make-$(uname -m)-linux-musl

test:
sudo rm -rf .temp/*
Expand Down
2 changes: 2 additions & 0 deletions testsuit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ tokio-rustls = "0.24.1"
rustls-pki-types = "1.0.1"
rustls = "0.21.9"
http-body = "0.4.6"
termion = "3.0.0"
thiserror = "1.0.56"

[dependencies.tokio-stream]
version = "0.1.14"
Expand Down
58 changes: 15 additions & 43 deletions testsuit/src/case.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,22 @@
use tonic::async_trait;

#[async_trait]
pub trait Case<S: Send + Sync> {
const NAME: &'static str;

async fn run(&self, state: &mut S) -> Result<(), String>;
#[derive(Debug,thiserror::Error)]
pub enum Error{
#[error("unreachable")]
Unreachable
}

#[async_trait]
trait Runable<S: Send + Sync> {
async fn run(&self, state: &mut S) -> Result<(), String>;
fn name(&self) -> &'static str;
}
// pub struct StackedTestcase<O> where Self:Sized{
// next:Box<dyn Testcase<O>>
// }

#[async_trait]
impl<S: Send + Sync, Rhs: Case<S> + Send + Sync + 'static> Runable<S> for Rhs {
async fn run(&self, state: &mut S) -> Result<(), String> {
<Self as Case<S>>::run(self, state).await
}
// impl<I, O> Testcase<I> for StackedTestcase<O> {

fn name(&self) -> &'static str {
<Self as Case<S>>::NAME
}
}
// }
// #[async_trait]
// pub trait Testcase<I,O>{
// fn run_inner(&self,state:I)->Result<I,Error>{

#[derive(Default)]
pub struct CaseRunner<S: Send + Sync + Default> {
case: Vec<Box<dyn Runable<S> + Send + Sync + 'static>>,
state: S,
}

impl<S: Send + Sync + Default> CaseRunner<S> {
pub fn add_case<Rhs: Case<S> + Send + Sync + 'static>(&mut self, case: Rhs) {
self.case.push(Box::new(case));
}
pub async fn run(mut self, title: &'static str) -> S {
log::info!("Start testsuit {}", title);
for (i, case) in self.case.into_iter().enumerate() {
log::info!("Running case {} {}", i, case.name());
if let Err(err) = case.run(&mut self.state).await {
log::error!("test fail: {}", err);
break;
}
}
log::info!("End");

self.state
}
}
// }
// fn stack(self,state:I,next:Box<dyn Testcase<O>>)->Result<O,Error>where O:From<I>;
// }
8 changes: 4 additions & 4 deletions testsuit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ pub mod user;

#[tokio::main(flavor = "current_thread")]
async fn main() {
env_logger::builder()
.filter_module("testsuit", log::LevelFilter::Trace)
.init();
// env_logger::builder()
// .filter_module("testsuit", log::LevelFilter::Trace)
// .init();

user::run().await;
// user::run().await;
}
92 changes: 46 additions & 46 deletions testsuit/src/user/login.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
use crate::{
case::Case,
client::connection,
constant,
grpc::backend::{token_set_client::TokenSetClient, LoginRequest},
};

use super::State;

pub struct AdminLogin;

#[tonic::async_trait]
impl Case<State> for AdminLogin {
const NAME: &'static str = "login as admin@admin";

async fn run(&self, state: &mut State) -> Result<(), String> {
let mut client =
TokenSetClient::with_origin(connection(), constant::SERVER.try_into().unwrap());

let res = client
.create(LoginRequest {
username: constant::ADMIN.to_string(),
password: constant::ADMIN_PWD.to_string(),
expiry: None,
})
.await
.unwrap();

let res = res.into_inner();

assert!(res.permission.can_root);
assert!(!res.permission.can_link);
assert!(!res.permission.can_manage_announcement);
assert!(!res.permission.can_manage_chat);
assert!(!res.permission.can_manage_contest);
assert!(!res.permission.can_manage_education);
assert!(!res.permission.can_manage_problem);
assert!(!res.permission.can_manage_submit);
assert!(!res.permission.can_manage_user);
assert!(!res.permission.can_publish);
assert!(!res.permission.can_imgur);

state.token = Some(res.token.signature);
Ok(())
}
}
// use crate::{
// case::Case,
// client::connection,
// constant,
// grpc::backend::{token_set_client::TokenSetClient, LoginRequest},
// };

// use super::State;

// pub struct AdminLogin;

// #[tonic::async_trait]
// impl Case<State> for AdminLogin {
// const NAME: &'static str = "login as admin@admin";

// async fn run(&self, state: &mut State) -> Result<(), String> {
// let mut client =
// TokenSetClient::with_origin(connection(), constant::SERVER.try_into().unwrap());

// let res = client
// .create(LoginRequest {
// username: constant::ADMIN.to_string(),
// password: constant::ADMIN_PWD.to_string(),
// expiry: None,
// })
// .await
// .unwrap();

// let res = res.into_inner();

// assert!(res.permission.can_root);
// assert!(!res.permission.can_link);
// assert!(!res.permission.can_manage_announcement);
// assert!(!res.permission.can_manage_chat);
// assert!(!res.permission.can_manage_contest);
// assert!(!res.permission.can_manage_education);
// assert!(!res.permission.can_manage_problem);
// assert!(!res.permission.can_manage_submit);
// assert!(!res.permission.can_manage_user);
// assert!(!res.permission.can_publish);
// assert!(!res.permission.can_imgur);

// state.token = Some(res.token.signature);
// Ok(())
// }
// }
22 changes: 11 additions & 11 deletions testsuit/src/user/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
mod login;
use crate::{case::CaseRunner, user::login::AdminLogin};
// mod login;
// use crate::{case::CaseRunner, user::login::AdminLogin};

#[derive(Default)]
pub struct State {
token: Option<String>,
}
// #[derive(Default)]
// pub struct State {
// token: Option<String>,
// }

pub async fn run() -> State {
let mut user_runner = CaseRunner::<State>::default();
// pub async fn run() -> State {
// let mut user_runner = CaseRunner::<State>::default();

user_runner.add_case(AdminLogin);
// user_runner.add_case(AdminLogin);

user_runner.run("user").await
}
// user_runner.run("user").await
// }

0 comments on commit fc58808

Please sign in to comment.