2023-04-03 18:39:01 -07:00
|
|
|
package setup_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
gs "github.com/dragonheim/gagent/internal/gstructs"
|
|
|
|
"github.com/dragonheim/gagent/internal/setup"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetupMain(t *testing.T) {
|
|
|
|
config := gs.GagentConfig{
|
|
|
|
Name: "test-config",
|
|
|
|
Mode: "client",
|
|
|
|
UUID: "test-uuid",
|
|
|
|
ListenAddr: "127.0.0.1",
|
|
|
|
ClientPort: 1234,
|
|
|
|
RouterPort: 5678,
|
|
|
|
WorkerPort: 9012,
|
|
|
|
}
|
|
|
|
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
capturedOutput := captureOutput(func() {
|
|
|
|
setup.Main(wg, config)
|
|
|
|
})
|
|
|
|
|
|
|
|
expectedOutput := `Configuration file created`
|
|
|
|
if !strings.Contains(capturedOutput, expectedOutput) {
|
|
|
|
t.Errorf("Expected output to contain '%s', got '%s'", expectedOutput, capturedOutput)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func captureOutput(f func()) string {
|
2023-05-18 18:04:20 -07:00
|
|
|
original := log.Writer()
|
2023-04-03 18:39:01 -07:00
|
|
|
r, w, _ := os.Pipe()
|
|
|
|
log.SetOutput(w)
|
|
|
|
|
|
|
|
f()
|
|
|
|
|
|
|
|
w.Close()
|
|
|
|
log.SetOutput(original)
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
io.Copy(&buf, r)
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|