Skip to content

Commit 02fd408

Browse files
committed
ci: run tests that requires SG on CI
1 parent 1cc2779 commit 02fd408

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

.github/workflows/main.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ jobs:
9696
cargo install cargo-valgrind
9797
python ci/build_and_run_tests.py --rust-only-with-valigrind
9898
shell: bash
99+
- name: Run tests that requires SG up and running
100+
if: matrix.os == 'ubuntu-20.04' && matrix.rust == 'stable'
101+
run: |
102+
set -e
103+
python ci/docker-up-down.py up
104+
python ci/build_and_run_tests.py --with-server-only
105+
python ci/docker-up-down.py down
99106
ios-tests:
100107
name: Check work with iOS
101108
runs-on: macos-13

ci/build_and_run_tests.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import os, time, sys
55
from pathlib import Path
6-
from subprocess import check_call
6+
from subprocess import check_call, Popen
77
from multiprocessing import cpu_count
88
from typing import List
99

@@ -77,9 +77,13 @@ def build_and_test_rust_part_for_ios(src_root: str) -> None:
7777

7878
@show_timing
7979
def run_tests_that_require_server(src_root: str) -> None:
80+
my_env = os.environ.copy()
81+
my_env["SG_PASS"] = "password"
82+
my_env["SG_USER"] = "sguser"
83+
my_env["SG_URL"] = 'ws://localhost:4884/scratch-30/'
8084
for test_name in ["test_double_replicator_restart", "test_wrong_sync_packets_order"]:
8185
check_call(["cargo", "test", "--release", "-p", "couchbase-lite",
82-
"-vv", test_name, "--", "--ignored"], cwd = src_root)
86+
"-vv", test_name, "--", "--ignored"], cwd = src_root, env = my_env)
8387

8488
@show_timing
8589
def main() -> None:
@@ -114,5 +118,6 @@ def main() -> None:
114118
if WITH_SERVER_TESTS in tests:
115119
run_tests_that_require_server(src_root)
116120

121+
117122
if __name__ == "__main__":
118123
main()

couchbase-lite/tests/need_sync_gateway/mod.rs

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
44
use couchbase_lite::*;
55
use serde::{Deserialize, Serialize};
6-
use std::{path::Path, str};
6+
use std::{env, path::Path, str, thread, time::Duration};
77
use tempfile::{tempdir, TempDir};
88
use tokio::runtime;
99

10-
// See https://github.com/Dushistov/couchbase-lite-rust/issues/54
11-
#[test]
10+
/// See https://github.com/Dushistov/couchbase-lite-rust/issues/54
1211
#[ignore]
12+
#[test]
1313
fn test_double_replicator_restart() {
14-
let (url, auth, tmp_dir) = init_env();
14+
let Some((url, auth, tmp_dir)) = init_test_env() else {
15+
println!("SKIPING test_double_replicator_restart, env variables not setted");
16+
return;
17+
};
1518

1619
let runtime = runtime::Builder::new_current_thread()
1720
.enable_io()
@@ -66,48 +69,51 @@ fn test_double_replicator_restart() {
6669
println!("pushing {pushing}, docs {docs:?}");
6770
},
6871
);
69-
let mut repl = Replicator::new(&db, url, params).unwrap();
72+
let mut repl = Replicator::new(&db, &url, params).unwrap();
7073
repl.start(false).unwrap();
7174
repl
7275
};
7376

7477
let (stop_tx, stop_rx) = tokio::sync::oneshot::channel();
7578

7679
let thread_join_handle = {
77-
std::thread::spawn(move || {
80+
thread::spawn(move || {
7881
runtime.block_on(async {
7982
rx.recv().await.unwrap();
8083
println!("got async event that replicator was idle");
8184
rx.recv().await.unwrap();
8285
let _: () = stop_rx.await.unwrap();
8386
println!("get value from stop_rx, waiting last messages processing");
84-
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
87+
tokio::time::sleep(Duration::from_secs(1)).await;
8588
});
8689
})
8790
};
8891
sync_rx.recv().unwrap();
8992
println!("got SYNC event that replicator was idle");
9093
for _ in 0..10 {
91-
repl = repl.restart(&db, url, &auth, false).unwrap();
94+
repl = repl.restart(&db, &url, &auth, false).unwrap();
9295
}
9396
println!("multi restart done");
94-
std::thread::sleep(std::time::Duration::from_secs(2));
97+
thread::sleep(Duration::from_secs(3));
9598
repl.stop();
9699
stop_tx.send(()).unwrap();
97100
thread_join_handle.join().unwrap();
98-
99101
println!("tokio done");
100102
}
101103

102-
// See https://github.com/Dushistov/couchbase-lite-rust/issues/94
104+
/// See https://github.com/Dushistov/couchbase-lite-rust/issues/94
103105
#[ignore]
104106
#[test]
105107
fn test_wrong_sync_packets_order() {
106-
let (url, auth, tmp_dir) = init_env();
108+
let Some((url, auth, tmp_dir)) = init_test_env() else {
109+
println!("SKIPING test_wrong_sync_packets_order, env variables not setted");
110+
return;
111+
};
112+
107113
let runtime = runtime::Runtime::new().unwrap();
108114
Database::init_socket_impl(runtime.handle().clone());
109115

110-
start_repl_and_save_documents(tmp_dir.path(), "a", 10_000, url, auth).unwrap();
116+
start_repl_and_save_documents(tmp_dir.path(), "a", 10_000, &url, auth).unwrap();
111117
}
112118

113119
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
@@ -201,16 +207,35 @@ fn start_repl_and_save_documents(
201207
ReplicatorState::Busy(_) => was_busy = true,
202208
}
203209
}
204-
std::thread::sleep(std::time::Duration::from_secs(2));
210+
thread::sleep(Duration::from_secs(2));
205211
repl.stop();
206212
Ok(())
207213
}
208214

209-
fn init_env() -> (&'static str, ReplicatorAuthentication, TempDir) {
215+
fn get_env_var(name: &str) -> Result<String, String> {
216+
match env::var(name) {
217+
Err(err) => Err(format!("Can not get {name}: {err}")),
218+
Ok(val) if val.trim().is_empty() => Err(format!("{name} is setted but empty")),
219+
Ok(val) => Ok(val),
220+
}
221+
}
222+
223+
fn init_test_env() -> Option<(String, ReplicatorAuthentication, TempDir)> {
210224
let _ = env_logger::try_init();
211225
let tmp_dir = tempdir().expect("Can not create tmp directory");
212226
println!("we create tempdir at {:?}", tmp_dir.path());
213-
let url = "ws://127.0.0.1:4984/demo/";
214-
let auth = ReplicatorAuthentication::None;
215-
(url, auth, tmp_dir)
227+
let Ok(url) = get_env_var("SG_URL") else {
228+
return None;
229+
};
230+
let auth = if env::var("SG_USER").is_ok() {
231+
let username = get_env_var("SG_USER").unwrap();
232+
let password = get_env_var("SG_PASS").unwrap();
233+
ReplicatorAuthentication::Basic { username, password }
234+
} else if env::var("SG_TOKEN").is_ok() {
235+
let token = get_env_var("SG_TOKEN").unwrap();
236+
ReplicatorAuthentication::SessionToken(token)
237+
} else {
238+
ReplicatorAuthentication::None
239+
};
240+
Some((url, auth, tmp_dir))
216241
}

0 commit comments

Comments
 (0)