Skip to content

Commit

Permalink
Mock the local time in the test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars T Hansen committed Nov 13, 2024
1 parent 6bdb4ef commit 466c847
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/slurmjobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ pub fn show_slurm_jobs(window: &Option<u32>, span: &Option<String>) {
}
Ok(sacct_output) => {
let mut writer = io::stdout();
format_jobs(&mut writer, &sacct_output, &field_names);
let local = time::now_local();
format_jobs(&mut writer, &sacct_output, &field_names, &local);
}
}
}
Expand Down Expand Up @@ -130,7 +131,7 @@ fn check_ymd(s: &str) -> bool {
return k == 3;
}

fn format_jobs(writer: &mut dyn io::Write, sacct_output: &str, field_names: &[&str]) {
fn format_jobs(writer: &mut dyn io::Write, sacct_output: &str, field_names: &[&str], local: &libc::tm) {
// Fields that are dates that may be reinterpreted before transmission.
let date_fields = HashSet::from(["Start", "End", "Submit"]);

Expand Down Expand Up @@ -163,7 +164,6 @@ fn format_jobs(writer: &mut dyn io::Write, sacct_output: &str, field_names: &[&s
// is bound to lead to problems eventually, so reformat. If parsing
// fails, just transmit the date and let the consumer deal with it.
if let Ok(mut t) = time::parse_date_and_time_no_tzo(&val) {
let local = time::now_local();
t.tm_gmtoff = local.tm_gmtoff;
t.tm_isdst = local.tm_isdst;
// If t.tm_zone is not null then it must point to static data, so
Expand Down Expand Up @@ -285,7 +285,11 @@ pub fn test_format_jobs() {
"#;

let mut output = Vec::new();
format_jobs(&mut output, sacct_output, &field_names);
let mut local = time::now_local();
// The output below depends on us being in UTC+01:00 and not in dst so mock that.
local.tm_gmtoff = 3600;
local.tm_isdst = 0;
format_jobs(&mut output, sacct_output, &field_names, &local);

// The golang `sacctd` output for the above input.
let expected = r#"v=0.1.0,JobID=973821,JobIDRaw=973821,User=ec-aaaaa,Account=ec85,State=COMPLETED,Start=2024-11-13T11:08:00+01:00,End=2024-11-13T13:07:24+01:00,ElapsedRaw=7164,ReqCPUS=6,ReqMem=10000M,ReqNodes=1,Submit=2024-11-13T08:30:40+01:00,SystemCPU=22:53.824,TimelimitRaw=400,UserCPU=11:06:33,NodeList=c1-28,Partition=normal,"AllocTRES=billing=6,cpu=6,mem=10000M,node=1",JobName=command
Expand Down

0 comments on commit 466c847

Please sign in to comment.