1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2024-12-14 12:37:31 +00:00
ctrl/requests_test.go

298 lines
7.6 KiB
Go
Raw Normal View History

2022-05-22 04:36:02 +00:00
package steward
import (
"bytes"
"encoding/json"
2022-05-23 10:57:07 +00:00
"io"
"log"
"net"
2022-05-22 13:56:54 +00:00
"net/http"
2022-05-23 04:37:31 +00:00
"path/filepath"
"strings"
"testing"
natsserver "github.com/nats-io/nats-server/v2/server"
)
2022-05-23 10:57:07 +00:00
func newServerForTesting(t *testing.T, addressAndPort string, testFolder string) (*server, *Configuration) {
if !*logging {
log.SetOutput(io.Discard)
}
// Start Steward instance
// ---------------------------------------
// tempdir := t.TempDir()
// Create the config to run a steward instance.
2022-05-22 13:56:54 +00:00
//tempdir := "./tmp"
conf := newConfigurationDefaults()
2022-05-23 03:51:06 +00:00
conf.BrokerAddress = addressAndPort
2022-05-22 13:56:54 +00:00
conf.NodeName = "central"
conf.CentralNodeName = "central"
2022-05-23 10:57:07 +00:00
conf.ConfigFolder = testFolder
conf.SubscribersDataFolder = testFolder
conf.SocketFolder = testFolder
conf.SubscribersDataFolder = testFolder
conf.DatabaseFolder = testFolder
2022-05-23 03:51:06 +00:00
conf.StartSubREQErrorLog = true
2022-05-22 13:56:54 +00:00
stewardServer, err := NewServer(&conf, "test")
if err != nil {
t.Fatalf(" * failed: could not start the Steward instance %v\n", err)
}
2022-05-23 03:51:06 +00:00
return stewardServer, &conf
}
// Start up the nats-server message broker for testing purposes.
2022-05-23 03:51:06 +00:00
func newNatsServerForTesting(t *testing.T, port int) *natsserver.Server {
// Start up the nats-server message broker.
nsOpt := &natsserver.Options{
Host: "127.0.0.1",
2022-05-23 03:51:06 +00:00
Port: port,
}
ns, err := natsserver.NewServer(nsOpt)
if err != nil {
t.Fatalf(" * failed: could not start the nats-server %v\n", err)
}
return ns
}
// Write message to socket for testing purposes.
func writeMsgsToSocketTest(conf *Configuration, messages []Message, t *testing.T) {
js, err := json.Marshal(messages)
if err != nil {
t.Fatalf("writeMsgsToSocketTest: %v\n ", err)
}
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()
_, err = socket.Write(js)
if err != nil {
t.Fatalf(" * failed: could not write to socket: %v\n", err)
}
}
func TestRequest(t *testing.T) {
2022-05-22 13:56:54 +00:00
type containsOrEquals int
const (
REQTestContains containsOrEquals = iota
REQTestEquals containsOrEquals = iota
fileContains containsOrEquals = iota
)
type viaSocketOrCh int
const (
viaSocket viaSocketOrCh = iota
viaCh viaSocketOrCh = iota
)
2022-05-22 13:56:54 +00:00
2022-05-22 04:36:02 +00:00
type test struct {
2022-05-22 13:56:54 +00:00
info string
message Message
want []byte
2022-05-22 13:56:54 +00:00
containsOrEquals
viaSocketOrCh
}
2022-05-23 03:51:06 +00:00
ns := newNatsServerForTesting(t, 42222)
if err := natsserver.Run(ns); err != nil {
natsserver.PrintAndDie(err.Error())
}
defer ns.Shutdown()
2022-05-23 13:11:58 +00:00
// tempdir := t.TempDir()
tempdir := "tmp"
2022-05-23 10:57:07 +00:00
srv, conf := newServerForTesting(t, "127.0.0.1:42222", tempdir)
srv.Start()
defer srv.Stop()
2022-05-22 13:56:54 +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)
}()
}
tests := []test{
2022-05-23 13:11:58 +00:00
{
info: "REQHello test",
message: Message{
ToNode: "errorCentral",
FromNode: "errorCentral",
Method: REQErrorLog,
MethodArgs: []string{},
MethodTimeout: 5,
Data: []byte("error data"),
// ReplyMethod: REQTest,
Directory: "error_log",
FileName: "error.results",
}, want: []byte("error data"),
containsOrEquals: fileContains,
viaSocketOrCh: viaCh,
},
2022-05-23 10:57:07 +00:00
{
info: "REQHello test",
message: Message{
ToNode: "central",
FromNode: "central",
Method: REQHello,
MethodArgs: []string{},
MethodTimeout: 5,
// ReplyMethod: REQTest,
Directory: "test",
FileName: "hello.results",
}, want: []byte("Received hello from \"central\""),
containsOrEquals: fileContains,
viaSocketOrCh: viaCh,
},
2022-05-22 13:56:54 +00:00
{
info: "REQCliCommand test, echo gris",
2022-05-22 13:56:54 +00:00
message: Message{
ToNode: "central",
FromNode: "central",
Method: REQCliCommand,
MethodArgs: []string{"bash", "-c", "echo gris"},
MethodTimeout: 5,
ReplyMethod: REQTest,
}, want: []byte("gris"),
2022-05-23 04:37:31 +00:00
containsOrEquals: REQTestEquals,
viaSocketOrCh: viaCh,
2022-05-22 13:56:54 +00:00
},
{
info: "REQCliCommand test via socket, echo sau",
2022-05-22 13:56:54 +00:00
message: Message{
ToNode: "central",
FromNode: "central",
Method: REQCliCommand,
MethodArgs: []string{"bash", "-c", "echo sau"},
MethodTimeout: 5,
ReplyMethod: REQTest,
}, want: []byte("sau"),
2022-05-23 04:37:31 +00:00
containsOrEquals: REQTestEquals,
viaSocketOrCh: viaSocket,
2022-05-23 04:37:31 +00:00
},
{
info: "REQCliCommand test, echo sau, result in file",
2022-05-23 04:37:31 +00:00
message: Message{
ToNode: "central",
FromNode: "central",
Method: REQCliCommand,
MethodArgs: []string{"bash", "-c", "echo sau"},
MethodTimeout: 5,
ReplyMethod: REQToFile,
Directory: "test",
FileName: "file1.result",
2022-05-23 04:37:31 +00:00
}, want: []byte("sau"),
containsOrEquals: fileContains,
viaSocketOrCh: viaCh,
2022-05-22 13:56:54 +00:00
},
{
info: "REQCliCommand test, echo several, result in file continous",
message: Message{
ToNode: "central",
FromNode: "central",
Method: REQCliCommand,
MethodArgs: []string{"bash", "-c", "echo giraff && echo sau && echo apekatt"},
MethodTimeout: 5,
ReplyMethod: REQToFile,
Directory: "test",
FileName: "file2.result",
}, want: []byte("sau"),
containsOrEquals: fileContains,
viaSocketOrCh: viaCh,
},
{
info: "REQHttpGet test, localhost:10080",
2022-05-22 13:56:54 +00:00
message: Message{
ToNode: "central",
FromNode: "central",
Method: REQHttpGet,
MethodArgs: []string{"http://localhost:10080"},
MethodTimeout: 5,
ReplyMethod: REQTest,
}, want: []byte("web page content"),
2022-05-23 04:37:31 +00:00
containsOrEquals: REQTestContains,
viaSocketOrCh: viaCh,
2022-05-22 13:56:54 +00:00
},
{
info: "REQOpProcessList test",
message: Message{
ToNode: "central",
FromNode: "central",
Method: REQOpProcessList,
MethodArgs: []string{},
MethodTimeout: 5,
ReplyMethod: REQTest,
}, want: []byte("central.REQHttpGet.EventACK"),
2022-05-23 04:37:31 +00:00
containsOrEquals: REQTestContains,
viaSocketOrCh: viaCh,
},
}
2022-05-23 10:57:07 +00:00
// Range over the tests defined, and execute them, one at a time.
for _, tt := range tests {
switch tt.viaSocketOrCh {
case viaCh:
sam, err := newSubjectAndMessage(tt.message)
if err != nil {
t.Fatalf("newSubjectAndMessage failed: %v\n", err)
}
srv.toRingBufferCh <- []subjectAndMessage{sam}
case viaSocket:
msgs := []Message{tt.message}
writeMsgsToSocketTest(conf, msgs, t)
2022-05-22 13:56:54 +00:00
}
switch tt.containsOrEquals {
2022-05-23 04:37:31 +00:00
case REQTestEquals:
result := <-srv.errorKernel.testCh
resStr := string(result)
resStr = strings.TrimSuffix(resStr, "\n")
result = []byte(resStr)
2022-05-22 13:56:54 +00:00
if !bytes.Equal(result, tt.want) {
t.Fatalf(" \U0001F631 [FAILED] :%v : want: %v, got: %v\n", tt.info, string(tt.want), string(result))
}
t.Logf(" \U0001f600 [SUCCESS] : %v\n", tt.info)
2022-05-23 04:37:31 +00:00
case REQTestContains:
result := <-srv.errorKernel.testCh
resStr := string(result)
resStr = strings.TrimSuffix(resStr, "\n")
result = []byte(resStr)
2022-05-22 13:56:54 +00:00
if !strings.Contains(string(result), string(tt.want)) {
t.Fatalf(" \U0001F631 [FAILED] :%v : want: %v, got: %v\n", tt.info, string(tt.want), string(result))
}
t.Logf(" \U0001f600 [SUCCESS] : %v\n", tt.info)
2022-05-23 04:37:31 +00:00
case fileContains:
resultFile := filepath.Join(conf.SubscribersDataFolder, tt.message.Directory, string(tt.message.FromNode), tt.message.FileName)
2022-05-23 10:57:07 +00:00
found, err := findStringInFileTest(string(tt.want), resultFile, conf, t)
if err != nil || found == false {
2022-05-23 04:37:31 +00:00
t.Fatalf(" \U0001F631 [FAILED] : %v: %v\n", tt.info, err)
}
t.Logf(" \U0001f600 [SUCCESS] : %v\n", tt.info)
}
2022-05-22 04:36:02 +00:00
}
}