diff --git a/tests/SuccessHook.yml b/tests/SuccessHook.yml index 058db73..8efa72a 100755 --- a/tests/SuccessHook.yml +++ b/tests/SuccessHook.yml @@ -7,7 +7,7 @@ commands: success: - successCmd - errorCmd: + successCmd: name: get docker version cmd: docker getOutput: true diff --git a/tests/files_test.go b/tests/files_test.go index 9b04225..63b251d 100644 --- a/tests/files_test.go +++ b/tests/files_test.go @@ -7,9 +7,16 @@ import ( ) func TestRunCommandFileTest(t *testing.T) { - filePath := "packageCommands.yml" cmdLineStr := fmt.Sprintf("go run ../backy.go exec host -c checkDockerNoVersion -m localhost --cmdStdOut -f %s", filePath) - exec.Command("bash", "-c", cmdLineStr).Output() + cmd := exec.Command("bash", "-c", cmdLineStr) + output, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("Command failed: %v, Output: %s", err, string(output)) + } + + if len(output) == 0 { + t.Fatal("Expected command output, got none") + } } diff --git a/tests/integration_test.go b/tests/integration_test.go new file mode 100644 index 0000000..e25e28e --- /dev/null +++ b/tests/integration_test.go @@ -0,0 +1,61 @@ +package tests + +import ( + "os" + "os/exec" + "testing" +) + +func TestIntegration_ExecuteCommand(t *testing.T) { + tests := []struct { + name string + args []string + expectFail bool + }{ + { + name: "Version Command", + args: []string{"version"}, + expectFail: false, + }, + { + name: "Invalid Command", + args: []string{"invalid"}, + expectFail: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cmd := exec.Command("go", append([]string{"run", "../backy.go"}, tt.args...)...) + output, err := cmd.CombinedOutput() + + if tt.expectFail && err == nil { + t.Fatalf("Expected failure but got success. Output: %s", string(output)) + } + + if !tt.expectFail && err != nil { + t.Fatalf("Expected success but got failure. Error: %v, Output: %s", err, string(output)) + } + }) + } +} + +func TestIntegration_ExecuteCommandWithConfig(t *testing.T) { + configFile := "./SuccessHook.yml" + if _, err := os.Stat(configFile); os.IsNotExist(err) { + t.Fatalf("Config file not found: %s", configFile) + } + + cmd := exec.Command("go", "run", "../backy.go", "exec", "--config", configFile, "echoTestSuccess") + output, err := cmd.CombinedOutput() + + if err != nil { + t.Fatalf("Command execution failed. Error: %v, Output: %s", err, string(output)) + } + + if len(output) == 0 { + t.Fatal("Expected command output, got none") + } + + t.Logf("Command output: %s", string(output)) +}