aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/tests/integration.rs
blob: c2ab2f6452c10b9c9df4c9006e21532f59416631 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use axum::{
    body::Body,
    http::{self, Request, StatusCode},
};
use diesel::{PgConnection, RunQueryDsl};
use planetwars_server::{create_db_pool, create_pw_api, db, modules, DbPool, 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");
}

/// Setup a simple text fixture, having simplebot and the hex map.
/// This is enough to run a simple match.
fn setup_simple_fixture(db_conn: &PgConnection, config: &GlobalConfig) {
    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");
}

struct TestApp<'a> {
    // exclusive connection to the test database
    #[allow(dead_code)]
    db_guard: parking_lot::MutexGuard<'a, ()>,
    db_pool: DbPool,

    // temporary data directory
    #[allow(dead_code)]
    data_dir: TempDir,

    config: Arc<GlobalConfig>,
}

impl<'a> TestApp<'a> {
    async fn create() -> io::Result<TestApp<'a>> {
        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_guard = DB_LOCK.lock();
        let db_pool = create_db_pool(&config).await;

        Ok(TestApp {
            db_guard,
            config,
            data_dir,
            db_pool,
        })
    }

    async fn with_db_conn<F, R>(&self, function: F) -> R
    where
        F: FnOnce(&PgConnection) -> R,
    {
        let db_conn = self
            .db_pool
            .get()
            .await
            .expect("could not get db connection");
        function(&db_conn)
    }
}

#[tokio::test(flavor = "multi_thread")]
async fn test_submit_bot() -> io::Result<()> {
    let test_app = TestApp::create().await.unwrap();
    test_app
        .with_db_conn(|db_conn| {
            clear_database(db_conn);
            setup_simple_fixture(db_conn, &test_app.config);
        })
        .await;

    let mut app = create_pw_api(test_app.config, test_app.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),
        }
    }
}