Skip to content

Commit 6f8755e

Browse files
committed
Moving all podman tests as integration tests
This change required some rework and making arcam a library so i could access all the needed functions and things
1 parent 6dc1170 commit 6f8755e

25 files changed

+501
-459
lines changed

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ categories = ["command-line-utilities", "virtualization"]
99
repository = "https://github.com/sandorex/arcam"
1010
readme = "README.md"
1111

12+
[lib]
13+
name = "arcam"
14+
15+
[[bin]]
16+
name = "arcam"
17+
1218
[dependencies]
1319
clap_complete = "4.5.42"
1420
clap = { version = "4.5.11", features = ["derive", "env"] }

src/cli.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ impl ConfigArg {
5656
|| input.starts_with(".") // ex. .arcam.toml
5757
|| input.starts_with("/") // ex. /etc/arcam/configs/something.toml
5858
|| input.starts_with("~/") // ex. ~/.config/arcam/configs/something.toml
59-
|| input.ends_with(".toml")
59+
|| input.ends_with(".toml") // well no image is gonna end with .toml? right?
6060
{
61-
// well no image is gonna end with .toml? right?
62-
// it must be a path
6361
Ok(Self::File(PathBuf::from(input)))
6462
} else if let Some(config_name) = input.strip_prefix("@") {
6563
// @ is prefix for a config

src/commands/cmd_completion_generator.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
use crate::cli::CmdCompletionArgs;
2-
use crate::prelude::*;
1+
use crate::{APP_NAME, cli::CmdCompletionArgs, prelude::*};
32
use clap::CommandFactory;
43
use clap_complete::{generate, Shell};
5-
use std::io::IsTerminal;
6-
use std::io::Write;
4+
use std::io::{IsTerminal, Write};
75

86
fn gen(shell: Shell, buf: &mut dyn Write) {
97
let mut cmd = crate::cli::Cli::command();
10-
generate(shell, &mut cmd, env!("CARGO_BIN_NAME"), buf);
8+
generate(shell, &mut cmd, APP_NAME, buf);
119
}
1210

1311
fn detect_shell() -> Result<Shell> {

src/commands/cmd_exec.rs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -72,43 +72,3 @@ pub fn container_exec(ctx: Context, mut cli_args: cli::CmdExecArgs) -> Result<()
7272

7373
Ok(())
7474
}
75-
76-
#[cfg(test)]
77-
mod tests {
78-
use crate::engine::Podman;
79-
use crate::tests_prelude::*;
80-
use assert_cmd::Command;
81-
82-
#[test]
83-
#[ignore]
84-
fn cmd_exec_podman() -> Result<()> {
85-
let tempdir = tempfile::tempdir()?;
86-
87-
// create the container
88-
let cmd = Command::cargo_bin(env!("CARGO_BIN_NAME"))?
89-
.args(["start", DEBIAN_IMAGE])
90-
.current_dir(tempdir.path())
91-
.assert()
92-
.success();
93-
94-
let container = Container {
95-
engine: Box::new(Podman),
96-
container: String::from_utf8_lossy(&cmd.get_output().stdout)
97-
.trim()
98-
.to_string(),
99-
};
100-
101-
// create file in cwd
102-
Command::cargo_bin(env!("CARGO_BIN_NAME"))?
103-
.args(["exec", &container, "--", "touch", "file.txt"])
104-
.current_dir(tempdir.path())
105-
.assert()
106-
.success();
107-
108-
assert!(tempdir.path().join("file.txt").exists(), "File not created");
109-
110-
Ok(())
111-
}
112-
113-
// TODO test environment passing and --shell
114-
}

src/commands/cmd_exists.rs

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,55 +15,3 @@ pub fn container_exists(ctx: Context, cli_args: cli::CmdExistsArgs) -> Result<()
1515
exit(1);
1616
}
1717
}
18-
19-
#[cfg(test)]
20-
mod tests {
21-
use crate::engine::Podman;
22-
use crate::tests_prelude::*;
23-
use assert_cmd::Command;
24-
25-
#[test]
26-
#[ignore]
27-
fn cmd_exists_podman() -> Result<()> {
28-
let tempdir = tempfile::tempdir()?;
29-
30-
// no cwd containers yet
31-
Command::cargo_bin(env!("CARGO_BIN_NAME"))?
32-
.args(["exists"])
33-
.current_dir(tempdir.path())
34-
.assert()
35-
.failure()
36-
.code(1);
37-
38-
// create the container
39-
let cmd = Command::cargo_bin(env!("CARGO_BIN_NAME"))?
40-
.args(["start", DEBIAN_IMAGE])
41-
.current_dir(tempdir.path())
42-
.assert()
43-
.success();
44-
45-
let container = Container {
46-
engine: Box::new(Podman),
47-
container: String::from_utf8_lossy(&cmd.get_output().stdout)
48-
.trim()
49-
.to_string(),
50-
};
51-
52-
assert!(!container.is_empty(), "Container name is empty");
53-
54-
// test with explicitly set container_name
55-
Command::cargo_bin(env!("CARGO_BIN_NAME"))?
56-
.args(["exists", &container])
57-
.assert()
58-
.success();
59-
60-
// detect container from cwd
61-
Command::cargo_bin(env!("CARGO_BIN_NAME"))?
62-
.args(["exists"])
63-
.current_dir(tempdir.path())
64-
.assert()
65-
.success();
66-
67-
Ok(())
68-
}
69-
}

src/commands/cmd_init.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//! Contains all code that should run inside the container as the init
22
3-
use crate::command_extensions::*;
4-
use crate::prelude::*;
5-
use crate::FULL_VERSION;
3+
use crate::{APP_NAME, FULL_VERSION, command_extensions::*, prelude::*};
64
use std::fs::OpenOptions;
75
use std::io::prelude::*;
86
use std::os::unix::fs::{chown, lchown, symlink, PermissionsExt};
@@ -62,7 +60,7 @@ fn make_executable(path: &Path) -> Result<(), std::io::Error> {
6260
}
6361

6462
fn initialization() -> Result<()> {
65-
println!("{} {}", env!("CARGO_BIN_NAME"), FULL_VERSION);
63+
println!("{} {}", APP_NAME, FULL_VERSION);
6664

6765
let user = std::env::var("HOST_USER").context("HOST_USER is undefined")?;
6866
let uid = std::env::var("HOST_USER_UID").context("HOST_USER_UID is undefined")?;

src/commands/cmd_kill.rs

Lines changed: 0 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -59,117 +59,3 @@ pub fn kill_container(ctx: Context, mut cli_args: cli::CmdKillArgs) -> Result<()
5959

6060
Ok(())
6161
}
62-
63-
#[cfg(test)]
64-
mod tests {
65-
use crate::engine::Podman;
66-
use crate::tests_prelude::*;
67-
use assert_cmd::prelude::*;
68-
use rexpect::session::spawn_command;
69-
use std::process::Command;
70-
71-
#[test]
72-
#[ignore]
73-
fn cmd_kill_podman() -> Result<()> {
74-
let tempdir = tempfile::tempdir()?;
75-
76-
// create the container
77-
let cmd = Command::cargo_bin(env!("CARGO_BIN_NAME"))?
78-
.args(["start", DEBIAN_IMAGE])
79-
.current_dir(tempdir.path())
80-
.assert()
81-
.success();
82-
83-
let container = Container {
84-
engine: Box::new(Podman),
85-
container: String::from_utf8_lossy(&cmd.get_output().stdout)
86-
.trim()
87-
.to_string(),
88-
};
89-
90-
// it should exist now
91-
Command::cargo_bin(env!("CARGO_BIN_NAME"))?
92-
.args(["exists"])
93-
.current_dir(tempdir.path())
94-
.assert()
95-
.success();
96-
97-
// test with --yes
98-
Command::cargo_bin(env!("CARGO_BIN_NAME"))?
99-
.args(["kill", "-y", &container])
100-
.current_dir(tempdir.path())
101-
.assert()
102-
.success();
103-
104-
Ok(())
105-
}
106-
107-
#[test]
108-
#[ignore]
109-
fn cmd_kill_interactive_podman() -> Result<()> {
110-
let tempdir = tempfile::tempdir()?;
111-
112-
let cmd = Command::cargo_bin(env!("CARGO_BIN_NAME"))?
113-
.args(["start", DEBIAN_IMAGE])
114-
.current_dir(tempdir.path())
115-
.assert()
116-
.success();
117-
118-
let container = Container {
119-
engine: Box::new(Podman),
120-
container: String::from_utf8_lossy(&cmd.get_output().stdout)
121-
.trim()
122-
.to_string(),
123-
};
124-
125-
// try to kill to get the prompt
126-
let mut pty = {
127-
let mut c = Command::cargo_bin(env!("CARGO_PKG_NAME"))?;
128-
c.args(["kill", &container]);
129-
130-
spawn_command(c, Some(5_000))
131-
}?;
132-
133-
let (_, matched) = pty.exp_regex(r#"\"(.+)\".*\[y/N\]"#)?;
134-
assert!(
135-
matched.contains(&*container),
136-
"Wrong container name in prompt?"
137-
);
138-
139-
// send enter? so i check the default action
140-
pty.send_line("")?;
141-
pty.exp_eof()?;
142-
143-
// check if container is still running, as it should be
144-
Command::new("podman")
145-
.args(["container", "exists", &container])
146-
.assert()
147-
.success();
148-
149-
// run again and answer y
150-
let mut pty = {
151-
let mut c = Command::cargo_bin(env!("CARGO_PKG_NAME"))?;
152-
c.args(["kill", &container]);
153-
154-
spawn_command(c, Some(5_000))
155-
}?;
156-
157-
let (_, matched) = pty.exp_regex(r#"\"(.+)\".*\[y/N\]"#)?;
158-
assert!(
159-
matched.contains(&*container),
160-
"Wrong container name in prompt?"
161-
);
162-
163-
// send enter? so i check the default action
164-
pty.send_line("y")?;
165-
pty.exp_eof()?;
166-
167-
// check if container is still running, it should not be at this point
168-
Command::new("podman")
169-
.args(["container", "exists", &container])
170-
.assert()
171-
.failure();
172-
173-
Ok(())
174-
}
175-
}

src/commands/cmd_shell.rs

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -72,58 +72,3 @@ pub fn open_shell(ctx: Context, mut cli_args: cli::CmdShellArgs) -> Result<()> {
7272

7373
Ok(())
7474
}
75-
76-
#[cfg(test)]
77-
mod tests {
78-
use crate::{engine::Podman, tests_prelude::*};
79-
use assert_cmd::prelude::*;
80-
use rexpect::session::{spawn_command, PtyReplSession};
81-
use std::process::Command;
82-
83-
#[test]
84-
#[ignore]
85-
fn cmd_shell_podman() -> Result<()> {
86-
let tempdir = tempfile::tempdir()?;
87-
88-
let cmd = Command::cargo_bin(env!("CARGO_BIN_NAME"))?
89-
.args(["start", DEBIAN_IMAGE])
90-
.current_dir(tempdir.path())
91-
.assert()
92-
.success();
93-
94-
let container = Container {
95-
engine: Box::new(Podman),
96-
container: String::from_utf8_lossy(&cmd.get_output().stdout)
97-
.trim()
98-
.to_string(),
99-
};
100-
101-
let mut c = Command::cargo_bin(env!("CARGO_PKG_NAME"))?;
102-
c.args(["shell", &container]);
103-
c.current_dir(tempdir.path());
104-
105-
let mut pty = spawn_command(c, Some(5_000)).and_then(|p| {
106-
let mut session = PtyReplSession::new(p, "$".to_owned())
107-
.echo_on(true)
108-
.quit_command(Some("exit".to_owned()));
109-
110-
// wait until the prompt appears
111-
session.wait_for_prompt()?;
112-
113-
// set prompt to something simple
114-
session.send_line("export PS1='$ '")?;
115-
116-
// wait for prompt again
117-
session.wait_for_prompt()?;
118-
119-
Ok(session)
120-
})?;
121-
122-
// check if its running properly
123-
pty.send_line("echo $ARCAM_VERSION")?;
124-
pty.exp_string(env!("CARGO_PKG_VERSION"))?;
125-
pty.wait_for_prompt()?;
126-
127-
Ok(())
128-
}
129-
}

src/commands/cmd_start.rs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -445,50 +445,3 @@ asroot chown "$USER:$USER" {0}
445445
}
446446
}
447447
}
448-
449-
#[cfg(test)]
450-
mod tests {
451-
use crate::engine::Podman;
452-
use crate::tests_prelude::*;
453-
use assert_cmd::Command;
454-
455-
#[test]
456-
#[ignore]
457-
fn cmd_start_podman() -> Result<()> {
458-
let tempdir = tempfile::tempdir()?;
459-
460-
let cmd = Command::cargo_bin(env!("CARGO_BIN_NAME"))?
461-
.args(["start", DEBIAN_IMAGE])
462-
.current_dir(tempdir.path())
463-
.assert()
464-
.success();
465-
466-
let container = Container {
467-
engine: Box::new(Podman),
468-
container: String::from_utf8_lossy(&cmd.get_output().stdout)
469-
.trim()
470-
.to_string(),
471-
};
472-
473-
// make sure the returned container name is proper, to prevent issues
474-
// like with interactive downloading of images
475-
let re = regex::Regex::new(r"[A-Za-z0-9-]").unwrap();
476-
assert!(
477-
re.is_match(&container),
478-
"Container name from start command is invalid"
479-
);
480-
481-
// try to start another container in same directory
482-
Command::cargo_bin(env!("CARGO_BIN_NAME"))?
483-
.args(["start", "--name", &container, DEBIAN_IMAGE])
484-
.current_dir(tempdir.path())
485-
.assert()
486-
.failure()
487-
.stderr(format!(
488-
"Error: There are containers running in current directory: {:?}\n",
489-
container
490-
));
491-
492-
Ok(())
493-
}
494-
}

0 commit comments

Comments
 (0)