2022-05-22 04:36:02 +00:00
|
|
|
package steward
|
|
|
|
|
2022-05-22 06:08:32 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
2022-05-23 05:43:34 +00:00
|
|
|
"encoding/json"
|
2022-05-23 10:57:07 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
2022-05-23 05:43:34 +00:00
|
|
|
"net"
|
2022-05-22 13:56:54 +00:00
|
|
|
"net/http"
|
2022-05-23 04:37:31 +00:00
|
|
|
"path/filepath"
|
2022-05-22 06:08:32 +00:00
|
|
|
"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)
|
|
|
|
}
|
2022-05-22 06:08:32 +00:00
|
|
|
|
|
|
|
// 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")
|
2022-05-22 06:08:32 +00:00
|
|
|
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
|
2022-05-22 06:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2022-05-22 06:08:32 +00:00
|
|
|
// 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,
|
2022-05-22 06:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ns, err := natsserver.NewServer(nsOpt)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(" * failed: could not start the nats-server %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ns
|
|
|
|
}
|
|
|
|
|
2022-05-23 05:43:34 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-22 06:08:32 +00:00
|
|
|
func TestRequest(t *testing.T) {
|
2022-05-22 13:56:54 +00:00
|
|
|
type containsOrEquals int
|
2022-05-23 05:43:34 +00:00
|
|
|
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
|
2022-05-22 06:08:32 +00:00
|
|
|
message Message
|
|
|
|
want []byte
|
2022-05-22 13:56:54 +00:00
|
|
|
containsOrEquals
|
2022-05-23 05:43:34 +00:00
|
|
|
viaSocketOrCh
|
2022-05-22 06:08:32 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 03:51:06 +00:00
|
|
|
ns := newNatsServerForTesting(t, 42222)
|
2022-05-22 06:08:32 +00:00
|
|
|
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)
|
2022-05-22 06:08:32 +00:00
|
|
|
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)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-05-22 06:08:32 +00:00
|
|
|
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
|
|
|
{
|
2022-05-23 05:43:34 +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,
|
2022-05-23 05:43:34 +00:00
|
|
|
viaSocketOrCh: viaCh,
|
2022-05-22 13:56:54 +00:00
|
|
|
},
|
|
|
|
{
|
2022-05-23 05:43:34 +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,
|
2022-05-23 05:43:34 +00:00
|
|
|
viaSocketOrCh: viaSocket,
|
2022-05-23 04:37:31 +00:00
|
|
|
},
|
|
|
|
{
|
2022-05-23 05:43:34 +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",
|
2022-05-23 05:43:34 +00:00
|
|
|
FileName: "file1.result",
|
2022-05-23 04:37:31 +00:00
|
|
|
}, want: []byte("sau"),
|
|
|
|
containsOrEquals: fileContains,
|
2022-05-23 05:43:34 +00:00
|
|
|
viaSocketOrCh: viaCh,
|
2022-05-22 13:56:54 +00:00
|
|
|
},
|
|
|
|
{
|
2022-05-23 05:43:34 +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,
|
2022-05-23 05:43:34 +00:00
|
|
|
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,
|
2022-05-23 05:43:34 +00:00
|
|
|
viaSocketOrCh: viaCh,
|
2022-05-22 06:08:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-05-23 10:57:07 +00:00
|
|
|
// Range over the tests defined, and execute them, one at a time.
|
2022-05-22 06:08:32 +00:00
|
|
|
for _, tt := range tests {
|
2022-05-23 05:43:34 +00:00
|
|
|
switch tt.viaSocketOrCh {
|
|
|
|
case viaCh:
|
|
|
|
sam, err := newSubjectAndMessage(tt.message)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("newSubjectAndMessage failed: %v\n", err)
|
|
|
|
}
|
2022-05-22 06:08:32 +00:00
|
|
|
|
2022-05-23 05:43:34 +00:00
|
|
|
srv.toRingBufferCh <- []subjectAndMessage{sam}
|
2022-05-22 06:08:32 +00:00
|
|
|
|
2022-05-23 05:43:34 +00:00
|
|
|
case viaSocket:
|
|
|
|
msgs := []Message{tt.message}
|
|
|
|
writeMsgsToSocketTest(conf, msgs, t)
|
2022-05-22 13:56:54 +00:00
|
|
|
|
2022-05-23 05:43:34 +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 06:08:32 +00:00
|
|
|
}
|
2022-05-22 04:36:02 +00:00
|
|
|
}
|
|
|
|
}
|