From e729ea3d19845a9bd66b61209266e37e4bbc7c7e Mon Sep 17 00:00:00 2001 From: postmannen Date: Tue, 10 Aug 2021 10:53:15 +0200 Subject: [PATCH] test of hello msg. Hello also written to file --- startup_processes.go | 9 +++--- steward_test.go | 60 +++++++++++++++++++++++++++++++++----- subscriber_method_types.go | 33 ++++++++++++++++++++- 3 files changed, 90 insertions(+), 12 deletions(-) diff --git a/startup_processes.go b/startup_processes.go index 7b43996..b662b6c 100644 --- a/startup_processes.go +++ b/startup_processes.go @@ -113,10 +113,11 @@ func (s startup) pubREQHello(p process) { d := fmt.Sprintf("Hello from %v\n", p.node) m := Message{ - ToNode: Node(p.configuration.CentralNodeName), - FromNode: Node(p.node), - Data: []string{d}, - Method: REQHello, + Directory: "hello-messages", + ToNode: Node(p.configuration.CentralNodeName), + FromNode: Node(p.node), + Data: []string{d}, + Method: REQHello, } sam, err := newSAM(m) diff --git a/steward_test.go b/steward_test.go index bcf5675..2b2c462 100644 --- a/steward_test.go +++ b/steward_test.go @@ -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"), @@ -42,10 +47,13 @@ func TestStewardServer(t *testing.T) { DefaultMessageRetries: 1, DefaultMessageTimeout: 3, - StartSubREQCliCommand: flagNodeSlice{OK: true, Values: []Node{"*"}}, - StartSubREQnCliCommand: flagNodeSlice{OK: true, Values: []Node{"*"}}, + 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") } } diff --git a/subscriber_method_types.go b/subscriber_method_types.go index f4bc7ee..93805b4 100644 --- a/subscriber_method_types.go +++ b/subscriber_method_types.go @@ -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.