use engine::{BytesInput, BytesOutput, Timestamp};
use os::HostIdentity;
use os::linux::Linux;
use os::macos::MacOS;
use shell::Shell;
use shell::bash::Bash;
use shell::zsh::Zsh;
use terminal::{Size, Terminal, TerminalModel};
#[test]
fn end_to_end_shell_operations() {
let linux = Linux::with_identity(
&HostIdentity::new("demo", "demo-host"),
Timestamp::from_nanos(1_772_323_200 * 1_000_000_000),
)
.unwrap();
let demo = linux
.unix()
.users()
.authenticate("demo", "demo", linux.unix().groups())
.unwrap();
let root = demo
.elevate(os::unix::user::UserId::ROOT, os::unix::group::GroupId::ROOT)
.unwrap();
let mut shell = Bash::new(linux.unix(), demo.clone()).unwrap();
let mut shell_root = Bash::new(linux.unix(), root.clone()).unwrap();
let mut stdin = BytesInput::new(b"");
let mut stdout = BytesOutput::new();
let mut stderr = BytesOutput::new();
shell.execute("which ls", &mut stdin, &mut stdout, &mut stderr);
assert_eq!(stdout.bytes(), b"/usr/bin/ls\n");
stdout = BytesOutput::new();
shell.execute("ls /", &mut stdin, &mut stdout, &mut stderr);
let output = String::from_utf8(stdout.into_bytes()).unwrap();
assert!(output.contains("bin"));
assert!(output.contains("etc"));
let mut stdout = BytesOutput::new();
shell_root.execute("rm /bin/ls", &mut stdin, &mut stdout, &mut stderr);
let mut which_out = BytesOutput::new();
let status = shell.execute("which ls", &mut stdin, &mut which_out, &mut stderr);
assert_eq!(status.code(), 1, "which should fail after rm");
assert!(
which_out.bytes().is_empty()
|| String::from_utf8_lossy(which_out.bytes()).contains("no ls")
);
let mut which_cat = BytesOutput::new();
shell.execute("which cat", &mut stdin, &mut which_cat, &mut stderr);
assert_eq!(which_cat.bytes(), b"/usr/bin/cat\n");
shell_root.execute(
"mv /usr/bin/cat /home/demo/mycat",
&mut stdin,
&mut stdout,
&mut stderr,
);
let mut which_after = BytesOutput::new();
let status = shell.execute("which cat", &mut stdin, &mut which_after, &mut stderr);
assert_eq!(
status.code(),
1,
"cat should not be found via PATH after mv"
);
let mut cat_out = BytesOutput::new();
shell.execute(
"/home/demo/mycat /etc/passwd",
&mut stdin,
&mut cat_out,
&mut stderr,
);
let cat_output = String::from_utf8(cat_out.into_bytes()).unwrap();
assert!(
cat_output.contains("root:"),
"mycat should read /etc/passwd"
);
shell_root.execute(
"cp /home/demo/mycat /bin/cat",
&mut stdin,
&mut stdout,
&mut stderr,
);
shell_root.execute("apt install curl", &mut stdin, &mut stdout, &mut stderr);
let mut which_curl = BytesOutput::new();
shell.execute("which curl", &mut stdin, &mut which_curl, &mut stderr);
assert_eq!(which_curl.bytes(), b"/usr/bin/curl\n");
let mut curl_out = BytesOutput::new();
shell.execute("curl", &mut stdin, &mut curl_out, &mut stderr);
assert!(
String::from_utf8(curl_out.into_bytes())
.unwrap()
.contains("curl")
);
shell_root.execute("cp /bin/cat /bin/ls", &mut stdin, &mut stdout, &mut stderr);
shell_root.execute("cp /bin/cat /bin/ls", &mut stdin, &mut stdout, &mut stderr);
let mut which_ls2 = BytesOutput::new();
shell.execute("which ls", &mut stdin, &mut which_ls2, &mut stderr);
assert_eq!(which_ls2.bytes(), b"/usr/bin/ls\n");
let mut terminal = TerminalModel::new(Size::new(24, 80));
terminal.write(b"previous shell line\n");
let scrollback_before = terminal.scrollback().count();
terminal.push_keys(&[terminal::KeyEvent::character('q')]);
let mut man_stdin = BytesInput::new(b"");
let mut man_stdout = BytesOutput::new();
let mut man_stderr = BytesOutput::new();
shell.execute_with_terminal(
"man ls",
&mut man_stdin,
&mut man_stdout,
&mut man_stderr,
Some(&mut terminal),
);
assert!(
!terminal.in_alternate_screen(),
"man should leave alternate screen"
);
let visible = terminal.visible_screen().to_text();
assert!(
visible.contains("previous shell line")
|| terminal.scrollback().count() >= scrollback_before
);
let mut nano_terminal = TerminalModel::new(Size::new(24, 80));
nano_terminal.push_keys(&[
terminal::KeyEvent::character('h'),
terminal::KeyEvent::character('i'),
terminal::KeyEvent::ctrl('x'),
terminal::KeyEvent::character('y'),
]);
let mut nano_stdin = BytesInput::new(b"");
let mut nano_stdout = BytesOutput::new();
let mut nano_stderr = BytesOutput::new();
let nano_status = shell.execute_with_terminal(
"nano /tmp/nano_test.txt",
&mut nano_stdin,
&mut nano_stdout,
&mut nano_stderr,
Some(&mut nano_terminal),
);
let _ = nano_status;
let mut cat_nano = BytesOutput::new();
let mut cat_stderr = BytesOutput::new();
shell.execute(
"cat /tmp/nano_test.txt",
&mut stdin,
&mut cat_nano,
&mut cat_stderr,
);
assert_eq!(cat_nano.bytes(), b"hi\n");
let engine = linux.unix().engine();
let events = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let events_clone = events.clone();
let sub = engine.subscribe(engine::EventType::ObjectWritten, move |event| {
events_clone.lock().unwrap().push(event.object);
});
let file = engine
.lookup(Some(demo.actor()), &"/tmp/nano_test.txt".parse().unwrap())
.unwrap();
engine
.write(Some(demo.actor()), file, b"new content".to_vec())
.unwrap();
assert!(
!events.lock().unwrap().is_empty(),
"write should generate event"
);
engine.unsubscribe(sub);
shell.execute(
"echo 'echo hello > /tmp/script_out.txt' > /tmp/script.sh",
&mut stdin,
&mut stdout,
&mut stderr,
);
let mut script_stdin = BytesInput::new(b"");
let mut script_stdout = BytesOutput::new();
let mut script_stderr = BytesOutput::new();
let status = shell.execute(
"sh /tmp/script.sh",
&mut script_stdin,
&mut script_stdout,
&mut script_stderr,
);
assert_eq!(status.code(), 0, "script executes through sh");
let mut script_out = BytesOutput::new();
shell.execute(
"cat /tmp/script_out.txt",
&mut stdin,
&mut script_out,
&mut stderr,
);
assert_eq!(script_out.bytes(), b"hello\n");
}
#[test]
fn homebrew_cask_state_flows_from_catalog_to_process_and_back() {
let macos = MacOS::with_identity(
&HostIdentity::new("demo", "demo-host"),
Timestamp::from_nanos(1_772_323_200 * 1_000_000_000),
)
.unwrap();
let unix = macos.unix();
let demo = unix
.users()
.authenticate("demo", "demo", unix.groups())
.unwrap();
let mut shell = Zsh::new(unix, demo.clone()).unwrap();
let mut stdin = BytesInput::new(b"");
let mut stderr = BytesOutput::new();
let mut search = BytesOutput::new();
assert!(
shell
.execute(
"brew search --cask firefox",
&mut stdin,
&mut search,
&mut stderr,
)
.is_success()
);
assert!(String::from_utf8_lossy(search.bytes()).contains("firefox"));
assert!(
unix.engine()
.lookup(Some(demo.actor()), &"/homebrew-catalog".parse().unwrap(),)
.is_err(),
"the upstream catalog is detached from the machine filesystem"
);
assert!(
unix.engine()
.lookup(
Some(demo.actor()),
&"/Applications/Firefox.app".parse().unwrap(),
)
.is_err(),
"catalog availability is not installation"
);
let mut formula_install = BytesOutput::new();
assert!(
shell
.execute(
"brew install --formula curl",
&mut stdin,
&mut formula_install,
&mut stderr,
)
.is_success()
);
let curl_link = unix
.engine()
.lookup_no_follow(
Some(demo.actor()),
&"/opt/homebrew/bin/curl".parse().unwrap(),
)
.unwrap();
assert_eq!(
unix.engine()
.read_link(Some(demo.actor()), curl_link)
.unwrap(),
Some("../Cellar/curl/8.14.1/bin/curl".to_owned())
);
let mut formula_list = BytesOutput::new();
shell.execute(
"brew list --formula",
&mut stdin,
&mut formula_list,
&mut stderr,
);
assert_eq!(formula_list.bytes(), b"curl\n");
let curl_cellar = unix
.engine()
.lookup(
Some(demo.actor()),
&"/opt/homebrew/Cellar/curl".parse().unwrap(),
)
.unwrap();
unix.engine()
.delete(Some(demo.actor()), curl_cellar)
.unwrap();
let mut after_manual_removal = BytesOutput::new();
shell.execute(
"brew list --formula",
&mut stdin,
&mut after_manual_removal,
&mut stderr,
);
assert!(
after_manual_removal.bytes().is_empty(),
"Cellar state, not an internal marker, decides what is installed"
);
let mut install = BytesOutput::new();
assert!(
shell
.execute(
"brew install --cask firefox",
&mut stdin,
&mut install,
&mut stderr,
)
.is_success()
);
for path in [
"/opt/homebrew/Caskroom/firefox/142.0.1/.metadata/INSTALL_RECEIPT.json",
"/opt/homebrew/Caskroom/firefox/142.0.1/firefox.wrapper.sh",
"/Applications/Firefox.app/Contents/MacOS/Firefox",
] {
unix.engine()
.lookup(Some(demo.actor()), &path.parse().unwrap())
.unwrap_or_else(|_| panic!("Homebrew materialized {path}"));
}
let link = unix
.engine()
.lookup_no_follow(
Some(demo.actor()),
&"/opt/homebrew/bin/firefox".parse().unwrap(),
)
.unwrap();
assert_eq!(
unix.engine().read_link(Some(demo.actor()), link).unwrap(),
Some("../Caskroom/firefox/142.0.1/firefox.wrapper.sh".to_owned())
);
let mut listing = BytesOutput::new();
shell.execute("brew list --cask", &mut stdin, &mut listing, &mut stderr);
assert_eq!(listing.bytes(), b"firefox\n");
let mut opened = BytesOutput::new();
assert!(
shell
.execute("open -a Firefox", &mut stdin, &mut opened, &mut stderr,)
.is_success()
);
assert!(
opened.bytes().is_empty(),
"open has no fabricated GUI output"
);
let mut ps = BytesOutput::new();
shell.execute("ps aux", &mut stdin, &mut ps, &mut stderr);
let ps = String::from_utf8(ps.into_bytes()).unwrap();
let firefox_line = ps.lines().find(|line| line.contains("Firefox")).unwrap();
let pid = firefox_line.split_whitespace().next().unwrap();
let mut killed = BytesOutput::new();
assert!(
shell
.execute(&format!("kill {pid}"), &mut stdin, &mut killed, &mut stderr,)
.is_success()
);
let mut after_kill = BytesOutput::new();
shell.execute("ps aux", &mut stdin, &mut after_kill, &mut stderr);
assert!(!String::from_utf8_lossy(after_kill.bytes()).contains("Firefox"));
let mut uninstall = BytesOutput::new();
assert!(
shell
.execute(
"brew uninstall --cask firefox",
&mut stdin,
&mut uninstall,
&mut stderr,
)
.is_success()
);
for path in [
"/opt/homebrew/Caskroom/firefox",
"/opt/homebrew/bin/firefox",
"/Applications/Firefox.app",
] {
assert!(
unix.engine()
.lookup_no_follow(Some(demo.actor()), &path.parse().unwrap())
.is_err(),
"uninstall removed {path}"
);
}
}