Skip to content

Commit 9517122

Browse files
Add tests to procps::pid::status
1 parent 52b24f7 commit 9517122

File tree

2 files changed

+145
-8
lines changed

2 files changed

+145
-8
lines changed

src/procrs/error.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,15 @@ impl Error for ProcError {
179179
impl fmt::Debug for ProcError {
180180
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
181181
let more = self.more.unwrap_or("");
182-
let error;
183182
if let Some(e) = self.inner.as_ref() {
184-
error = e.description();
183+
write!(f, "error {} ({}) from {}: {}",
184+
self.operation.description(), more,
185+
self.file.description(), e)
185186
} else {
186-
error = "";
187+
write!(f, "error {} ({}) from {}",
188+
self.operation.description(), more,
189+
self.file.description())
187190
}
188-
write!(f, "error {} ({}) from {}: {}",
189-
self.operation.description(), more,
190-
self.file.description(), error)
191191
}
192192
}
193193

src/procrs/pid/status.rs

Lines changed: 139 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::str::FromStr;
77
use ::error::{ProcError, ProcFile, ProcOper};
88
use ::{TaskId, MemSize};
99

10-
#[derive(Debug)]
10+
#[derive(Debug, PartialEq)]
1111
pub struct PidStatus {
1212
pub name: String,
1313
pub tgid: TaskId,
@@ -99,7 +99,7 @@ impl PidStatus {
9999
Ok((key.trim().to_owned(),
100100
value.trim().to_owned())),
101101
_ => Err(ProcError::new_more(ProcOper::Parsing, ProcFile::PidStatus,
102-
Some("No colon on line")))
102+
Some("Line missing colon")))
103103
}
104104
},
105105
Err(e) => Err(e)
@@ -166,3 +166,140 @@ fn parse_mem(mem_str: String) -> Result<MemSize, ParseIntError> {
166166
.parse::<MemSize>()
167167
.map(|n| n * 1024)
168168
}
169+
170+
#[test]
171+
fn test_no_colon() {
172+
let lines = "Name".lines().map(|l| Ok(l.to_owned()));
173+
let status = PidStatus::parse_string(lines);
174+
assert_eq!(status,
175+
Err(ProcError::new_more(ProcOper::Parsing, ProcFile::PidStatus, Some("Line missing colon")))
176+
);
177+
}
178+
179+
#[test]
180+
fn test_missing_tgid() {
181+
let lines = "Name: a\n\
182+
Pid: 4\n\
183+
".lines().map(|l| Ok(l.to_owned()));
184+
let status = PidStatus::parse_string(lines);
185+
assert_eq!(status,
186+
Err(ProcError::new_more(ProcOper::ParsingField, ProcFile::PidStatus, Some("missing Tgid")))
187+
);
188+
}
189+
190+
#[test]
191+
fn test_uid_parse() {
192+
let lines = "Name: bash\n\
193+
Tgid: 27899\n\
194+
Ngid: 0\n\
195+
Pid: 27899\n\
196+
PPid: 4351\n\
197+
TracerPid: 0\n\
198+
Uid: 1000 1000 a000 1000\n\
199+
".lines().map(|l| Ok(l.to_owned()));
200+
let status = PidStatus::parse_string(lines);
201+
assert_eq!(status,
202+
Err(ProcError::new(ProcOper::ParsingField, ProcFile::PidStatus,
203+
Some("a".parse::<u8>().unwrap_err()), Some("Uid")))
204+
);
205+
}
206+
207+
#[test]
208+
fn test_uid_count() {
209+
let lines = "Name: bash\n\
210+
Tgid: 27899\n\
211+
Ngid: 0\n\
212+
Pid: 27899\n\
213+
PPid: 4351\n\
214+
TracerPid: 0\n\
215+
Uid: 1000 1000 1000\n\
216+
".lines().map(|l| Ok(l.to_owned()));
217+
let status = PidStatus::parse_string(lines);
218+
assert_eq!(status,
219+
Err(ProcError::new_more(ProcOper::ParsingField, ProcFile::PidStatus, Some("Uid")))
220+
);
221+
}
222+
223+
#[test]
224+
fn test_mem_parse() {
225+
let lines = "Name: bash\n\
226+
Tgid: 27899\n\
227+
Ngid: 0\n\
228+
Pid: 27899\n\
229+
PPid: 4351\n\
230+
TracerPid: 0\n\
231+
Uid: 1000 1000 1000 1000\n\
232+
Gid: 1000 1000 1000 1000\n\
233+
FDSize: 256\n\
234+
Groups: 10 18 27 35 101 103 104 105 250 1000 1001 \n\
235+
NStgid: 27899\n\
236+
NSpid: 27899\n\
237+
NSpgid: 27899\n\
238+
NSsid: 27899\n\
239+
VmPeak: a0896 kB\n\
240+
".lines().map(|l| Ok(l.to_owned()));
241+
let status = PidStatus::parse_string(lines);
242+
assert_eq!(status,
243+
Err(ProcError::new(ProcOper::ParsingField, ProcFile::PidStatus,
244+
Some("a".parse::<u8>().unwrap_err()), Some("VmPeak")))
245+
);
246+
}
247+
248+
#[test]
249+
fn test_parsing() {
250+
let lines = "Name: bash\n\
251+
Tgid: 27899\n\
252+
Pid: 27899\n\
253+
PPid: 4351\n\
254+
TracerPid: 0\n\
255+
Uid: 1000 1000 1000 1000\n\
256+
Gid: 1000 1000 1000 1000\n\
257+
FDSize: 256\n\
258+
Groups: 10 18 27 35 101 103 104 105 250 1000 1001 \n\
259+
NStgid: 27899\n\
260+
NSpid: 27899\n\
261+
NSpgid: 27899\n\
262+
NSsid: 27899\n\
263+
VmPeak: 20896 kB\n\
264+
VmSize: 20868 kB\n\
265+
VmLck: 0 kB\n\
266+
VmPin: 0 kB\n\
267+
VmHWM: 4584 kB\n\
268+
VmRSS: 4584 kB\n\
269+
VmData: 1176 kB\n\
270+
VmStk: 136 kB\n\
271+
VmExe: 688 kB\n\
272+
VmLib: 2540 kB\n\
273+
VmPTE: 64 kB\n\
274+
VmPMD: 12 kB\n\
275+
VmSwap: 0 kB\n\
276+
Threads: 1\n\
277+
".lines().map(|l| Ok(l.to_owned()));
278+
let status = PidStatus::parse_string(lines);
279+
assert_eq!(status,
280+
Ok(PidStatus {
281+
name: "bash".to_owned(),
282+
tgid: 27899,
283+
pid: 27899,
284+
ppid: 4351,
285+
tracerpid: 0,
286+
uid: (1000, 1000, 1000, 1000),
287+
gid: (1000, 1000, 1000, 1000),
288+
fdsize: 256,
289+
vmpeak: Some(21397504),
290+
vmsize: Some(21368832),
291+
vmlck: Some(0),
292+
vmpin: Some(0),
293+
vmhwm: Some(4694016),
294+
vmrss: Some(4694016),
295+
vmdata: Some(1204224),
296+
vmstk: Some(139264),
297+
vmexe: Some(704512),
298+
vmlib: Some(2600960),
299+
vmpte: Some(65536),
300+
vmpmd: Some(12288),
301+
vmswap: Some(0),
302+
threads: 1
303+
})
304+
);
305+
}

0 commit comments

Comments
 (0)