Skip to content

Commit

Permalink
Merge pull request #5 from erikwilson/github-ci-fix
Browse files Browse the repository at this point in the history
Fix github CI tests
  • Loading branch information
erikwilson committed Jan 31, 2022
2 parents f55d763 + d4e3cf2 commit 8b76bec
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 44 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ whoami = "~1.2"
chrono = "~0.4"
positioned-io = "~0.2"
tempfile = "3"
stdio-override = "~0.1"
2 changes: 1 addition & 1 deletion examples/test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env cargo run -- -l /dev/stderr script
#!/usr/bin/env -S cargo run -- -l /dev/stderr script
---
# Start example without args
Start:
Expand Down
16 changes: 8 additions & 8 deletions src/file/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ use tempfile::{NamedTempFile, TempDir};

#[test]
fn test_create() {
let dir = &TempDir::new().unwrap();
let dir = TempDir::new().unwrap();
let file = dir.path().join("create-test");
assert_eq!(file.is_file(), false);
assert!(!file.is_file());
create(file.to_str().unwrap());
assert_eq!(file.is_file(), true);
assert!(file.is_file());
}

#[test]
fn test_modify() {
let tmp_file = &NamedTempFile::new().unwrap();
assert_eq!(tmp_file.path().is_file(), true);
let tmp_file = NamedTempFile::new().unwrap();
assert!(tmp_file.path().is_file());
let file = tmp_file.path().to_str().unwrap();
assert_eq!(read_to_string(file).unwrap(), "");
modify(file, "ok", &0);
Expand All @@ -25,8 +25,8 @@ fn test_modify() {

#[test]
fn test_delete() {
let tmp_file = &NamedTempFile::new().unwrap();
assert_eq!(tmp_file.path().is_file(), true);
let tmp_file = NamedTempFile::new().unwrap();
assert!(tmp_file.path().is_file());
delete(tmp_file.path().to_str().unwrap());
assert_eq!(tmp_file.path().is_file(), false);
assert!(!tmp_file.path().is_file());
}
2 changes: 1 addition & 1 deletion src/network/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::net::{TcpListener, TcpStream, UdpSocket};

#[test]
fn test_send_tcp() {
let listener = &TcpListener::bind("127.0.0.1:0").unwrap();
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = &listener.local_addr().unwrap();
send(addr, "hello tcp", &Protocol::TCP);
let (mut stream, _) = listener.accept().unwrap();
Expand Down
6 changes: 3 additions & 3 deletions src/process/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use tempfile::TempDir;

#[test]
fn test_start() {
let dir = &TempDir::new().unwrap();
let dir = TempDir::new().unwrap();
let file = dir.path().join("proc-start-touch-test");
assert_eq!(file.is_file(), false);
assert!(!file.is_file());
start("touch", &[file.to_str().unwrap().to_string()]);
assert_eq!(file.is_file(), true);
assert!(file.is_file());
}
10 changes: 5 additions & 5 deletions src/util/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ use tempfile::TempDir;

#[test]
fn test_script() {
let dir = &TempDir::new().unwrap();
let dir = TempDir::new().unwrap();
let file = dir.path().join("proc-start-touch-test");
let src: &str = &dir.path().join("touch.yaml").to_str().unwrap().to_string();
let src = &dir.path().join("touch.yaml").to_str().unwrap().to_string();
let command = &cmd::Commands::Start {
exec: String::from("touch"),
args: vec![file.to_str().unwrap().to_string()],
};
let cmd_src = format!("{}", serde_yaml::to_string(command).unwrap());
let cmd_src = serde_yaml::to_string(command).unwrap();
File::create(src).unwrap().write_all(cmd_src.as_bytes());
assert_eq!(file.is_file(), false);
assert!(!file.is_file());
script(src);
assert_eq!(file.is_file(), true);
assert!(file.is_file());
}
79 changes: 53 additions & 26 deletions tests/example_script.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,82 @@
use serde_json::Value;
use std::fs;
use std::io::prelude::*;
use std::net::TcpListener;
use std::process::Command;
use stdio_override::StdoutOverride;
use substring::Substring;
use tempfile::NamedTempFile;

// Needs a unix system with user-land

#[test]
#[ignore] // skip for code coverage, run with: cargo test -- --ignored
fn test_example_script() {
let _listener = &TcpListener::bind("127.0.0.1:8888").unwrap();
let _listener = TcpListener::bind("127.0.0.1:8888").unwrap();
let output = Command::new("./examples/test.yaml").output().unwrap();
let data = String::from_utf8(output.stdout).unwrap();
let logs = String::from_utf8(output.stderr).unwrap();
assert_eq!(data, "\nok\n\n");
for line in logs.split("\n") {
verify(data, logs);
}

#[test]
fn test_script_api() {
let _listener = TcpListener::bind("127.0.0.1:8888").unwrap();
let log_file_tmp = NamedTempFile::new().unwrap();
let log_file = log_file_tmp.path().to_str().unwrap();
cowbird::log::set_log_file(log_file.to_string());
let stdout_file_tmp = NamedTempFile::new().unwrap();
let stdout_file = stdout_file_tmp.path().to_str().unwrap();
let guard = StdoutOverride::override_file(stdout_file).unwrap();
cowbird::util::script("./examples/test.yaml");
drop(guard);
let data = fs::read_to_string(stdout_file).unwrap();
let logs = fs::read_to_string(log_file).unwrap();
verify(data, logs);
}

// verify log data for needed fields
fn verify(data: String, logs: String) {
assert_eq!(data, "\nok\n\n", "stderr: {}", logs);
for line in logs.split('\n') {
if line.substring(0, 1) == "{" {
let _ = write!(std::io::stdout(), "{}\n", line);
let _ = writeln!(std::io::stdout(), "{}", line);
let v: Value = serde_json::from_str(line).unwrap();
assert_eq!(v["msg"], "ok");
assert_eq!(v["ts"].is_null(), false);
assert_eq!(v["level"].is_null(), false);
assert_eq!(v["name"].is_null(), false);
assert_eq!(v["version"].is_null(), false);
assert_eq!(v["username"].is_null(), false);
assert_eq!(v["cmd_line"].is_null(), false);
assert_eq!(v["pid"].is_null(), false);
assert_eq!(v["type"].is_null(), false);
assert_eq!(v["cmd"].is_null(), false);
assert!(!v["ts"].is_null());
assert!(!v["level"].is_null());
assert!(!v["name"].is_null());
assert!(!v["version"].is_null());
assert!(!v["username"].is_null());
assert!(!v["cmd_line"].is_null());
assert!(!v["pid"].is_null());
assert!(!v["type"].is_null());
assert!(!v["cmd"].is_null());
let cmd = &*v["cmd"].as_str().unwrap();
match cmd {
"start" => {
assert_eq!(v["start_ts"].is_null(), false);
assert_eq!(v["exec"].is_null(), false);
assert_eq!(v["args"].is_null(), false);
assert!(!v["start_ts"].is_null());
assert!(!v["exec"].is_null());
assert!(!v["args"].is_null());
}
"create" => {
assert_eq!(v["file"].is_null(), false);
assert!(!v["file"].is_null());
}
"modify" => {
assert_eq!(v["file"].is_null(), false);
assert_eq!(v["offset"].is_null(), false);
assert_eq!(v["size"].is_null(), false);
assert!(!v["file"].is_null());
assert!(!v["offset"].is_null());
assert!(!v["size"].is_null());
}
"delete" => {
assert_eq!(v["file"].is_null(), false);
assert!(!v["file"].is_null());
}
"send" => {
assert_eq!(v["source"].is_null(), false);
assert_eq!(v["dest"].is_null(), false);
assert_eq!(v["proto"].is_null(), false);
assert_eq!(v["size"].is_null(), false);
assert!(!v["source"].is_null());
assert!(!v["dest"].is_null());
assert!(!v["proto"].is_null());
assert!(!v["size"].is_null());
}
_ => assert!(false, "unknown cmd: {}", cmd),
_ => panic!("unknown cmd: {}", cmd),
}
};
}
Expand Down

0 comments on commit 8b76bec

Please sign in to comment.