Skip to content

Commit 615c13c

Browse files
committed
chore: test use test_util
1 parent 900e3fc commit 615c13c

File tree

3 files changed

+5
-100
lines changed

3 files changed

+5
-100
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ maintenance = { status = "experimental" }
4242

4343
[dev-dependencies]
4444
target-triple = "0.1.2"
45+
ngx = { path ="./", features = ["test_util"] }

tests/log_test.rs

+3-100
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,14 @@
1-
use std::fs;
2-
use std::io::Result;
3-
#[cfg(unix)]
4-
use std::os::unix::ffi::OsStrExt;
5-
use std::path::{Path, PathBuf};
6-
use std::process::Command;
7-
use std::process::Output;
8-
9-
use ngx::ffi::{NGX_CONF_PATH, NGX_PREFIX, NGX_SBIN_PATH};
10-
11-
/// Convert a CStr to a PathBuf
12-
pub fn cstr_to_path(val: &std::ffi::CStr) -> Option<PathBuf> {
13-
if val.is_empty() {
14-
return None;
15-
}
16-
17-
#[cfg(unix)]
18-
let str = std::ffi::OsStr::from_bytes(val.to_bytes());
19-
#[cfg(not(unix))]
20-
let str = std::str::from_utf8(val.to_bytes()).ok()?;
21-
22-
Some(PathBuf::from(str))
23-
}
24-
25-
/// harness to test nginx
26-
pub struct Nginx {
27-
pub install_path: PathBuf,
28-
pub config_path: PathBuf,
29-
}
30-
31-
impl Default for Nginx {
32-
/// create nginx with default
33-
fn default() -> Nginx {
34-
let install_path = cstr_to_path(NGX_PREFIX).expect("installation prefix");
35-
Nginx::new(install_path)
36-
}
37-
}
38-
39-
impl Nginx {
40-
pub fn new<P: AsRef<Path>>(path: P) -> Nginx {
41-
let install_path = path.as_ref();
42-
let config_path = cstr_to_path(NGX_CONF_PATH).expect("configuration path");
43-
let config_path = install_path.join(config_path);
44-
45-
Nginx {
46-
install_path: install_path.into(),
47-
config_path,
48-
}
49-
}
50-
51-
/// get bin path to nginx instance
52-
pub fn bin_path(&mut self) -> PathBuf {
53-
let bin_path = cstr_to_path(NGX_SBIN_PATH).expect("binary path");
54-
self.install_path.join(bin_path)
55-
}
56-
57-
/// start nginx process with arguments
58-
pub fn cmd(&mut self, args: &[&str]) -> Result<Output> {
59-
let bin_path = self.bin_path();
60-
let result = Command::new(bin_path).args(args).output();
61-
62-
match result {
63-
Err(e) => Err(e),
64-
65-
Ok(output) => {
66-
println!("status: {}", output.status);
67-
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
68-
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
69-
Ok(output)
70-
}
71-
}
72-
}
73-
74-
/// complete stop the nginx binary
75-
pub fn stop(&mut self) -> Result<Output> {
76-
self.cmd(&["-s", "stop"])
77-
}
78-
79-
/// start the nginx binary
80-
pub fn start(&mut self) -> Result<Output> {
81-
self.cmd(&[])
82-
}
83-
84-
// make sure we stop existing nginx and start new master process
85-
// intentinally ignore failure in stop
86-
pub fn restart(&mut self) -> Result<Output> {
87-
let _ = self.stop();
88-
self.start()
89-
}
90-
91-
// replace config with another config
92-
pub fn replace_config<P: AsRef<Path>>(&mut self, from: P) -> Result<u64> {
93-
println!("copying config from: {:?} to: {:?}", from.as_ref(), self.config_path); // replace with logging
94-
fs::copy(from, &self.config_path)
95-
}
96-
}
97-
981
#[cfg(test)]
992
mod tests {
1003
use std::env;
1014

102-
use super::*;
5+
use ngx::test_util::NginxBuilder;
1036

1047
const TEST_NGINX_CONFIG: &str = "tests/nginx.conf";
1058

1069
#[test]
10710
fn test() {
108-
let mut nginx = Nginx::default();
11+
let mut nginx = NginxBuilder::default().build();
10912

11013
let current_dir = env::current_dir().expect("Unable to get current directory");
11114
let test_config_path = current_dir.join(TEST_NGINX_CONFIG);
@@ -118,7 +21,7 @@ mod tests {
11821
);
11922

12023
nginx
121-
.replace_config(&test_config_path)
24+
.copy_main_config(&test_config_path)
12225
.unwrap_or_else(|_| panic!("Unable to load config file: {}", test_config_path.to_string_lossy()));
12326
let output = nginx.restart().expect("Unable to restart NGINX");
12427
assert!(output.status.success());

0 commit comments

Comments
 (0)