1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
use axum::{
body::Body,
http::{self, Request, StatusCode},
};
use diesel::{PgConnection, RunQueryDsl};
use planetwars_server::{create_db_pool, create_pw_api, db, modules, GlobalConfig};
use serde_json::{self, json, Value as JsonValue};
use std::{
io,
path::{Path, PathBuf},
sync::Arc,
time::Duration,
};
use tempfile::TempDir;
use tower::Service;
// Used to serialize tests that access the database.
// TODO: see to what degree we could support transactional testing.
static DB_LOCK: parking_lot::Mutex<()> = parking_lot::Mutex::new(());
fn create_subdir<P: AsRef<Path>>(base_path: &Path, p: P) -> io::Result<String> {
let dir_path = base_path.join(p);
std::fs::create_dir(&dir_path)?;
let dir_path_string = dir_path.into_os_string().into_string().unwrap();
Ok(dir_path_string)
}
fn clear_database(conn: &PgConnection) {
diesel::sql_query(
"TRUNCATE TABLE
bots,
bot_versions,
maps,
matches,
match_players,
ratings,
sessions,
users",
)
.execute(conn)
.expect("failed to clear database");
}
#[tokio::test(flavor = "multi_thread")]
async fn test_application() -> io::Result<()> {
let _db_guard = DB_LOCK.lock();
let data_dir = TempDir::new().expect("failed to create temp dir");
let config = Arc::new(GlobalConfig {
database_url: "postgresql://planetwars:planetwars@localhost/planetwars-test".to_string(),
python_runner_image: "python:3.10-slim-buster".to_string(),
container_registry_url: "localhost:9001".to_string(),
root_url: "localhost:3000".to_string(),
bots_directory: create_subdir(data_dir.path(), "bots")?,
match_logs_directory: create_subdir(data_dir.path(), "matches")?,
maps_directory: create_subdir(data_dir.path(), "maps")?,
registry_directory: create_subdir(data_dir.path(), "registry")?,
registry_admin_password: "secret_admin_password".to_string(),
ranker_enabled: false,
});
let db_pool = create_db_pool(&config).await;
{
let db_conn = db_pool.get().await.expect("failed to get db connection");
clear_database(&db_conn);
let bot = db::bots::create_bot(
&db::bots::NewBot {
owner_id: None,
name: "simplebot",
},
&db_conn,
)
.expect("could not create simplebot");
let simplebot_code = std::fs::read_to_string("../simplebot/simplebot.py")
.expect("could not read simplebot code");
let _bot_version =
modules::bots::save_code_string(&simplebot_code, Some(bot.id), &db_conn, &config)
.expect("could not save bot version");
std::fs::copy(
"../maps/hex.json",
PathBuf::from(&config.maps_directory).join("hex.json"),
)
.expect("could not copy map");
db::maps::create_map(
db::maps::NewMap {
name: "hex",
file_path: "hex.json",
},
&db_conn,
)
.expect("could not save map");
}
let mut app = create_pw_api(config, db_pool);
let simplebot_code = std::fs::read_to_string("../simplebot/simplebot.py")
.expect("could not read simplebot code");
let payload = json!({
"code": simplebot_code,
});
let response = app
.call(
Request::builder()
.method(http::Method::POST)
.header("Content-Type", "application/json")
.uri("/api/submit_bot")
.body(serde_json::to_vec(&payload).unwrap().into())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
let resp: JsonValue = serde_json::from_slice(&body).unwrap();
let match_id = &resp["match"]["id"];
let mut num_tries = 0;
loop {
num_tries += 1;
assert!(num_tries <= 100, "time limit exceeded");
tokio::time::sleep(Duration::from_millis(100)).await;
let response = app
.call(
Request::builder()
.method(http::Method::GET)
.header("Content-Type", "application/json")
.uri(format!("/api/matches/{}", match_id))
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
let resp: JsonValue = serde_json::from_slice(&body).unwrap();
match resp["state"].as_str() {
Some("Playing") => (), // continue,
Some("Finished") => return Ok(()), // success
value => panic!("got unexpected match state {:?}", value),
}
}
}
|