summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2023-03-08 16:57:11 +0200
committerJustin Wernick <justin@worthe-it.co.za>2023-03-08 16:57:11 +0200
commitf58178da13a1a5bfa7ea783ebf7e9a9511dd3f11 (patch)
tree4c832071d5b18ee769b47e0cffae889a5853d72f /tests
parentfd7f699029899cdcedc4f801d76a9c3268ecb532 (diff)
Start building the test infrastructure to launch this docker ssh daemon
Diffstat (limited to 'tests')
-rw-r--r--tests/server_shell.rs67
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/server_shell.rs b/tests/server_shell.rs
new file mode 100644
index 0000000..bf3e1d6
--- /dev/null
+++ b/tests/server_shell.rs
@@ -0,0 +1,67 @@
+use anyhow::{bail, Result};
+use assert_cmd::{cargo::cargo_bin, Command};
+use rexpect::session::{spawn_command, PtySession};
+use tempfile::TempDir;
+
+struct TestContext {
+ workdir: TempDir,
+ ssh_port: u32,
+ docker_process: PtySession,
+}
+
+fn build_docker_image() -> Result<()> {
+ let mut command = std::process::Command::new("docker");
+ command.args([
+ "build",
+ "-t",
+ "shackle-server",
+ "--build-arg",
+ &format!("SHELL={}", cargo_bin(env!("CARGO_PKG_NAME")).display()),
+ "./",
+ ]);
+
+ let status = command.status()?;
+ if !status.success() {
+ bail!("Failed to build dockerfile");
+ }
+
+ Ok(())
+}
+
+fn spawn_ssh_server() -> Result<TestContext> {
+ build_docker_image()?;
+
+ let workdir = tempfile::tempdir()?;
+
+ let mut command = std::process::Command::new("docker");
+ // TODO: randomize port?
+ command.args(["run", "-it", "-p", "2022:22", "shackle-server"]);
+ command.current_dir(&workdir);
+ let docker_process = spawn_command(command, Some(3000))?;
+ Ok(TestContext {
+ workdir,
+ ssh_port: 2022,
+ docker_process,
+ })
+}
+
+fn connect_to_ssh_server_interactively(c: &TestContext) -> Result<PtySession> {
+ let mut command = std::process::Command::new("ssh");
+ command.args(["-p", &c.ssh_port.to_string(), "shukkie@localhost"]);
+ command.current_dir(&c.workdir);
+ let mut p = spawn_command(command, Some(3000))?;
+ expect_prompt(&mut p)?;
+ Ok(p)
+}
+
+fn expect_prompt(p: &mut PtySession) -> Result<()> {
+ p.exp_string("> ")?;
+ Ok(())
+}
+
+#[test]
+fn shows_a_prompt() -> Result<()> {
+ let c = spawn_ssh_server()?;
+ connect_to_ssh_server_interactively(&c)?;
+ Ok(())
+}