diff --git a/.semaphore/docker-compose.yml b/.semaphore/docker-compose.yml new file mode 100644 index 0000000..2b7e102 --- /dev/null +++ b/.semaphore/docker-compose.yml @@ -0,0 +1,24 @@ +version: '3.7' + +services: + mongodb: + image: mongo:3.6 + environment: + - MONGO_DATA_DIR=/data/db + ports: + - "27017:27017" + command: mongod --smallfiles + rust: + image: rust:latest + container_name: "rust" + depends_on: + - mongodb + environment: + CARGO_HOME: "/tmp/_cargo" + RUST_TEST_THREADS: "1" + RUST_BACKTRACE: "full" + LIBCLANG_PATH: "/usr/lib/llvm-7/lib" + SKIP_SSL_CONNECTION_TESTS: "true" + MONGODB_CONNECTION_STRING: "mongodb://mongodb:27017" + working_dir: /project + command: tail -f /dev/null diff --git a/.semaphore/semaphore.yml b/.semaphore/semaphore.yml new file mode 100644 index 0000000..8bd2376 --- /dev/null +++ b/.semaphore/semaphore.yml @@ -0,0 +1,58 @@ +version: v1.0 +name: Mongo Rust Driver tests + +agent: + machine: + type: e1-standard-8 + os_image: ubuntu1804 + +# Cancel all running and queued workflows before this one +auto_cancel: + running: + # Ignore main AND develop branch as we want it to build all workflows + when: "branch != 'main' AND branch != 'develop'" + +global_job_config: + env_vars: + - name: "CONTAINER_NAME" + value: rust + - name: "COMPOSE_OPTIONS" + value: "-f .semaphore/docker-compose.yml" + + prologue: + commands: + - checkout + +blocks: + - name: Build & Test + dependencies: [] + task: + prologue: + commands: + # Create containers but don't start them yet. We first want to copy the caches onto it + - docker-compose $COMPOSE_OPTIONS up --no-start + # Copy cargo cache to main test container + - cache restore v3-cargo-$(checksum Cargo.lock),v3-cargo + - "[ -d \"/tmp/_cargo\" ] && docker cp /tmp/_cargo $CONTAINER_NAME:/tmp/_cargo || echo 'No cache to copy'" + # Copy project to main test container + - cache restore v3-project-$(cat PACKAGES_CHECKSUM),v3-project + - docker cp . $CONTAINER_NAME:/project + + # Then start the containers so we can run the test on it + - docker-compose $COMPOSE_OPTIONS up -d --no-recreate + jobs: + - name: Test + commands: + - docker-compose $COMPOSE_OPTIONS exec $CONTAINER_NAME cargo test + epilogue: + always: + commands: + # Pull artifacts from containers and cache them + - rm -rf target + - docker cp $CONTAINER_NAME:/project/target target + - cache store v3-project-$(cat PACKAGES_CHECKSUM) target + + # Pull cargo dir and cache it + - rm -rf /tmp/_cargo + - docker cp $CONTAINER_NAME:/tmp/_cargo /tmp/_cargo + - cache store v3-cargo-$(checksum Cargo.lock) /tmp/_cargo diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 22316c6..0000000 --- a/.travis.yml +++ /dev/null @@ -1,9 +0,0 @@ -language: rust -rust: - - stable - - beta - - nightly -services: - - mongodb -env: - - SKIP_SSL_CONNECTION_TESTS=true SKIP_EXTENDED_BULK_OPERATION_TESTS=true diff --git a/src/lib.rs b/src/lib.rs index deb41c3..01cd276 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,7 @@ //! //! # Example //! -//! ``` +//! ```no_run //! use std::sync::Arc; //! use mongo_driver::client::{ClientPool,Uri}; //! diff --git a/tests/bson_encode_decode.rs b/tests/bson_encode_decode.rs index b15cbfd..4153f05 100644 --- a/tests/bson_encode_decode.rs +++ b/tests/bson_encode_decode.rs @@ -2,6 +2,8 @@ extern crate bson; extern crate chrono; extern crate mongo_driver; +mod helpers; + use chrono::prelude::*; use mongo_driver::client::{ClientPool,Uri}; @@ -16,7 +18,7 @@ use bson::spec::BinarySubtype; #[test] fn test_bson_encode_decode() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri, None); let client = pool.pop(); let mut collection = client.get_collection("rust_driver_test", "bson"); @@ -38,7 +40,7 @@ fn test_bson_encode_decode() { bytes: vec![0, 1, 2, 3, 4] }) }; - assert!(collection.insert(&document, None).is_ok()); + collection.insert(&document, None).expect("Could not insert"); let found_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); diff --git a/tests/bulk_operation.rs b/tests/bulk_operation.rs index 266b357..f781acc 100644 --- a/tests/bulk_operation.rs +++ b/tests/bulk_operation.rs @@ -1,6 +1,8 @@ extern crate bson; extern crate mongo_driver; +mod helpers; + use std::env; use bson::doc; @@ -8,7 +10,7 @@ use mongo_driver::client::{ClientPool,Uri}; #[test] fn test_execute_error() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri, None); let client = pool.pop(); let mut collection = client.get_collection("rust_driver_test", "bulk_operation_error"); @@ -25,7 +27,7 @@ fn test_execute_error() { #[test] fn test_basics() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri, None); let client = pool.pop(); let mut collection = client.get_collection("rust_driver_test", "bulk_operation_basics"); @@ -35,7 +37,7 @@ fn test_basics() { let document = doc! {"key_1": "Value 1"}; bulk_operation.insert(&document).expect("Could not insert"); - assert!(bulk_operation.execute().is_ok()); + bulk_operation.execute().expect("Could not execute bulk operation"); let first_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); assert_eq!( @@ -46,7 +48,7 @@ fn test_basics() { #[test] fn test_utf8() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri, None); let client = pool.pop(); let mut collection = client.get_collection("rust_driver_test", "bulk_operation_utf8"); @@ -56,7 +58,7 @@ fn test_utf8() { let document = doc! {"key_1": "kācaṃ śaknomyattum; nopahinasti mām."}; bulk_operation.insert(&document).expect("Could not insert"); - assert!(bulk_operation.execute().is_ok()); + bulk_operation.execute().expect("Could not execute bulk operation"); let first_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); assert_eq!( @@ -71,7 +73,7 @@ fn test_insert_remove_replace_update_extended() { return } - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri, None); let client = pool.pop(); let mut collection = client.get_collection("rust_driver_test", "bulk_operation_extended"); @@ -89,11 +91,10 @@ fn test_insert_remove_replace_update_extended() { bulk_operation.insert(&document).unwrap(); } - let result = bulk_operation.execute(); - assert!(result.is_ok()); + let result = bulk_operation.execute().expect("Could not execute bulk operation"); assert_eq!( - result.ok().unwrap().get("nInserted").unwrap(), + result.get("nInserted").unwrap(), &bson::Bson::Int32(5) ); assert_eq!(5, collection.count(&doc!{}, None).unwrap()); @@ -114,12 +115,10 @@ fn test_insert_remove_replace_update_extended() { false ).unwrap(); - let result = bulk_operation.execute(); - println!("{:?}", result); - assert!(result.is_ok()); + let result = bulk_operation.execute().expect("Could not execute bulk operation"); assert_eq!( - result.ok().unwrap().get("nModified").unwrap(), + result.get("nModified").unwrap(), &bson::Bson::Int32(1) ); @@ -141,12 +140,10 @@ fn test_insert_remove_replace_update_extended() { false ).unwrap(); - let result = bulk_operation.execute(); - println!("{:?}", result); - assert!(result.is_ok()); + let result = bulk_operation.execute().expect("Could not execute bulk operation"); assert_eq!( - result.ok().unwrap().get("nModified").unwrap(), + result.get("nModified").unwrap(), &bson::Bson::Int32(4) ); @@ -171,11 +168,10 @@ fn test_insert_remove_replace_update_extended() { false ).unwrap(); - let result = bulk_operation.execute(); - assert!(result.is_ok()); + let result = bulk_operation.execute().expect("Could not execute bulk operation"); assert_eq!( - result.ok().unwrap().get("nModified").unwrap(), + result.get("nModified").unwrap(), &bson::Bson::Int32(1) ); @@ -193,11 +189,10 @@ fn test_insert_remove_replace_update_extended() { let bulk_operation = collection.create_bulk_operation(None); bulk_operation.remove_one(&query).unwrap(); - let result = bulk_operation.execute(); - assert!(result.is_ok()); + let result = bulk_operation.execute().expect("Could not execute bulk operation"); assert_eq!( - result.ok().unwrap().get("nRemoved").unwrap(), + result.get("nRemoved").unwrap(), &bson::Bson::Int32(1) ); assert_eq!(4, collection.count(&query, None).unwrap()); @@ -208,11 +203,10 @@ fn test_insert_remove_replace_update_extended() { let bulk_operation = collection.create_bulk_operation(None); bulk_operation.remove(&query).unwrap(); - let result = bulk_operation.execute(); - assert!(result.is_ok()); + let result = bulk_operation.execute().expect("Could not execute bulk operation"); assert_eq!( - result.ok().unwrap().get("nRemoved").unwrap(), + result.get("nRemoved").unwrap(), &bson::Bson::Int32(4) ); assert_eq!(0, collection.count(&query, None).unwrap()); diff --git a/tests/client.rs b/tests/client.rs index 4bd4681..ba0be1f 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -1,6 +1,8 @@ extern crate bson; extern crate mongo_driver; +mod helpers; + use std::env; use std::path::PathBuf; use std::sync::Arc; @@ -11,7 +13,7 @@ use mongo_driver::client::{ClientPool,SslOptions,Uri}; #[test] fn test_new_pool_pop_client_and_borrow_collection() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri.clone(), None); assert_eq!(pool.get_uri(), &uri); @@ -28,7 +30,7 @@ fn test_new_pool_pop_client_and_borrow_collection() { #[test] fn test_new_pool_pop_client_and_take_collection() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri.clone(), None); assert_eq!(pool.get_uri(), &uri); @@ -40,7 +42,7 @@ fn test_new_pool_pop_client_and_take_collection() { #[test] fn test_new_pool_pop_client_and_take_database_and_collection() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri.clone(), None); assert_eq!(pool.get_uri(), &uri); @@ -54,7 +56,7 @@ fn test_new_pool_pop_client_and_take_database_and_collection() { #[test] fn test_new_pool_and_pop_client_in_threads() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = Arc::new(ClientPool::new(uri, None)); let pool1 = pool.clone(); @@ -75,7 +77,7 @@ fn test_new_pool_and_pop_client_in_threads() { #[test] fn test_get_server_status() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri, None); let client = pool.pop(); @@ -87,7 +89,7 @@ fn test_get_server_status() { #[test] fn test_read_command_with_opts() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri, None); let client = pool.pop(); let db_name = "rust_driver_test"; @@ -110,7 +112,7 @@ fn test_read_command_with_opts() { #[test] fn test_new_pool_with_ssl_options() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let ssl_options = SslOptions::new( Some(PathBuf::from("./README.md")), Some("password".to_string()), @@ -118,9 +120,8 @@ fn test_new_pool_with_ssl_options() { Some(PathBuf::from("./README.md")), Some(PathBuf::from("./README.md")), false - ); - assert!(ssl_options.is_ok()); - ClientPool::new(uri, Some(ssl_options.unwrap())); + ).expect("Ssl options not correct"); + ClientPool::new(uri, Some(ssl_options)); } #[test] diff --git a/tests/collection.rs b/tests/collection.rs index f3c6167..55f34c0 100644 --- a/tests/collection.rs +++ b/tests/collection.rs @@ -1,6 +1,8 @@ extern crate bson; extern crate mongo_driver; +mod helpers; + use bson::doc; use mongo_driver::CommandAndFindOptions; @@ -10,14 +12,14 @@ use mongo_driver::flags; #[test] fn test_aggregate() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri, None); let client = pool.pop(); let mut collection = client.get_collection("rust_driver_test", "aggregate"); collection.drop().unwrap_or(()); for _ in 0..5 { - assert!(collection.insert(&doc!{"key": 1}, None).is_ok()); + collection.insert(&doc!{"key": 1}, None).expect("Could not insert"); } let pipeline = doc!{ @@ -39,7 +41,7 @@ fn test_aggregate() { #[cfg_attr(target_os = "windows", ignore)] #[test] fn test_command() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri, None); let client = pool.pop(); let collection = client.get_collection("rust_driver_test", "items"); @@ -52,7 +54,7 @@ fn test_command() { #[test] fn test_command_simple() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri, None); let client = pool.pop(); let collection = client.get_collection("rust_driver_test", "items"); @@ -65,7 +67,7 @@ fn test_command_simple() { #[test] fn test_mutation_and_finding() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri, None); let client = pool.pop(); let _ = client.get_collection("rust_driver_test".to_string(), "items"); @@ -94,7 +96,7 @@ fn test_mutation_and_finding() { let second_document = doc! { "key_1": "Value 3" }; - assert!(collection.insert(&second_document, None).is_ok()); + collection.insert(&second_document, None).expect("Could not insert"); let query = doc!{}; @@ -119,7 +121,7 @@ fn test_mutation_and_finding() { // Update the second document let update = doc!{"$set": {"key_1": "Value 4"}}; - assert!(collection.update(&second_document, &update, None).is_ok()); + collection.update(&second_document, &update, None).expect("Could not update"); // Reload and check value let query_after_update = doc! { @@ -133,7 +135,7 @@ fn test_mutation_and_finding() { // Save the second document found_document.insert("key_1".to_string(), bson::Bson::String("Value 5".to_string())); - assert!(collection.save(&found_document, None).is_ok()); + collection.save(&found_document, None).expect("Could not save"); // Reload and check value let found_document = collection.find(&found_document, None).unwrap().next().unwrap().unwrap(); @@ -143,7 +145,7 @@ fn test_mutation_and_finding() { ); // Remove one - assert!(collection.remove(&found_document, None).is_ok()); + collection.remove(&found_document, None).expect("Could not remove"); // Count again assert_eq!(1, collection.count(&query, None).unwrap()); @@ -186,7 +188,7 @@ fn test_mutation_and_finding() { #[test] fn test_find_and_modify() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri, None); let client = pool.pop(); let mut collection = client.get_collection("rust_driver_test", "find_and_modify"); @@ -199,12 +201,11 @@ fn test_find_and_modify() { let update = doc! { "$set": {"content": 1i32} }; - let result = collection.find_and_modify( + collection.find_and_modify( &query, FindAndModifyOperation::Upsert(&update), None - ); - assert!(result.is_ok()); + ).expect("Could not find and modify"); assert_eq!(1, collection.count(&query, None).unwrap()); let found_document = collection.find(&query, None).unwrap().next().unwrap().unwrap(); assert_eq!(found_document.get_i32("content"), Ok(1)); @@ -213,23 +214,21 @@ fn test_find_and_modify() { let update2 = doc! { "$set": {"content": 2i32} }; - let result = collection.find_and_modify( + collection.find_and_modify( &query, FindAndModifyOperation::Update(&update2), None - ); - assert!(result.is_ok()); + ).expect("Could not find and modify"); assert_eq!(1, collection.count(&query, None).unwrap()); let found_document = collection.find(&query, None).unwrap().next().unwrap().unwrap(); assert_eq!(found_document.get_i32("content"), Ok(2)); // Remove it - let result = collection.find_and_modify( + collection.find_and_modify( &query, FindAndModifyOperation::Remove, None - ); - assert!(result.is_ok()); + ).expect("Could not find and modify"); assert_eq!(0, collection.count(&query, None).unwrap()); } diff --git a/tests/cursor.rs b/tests/cursor.rs index 81f8afe..5ba6139 100644 --- a/tests/cursor.rs +++ b/tests/cursor.rs @@ -1,6 +1,8 @@ extern crate bson; extern crate mongo_driver; +mod helpers; + use std::sync::Arc; use std::thread; use std::time::Duration; @@ -12,7 +14,7 @@ use mongo_driver::Result; #[test] fn test_cursor() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri, None); let client = pool.pop(); let mut collection = client.get_collection("rust_driver_test", "cursor_items"); @@ -21,7 +23,7 @@ fn test_cursor() { collection.drop().unwrap_or(()); for _ in 0..10 { - assert!(collection.insert(&document, None).is_ok()); + collection.insert(&document, None).expect("Could not insert"); } let query = doc! {}; @@ -37,7 +39,7 @@ fn test_cursor() { fn test_tailing_cursor() { // See: http://mongoc.org/libmongoc/current/cursors.html#tailable - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = Arc::new(ClientPool::new(uri, None)); let client = pool.pop(); let database = client.get_database("rust_test"); @@ -68,7 +70,7 @@ fn test_tailing_cursor() { let cursor = collection.tail(doc!{}, None, None); let mut counter = 0usize; for result in cursor.into_iter() { - assert!(result.is_ok()); + result.expect("Error tailing"); counter += 1; if counter == 25 { break; @@ -94,7 +96,7 @@ fn test_tailing_cursor() { #[cfg_attr(target_os = "windows", ignore)] #[test] fn test_batch_cursor() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = Arc::new(ClientPool::new(uri, None)); let client = pool.pop(); let database = client.get_database("rust_test"); @@ -115,11 +117,10 @@ fn test_batch_cursor() { bulk_operation.insert(&doc!{"key": i}).unwrap(); } - let result = bulk_operation.execute(); - assert!(result.is_ok()); + let result = bulk_operation.execute().expect("Could not execute bulk operation"); assert_eq!( - result.ok().unwrap().get("nInserted").unwrap(), // why is this an i32? + result.get("nInserted").unwrap(), // why is this an i32? &bson::Bson::Int32(NUM_TO_TEST) ); assert_eq!(NUM_TO_TEST as i64, collection.count(&doc!{}, None).unwrap()); diff --git a/tests/database.rs b/tests/database.rs index 6b1487c..76757bc 100644 --- a/tests/database.rs +++ b/tests/database.rs @@ -1,6 +1,8 @@ extern crate bson; extern crate mongo_driver; +mod helpers; + use bson::doc; use mongo_driver::client::{ClientPool,Uri}; @@ -8,7 +10,7 @@ use mongo_driver::client::{ClientPool,Uri}; #[cfg_attr(target_os = "windows", ignore)] #[test] fn test_command() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri, None); let client = pool.pop(); let database = client.get_database("rust_test"); @@ -21,7 +23,7 @@ fn test_command() { #[test] fn test_command_simple() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri, None); let client = pool.pop(); let database = client.get_database("rust_test"); @@ -34,7 +36,7 @@ fn test_command_simple() { #[test] fn test_get_collection_and_name() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri, None); let client = pool.pop(); let database = client.get_database("rust_test"); @@ -47,7 +49,7 @@ fn test_get_collection_and_name() { #[test] fn test_create_collection() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri, None); let client = pool.pop(); let database = client.get_database("rust_test"); @@ -63,7 +65,7 @@ fn test_create_collection() { #[test] fn test_has_collection() { - let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); let pool = ClientPool::new(uri, None); let client = pool.pop(); let database = client.get_database("rust_test"); diff --git a/tests/helpers/mod.rs b/tests/helpers/mod.rs new file mode 100644 index 0000000..b6c4d17 --- /dev/null +++ b/tests/helpers/mod.rs @@ -0,0 +1,8 @@ +use std::env; + +pub fn mongodb_test_connection_string() -> &'static str { + match env::var("MONGODB_CONNECTION_STRING") { + Ok(value) => Box::leak(value.into_boxed_str()), + Err(_) => "mongodb://localhost:27017", + } +}