1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2024-12-15 17:51:15 +00:00
ctrl/steward_test.go

581 lines
15 KiB
Go
Raw Normal View History

// Package functionality tests for Steward.
//
// To turn on the normal logging functionality during tests, use:
// go test -logging=true
2021-08-09 07:18:30 +00:00
package steward
import (
2021-08-13 10:03:30 +00:00
"context"
2021-08-10 05:41:30 +00:00
"flag"
2021-08-13 10:03:30 +00:00
"fmt"
2021-08-09 12:41:31 +00:00
"io"
2021-08-10 05:41:30 +00:00
"log"
2021-08-09 12:41:31 +00:00
"net"
2021-08-12 12:52:01 +00:00
"net/http"
2021-08-09 12:41:31 +00:00
"os"
2021-08-09 07:18:30 +00:00
"path/filepath"
2021-08-09 12:41:31 +00:00
"strings"
2021-08-09 07:18:30 +00:00
"testing"
"time"
2021-08-13 10:03:30 +00:00
"github.com/fsnotify/fsnotify"
2021-08-09 07:18:30 +00:00
natsserver "github.com/nats-io/nats-server/v2/server"
)
// Set the default logging functionality of the package to false.
2021-08-10 05:41:30 +00:00
var logging = flag.Bool("logging", false, "set to true to enable the normal logger of the package")
2021-08-09 07:18:30 +00:00
2021-08-10 05:41:30 +00:00
// Test the overall functionality of Steward.
// Starting up the server.
// Message passing.
// The different REQ types.
func TestStewardServer(t *testing.T) {
if !*logging {
log.SetOutput(io.Discard)
2021-08-09 07:18:30 +00:00
}
2021-08-10 05:41:30 +00:00
// Start the nats-server message broker.
startNatsServerTest(t)
2021-08-09 07:18:30 +00:00
2021-08-09 12:41:31 +00:00
// Start Steward instance
// ---------------------------------------
// tempdir := t.TempDir()
2021-08-16 11:01:12 +00:00
// Create the config to run a steward instance.
tempdir := "./tmp"
2021-08-09 07:18:30 +00:00
conf := &Configuration{
SocketFolder: filepath.Join(tempdir, "tmp"),
DatabaseFolder: filepath.Join(tempdir, "var/lib"),
//SubscribersDataFolder: filepath.Join(tempdir, "data"),
SubscribersDataFolder: "./tmp/",
2022-02-07 06:25:43 +00:00
ConfigFolder: "./tmp/etc",
2021-08-10 05:41:30 +00:00
BrokerAddress: "127.0.0.1:40222",
2021-08-18 10:16:21 +00:00
PromHostAndPort: ":2112",
2021-08-10 05:41:30 +00:00
NodeName: "central",
CentralNodeName: "central",
DefaultMessageRetries: 1,
DefaultMessageTimeout: 3,
2022-01-19 10:22:56 +00:00
EnableSocket: true,
2022-02-07 03:23:13 +00:00
// AllowEmptySignature: true,
EnableDebug: true,
2021-08-10 05:41:30 +00:00
StartSubREQCliCommand: true,
StartSubREQCliCommandCont: true,
StartSubREQToConsole: true,
StartSubREQToFileAppend: true,
StartSubREQToFile: true,
StartSubREQHello: true,
StartSubREQErrorLog: true,
StartSubREQHttpGet: true,
StartSubREQTailFile: true,
// StartSubREQToSocket: flagNodeSlice{OK: true, Values: []Node{"*"}},
2021-08-09 07:18:30 +00:00
}
stewardServer, err := NewServer(conf, "test")
2021-08-09 07:18:30 +00:00
if err != nil {
2021-08-10 05:41:30 +00:00
t.Fatalf(" * failed: could not start the Steward instance %v\n", err)
2021-08-09 07:18:30 +00:00
}
2021-08-13 13:36:14 +00:00
stewardServer.Start()
2021-08-09 07:18:30 +00:00
2021-08-10 05:41:30 +00:00
// Run the message tests
2021-08-09 12:41:31 +00:00
//
// ---------------------------------------
2021-08-18 10:16:21 +00:00
type testFunc func(*server, *Configuration, *testing.T) error
2021-08-13 13:36:14 +00:00
// Specify all the test funcs to run in the slice.
funcs := []testFunc{
2021-11-16 19:18:32 +00:00
checkREQOpProcessListTest,
2021-08-13 13:36:14 +00:00
checkREQCliCommandTest,
checkREQCliCommandContTest,
2021-08-13 13:36:14 +00:00
// checkREQToConsoleTest(conf, t), NB: No tests will be made for console ouput.
// checkREQToFileAppendTest(conf, t), NB: Already tested via others
// checkREQToFileTest(conf, t), NB: Already tested via others
checkREQHelloTest,
checkREQErrorLogTest,
// checkREQPingTest(conf, t)
// checkREQPongTest(conf, t)
checkREQHttpGetTest,
checkREQTailFileTest,
// checkREQToSocketTest(conf, t)
checkErrorKernelMalformedJSONtest,
2021-08-18 10:16:21 +00:00
checkMetricValuesTest,
2021-11-11 09:26:23 +00:00
2022-04-20 05:13:47 +00:00
// checkREQRelay
2021-08-13 13:36:14 +00:00
}
2021-08-12 12:52:01 +00:00
2021-08-13 13:36:14 +00:00
for _, f := range funcs {
2021-08-18 10:16:21 +00:00
err := f(stewardServer, conf, t)
2021-08-13 13:36:14 +00:00
if err != nil {
t.Errorf("%v\n", err)
}
}
2021-08-10 05:41:30 +00:00
// ---------------------------------------
2021-08-09 07:34:05 +00:00
2021-08-13 13:36:14 +00:00
stewardServer.Stop()
2021-08-09 12:41:31 +00:00
2021-08-10 05:41:30 +00:00
}
2021-08-12 12:52:01 +00:00
// ----------------------------------------------------------------------------
// Check REQ types
// ----------------------------------------------------------------------------
2021-08-10 05:41:30 +00:00
// Testing op (operator) Commands.
2021-11-16 19:18:32 +00:00
func checkREQOpProcessListTest(stewardServer *server, conf *Configuration, t *testing.T) error {
2021-08-10 05:41:30 +00:00
m := `[
{
"directory":"opCommand",
"fileName": "fileName.result",
2021-08-10 05:41:30 +00:00
"toNode": "central",
"data": [],
2021-11-16 19:18:32 +00:00
"method":"REQOpProcessList",
"replyMethod":"REQToFile",
2021-08-10 05:41:30 +00:00
"ACKTimeout":3,
"retries":3,
"replyACKTimeout":3,
"replyRetries":3,
"MethodTimeout": 7
}
]`
writeToSocketTest(conf, m, t)
2021-09-07 13:24:04 +00:00
resultFile := filepath.Join(conf.SubscribersDataFolder, "opCommand", "central", "fileName.result")
2021-11-16 19:18:32 +00:00
_, err := findStringInFileTest("central.REQHttpGet.CommandACK", resultFile, conf, t)
2021-08-13 10:27:19 +00:00
if err != nil {
2021-11-16 19:18:32 +00:00
return fmt.Errorf(" \U0001F631 [FAILED] : checkREQOpProcessListTest: %v", err)
2021-08-13 10:27:19 +00:00
}
2021-08-10 05:41:30 +00:00
2021-09-10 05:47:40 +00:00
t.Logf(" \U0001f600 [SUCCESS] : checkREQOpCommandTest\n")
2021-08-13 13:36:14 +00:00
return nil
2021-08-10 05:41:30 +00:00
}
// Sending of CLI Commands.
2021-08-18 10:16:21 +00:00
func checkREQCliCommandTest(stewardServer *server, conf *Configuration, t *testing.T) error {
2021-08-09 12:41:31 +00:00
m := `[
{
"directory":"commands-executed",
"fileName":"fileName.result",
2021-08-09 12:41:31 +00:00
"toNode": "central",
2021-11-11 09:15:02 +00:00
"methodArgs": ["bash","-c","echo apekatt"],
2021-08-09 12:41:31 +00:00
"replyMethod":"REQToFileAppend",
2021-08-10 05:41:30 +00:00
"method":"REQCliCommand",
2021-08-09 12:41:31 +00:00
"ACKTimeout":3,
"retries":3,
"methodTimeout": 10
}
]`
2021-08-10 05:41:30 +00:00
writeToSocketTest(conf, m, t)
2021-08-09 12:41:31 +00:00
2021-09-07 13:24:04 +00:00
resultFile := filepath.Join(conf.SubscribersDataFolder, "commands-executed", "central", "fileName.result")
2021-08-13 10:27:19 +00:00
_, err := findStringInFileTest("apekatt", resultFile, conf, t)
if err != nil {
2021-11-11 09:15:02 +00:00
return fmt.Errorf(" \U0001F631 [FAILED] : checkREQCliCommandTest: %v", err)
2021-08-13 10:27:19 +00:00
}
2021-08-10 05:41:30 +00:00
2021-09-10 05:47:40 +00:00
t.Logf(" \U0001f600 [SUCCESS] : checkREQCliCommandTest\n")
2021-08-13 13:36:14 +00:00
return nil
2021-08-10 05:41:30 +00:00
}
2021-08-16 11:01:12 +00:00
// The continous non-sequential sending of CLI Commands.
func checkREQCliCommandContTest(stewardServer *server, conf *Configuration, t *testing.T) error {
m := `[
{
"directory":"commands-executed",
"fileName":"fileName.result",
"toNode": "central",
2021-11-11 09:15:02 +00:00
"methodArgs": ["bash","-c","echo apekatt && sleep 5 && echo gris"],
"replyMethod":"REQToFileAppend",
"method":"REQCliCommandCont",
"ACKTimeout":3,
"retries":3,
"methodTimeout": 5
}
]`
writeToSocketTest(conf, m, t)
2021-09-07 13:24:04 +00:00
resultFile := filepath.Join(conf.SubscribersDataFolder, "commands-executed", "central", "fileName.result")
2021-08-13 10:27:19 +00:00
_, err := findStringInFileTest("apekatt", resultFile, conf, t)
if err != nil {
2021-11-11 09:15:02 +00:00
return fmt.Errorf(" \U0001F631 [FAILED] : checkREQCliCommandContTest: %v", err)
2021-08-13 10:27:19 +00:00
}
t.Logf(" \U0001f600 [SUCCESS] : checkREQCliCommandContTest\n")
2021-08-13 13:36:14 +00:00
return nil
}
// Sending of Hello.
2021-08-18 10:16:21 +00:00
func checkREQHelloTest(stewardServer *server, conf *Configuration, t *testing.T) error {
m := `[
{
"directory":"commands-executed",
"fileName":"fileName.result",
"toNode": "central",
"data": [],
"method":"REQHello"
}
]`
writeToSocketTest(conf, m, t)
2021-09-07 13:24:04 +00:00
resultFile := filepath.Join(conf.SubscribersDataFolder, "commands-executed", "central", "fileName.result")
2021-08-13 10:27:19 +00:00
_, err := findStringInFileTest("Received hello from", resultFile, conf, t)
if err != nil {
2021-11-11 09:15:02 +00:00
return fmt.Errorf(" \U0001F631 [FAILED] : checkREQHelloTest: %v", err)
2021-08-13 10:27:19 +00:00
}
2021-09-10 05:47:40 +00:00
t.Logf(" \U0001f600 [SUCCESS] : checkREQHelloTest\n")
2021-08-13 13:36:14 +00:00
return nil
}
2021-08-16 11:01:12 +00:00
// Check the error logger type.
2021-08-18 10:16:21 +00:00
func checkREQErrorLogTest(stewardServer *server, conf *Configuration, t *testing.T) error {
2022-02-07 05:18:59 +00:00
m := Message{
ToNode: "somenode",
}
2021-08-12 12:52:01 +00:00
2022-04-01 05:09:55 +00:00
p := newProcess(stewardServer.ctx, stewardServer, Subject{}, processKindSubscriber, nil)
2022-02-07 05:18:59 +00:00
stewardServer.errorKernel.errSend(p, m, fmt.Errorf("some error"))
2021-08-12 12:52:01 +00:00
2022-02-07 05:18:59 +00:00
resultFile := filepath.Join(conf.SubscribersDataFolder, "errorLog", "errorCentral", "error.log")
2021-08-13 10:27:19 +00:00
_, err := findStringInFileTest("some error", resultFile, conf, t)
if err != nil {
2021-11-11 09:15:02 +00:00
return fmt.Errorf(" \U0001F631 [FAILED] : checkREQErrorLogTest: %v", err)
2021-08-13 10:27:19 +00:00
}
2021-08-12 12:52:01 +00:00
2021-09-10 05:47:40 +00:00
t.Logf(" \U0001f600 [SUCCESS] : checkREQErrorLogTest\n")
2021-08-13 13:36:14 +00:00
return nil
2021-08-12 12:52:01 +00:00
}
2021-08-16 11:01:12 +00:00
// Check http get method.
2021-08-18 10:16:21 +00:00
func checkREQHttpGetTest(stewardServer *server, conf *Configuration, t *testing.T) error {
2021-08-12 12:52:01 +00:00
// Web server for testing.
{
h := func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("web page content"))
}
http.HandleFunc("/", h)
go func() {
http.ListenAndServe(":10080", nil)
}()
}
m := `[
{
"directory": "httpget",
"fileName":"fileName.result",
2021-08-12 12:52:01 +00:00
"toNode": "central",
2021-11-11 09:15:02 +00:00
"methodArgs": ["http://127.0.0.1:10080/"],
2021-08-12 12:52:01 +00:00
"method": "REQHttpGet",
"replyMethod":"REQToFile",
"ACKTimeout":5,
"methodTimeout": 5
}
]`
writeToSocketTest(conf, m, t)
2021-09-07 13:24:04 +00:00
resultFile := filepath.Join(conf.SubscribersDataFolder, "httpget", "central", "fileName.result")
2021-08-13 10:27:19 +00:00
_, err := findStringInFileTest("web page content", resultFile, conf, t)
if err != nil {
2021-11-11 09:15:02 +00:00
return fmt.Errorf(" \U0001F631 [FAILED] : checkREQHttpGetTest: %v", err)
2021-08-13 10:27:19 +00:00
}
2021-08-12 12:52:01 +00:00
2021-09-10 05:47:40 +00:00
t.Logf(" \U0001f600 [SUCCESS] : checkREQHttpGetTest\n")
2021-08-13 13:36:14 +00:00
return nil
2021-08-12 12:52:01 +00:00
}
2021-08-16 11:01:12 +00:00
// Check the tailing of files type.
2021-08-18 10:16:21 +00:00
func checkREQTailFileTest(stewardServer *server, conf *Configuration, t *testing.T) error {
2021-08-13 10:03:30 +00:00
// Create a file with some content.
fh, err := os.OpenFile("test.file", os.O_APPEND|os.O_RDWR|os.O_CREATE|os.O_SYNC, 0600)
if err != nil {
2021-08-13 13:36:14 +00:00
return fmt.Errorf(" * failed: unable to open temporary file: %v", err)
2021-08-12 14:39:09 +00:00
}
2021-08-13 10:03:30 +00:00
defer fh.Close()
ctx, cancel := context.WithCancel(context.Background())
2021-08-13 10:59:57 +00:00
// Write content to the file with specified intervals.
2021-08-13 10:03:30 +00:00
go func() {
for i := 1; i <= 10; i++ {
_, err = fh.Write([]byte("some file content\n"))
if err != nil {
fmt.Printf(" * failed: writing to temporary file: %v\n", err)
}
fh.Sync()
time.Sleep(time.Millisecond * 500)
2021-08-12 14:39:09 +00:00
2021-08-13 10:59:57 +00:00
// Check if we've received a done, else default to continuing.
2021-08-13 10:03:30 +00:00
select {
case <-ctx.Done():
return
default:
// no done received, we're continuing.
}
}
}()
wd, err := os.Getwd()
2021-08-12 14:39:09 +00:00
if err != nil {
2021-08-13 13:36:14 +00:00
cancel()
2021-11-11 09:15:02 +00:00
return fmt.Errorf(" \U0001F631 [FAILED] : checkREQTailFileTest: : getting current working directory: %v", err)
2021-08-12 14:39:09 +00:00
}
2021-08-13 10:03:30 +00:00
file := filepath.Join(wd, "test.file")
s := `[
2021-08-13 10:03:30 +00:00
{
"directory": "tail-files",
"fileName": "fileName.result",
2021-08-13 10:03:30 +00:00
"toNode": "central",
2021-11-11 09:15:02 +00:00
"methodArgs": ["` + file + `"],
2021-08-13 10:03:30 +00:00
"method":"REQTailFile",
"ACKTimeout":5,
"retries":3,
"methodTimeout": 10
}
]`
2021-08-13 10:03:30 +00:00
writeToSocketTest(conf, s, t)
2021-08-12 14:39:09 +00:00
2021-08-13 10:59:57 +00:00
// time.Sleep(time.Second * 5)
2021-08-12 14:39:09 +00:00
2021-09-07 13:24:04 +00:00
resultFile := filepath.Join(conf.SubscribersDataFolder, "tail-files", "central", "fileName.result")
2021-08-13 10:59:57 +00:00
// Wait n times for result file to be created.
n := 5
for i := 0; i <= n; i++ {
_, err := os.Stat(resultFile)
if os.IsNotExist(err) {
time.Sleep(time.Millisecond * 1000)
continue
}
if os.IsNotExist(err) && i >= n {
2021-08-13 13:36:14 +00:00
cancel()
2021-11-11 09:15:02 +00:00
return fmt.Errorf(" \U0001F631 [FAILED] : checkREQTailFileTest: no result file created for request within the given time")
2021-08-13 10:59:57 +00:00
}
}
cancel()
2021-08-13 10:27:19 +00:00
_, err = findStringInFileTest("some file content", resultFile, conf, t)
if err != nil {
2021-11-11 09:15:02 +00:00
return fmt.Errorf(" \U0001F631 [FAILED] : checkREQTailFileTest: %v", err)
2021-08-13 10:27:19 +00:00
}
2021-08-12 14:39:09 +00:00
2021-09-10 05:47:40 +00:00
t.Logf(" \U0001f600 [SUCCESS] : checkREQTailFileTest\n")
2021-08-13 13:36:14 +00:00
return nil
2021-08-12 14:39:09 +00:00
}
2021-08-13 13:36:14 +00:00
// ----------------------------------------------------------------------------
// Functionality checks
// ----------------------------------------------------------------------------
2021-08-12 12:52:01 +00:00
// Check errorKernel
2021-08-18 10:16:21 +00:00
func checkErrorKernelMalformedJSONtest(stewardServer *server, conf *Configuration, t *testing.T) error {
2021-08-13 10:03:30 +00:00
// JSON message with error, missing brace.
2021-08-12 12:52:01 +00:00
m := `[
{
"directory": "some dir",
"fileName":"someext",
2021-08-12 12:52:01 +00:00
"toNode": "somenode",
"data": ["some data"],
"method": "REQErrorLog"
missing brace here.....
]`
writeToSocketTest(conf, m, t)
2021-09-08 17:52:06 +00:00
resultFile := filepath.Join(conf.SubscribersDataFolder, "errorLog", "errorCentral", "error.log")
2021-08-13 10:03:30 +00:00
// Wait n times for error file to be created.
n := 5
for i := 0; i <= n; i++ {
_, err := os.Stat(resultFile)
if os.IsNotExist(err) {
time.Sleep(time.Millisecond * 1000)
continue
}
if os.IsNotExist(err) && i >= n {
2021-11-11 09:15:02 +00:00
return fmt.Errorf(" \U0001F631 [FAILED] : checkErrorKernelMalformedJSONtest: no result file created for request within the given time")
2021-08-13 10:03:30 +00:00
}
}
// Start checking if the result file is being updated.
chUpdated := make(chan bool)
go checkFileUpdated(resultFile, chUpdated)
// We wait 5 seconds for an update, or else we fail.
ticker := time.NewTicker(time.Second * 5)
2021-08-13 10:27:19 +00:00
for {
select {
case <-chUpdated:
// We got an update, so we continue to check if we find the string we're
// looking for.
found, err := findStringInFileTest("error: malformed json", resultFile, conf, t)
if !found && err != nil {
2021-11-11 09:15:02 +00:00
return fmt.Errorf(" \U0001F631 [FAILED] : checkErrorKernelMalformedJSONtest: %v", err)
2021-08-13 10:27:19 +00:00
}
if !found && err == nil {
continue
}
if found {
2021-09-10 05:47:40 +00:00
t.Logf(" \U0001f600 [SUCCESS] : checkErrorKernelMalformedJSONtest")
2021-08-13 13:36:14 +00:00
return nil
2021-08-13 10:27:19 +00:00
}
case <-ticker.C:
2021-08-13 13:36:14 +00:00
return fmt.Errorf(" * failed: did not get an update in the errorKernel log file")
2021-08-13 10:27:19 +00:00
}
2021-08-13 10:03:30 +00:00
}
2021-08-12 12:52:01 +00:00
}
2021-08-18 10:16:21 +00:00
func checkMetricValuesTest(stewardServer *server, conf *Configuration, t *testing.T) error {
mfs, err := stewardServer.metrics.promRegistry.Gather()
if err != nil {
return fmt.Errorf("error: promRegistry.gathering: %v", mfs)
2021-08-18 10:16:21 +00:00
}
if len(mfs) <= 0 {
return fmt.Errorf("error: promRegistry.gathering: did not find any metric families: %v", mfs)
}
found := false
2021-08-18 10:16:21 +00:00
for _, mf := range mfs {
2021-09-08 17:52:06 +00:00
if mf.GetName() == "steward_processes_total" {
found = true
m := mf.GetMetric()
2021-08-18 10:16:21 +00:00
if m[0].Gauge.GetValue() <= 0 {
return fmt.Errorf("error: promRegistry.gathering: did not find any running processes in metric for processes_total : %v", m[0].Gauge.GetValue())
}
}
2021-08-18 10:16:21 +00:00
}
if !found {
return fmt.Errorf("error: promRegistry.gathering: did not find specified metric processes_total")
}
2021-09-10 05:47:40 +00:00
t.Logf(" \U0001f600 [SUCCESS] : checkMetricValuesTest")
2021-08-18 10:16:21 +00:00
return nil
}
// ----------------------------------------------------------------------------
// Helper functions for tests
// ----------------------------------------------------------------------------
2021-08-13 10:03:30 +00:00
// Check if file are getting updated with new content.
func checkFileUpdated(fileRealPath string, fileUpdated chan bool) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Println("Failed fsnotify.NewWatcher")
return
}
defer watcher.Close()
done := make(chan bool)
go func() {
//Give a true value to updated so it reads the file the first time.
fileUpdated <- true
for {
select {
case event := <-watcher.Events:
log.Println("event:", event)
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("modified file:", event.Name)
//testing with an update chan to get updates
fileUpdated <- true
}
case err := <-watcher.Errors:
log.Println("error:", err)
}
}
}()
err = watcher.Add(fileRealPath)
if err != nil {
log.Fatal(err)
}
<-done
}
2021-08-10 05:41:30 +00:00
// Check if a file contains the given string.
2021-08-13 10:27:19 +00:00
func findStringInFileTest(want string, fileName string, conf *Configuration, t *testing.T) (bool, error) {
2021-08-10 05:41:30 +00:00
// Wait n seconds for the results file to be created
n := 5
for i := 0; i <= n; i++ {
2021-08-10 05:41:30 +00:00
_, err := os.Stat(fileName)
if os.IsNotExist(err) {
time.Sleep(time.Millisecond * 500)
continue
}
if os.IsNotExist(err) && i >= n {
2021-08-13 10:27:19 +00:00
return false, fmt.Errorf(" * failed: no result file created for request within the given time\n")
2021-08-10 05:41:30 +00:00
}
}
fh, err := os.Open(fileName)
2021-08-09 12:41:31 +00:00
if err != nil {
2021-08-13 13:36:14 +00:00
return false, fmt.Errorf(" * failed: could not open result file: %v", err)
2021-08-09 12:41:31 +00:00
}
result, err := io.ReadAll(fh)
if err != nil {
2021-08-13 13:36:14 +00:00
return false, fmt.Errorf(" * failed: could not read result file: %v", err)
2021-08-09 12:41:31 +00:00
}
2021-08-09 07:18:30 +00:00
2021-08-10 05:41:30 +00:00
if !strings.Contains(string(result), want) {
2021-08-13 10:27:19 +00:00
return false, nil
2021-08-09 12:41:31 +00:00
}
2021-08-13 10:27:19 +00:00
return true, nil
2021-08-10 05:41:30 +00:00
}
2021-08-09 12:41:31 +00:00
2021-08-10 05:41:30 +00:00
// Write message to socket for testing purposes.
func writeToSocketTest(conf *Configuration, messageText string, t *testing.T) {
socket, err := net.Dial("unix", filepath.Join(conf.SocketFolder, "steward.sock"))
if err != nil {
t.Fatalf(" * failed: could to open socket file for writing: %v\n", err)
}
defer socket.Close()
2021-08-09 07:18:30 +00:00
2021-08-10 05:41:30 +00:00
_, err = socket.Write([]byte(messageText))
if err != nil {
t.Fatalf(" * failed: could not write to socket: %v\n", err)
}
2021-08-09 12:41:31 +00:00
2021-08-10 05:41:30 +00:00
}
// Start up the nats-server message broker.
func startNatsServerTest(t *testing.T) {
// Start up the nats-server message broker.
nsOpt := &natsserver.Options{
Host: "127.0.0.1",
Port: 40222,
}
ns, err := natsserver.NewServer(nsOpt)
if err != nil {
t.Fatalf(" * failed: could not start the nats-server %v\n", err)
}
if err := natsserver.Run(ns); err != nil {
natsserver.PrintAndDie(err.Error())
}
2021-08-09 07:18:30 +00:00
}