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
-
98
1
#[ cfg( test) ]
99
2
mod tests {
100
3
use std:: env;
101
4
102
- use super :: * ;
5
+ use ngx :: test_util :: NginxBuilder ;
103
6
104
7
const TEST_NGINX_CONFIG : & str = "tests/nginx.conf" ;
105
8
106
9
#[ test]
107
10
fn test ( ) {
108
- let mut nginx = Nginx :: default ( ) ;
11
+ let mut nginx = NginxBuilder :: default ( ) . build ( ) ;
109
12
110
13
let current_dir = env:: current_dir ( ) . expect ( "Unable to get current directory" ) ;
111
14
let test_config_path = current_dir. join ( TEST_NGINX_CONFIG ) ;
@@ -118,7 +21,7 @@ mod tests {
118
21
) ;
119
22
120
23
nginx
121
- . replace_config ( & test_config_path)
24
+ . copy_main_config ( & test_config_path)
122
25
. unwrap_or_else ( |_| panic ! ( "Unable to load config file: {}" , test_config_path. to_string_lossy( ) ) ) ;
123
26
let output = nginx. restart ( ) . expect ( "Unable to restart NGINX" ) ;
124
27
assert ! ( output. status. success( ) ) ;
0 commit comments