Skip to content

Commit

Permalink
adds remote integration testing with docker
Browse files Browse the repository at this point in the history
  • Loading branch information
flawiddsouza committed Nov 9, 2024
1 parent 2f92c5a commit fa91eef
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 2 deletions.
5 changes: 5 additions & 0 deletions servers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ hosts:
ssh_key_path: /home/username/.ssh/my_host
local:
host: localhost
remote:
host: localhost
port: 2222
user: root
password: password
2 changes: 1 addition & 1 deletion test-ymls/nested-json-parsing.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: Test
hosts: local
hosts: local, remote
tasks:
- name: Set vars
vars:
Expand Down
16 changes: 16 additions & 0 deletions test-ymls/nested-json-parsing.yml.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
Starting deployment: Test

Processing host: local

Executing task: Set vars

Executing task: Parse to json
Debug:
msg:
AccessKeyId: abc, SecretAccessKey: def

Executing task: Set and parse vars in one step
Debug:
msg:
AccessKeyId: ghi, SecretAccessKey: jkl

Processing host: remote

Executing task: Set vars

Executing task: Parse to json
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: Test
hosts: local
hosts: local, remote
tasks:
- name: Set & use var
vars:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
Starting deployment: Test

Processing host: local

Executing task: Set & use var
> echo test
test

Executing task: Set & use var
> echo test2
test2

Processing host: remote

Executing task: Set & use var
> echo test
test
Expand Down
47 changes: 47 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
use std::fs;
use std::process::Command;
use std::sync::Once;

static INIT: Once = Once::new();

struct DockerGuard;

impl Drop for DockerGuard {
fn drop(&mut self) {
stop_docker_container();
}
}

fn start_docker_container() {
let start_output = Command::new("docker")
.args(&["run", "-d", "--rm", "-p", "2222:2222", "--name", "ssh_test_server", "-e", "USER_NAME=root", "-e", "USER_PASSWD=password", "forumi0721/alpine-sshd:x64"])
.output()
.expect("Failed to start Docker container");

assert!(start_output.status.success());
}

fn stop_docker_container() {
let _stop_output = Command::new("docker")
.args(&["stop", "ssh_test_server"])
.output();
}

fn run_test(yml_file: &str, should_fail: bool, extra_vars: &str) {
let output = Command::new("cargo")
Expand All @@ -24,18 +50,28 @@ fn run_test(yml_file: &str, should_fail: bool, extra_vars: &str) {
assert_eq!(full_output, expected_output);
}

fn setup() -> DockerGuard {
INIT.call_once(|| {
start_docker_container();
});
DockerGuard
}

#[test]
fn setting_and_debugging_vars() {
setup();
run_test("test-ymls/setting-and-debugging-vars.yml", false, "");
}

#[test]
fn use_vars_in_command_and_shell() {
setup();
run_test("test-ymls/use-vars-in-command-and-shell.yml", false, "");
}

#[test]
fn setting_working_directory_before_running_commands() {
setup();
run_test(
"test-ymls/setting-working-directory-before-running-commands.yml",
false,
Expand All @@ -45,11 +81,13 @@ fn setting_working_directory_before_running_commands() {

#[test]
fn nested_json_parsing() {
setup();
run_test("test-ymls/nested-json-parsing.yml", false, "");
}

#[test]
fn setting_global_working_directory_before_running_commands() {
setup();
run_test(
"test-ymls/setting-global-working-directory-before-running-commands.yml",
false,
Expand All @@ -59,6 +97,7 @@ fn setting_global_working_directory_before_running_commands() {

#[test]
fn dont_run_2nd_deploy_if_1st_fails() {
setup();
run_test(
"test-ymls/dont-run-2nd-task-or-2nd-deploy-if-1st-fails.yml",
true,
Expand All @@ -68,6 +107,7 @@ fn dont_run_2nd_deploy_if_1st_fails() {

#[test]
fn use_output_of_one_task_shell_in_another_task_shell() {
setup();
run_test(
"test-ymls/use-output-of-one-task-shell-in-another-task-shell.yml",
false,
Expand All @@ -77,6 +117,7 @@ fn use_output_of_one_task_shell_in_another_task_shell() {

#[test]
fn set_and_use_vars_immediately_in_shell_and_command() {
setup();
run_test(
"test-ymls/set-and-use-vars-immediately-in-shell-and-command.yml",
false,
Expand All @@ -86,6 +127,7 @@ fn set_and_use_vars_immediately_in_shell_and_command() {

#[test]
fn debug_should_come_before_command_and_shell() {
setup();
run_test(
"test-ymls/debug-should-come-before-command-and-shell.yml",
false,
Expand All @@ -95,6 +137,7 @@ fn debug_should_come_before_command_and_shell() {

#[test]
fn nested_json_parsing_missing_property_error() {
setup();
run_test(
"test-ymls/nested-json-parsing-missing-property-error.yml",
true,
Expand All @@ -104,16 +147,19 @@ fn nested_json_parsing_missing_property_error() {

#[test]
fn missing_var_error() {
setup();
run_test("test-ymls/missing-var-error.yml", true, "");
}

#[test]
fn invalid_json_error() {
setup();
run_test("test-ymls/invalid-json-error.yml", true, "");
}

#[test]
fn extra_vars() {
setup();
run_test("test-ymls/extra-vars.yml", false, "cat=1 bat=2");
run_test(
"test-ymls/extra-vars.yml",
Expand All @@ -129,5 +175,6 @@ fn extra_vars() {

#[test]
fn when_condition() {
setup();
run_test("test-ymls/when-condition.yml", false, "condition=true");
}

0 comments on commit fa91eef

Please sign in to comment.