diff --git a/src/daemon/filter/mod.rs b/src/daemon/filter/mod.rs index 864009c..b847586 100644 --- a/src/daemon/filter/mod.rs +++ b/src/daemon/filter/mod.rs @@ -257,7 +257,9 @@ impl FilterManager { .filter(|action| !startup || !action.oneshot) // skip any actions .skip(match actions_left { - Some(actions_left) => self.filter.actions.len() - actions_left as usize, + Some(actions_left) => { + self.filter.filtered_actions_from_match(&m).len() - actions_left as usize + } None => 0, }); diff --git a/tests/persistence.rs b/tests/persistence.rs new file mode 100644 index 0000000..3cbfd74 --- /dev/null +++ b/tests/persistence.rs @@ -0,0 +1,51 @@ +use std::{error::Error, path::Path, time::Duration}; + +use assert_cmd::Command; +use assert_fs::prelude::*; +use predicates::prelude::predicate; + +#[test] +fn resume_action() -> Result<(), Box> { + let tmp_dir = assert_fs::TempDir::new()?; + + tmp_dir + .child("config.jsonnet") + .write_file(Path::new("tests/test-conf/test-resume-action.jsonnet"))?; + + // first run + let mut cmd = Command::cargo_bin("reaction")?; + cmd.args(["start", "--socket", "./s", "--config", "./config.jsonnet"]); + cmd.current_dir(tmp_dir.path()); + cmd.timeout(Duration::from_secs(5)); + // Expected exit 1: all stream exited + cmd.assert().code(predicate::eq(1)); + + // expect a single match from the stream command + let expected = ["starting", "start4 10.1.0.1", "stopping"].join("\n") + "\n"; + tmp_dir.child("log").assert(&expected); + + // second run, expect to resume action + let mut cmd = Command::cargo_bin("reaction")?; + cmd.args(["start", "--socket", "./s", "--config", "./config.jsonnet"]); + cmd.current_dir(tmp_dir.path()); + cmd.timeout(Duration::from_secs(5)); + // Expected exit 1: all stream exited + cmd.assert().code(predicate::eq(1)); + + let expected = [ + "starting", + "start4 10.1.0.1", // from the stream command + "stopping", + "starting", + "start4 10.1.0.1", // previous action loaded from db + "stop4 10.1.0.1", // previous action lapses + "start4 10.1.0.1", // from the stream command + "stopping", + ] + .join("\n") + + "\n"; + + tmp_dir.child("log").assert(&expected); + + Ok(()) +} diff --git a/tests/test-conf/test-resume-action.jsonnet b/tests/test-conf/test-resume-action.jsonnet new file mode 100644 index 0000000..3e92ef8 --- /dev/null +++ b/tests/test-conf/test-resume-action.jsonnet @@ -0,0 +1,46 @@ +{ + patterns: { + ip: { + type: 'ip', + ipv6mask: 64, + }, + }, + start: [ + ['sh', '-c', 'echo starting >>./log'], + ], + stop: [ + ['sh', '-c', 'echo stopping >>./log'], + ], + streams: { + s1: { + cmd: ['sh', '-c', 'sleep 2; echo T 10.1.0.1; sleep 0.2; echo T 10.1.0.1; sleep 0.8'], + filters: { + f1: { + regex: ['T '], + actions: { + log_start4: { + ipv4only: true, + cmd: ['sh', '-c', 'echo start4 >>./log'], + }, + log_start6: { + ipv6only: true, + cmd: ['sh', '-c', 'echo start6 >>./log'], + }, + log_stop4: { + ipv4only: true, + onexit: false, + after: '2s', + cmd: ['sh', '-c', 'echo stop4 >> ./log'], + }, + log_stop6: { + ipv6only: true, + onexit: false, + after: '2s', + cmd: ['sh', '-c', 'echo stop6 >> ./log'], + }, + }, + }, + }, + }, + }, +}