From e6006b44d3d6960c0feac9e0417af5816fa51f51 Mon Sep 17 00:00:00 2001 From: postmannen Date: Mon, 23 May 2022 12:57:07 +0200 Subject: [PATCH] moved hello test to request testing --- message_readers.go | 2 +- requests_test.go | 43 ++++++++++---- steward_test.go | 141 ++++++--------------------------------------- 3 files changed, 51 insertions(+), 135 deletions(-) diff --git a/message_readers.go b/message_readers.go index 460d190..931e946 100644 --- a/message_readers.go +++ b/message_readers.go @@ -123,7 +123,7 @@ func (s *server) readStartupFolder() { func (s *server) getFilePaths(dirName string) ([]string, error) { dirPath, err := os.Executable() dirPath = filepath.Dir(dirPath) - fmt.Printf(" * DEBUG: %v\n", dirPath) + fmt.Printf(" * DEBUG: dirPath=%v\n", dirPath) if err != nil { return nil, fmt.Errorf("error: startup folder: unable to get the working directory %v: %v", dirPath, err) } diff --git a/requests_test.go b/requests_test.go index 4d80336..e3b8dd3 100644 --- a/requests_test.go +++ b/requests_test.go @@ -3,6 +3,8 @@ package steward import ( "bytes" "encoding/json" + "io" + "log" "net" "net/http" "path/filepath" @@ -12,10 +14,10 @@ import ( natsserver "github.com/nats-io/nats-server/v2/server" ) -func newServerForTesting(t *testing.T, addressAndPort string) (*server, *Configuration) { - //if !*logging { - // log.SetOutput(io.Discard) - //} +func newServerForTesting(t *testing.T, addressAndPort string, testFolder string) (*server, *Configuration) { + if !*logging { + log.SetOutput(io.Discard) + } // Start Steward instance // --------------------------------------- @@ -27,11 +29,11 @@ func newServerForTesting(t *testing.T, addressAndPort string) (*server, *Configu conf.BrokerAddress = addressAndPort conf.NodeName = "central" conf.CentralNodeName = "central" - conf.ConfigFolder = "tmp" - conf.SubscribersDataFolder = "tmp" - conf.SocketFolder = "tmp" - conf.SubscribersDataFolder = "tmp" - conf.DatabaseFolder = "tmp" + conf.ConfigFolder = testFolder + conf.SubscribersDataFolder = testFolder + conf.SocketFolder = testFolder + conf.SubscribersDataFolder = testFolder + conf.DatabaseFolder = testFolder conf.StartSubREQErrorLog = true stewardServer, err := NewServer(&conf, "test") @@ -106,7 +108,8 @@ func TestRequest(t *testing.T) { } defer ns.Shutdown() - srv, conf := newServerForTesting(t, "127.0.0.1:42222") + tempdir := t.TempDir() + srv, conf := newServerForTesting(t, "127.0.0.1:42222", tempdir) srv.Start() defer srv.Stop() @@ -123,6 +126,21 @@ func TestRequest(t *testing.T) { } tests := []test{ + { + 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, + }, { info: "REQCliCommand test, echo gris", message: Message{ @@ -207,6 +225,7 @@ func TestRequest(t *testing.T) { }, } + // Range over the tests defined, and execute them, one at a time. for _, tt := range tests { switch tt.viaSocketOrCh { case viaCh: @@ -249,8 +268,8 @@ func TestRequest(t *testing.T) { case fileContains: resultFile := filepath.Join(conf.SubscribersDataFolder, tt.message.Directory, string(tt.message.FromNode), tt.message.FileName) - _, err := findStringInFileTest(string(tt.want), resultFile, conf, t) - if err != nil { + found, err := findStringInFileTest(string(tt.want), resultFile, conf, t) + if err != nil || found == false { t.Fatalf(" \U0001F631 [FAILED] : %v: %v\n", tt.info, err) } diff --git a/steward_test.go b/steward_test.go index e14e367..7dbe17b 100644 --- a/steward_test.go +++ b/steward_test.go @@ -33,30 +33,31 @@ func TestStewardServer(t *testing.T) { // log.SetOutput(io.Discard) // } + t.Logf("--------------- 1\n") + ns := newNatsServerForTesting(t, 40222) if err := natsserver.Run(ns); err != nil { natsserver.PrintAndDie(err.Error()) } defer ns.Shutdown() - srv, conf := newServerForTesting(t, "127.0.0.1:40222") + t.Logf("--------------- 2\n") + + tempDir := t.TempDir() + srv, conf := newServerForTesting(t, "127.0.0.1:40222", tempDir) srv.Start() defer srv.Stop() + t.Logf("--------------- 3\n") + // Run the message tests // // --------------------------------------- - type testFunc func(*server, *Configuration, *testing.T) error + type testFunc func(*server, *Configuration, *testing.T, string) error // Specify all the test funcs to run in the slice. funcs := []testFunc{ - checkREQCliCommandTest, - checkREQCliCommandContTest, - // 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, checkREQErrorLogTest, checkREQTailFileTest, checkErrorKernelMalformedJSONtest, @@ -64,7 +65,7 @@ func TestStewardServer(t *testing.T) { } for _, f := range funcs { - err := f(srv, conf, t) + err := f(srv, conf, t, tempDir) if err != nil { t.Errorf("%v\n", err) } @@ -76,88 +77,8 @@ func TestStewardServer(t *testing.T) { // Check REQ types // ---------------------------------------------------------------------------- -// Sending of CLI Commands. -func checkREQCliCommandTest(stewardServer *server, conf *Configuration, t *testing.T) error { - m := `[ - { - "directory":"commands-executed", - "fileName":"fileName.result", - "toNode": "central", - "methodArgs": ["bash","-c","echo apekatt"], - "replyMethod":"REQToFileAppend", - "method":"REQCliCommand", - "ACKTimeout":3, - "retries":3, - "methodTimeout": 10 - } - ]` - - writeToSocketTest(conf, m, t) - - resultFile := filepath.Join(conf.SubscribersDataFolder, "commands-executed", "central", "fileName.result") - _, err := findStringInFileTest("apekatt", resultFile, conf, t) - if err != nil { - return fmt.Errorf(" \U0001F631 [FAILED] : checkREQCliCommandTest: %v", err) - } - - t.Logf(" \U0001f600 [SUCCESS] : checkREQCliCommandTest\n") - return nil -} - -// The continous non-sequential sending of CLI Commands. -func checkREQCliCommandContTest(stewardServer *server, conf *Configuration, t *testing.T) error { - m := `[ - { - "directory":"commands-executed", - "fileName":"fileName.result", - "toNode": "central", - "methodArgs": ["bash","-c","echo apekatt && sleep 5 && echo gris"], - "replyMethod":"REQToFileAppend", - "method":"REQCliCommandCont", - "ACKTimeout":3, - "retries":3, - "methodTimeout": 5 - } - ]` - - writeToSocketTest(conf, m, t) - - resultFile := filepath.Join(conf.SubscribersDataFolder, "commands-executed", "central", "fileName.result") - _, err := findStringInFileTest("apekatt", resultFile, conf, t) - if err != nil { - return fmt.Errorf(" \U0001F631 [FAILED] : checkREQCliCommandContTest: %v", err) - } - - t.Logf(" \U0001f600 [SUCCESS] : checkREQCliCommandContTest\n") - return nil -} - -// Sending of Hello. -func checkREQHelloTest(stewardServer *server, conf *Configuration, t *testing.T) error { - m := `[ - { - "directory":"commands-executed", - "fileName":"fileName.result", - "toNode": "central", - "data": [], - "method":"REQHello" - } - ]` - - writeToSocketTest(conf, m, t) - - resultFile := filepath.Join(conf.SubscribersDataFolder, "commands-executed", "central", "fileName.result") - _, err := findStringInFileTest("Received hello from", resultFile, conf, t) - if err != nil { - return fmt.Errorf(" \U0001F631 [FAILED] : checkREQHelloTest: %v", err) - } - - t.Logf(" \U0001f600 [SUCCESS] : checkREQHelloTest\n") - return nil -} - // Check the error logger type. -func checkREQErrorLogTest(stewardServer *server, conf *Configuration, t *testing.T) error { +func checkREQErrorLogTest(stewardServer *server, conf *Configuration, t *testing.T, tempDir string) error { m := Message{ ToNode: "somenode", } @@ -177,9 +98,10 @@ func checkREQErrorLogTest(stewardServer *server, conf *Configuration, t *testing } // Check the tailing of files type. -func checkREQTailFileTest(stewardServer *server, conf *Configuration, t *testing.T) error { +func checkREQTailFileTest(stewardServer *server, conf *Configuration, t *testing.T, tmpDir string) error { // Create a file with some content. - fh, err := os.OpenFile("tmp/test.file", os.O_APPEND|os.O_RDWR|os.O_CREATE|os.O_SYNC, 0600) + fp := filepath.Join(tmpDir, "test.file") + fh, err := os.OpenFile(fp, os.O_APPEND|os.O_RDWR|os.O_CREATE|os.O_SYNC, 0600) if err != nil { return fmt.Errorf(" * failed: unable to open temporary file: %v", err) } @@ -208,20 +130,12 @@ func checkREQTailFileTest(stewardServer *server, conf *Configuration, t *testing } }() - wd, err := os.Getwd() - if err != nil { - cancel() - return fmt.Errorf(" \U0001F631 [FAILED] : checkREQTailFileTest: : getting current working directory: %v", err) - } - - file := filepath.Join(wd, "tmp/test.file") - s := `[ { "directory": "tail-files", "fileName": "fileName.result", "toNode": "central", - "methodArgs": ["` + file + `"], + "methodArgs": ["` + fp + `"], "method":"REQTailFile", "ACKTimeout":5, "retries":3, @@ -266,7 +180,7 @@ func checkREQTailFileTest(stewardServer *server, conf *Configuration, t *testing // ---------------------------------------------------------------------------- // Check errorKernel -func checkErrorKernelMalformedJSONtest(stewardServer *server, conf *Configuration, t *testing.T) error { +func checkErrorKernelMalformedJSONtest(stewardServer *server, conf *Configuration, t *testing.T, tempDir string) error { // JSON message with error, missing brace. m := `[ @@ -328,7 +242,7 @@ func checkErrorKernelMalformedJSONtest(stewardServer *server, conf *Configuratio } } -func checkMetricValuesTest(stewardServer *server, conf *Configuration, t *testing.T) error { +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) @@ -426,7 +340,8 @@ func findStringInFileTest(want string, fileName string, conf *Configuration, t * return false, fmt.Errorf(" * failed: could not read result file: %v", err) } - if !strings.Contains(string(result), want) { + found := strings.Contains(string(result), want) + if !found { return false, nil } @@ -447,21 +362,3 @@ func writeToSocketTest(conf *Configuration, messageText string, t *testing.T) { } } - -// 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()) - } -}