From c756692b27175faa6cd86e74e5cb3bec546f2328 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Sun, 18 Feb 2024 14:44:57 -0500 Subject: [PATCH] server: Begin work on integration testing --- .gitignore | 2 +- Cargo.lock | 2 + kvdb-server/Cargo.toml | 2 + .../example-cfg-kvdbd.json | 0 {ssl => kvdb-server/ssl}/.gitignore | 0 {ssl => kvdb-server/ssl}/cert.conf | 0 {ssl => kvdb-server/ssl}/csr.conf | 0 {ssl => kvdb-server/ssl}/mkssl.sh | 0 kvdb-server/tests/integration.rs | 80 +++++++++++++++++++ 9 files changed, 85 insertions(+), 1 deletion(-) rename example-cfg-kvdbd.json => kvdb-server/example-cfg-kvdbd.json (100%) rename {ssl => kvdb-server/ssl}/.gitignore (100%) rename {ssl => kvdb-server/ssl}/cert.conf (100%) rename {ssl => kvdb-server/ssl}/csr.conf (100%) rename {ssl => kvdb-server/ssl}/mkssl.sh (100%) create mode 100644 kvdb-server/tests/integration.rs diff --git a/.gitignore b/.gitignore index 240b693..17cb964 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ /target **/*.rs.bk -cfg-kvdbd.json +kvdb-server/cfg-kvdbd.json db*.kv/ tls-*.pem diff --git a/Cargo.lock b/Cargo.lock index 31096b0..40bae8f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1003,9 +1003,11 @@ dependencies = [ "kvdb-lib", "openssl", "protobuf 3.3.0", + "reqwest", "serde", "serde_derive", "serde_json", + "tokio", ] [[package]] diff --git a/kvdb-server/Cargo.toml b/kvdb-server/Cargo.toml index 4702b3a..52237ba 100644 --- a/kvdb-server/Cargo.toml +++ b/kvdb-server/Cargo.toml @@ -14,6 +14,8 @@ serde = "1.0" serde_json = "1.0" serde_derive = "1.0" actix-web = { version = "4", features = ["openssl"] } +reqwest = "0.11" +tokio = { version = "1", features = ["full"] } env_logger = "0.11" bytes = "1.5" clap = "^3.2" diff --git a/example-cfg-kvdbd.json b/kvdb-server/example-cfg-kvdbd.json similarity index 100% rename from example-cfg-kvdbd.json rename to kvdb-server/example-cfg-kvdbd.json diff --git a/ssl/.gitignore b/kvdb-server/ssl/.gitignore similarity index 100% rename from ssl/.gitignore rename to kvdb-server/ssl/.gitignore diff --git a/ssl/cert.conf b/kvdb-server/ssl/cert.conf similarity index 100% rename from ssl/cert.conf rename to kvdb-server/ssl/cert.conf diff --git a/ssl/csr.conf b/kvdb-server/ssl/csr.conf similarity index 100% rename from ssl/csr.conf rename to kvdb-server/ssl/csr.conf diff --git a/ssl/mkssl.sh b/kvdb-server/ssl/mkssl.sh similarity index 100% rename from ssl/mkssl.sh rename to kvdb-server/ssl/mkssl.sh diff --git a/kvdb-server/tests/integration.rs b/kvdb-server/tests/integration.rs new file mode 100644 index 0000000..75b705d --- /dev/null +++ b/kvdb-server/tests/integration.rs @@ -0,0 +1,80 @@ +// +// tests/integration.rs -- basic API end-to-end integration testing +// +// Copyright (c) 2024 Jeff Garzik +// +// This file is part of the pcgtoolssoftware project covered under +// the MIT License. For the full license text, please see the LICENSE +// file in the root directory of this project. +// SPDX-License-Identifier: MIT + +use serde_json::Value; +use std::env; +use std::fs; +use std::path::Path; +use std::process::{Child, Command}; +use std::thread; +use std::time::Duration; + +const DEF_START_WAIT: u64 = 4; +const T_VALUE: &'static str = "helloworld"; + +// A utility function to prepare the environment before starting the server. +fn prepare_environment() { + // Use CARGO_MANIFEST_DIR to get the path to the source directory + let cargo_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + + // Construct the source path for the configuration file + let config_src_path = Path::new(&cargo_dir).join("example-cfg-kvdbd.json"); + // Define the destination path for the configuration file + let config_dest_path = Path::new("cfg-kvdbd.json"); + + // Copy the configuration file to the current directory. + fs::copy(config_src_path, config_dest_path).expect("Failed to copy configuration file"); + + const DB_DIRS: &[&str] = &["db1.kv", "db2.kv"]; + + for dirpath in DB_DIRS { + // Create the db*.kv directory if it does not exist. + let db_dir = Path::new(dirpath); + if !db_dir.exists() { + fs::create_dir(db_dir).expect("Failed to create directory"); + } + } +} + +// A utility function to start the kvdbd server. +// Returns a Child process handle, which can be used to kill the server later. +fn start_kvdbd_server() -> Child { + // Specify the binary name using "--bin kvdbd" parameter to `cargo run`. + let child = Command::new("cargo") + .args(["run", "--bin", "kvdbd"]) + .spawn() + .expect("Failed to start kvdbd server"); + + // Give the server some time to start up. + thread::sleep(Duration::from_secs(DEF_START_WAIT)); + + child +} + +// A utility function to stop the kvdbd server. +fn stop_kvdbd_server(mut child: Child) { + child.kill().expect("Failed to kill kvdbd server"); +} + +// Example of an integration test that starts the server, makes a request, and stops the server. +#[tokio::test] +async fn test_kvdbd_integration() { + // Prepare server environment + prepare_environment(); + + // Start the server in the background. + let server_process = start_kvdbd_server(); + + // Create HTTP client + let client = reqwest::Client::new(); + + // Stop the server. + stop_kvdbd_server(server_process); +}