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

656 lines
16 KiB
Go
Raw Normal View History

2022-05-22 04:36:02 +00:00
package steward
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
2022-05-23 10:57:07 +00:00
"io"
"log"
"net"
2022-05-22 13:56:54 +00:00
"net/http"
"os"
2022-05-23 04:37:31 +00:00
"path/filepath"
"strings"
"testing"
"time"
"github.com/fsnotify/fsnotify"
natsserver "github.com/nats-io/nats-server/v2/server"
)
var logging = flag.Bool("logging", false, "set to true to enable the normal logger of the package")
var persistTmp = flag.Bool("persistTmp", false, "set to true to persist the tmp folder")
2022-05-31 15:08:31 +00:00
var tstSrv *server
var tstConf *Configuration
var tstNats *natsserver.Server
var tstTempDir string
func TestMain(m *testing.M) {
flag.Parse()
if *persistTmp {
tstTempDir = "tmp"
} else {
tstTempDir = os.TempDir()
}
2022-06-22 12:50:26 +00:00
// NB: Forcing this for now.
2022-05-31 15:08:31 +00:00
tstTempDir = "tmp"
tstNats = newNatsServerForTesting(42222)
if err := natsserver.Run(tstNats); err != nil {
natsserver.PrintAndDie(err.Error())
}
tstSrv, tstConf = newServerForTesting("127.0.0.1:42222", tstTempDir)
tstSrv.Start()
exitCode := m.Run()
tstSrv.Stop()
tstNats.Shutdown()
os.Exit(exitCode)
}
func newServerForTesting(addressAndPort string, testFolder string) (*server, *Configuration) {
// 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()
2023-03-01 05:58:56 +00:00
if *logging {
conf.LogLevel = "warning"
}
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
conf.IsCentralErrorLogger = true
2022-05-31 15:08:31 +00:00
conf.IsCentralAuth = true
2023-03-01 05:58:56 +00:00
conf.EnableDebug = false
conf.LogLevel = "none"
2022-05-22 13:56:54 +00:00
stewardServer, err := NewServer(&conf, "test")
if err != nil {
2022-05-31 15:08:31 +00:00
log.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-31 15:08:31 +00:00
func newNatsServerForTesting(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 {
2022-05-31 15:08:31 +00:00
log.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) {
if !*logging {
log.SetOutput(io.Discard)
}
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-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)
}
2022-05-31 15:08:31 +00:00
tstSrv.toRingBufferCh <- []subjectAndMessage{sam}
case viaSocket:
msgs := []Message{tt.message}
2022-05-31 15:08:31 +00:00
writeMsgsToSocketTest(tstConf, msgs, t)
2022-05-22 13:56:54 +00:00
}
switch tt.containsOrEquals {
2022-05-23 04:37:31 +00:00
case REQTestEquals:
2022-05-31 15:08:31 +00:00
result := <-tstSrv.errorKernel.testCh
2022-05-23 04:37:31 +00:00
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:
2022-05-31 15:08:31 +00:00
result := <-tstSrv.errorKernel.testCh
2022-05-23 04:37:31 +00:00
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:
2022-05-31 15:08:31 +00:00
resultFile := filepath.Join(tstConf.SubscribersDataFolder, tt.message.Directory, string(tt.message.FromNode), tt.message.FileName)
2022-05-23 04:37:31 +00:00
2022-05-31 15:08:31 +00:00
found, err := findStringInFileTest(string(tt.want), resultFile, tstConf, t)
2022-05-23 10:57:07 +00:00
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
}
// --- Other REQ tests that does not fit well into the general table above.
2022-05-31 15:08:31 +00:00
checkREQTailFileTest(tstSrv, tstConf, t, tstTempDir)
checkMetricValuesTest(tstSrv, tstConf, t, tstTempDir)
checkErrorKernelMalformedJSONtest(tstSrv, tstConf, t, tstTempDir)
2022-06-22 05:31:43 +00:00
checkREQCopySrc(tstSrv, tstConf, t, tstTempDir)
}
// Check the tailing of files type.
func checkREQTailFileTest(stewardServer *server, conf *Configuration, t *testing.T, tmpDir string) error {
// Create a file with some content.
fp := filepath.Join(tmpDir, "test.file")
fh, err := os.OpenFile(fp, os.O_APPEND|os.O_RDWR|os.O_CREATE|os.O_SYNC, 0660)
if err != nil {
return fmt.Errorf(" * failed: unable to open temporary file: %v", err)
}
defer fh.Close()
ctx, cancel := context.WithCancel(context.Background())
// Write content to the file with specified intervals.
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)
// Check if we've received a done, else default to continuing.
select {
case <-ctx.Done():
return
default:
// no done received, we're continuing.
}
}
}()
s := `[
{
"directory": "tail-files",
"fileName": "fileName.result",
"toNode": "central",
"methodArgs": ["` + fp + `"],
"method":"REQTailFile",
"ACKTimeout":5,
"retries":3,
"methodTimeout": 10
}
]`
writeToSocketTest(conf, s, t)
resultFile := filepath.Join(conf.SubscribersDataFolder, "tail-files", "central", "fileName.result")
// Wait n times for result file to be created.
2022-06-22 05:31:43 +00:00
n := 50
for i := 0; i <= n; i++ {
_, err := os.Stat(resultFile)
if os.IsNotExist(err) {
2022-06-22 05:31:43 +00:00
time.Sleep(time.Millisecond * 100)
continue
}
if os.IsNotExist(err) && i >= n {
cancel()
return fmt.Errorf(" \U0001F631 [FAILED] : checkREQTailFileTest: no result file created for request within the given time")
}
}
cancel()
_, err = findStringInFileTest("some file content", resultFile, conf, t)
if err != nil {
return fmt.Errorf(" \U0001F631 [FAILED] : checkREQTailFileTest: %v", err)
}
t.Logf(" \U0001f600 [SUCCESS] : checkREQTailFileTest\n")
return nil
}
2022-06-22 05:31:43 +00:00
// Check the file copier.
func checkREQCopySrc(stewardServer *server, conf *Configuration, t *testing.T, tmpDir string) error {
testFiles := 5
for i := 1; i <= testFiles; i++ {
// Create a file with some content.
srcFileName := fmt.Sprintf("copysrc%v.file", i)
srcfp := filepath.Join(tmpDir, srcFileName)
fh, err := os.OpenFile(srcfp, os.O_APPEND|os.O_RDWR|os.O_CREATE|os.O_SYNC, 0660)
2022-06-22 05:31:43 +00:00
if err != nil {
t.Fatalf(" \U0001F631 [FAILED] : checkREQCopySrc: unable to open temporary file: %v", err)
}
defer fh.Close()
// Write content to the file.
_, err = fh.Write([]byte("some file content\n"))
if err != nil {
t.Fatalf(" \U0001F631 [FAILED] : checkREQCopySrc: writing to temporary file: %v\n", err)
}
dstFileName := fmt.Sprintf("copydst%v.file", i)
dstfp := filepath.Join(tmpDir, dstFileName)
s := `[
{
"toNode": "central",
"method":"REQCopySrc",
"methodArgs": ["` + srcfp + `","central","` + dstfp + `","20","10"],
"ACKTimeout":5,
"retries":3,
"methodTimeout": 10
}
]`
writeToSocketTest(conf, s, t)
// Wait n times for result file to be created.
n := 50
for i := 0; i <= n; i++ {
_, err := os.Stat(dstfp)
if os.IsNotExist(err) {
time.Sleep(time.Millisecond * 100)
continue
}
if os.IsNotExist(err) && i >= n {
t.Fatalf(" \U0001F631 [FAILED] : checkREQCopySrc: no result file created for request within the given time")
}
}
t.Logf(" \U0001f600 [SUCCESS] : src=%v, dst=%v", srcfp, dstfp)
}
return nil
}
func checkMetricValuesTest(stewardServer *server, conf *Configuration, t *testing.T, tempDir string) error {
mfs, err := stewardServer.metrics.promRegistry.Gather()
if err != nil {
return fmt.Errorf("error: promRegistry.gathering: %v", mfs)
}
if len(mfs) <= 0 {
return fmt.Errorf("error: promRegistry.gathering: did not find any metric families: %v", mfs)
}
found := false
for _, mf := range mfs {
if mf.GetName() == "steward_processes_total" {
found = true
m := mf.GetMetric()
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())
}
}
}
if !found {
return fmt.Errorf("error: promRegistry.gathering: did not find specified metric processes_total")
}
t.Logf(" \U0001f600 [SUCCESS] : checkMetricValuesTest")
return nil
}
// Check errorKernel
func checkErrorKernelMalformedJSONtest(stewardServer *server, conf *Configuration, t *testing.T, tempDir string) error {
// JSON message with error, missing brace.
m := `[
{
"directory": "some dir",
"fileName":"someext",
"toNode": "somenode",
"data": ["some data"],
"method": "REQErrorLog"
missing brace here.....
]`
writeToSocketTest(conf, m, t)
resultFile := filepath.Join(conf.SubscribersDataFolder, "errorLog", "errorCentral", "error.log")
// Wait n times for error file to be created.
2022-06-22 05:31:43 +00:00
n := 50
for i := 0; i <= n; i++ {
_, err := os.Stat(resultFile)
if os.IsNotExist(err) {
2022-06-22 05:31:43 +00:00
time.Sleep(time.Millisecond * 100)
continue
}
if os.IsNotExist(err) && i >= n {
return fmt.Errorf(" \U0001F631 [FAILED] : checkErrorKernelMalformedJSONtest: no result file created for request within the given time")
}
}
// 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)
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 {
return fmt.Errorf(" \U0001F631 [FAILED] : checkErrorKernelMalformedJSONtest: %v", err)
}
if !found && err == nil {
continue
}
if found {
t.Logf(" \U0001f600 [SUCCESS] : checkErrorKernelMalformedJSONtest")
return nil
}
case <-ticker.C:
return fmt.Errorf(" * failed: did not get an update in the errorKernel log file")
}
}
}
// 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
}
// Check if a file contains the given string.
func findStringInFileTest(want string, fileName string, conf *Configuration, t *testing.T) (bool, error) {
// Wait n seconds for the results file to be created
2022-06-22 05:31:43 +00:00
n := 50
for i := 0; i <= n; i++ {
_, err := os.Stat(fileName)
if os.IsNotExist(err) {
2022-06-22 05:31:43 +00:00
time.Sleep(time.Millisecond * 100)
continue
}
if os.IsNotExist(err) && i >= n {
return false, fmt.Errorf(" * failed: no result file created for request within the given time\n")
}
}
fh, err := os.Open(fileName)
if err != nil {
return false, fmt.Errorf(" * failed: could not open result file: %v", err)
}
result, err := io.ReadAll(fh)
if err != nil {
return false, fmt.Errorf(" * failed: could not read result file: %v", err)
}
found := strings.Contains(string(result), want)
if !found {
return false, nil
}
return true, nil
}
// 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()
_, err = socket.Write([]byte(messageText))
if err != nil {
t.Fatalf(" * failed: could not write to socket: %v\n", err)
}
2022-05-22 04:36:02 +00:00
}