1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2025-03-05 14:56:49 +00:00

test of hello msg. Hello also written to file

This commit is contained in:
postmannen 2021-08-10 10:53:15 +02:00
parent 0af326bba5
commit e729ea3d19
3 changed files with 90 additions and 12 deletions

View file

@ -113,6 +113,7 @@ func (s startup) pubREQHello(p process) {
d := fmt.Sprintf("Hello from %v\n", p.node)
m := Message{
Directory: "hello-messages",
ToNode: Node(p.configuration.CentralNodeName),
FromNode: Node(p.node),
Data: []string{d},

View file

@ -1,3 +1,7 @@
// Package functionality tests for Steward.
//
// To turn on the normal logging functionality during tests, use:
// go test -logging=true
package steward
import (
@ -14,6 +18,7 @@ import (
natsserver "github.com/nats-io/nats-server/v2/server"
)
// Set the default logging functionality of the package to false.
var logging = flag.Bool("logging", false, "set to true to enable the normal logger of the package")
// Test the overall functionality of Steward.
@ -30,8 +35,8 @@ func TestStewardServer(t *testing.T) {
// Start Steward instance
// ---------------------------------------
tempdir := t.TempDir()
// tempdir := t.TempDir()
tempdir := "./tmp"
conf := &Configuration{
SocketFolder: filepath.Join(tempdir, "tmp"),
DatabaseFolder: filepath.Join(tempdir, "var/lib"),
@ -44,8 +49,11 @@ func TestStewardServer(t *testing.T) {
StartSubREQCliCommand: flagNodeSlice{OK: true, Values: []Node{"*"}},
StartSubREQnCliCommand: flagNodeSlice{OK: true, Values: []Node{"*"}},
// StartSubREQnCliCommandCont: flagNodeSlice{OK: true, Values: []Node{"*"}},
StartSubREQErrorLog: flagNodeSlice{OK: true, Values: []Node{"*"}},
StartSubREQToFile: flagNodeSlice{OK: true, Values: []Node{"*"}},
StartSubREQToFileAppend: flagNodeSlice{OK: true, Values: []Node{"*"}},
StartSubREQHello: flagNodeSlice{OK: true, Values: []Node{"*"}},
}
s, err := NewServer(conf)
if err != nil {
@ -56,9 +64,22 @@ func TestStewardServer(t *testing.T) {
// Run the message tests
//
// ---------------------------------------
checkREQOpCommandTest(conf, t)
checkREQCliCommandTest(conf, t)
checkREQnCliCommandTest(conf, t)
// 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)
// ---------------------------------------
s.Stop()
@ -77,7 +98,7 @@ func checkREQOpCommandTest(conf *Configuration, t *testing.T) {
"operation":{
"opCmd":"ps"
},
"replyMethod":"REQToFileAppend",
"replyMethod":"REQToFile",
"ACKTimeout":3,
"retries":3,
"replyACKTimeout":3,
@ -139,17 +160,42 @@ func checkREQnCliCommandTest(conf *Configuration, t *testing.T) {
}
// 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
// ----------------------------------------------------------------------------
// 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
for i := 0; i <= 30; i++ {
n := 5
for i := 0; i <= n; i++ {
_, err := os.Stat(fileName)
if os.IsNotExist(err) {
time.Sleep(time.Millisecond * 500)
continue
}
if os.IsNotExist(err) && i >= 20 {
if os.IsNotExist(err) && i >= n {
t.Fatalf(" * failed: no result file created for request within the given time\n")
}
}

View file

@ -624,8 +624,39 @@ func (m methodREQHello) getKind() CommandOrEvent {
}
func (m methodREQHello) handler(proc process, message Message, node string) ([]byte, error) {
data := fmt.Sprintf("Received hello from %#v\n", message.FromNode)
log.Printf("<--- Received hello from %#v\n", message.FromNode)
// --------------------------
fileName := fmt.Sprintf("%v.%v%v", message.FromNode, message.Method, message.FileExtension)
folderTree := filepath.Join(proc.configuration.SubscribersDataFolder, message.Directory, string(message.ToNode))
// Check if folder structure exist, if not create it.
if _, err := os.Stat(folderTree); os.IsNotExist(err) {
err := os.MkdirAll(folderTree, 0700)
if err != nil {
return nil, fmt.Errorf("error: failed to create errorLog directory tree %v: %v", folderTree, err)
}
log.Printf("info: Creating subscribers data folder at %v\n", folderTree)
}
// Open file and write data.
file := filepath.Join(folderTree, fileName)
f, err := os.OpenFile(file, os.O_APPEND|os.O_RDWR|os.O_CREATE|os.O_SYNC, 0600)
if err != nil {
log.Printf("error: methodEventTextLogging.handler: failed to open file: %v\n", err)
return nil, err
}
defer f.Close()
_, err = f.Write([]byte(data))
f.Sync()
if err != nil {
log.Printf("error: methodEventTextLogging.handler: failed to write to file: %v\n", err)
}
// --------------------------
// send the message to the procFuncCh which is running alongside the process
// and can hold registries and handle special things for an individual process.