Skip to content

Commit

Permalink
all integration tests now test against a local system + a remote system
Browse files Browse the repository at this point in the history
  • Loading branch information
flawiddsouza committed Nov 9, 2024
1 parent d493162 commit 3c23d76
Show file tree
Hide file tree
Showing 20 changed files with 48 additions and 70 deletions.
2 changes: 1 addition & 1 deletion test-ymls/debug-should-come-before-command-and-shell.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- name: Test
hosts: local
hosts: test
tasks:
- name: test debug
debug:
Expand Down
4 changes: 2 additions & 2 deletions test-ymls/dont-run-2nd-task-or-2nd-deploy-if-1st-fails.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- name: Test
hosts: local
hosts: test
tasks:
- name: exit code 1
shell: |
Expand All @@ -10,7 +10,7 @@
shell: echo "Test"

- name: Test 2
hosts: local
hosts: test
tasks:
- name: Echo Test 2
shell: echo "Test 2"
2 changes: 1 addition & 1 deletion test-ymls/extra-vars.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- name: Test
hosts: local
hosts: test
tasks:
- name: print extra vars cat & bat
debug:
Expand Down
2 changes: 1 addition & 1 deletion test-ymls/invalid-json-error.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- name: Test
hosts: local
hosts: test
tasks:
- name: test debug
shell: |
Expand Down
2 changes: 1 addition & 1 deletion test-ymls/missing-var-error.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: Test
hosts: local
hosts: test
tasks:
- name: Set & print variable
vars:
Expand Down
2 changes: 1 addition & 1 deletion test-ymls/multiline-command-in-shell.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- name: Test
hosts: local
hosts: test
tasks:
- name: multi line command in shell
shell: |
Expand Down
2 changes: 1 addition & 1 deletion test-ymls/nested-json-parsing-missing-property-error.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: Test
hosts: local
hosts: test
tasks:
- name: Set, parse & print
vars:
Expand Down
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, remote
hosts: test
tasks:
- name: Set vars
vars:
Expand Down
16 changes: 0 additions & 16 deletions test-ymls/nested-json-parsing.yml.out
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
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, remote
hosts: test
tasks:
- name: Set & use var
vars:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
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
2 changes: 1 addition & 1 deletion test-ymls/setting-and-debugging-vars.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: Test
hosts: local
hosts: test
tasks:
- name: Set vars
vars:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: Test
hosts: local
hosts: test
chdir: tests/mocks/ls-dir
tasks:
- name: Global working directory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: Test
hosts: local
hosts: test
tasks:
- name: Set vars
chdir: tests/mocks/ls-dir
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: Test
hosts: local
hosts: test
tasks:
- name: Generate Output
shell: echo "hello"
Expand Down
2 changes: 1 addition & 1 deletion test-ymls/use-vars-in-command-and-shell.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: Test
hosts: local
hosts: test
tasks:
- name: Set vars
vars:
Expand Down
2 changes: 1 addition & 1 deletion test-ymls/when-condition.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- name: Test when condition
hosts: local
hosts: test
tasks:
- name: should run when condition is true
shell: echo "Condition is true"
Expand Down
49 changes: 23 additions & 26 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ fn stop_docker_container() {
.output();
}

fn run_test(yml_file: &str, should_fail: bool, extra_vars: &str) {
fn run_test(yml_file: &str, should_fail: bool, extra_vars: &str, inventory_file: &str) {
let output = Command::new("cargo")
.args(&["run", "--quiet", "--", yml_file, "--extra-vars", extra_vars])
.args(&["run", "--quiet", "--", yml_file, "--extra-vars", extra_vars, "--inventory", inventory_file])
.output()
.expect("Failed to execute command");

Expand Down Expand Up @@ -57,22 +57,27 @@ fn setup() -> DockerGuard {
DockerGuard
}

fn run_tests_for_both_inventories(yml_file: &str, should_fail: bool, extra_vars: &str) {
run_test(yml_file, should_fail, extra_vars, "tests/servers/local.yml");
run_test(yml_file, should_fail, extra_vars, "tests/servers/remote.yml");
}

#[test]
fn setting_and_debugging_vars() {
setup();
run_test("test-ymls/setting-and-debugging-vars.yml", false, "");
run_tests_for_both_inventories("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, "");
run_tests_for_both_inventories("test-ymls/use-vars-in-command-and-shell.yml", false, "");
}

#[test]
fn setting_working_directory_before_running_commands() {
setup();
run_test(
run_tests_for_both_inventories(
"test-ymls/setting-working-directory-before-running-commands.yml",
false,
"",
Expand All @@ -82,13 +87,13 @@ fn setting_working_directory_before_running_commands() {
#[test]
fn nested_json_parsing() {
setup();
run_test("test-ymls/nested-json-parsing.yml", false, "");
run_tests_for_both_inventories("test-ymls/nested-json-parsing.yml", false, "");
}

#[test]
fn setting_global_working_directory_before_running_commands() {
setup();
run_test(
run_tests_for_both_inventories(
"test-ymls/setting-global-working-directory-before-running-commands.yml",
false,
"",
Expand All @@ -98,7 +103,7 @@ fn setting_global_working_directory_before_running_commands() {
#[test]
fn dont_run_2nd_deploy_if_1st_fails() {
setup();
run_test(
run_tests_for_both_inventories(
"test-ymls/dont-run-2nd-task-or-2nd-deploy-if-1st-fails.yml",
true,
"",
Expand All @@ -108,7 +113,7 @@ fn dont_run_2nd_deploy_if_1st_fails() {
#[test]
fn use_output_of_one_task_shell_in_another_task_shell() {
setup();
run_test(
run_tests_for_both_inventories(
"test-ymls/use-output-of-one-task-shell-in-another-task-shell.yml",
false,
"",
Expand All @@ -118,7 +123,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(
run_tests_for_both_inventories(
"test-ymls/set-and-use-vars-immediately-in-shell-and-command.yml",
false,
"",
Expand All @@ -128,7 +133,7 @@ fn set_and_use_vars_immediately_in_shell_and_command() {
#[test]
fn debug_should_come_before_command_and_shell() {
setup();
run_test(
run_tests_for_both_inventories(
"test-ymls/debug-should-come-before-command-and-shell.yml",
false,
"",
Expand All @@ -138,7 +143,7 @@ fn debug_should_come_before_command_and_shell() {
#[test]
fn nested_json_parsing_missing_property_error() {
setup();
run_test(
run_tests_for_both_inventories(
"test-ymls/nested-json-parsing-missing-property-error.yml",
true,
"",
Expand All @@ -148,33 +153,25 @@ fn nested_json_parsing_missing_property_error() {
#[test]
fn missing_var_error() {
setup();
run_test("test-ymls/missing-var-error.yml", true, "");
run_tests_for_both_inventories("test-ymls/missing-var-error.yml", true, "");
}

#[test]
fn invalid_json_error() {
setup();
run_test("test-ymls/invalid-json-error.yml", true, "");
run_tests_for_both_inventories("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",
false,
"{ \"cat\": 1, \"bat\": 2 }",
);
run_test(
"test-ymls/extra-vars.yml",
false,
"@test-ymls/extra-vars.vars.yml",
);
run_tests_for_both_inventories("test-ymls/extra-vars.yml", false, "cat=1 bat=2");
run_tests_for_both_inventories("test-ymls/extra-vars.yml", false, "{ \"cat\": 1, \"bat\": 2 }");
run_tests_for_both_inventories("test-ymls/extra-vars.yml", false, "@test-ymls/extra-vars.vars.yml");
}

#[test]
fn when_condition() {
setup();
run_test("test-ymls/when-condition.yml", false, "condition=true");
run_tests_for_both_inventories("test-ymls/when-condition.yml", false, "condition=true");
}
3 changes: 3 additions & 0 deletions tests/servers/local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hosts:
test:
host: localhost
6 changes: 6 additions & 0 deletions tests/servers/remote.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
hosts:
test:
host: localhost
port: 2222
user: root
password: password

0 comments on commit 3c23d76

Please sign in to comment.