This commit is contained in:
parent
27c29c185a
commit
dfb8c35bc6
1 changed files with 11 additions and 37 deletions
|
@ -2,7 +2,6 @@ package tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -10,9 +9,7 @@ import (
|
||||||
"math/big"
|
"math/big"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -56,50 +53,27 @@ func TestApplication(t *testing.T) {
|
||||||
// Ensure the test runs from the root directory
|
// Ensure the test runs from the root directory
|
||||||
rootDir := "../" // Path to the root directory of the repository
|
rootDir := "../" // Path to the root directory of the repository
|
||||||
|
|
||||||
// Build the application using `run.sh --build`
|
// Run the application using `run.sh`
|
||||||
buildCmd := exec.Command("sh", "./run.sh", "--build")
|
runCmd := exec.Command("sh", "./run.sh", "--skip-config-check")
|
||||||
buildCmd.Dir = rootDir
|
runCmd.Dir = rootDir
|
||||||
|
|
||||||
buildOutput, err := buildCmd.CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to build application: %v\nOutput:\n%s", err, string(buildOutput))
|
|
||||||
}
|
|
||||||
t.Log("Application built successfully")
|
|
||||||
|
|
||||||
// Path to the built executable relative to rootDir
|
|
||||||
executablePath := "./qgato" // Since cmd.Dir is rootDir, this path is relative to rootDir
|
|
||||||
|
|
||||||
// Ensure the executable has execute permissions
|
|
||||||
execFullPath := filepath.Join(rootDir, "qgato")
|
|
||||||
if err := os.Chmod(execFullPath, 0755); err != nil {
|
|
||||||
t.Fatalf("Failed to set execute permissions on the executable: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a context with cancellation
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
defer cancel() // Ensure resources are cleaned up
|
|
||||||
|
|
||||||
// Start the application using the built executable
|
|
||||||
cmd := exec.CommandContext(ctx, executablePath, "--skip-config-check")
|
|
||||||
cmd.Dir = rootDir // Set the working directory to the root directory
|
|
||||||
|
|
||||||
// Set process group ID so we can kill it and its children
|
// Set process group ID so we can kill it and its children
|
||||||
cmd.SysProcAttr = &syscall.SysProcAttr{
|
runCmd.SysProcAttr = &syscall.SysProcAttr{
|
||||||
Setpgid: true,
|
Setpgid: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Capture application output for logging
|
// Capture application output for logging
|
||||||
appStdout, err := cmd.StdoutPipe()
|
appStdout, err := runCmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to capture stdout: %v", err)
|
t.Fatalf("Failed to capture stdout: %v", err)
|
||||||
}
|
}
|
||||||
appStderr, err := cmd.StderrPipe()
|
appStderr, err := runCmd.StderrPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to capture stderr: %v", err)
|
t.Fatalf("Failed to capture stderr: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the application
|
// Start the application
|
||||||
if err := cmd.Start(); err != nil {
|
if err := runCmd.Start(); err != nil {
|
||||||
t.Fatalf("Failed to start application: %v", err)
|
t.Fatalf("Failed to start application: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,14 +94,14 @@ func TestApplication(t *testing.T) {
|
||||||
// Defer cleanup to ensure process is killed after the test
|
// Defer cleanup to ensure process is killed after the test
|
||||||
defer func() {
|
defer func() {
|
||||||
// Kill the process group
|
// Kill the process group
|
||||||
pgid, err := syscall.Getpgid(cmd.Process.Pid)
|
pgid, err := syscall.Getpgid(runCmd.Process.Pid)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
syscall.Kill(-pgid, syscall.SIGKILL)
|
syscall.Kill(-pgid, syscall.SIGKILL)
|
||||||
} else {
|
} else {
|
||||||
t.Logf("Failed to get process group ID: %v", err)
|
t.Logf("Failed to get process group ID: %v", err)
|
||||||
cmd.Process.Kill()
|
runCmd.Process.Kill()
|
||||||
}
|
}
|
||||||
cmd.Wait()
|
runCmd.Wait()
|
||||||
|
|
||||||
// Print summary
|
// Print summary
|
||||||
printSummary(summary, t)
|
printSummary(summary, t)
|
||||||
|
@ -141,7 +115,7 @@ func TestApplication(t *testing.T) {
|
||||||
t.Log("Application is running")
|
t.Log("Application is running")
|
||||||
|
|
||||||
// Create a process instance for the application
|
// Create a process instance for the application
|
||||||
appProcess, err := process.NewProcess(int32(cmd.Process.Pid))
|
appProcess, err := process.NewProcess(int32(runCmd.Process.Pid))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to create process instance: %v", err)
|
t.Fatalf("Failed to create process instance: %v", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue