17
17
#[ cfg( test) ]
18
18
mod tests {
19
19
extern crate quickcheck;
20
+ extern crate tempdir;
20
21
21
22
use crate :: flowgger;
22
23
23
24
use quickcheck:: QuickCheck ;
25
+ use tempdir:: TempDir ;
24
26
25
27
use std:: fs;
26
28
use std:: io:: { BufRead , BufReader } ;
@@ -42,7 +44,7 @@ mod tests {
42
44
use toml:: Value ;
43
45
44
46
const DEFAULT_CONFIG_FILE : & str = "flowgger.toml" ;
45
- const DEFAULT_OUTPUT_FILEPATH : & str = "output.log" ;
47
+ const DEFAULT_OUTPUT_FILENAME : & str = "output.log" ;
46
48
const DEFAULT_QUEUE_SIZE : usize = 10_000_000 ;
47
49
48
50
const DEFAULT_OUTPUT_FORMAT : & str = "gelf" ;
@@ -62,15 +64,17 @@ mod tests {
62
64
63
65
#[ test]
64
66
fn test_fuzzer ( ) {
65
- let config = get_config ( ) ;
66
- let file_output_path = config
67
+ let mut config = get_config ( ) ;
68
+ let file_output_name = config
67
69
. lookup ( "output.file_path" )
68
- . map_or ( DEFAULT_OUTPUT_FILEPATH , |x| {
70
+ . map_or ( DEFAULT_OUTPUT_FILENAME , |x| {
69
71
x. as_str ( ) . expect ( "File output path missing in config" )
70
72
} ) ;
71
- remove_output_file ( & file_output_path ) ;
72
-
73
+ let output_dir = get_output_dir ( ) ;
74
+ let file_output_path = get_output_file_path ( & output_dir , & file_output_name ) ;
73
75
let ( tx, rx) : ( SyncSender < Vec < u8 > > , Receiver < Vec < u8 > > ) = sync_channel ( DEFAULT_QUEUE_SIZE ) ;
76
+
77
+ set_output_file_path_in_config ( & mut config, & file_output_path) ;
74
78
start_file_output ( & config, rx) ;
75
79
set_global_context ( & config, tx) ;
76
80
@@ -80,6 +84,21 @@ mod tests {
80
84
let _ = check_result ( & file_output_path) ;
81
85
}
82
86
87
+ fn get_output_dir ( ) -> TempDir {
88
+ let temp_dir = TempDir :: new ( "test_file_output" ) . expect ( "Couldn't create output directory" ) ;
89
+ return temp_dir;
90
+ }
91
+
92
+ fn get_output_file_path ( output_dir : & TempDir , file_output_name : & str ) -> String {
93
+ let file_base = output_dir
94
+ . path ( )
95
+ . join ( file_output_name)
96
+ . to_string_lossy ( )
97
+ . to_string ( ) ;
98
+
99
+ return file_base;
100
+ }
101
+
83
102
fn get_global_context ( ) -> * mut Mutex < Option < Context > > {
84
103
unsafe { addr_of_mut ! ( GLOBAL_CONTEXT ) }
85
104
}
@@ -124,7 +143,7 @@ mod tests {
124
143
125
144
/// Update the default file rotation size and time in the config file
126
145
/// This ensures output is sent to a single non-rotated file
127
- pub fn update_file_rotation_defaults_in_config ( config : & mut Config ) {
146
+ fn update_file_rotation_defaults_in_config ( config : & mut Config ) {
128
147
if let Some ( entry) = config
129
148
. config
130
149
. get_mut ( "output" )
@@ -144,12 +163,19 @@ mod tests {
144
163
}
145
164
}
146
165
147
- pub fn remove_output_file ( file_output_path : & str ) {
148
- let _ = fs:: remove_file ( file_output_path) ;
166
+ fn set_output_file_path_in_config ( config : & mut Config , file_output_path : & str ) {
167
+ if let Some ( entry) = config
168
+ . config
169
+ . get_mut ( "output" )
170
+ . unwrap ( )
171
+ . get_mut ( "file_path" )
172
+ {
173
+ * entry = Value :: String ( file_output_path. to_string ( ) ) ;
174
+ }
149
175
}
150
176
151
177
/// Start an input listener which writes data to the output file once received.
152
- pub fn start_file_output ( config : & Config , rx : Receiver < Vec < u8 > > ) {
178
+ fn start_file_output ( config : & Config , rx : Receiver < Vec < u8 > > ) {
153
179
let output_format = config
154
180
. lookup ( "output.format" )
155
181
. map_or ( DEFAULT_OUTPUT_FORMAT , |x| {
@@ -179,7 +205,7 @@ mod tests {
179
205
output. start ( arx, merger) ;
180
206
}
181
207
182
- pub fn fuzz_target_rfc3164 ( data : String ) {
208
+ fn fuzz_target_rfc3164 ( data : String ) {
183
209
unsafe {
184
210
let global_context = get_global_context ( ) . as_ref ( ) . unwrap ( ) ;
185
211
@@ -222,7 +248,6 @@ mod tests {
222
248
223
249
let file = fs:: File :: open ( file_output_path) . expect ( "Unable to open output file" ) ;
224
250
let reader = BufReader :: new ( file) ;
225
-
226
251
for line in reader. lines ( ) {
227
252
let line_item: String = line?;
228
253
if !line_item. trim ( ) . is_empty ( ) {
0 commit comments