2021-08-10 08:53:15 +00:00
|
|
|
// 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-10 05:41:30 +00:00
|
|
|
"flag"
|
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"
|
|
|
|
"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"
|
|
|
|
|
|
|
|
natsserver "github.com/nats-io/nats-server/v2/server"
|
|
|
|
)
|
|
|
|
|
2021-08-10 08:53:15 +00:00
|
|
|
// 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
|
|
|
|
// ---------------------------------------
|
2021-08-10 08:53:15 +00:00
|
|
|
// tempdir := t.TempDir()
|
|
|
|
tempdir := "./tmp"
|
2021-08-09 07:18:30 +00:00
|
|
|
conf := &Configuration{
|
2021-08-10 05:41:30 +00:00
|
|
|
SocketFolder: filepath.Join(tempdir, "tmp"),
|
|
|
|
DatabaseFolder: filepath.Join(tempdir, "var/lib"),
|
|
|
|
SubscribersDataFolder: filepath.Join(tempdir, "data"),
|
|
|
|
BrokerAddress: "127.0.0.1:40222",
|
|
|
|
NodeName: "central",
|
|
|
|
CentralNodeName: "central",
|
|
|
|
DefaultMessageRetries: 1,
|
|
|
|
DefaultMessageTimeout: 3,
|
|
|
|
|
2021-08-10 08:53:15 +00:00
|
|
|
StartSubREQCliCommand: flagNodeSlice{OK: true, Values: []Node{"*"}},
|
|
|
|
StartSubREQnCliCommand: flagNodeSlice{OK: true, Values: []Node{"*"}},
|
|
|
|
// StartSubREQnCliCommandCont: flagNodeSlice{OK: true, Values: []Node{"*"}},
|
2021-08-09 12:41:31 +00:00
|
|
|
StartSubREQErrorLog: flagNodeSlice{OK: true, Values: []Node{"*"}},
|
2021-08-10 08:53:15 +00:00
|
|
|
StartSubREQToFile: flagNodeSlice{OK: true, Values: []Node{"*"}},
|
2021-08-09 12:41:31 +00:00
|
|
|
StartSubREQToFileAppend: flagNodeSlice{OK: true, Values: []Node{"*"}},
|
2021-08-10 08:53:15 +00:00
|
|
|
StartSubREQHello: flagNodeSlice{OK: true, Values: []Node{"*"}},
|
2021-08-09 07:18:30 +00:00
|
|
|
}
|
|
|
|
s, err := NewServer(conf)
|
|
|
|
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
|
|
|
}
|
|
|
|
s.Start()
|
|
|
|
|
2021-08-10 05:41:30 +00:00
|
|
|
// Run the message tests
|
2021-08-09 12:41:31 +00:00
|
|
|
//
|
|
|
|
// ---------------------------------------
|
2021-08-10 08:53:15 +00:00
|
|
|
|
2021-08-10 05:41:30 +00:00
|
|
|
checkREQOpCommandTest(conf, t)
|
|
|
|
checkREQCliCommandTest(conf, t)
|
|
|
|
checkREQnCliCommandTest(conf, t)
|
2021-08-10 08:53:15 +00:00
|
|
|
// checkREQnCliCommandContTest(conf, t)
|
|
|
|
// 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(conf, t)
|
|
|
|
// checkREQErrorLogTest(conf, t)
|
|
|
|
// checkREQPingTest(conf, t)
|
|
|
|
// checkREQPongTest(conf, t)
|
|
|
|
// checkREQHttpGetTest(conf, t)
|
|
|
|
// checkREQTailFileTest(conf, t)
|
|
|
|
// checkREQToSocketTest(conf, t)
|
|
|
|
|
2021-08-10 05:41:30 +00:00
|
|
|
// ---------------------------------------
|
2021-08-09 07:34:05 +00:00
|
|
|
|
2021-08-10 05:41:30 +00:00
|
|
|
s.Stop()
|
2021-08-09 12:41:31 +00:00
|
|
|
|
2021-08-10 05:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Testing op (operator) Commands.
|
|
|
|
func checkREQOpCommandTest(conf *Configuration, t *testing.T) {
|
|
|
|
m := `[
|
|
|
|
{
|
|
|
|
"directory":"commands-executed",
|
|
|
|
"fileExtension": ".result",
|
|
|
|
"toNode": "central",
|
|
|
|
"data": [],
|
|
|
|
"method":"REQOpCommand",
|
|
|
|
"operation":{
|
|
|
|
"opCmd":"ps"
|
|
|
|
},
|
2021-08-10 08:53:15 +00:00
|
|
|
"replyMethod":"REQToFile",
|
2021-08-10 05:41:30 +00:00
|
|
|
"ACKTimeout":3,
|
|
|
|
"retries":3,
|
|
|
|
"replyACKTimeout":3,
|
|
|
|
"replyRetries":3,
|
|
|
|
"MethodTimeout": 7
|
|
|
|
}
|
|
|
|
]`
|
|
|
|
|
|
|
|
writeToSocketTest(conf, m, t)
|
|
|
|
|
|
|
|
resultFile := filepath.Join(conf.SubscribersDataFolder, "commands-executed", "central", "central.REQOpCommand.result")
|
|
|
|
findStringInFileTest("central.REQOpCommand.CommandACK", resultFile, conf, t)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sending of CLI Commands.
|
|
|
|
func checkREQCliCommandTest(conf *Configuration, t *testing.T) {
|
2021-08-09 12:41:31 +00:00
|
|
|
m := `[
|
|
|
|
{
|
|
|
|
"directory":"commands-executed",
|
|
|
|
"fileExtension":".result",
|
|
|
|
"toNode": "central",
|
|
|
|
"data": ["bash","-c","echo apekatt"],
|
|
|
|
"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-08-10 05:41:30 +00:00
|
|
|
resultFile := filepath.Join(conf.SubscribersDataFolder, "commands-executed", "central", "central.REQCliCommand.result")
|
|
|
|
findStringInFileTest("apekatt", resultFile, conf, t)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// The non-sequential sending of CLI Commands.
|
|
|
|
func checkREQnCliCommandTest(conf *Configuration, t *testing.T) {
|
|
|
|
m := `[
|
|
|
|
{
|
|
|
|
"directory":"commands-executed",
|
|
|
|
"fileExtension":".result",
|
|
|
|
"toNode": "central",
|
|
|
|
"data": ["bash","-c","echo apekatt"],
|
|
|
|
"replyMethod":"REQToFileAppend",
|
|
|
|
"method":"REQnCliCommand",
|
|
|
|
"ACKTimeout":3,
|
|
|
|
"retries":3,
|
|
|
|
"methodTimeout": 10
|
|
|
|
}
|
|
|
|
]`
|
|
|
|
|
|
|
|
writeToSocketTest(conf, m, t)
|
2021-08-09 12:41:31 +00:00
|
|
|
|
|
|
|
resultFile := filepath.Join(conf.SubscribersDataFolder, "commands-executed", "central", "central.REQnCliCommand.result")
|
2021-08-10 05:41:30 +00:00
|
|
|
findStringInFileTest("apekatt", resultFile, conf, t)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-10 08:53:15 +00:00
|
|
|
// Sending of Hello.
|
|
|
|
func checkREQHelloTest(conf *Configuration, t *testing.T) {
|
|
|
|
m := `[
|
|
|
|
{
|
|
|
|
"directory":"commands-executed",
|
|
|
|
"fileExtension":".result",
|
|
|
|
"toNode": "central",
|
|
|
|
"data": [],
|
|
|
|
"method":"REQHello"
|
|
|
|
}
|
|
|
|
]`
|
|
|
|
|
|
|
|
writeToSocketTest(conf, m, t)
|
|
|
|
|
|
|
|
resultFile := filepath.Join(conf.SubscribersDataFolder, "commands-executed", "central", "central.REQHello.result")
|
|
|
|
findStringInFileTest("Received hello from", resultFile, conf, t)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Helper functions for tests
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-08-10 05:41:30 +00:00
|
|
|
// Check if a file contains the given string.
|
|
|
|
func findStringInFileTest(want string, fileName string, conf *Configuration, t *testing.T) {
|
|
|
|
// Wait n seconds for the results file to be created
|
2021-08-10 08:53:15 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-10 08:53:15 +00:00
|
|
|
if os.IsNotExist(err) && i >= n {
|
2021-08-10 05:41:30 +00:00
|
|
|
t.Fatalf(" * failed: no result file created for request within the given time\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fh, err := os.Open(fileName)
|
2021-08-09 12:41:31 +00:00
|
|
|
if err != nil {
|
2021-08-10 05:41:30 +00:00
|
|
|
t.Fatalf(" * failed: could not open result file: %v\n", err)
|
2021-08-09 12:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result, err := io.ReadAll(fh)
|
|
|
|
if err != nil {
|
2021-08-10 05:41:30 +00:00
|
|
|
t.Fatalf(" * failed: could not read result file: %v\n", 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) {
|
|
|
|
t.Fatalf(" * failed: did not find expexted word `%v` in result file ", want)
|
2021-08-09 12:41:31 +00:00
|
|
|
}
|
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
|
|
|
}
|