From 090a253c94c4dbdb61575326c3ea141aed0b4854 Mon Sep 17 00:00:00 2001 From: postmannen Date: Sun, 22 May 2022 15:56:54 +0200 Subject: [PATCH] added table tests for reqeusts --- requests_std.go | 2 +- requests_test.go | 131 ++++++++++++++++++++++++++++++++--------------- steward_test.go | 79 ---------------------------- 3 files changed, 92 insertions(+), 120 deletions(-) diff --git a/requests_std.go b/requests_std.go index c6b38d2..2c45884 100644 --- a/requests_std.go +++ b/requests_std.go @@ -444,7 +444,7 @@ func (m methodREQTest) getKind() Event { // handler to be used as a reply method when testing requests. // We can then within the test listen on the testCh for received // data and validate it. -// If not test is listening the data will be dropped. +// If no test is listening the data will be dropped. func (m methodREQTest) handler(proc process, message Message, node string) ([]byte, error) { go func() { diff --git a/requests_test.go b/requests_test.go index 7c5608d..e5a6422 100644 --- a/requests_test.go +++ b/requests_test.go @@ -2,7 +2,7 @@ package steward import ( "bytes" - "path/filepath" + "net/http" "strings" "testing" @@ -19,35 +19,18 @@ func newServerForTesting(t *testing.T) *server { // tempdir := t.TempDir() // Create the config to run a steward instance. - tempdir := "./tmp" - conf := &Configuration{ - SocketFolder: filepath.Join(tempdir, "tmp"), - DatabaseFolder: filepath.Join(tempdir, "var/lib"), - //SubscribersDataFolder: filepath.Join(tempdir, "data"), - SubscribersDataFolder: "./tmp/", - ConfigFolder: "./tmp/etc", - BrokerAddress: "127.0.0.1:42222", - PromHostAndPort: ":2112", - NodeName: "central", - CentralNodeName: "central", - DefaultMessageRetries: 1, - DefaultMessageTimeout: 3, - EnableSocket: true, - // AllowEmptySignature: true, - EnableDebug: true, + //tempdir := "./tmp" + conf := newConfigurationDefaults() + conf.BrokerAddress = "127.0.0.1:42222" + conf.NodeName = "central" + conf.CentralNodeName = "central" + conf.ConfigFolder = "tmp" + conf.SubscribersDataFolder = "tmp" + conf.SocketFolder = "tmp" + conf.SubscribersDataFolder = "tmp" + conf.DatabaseFolder = "tmp" - StartSubREQCliCommand: true, - StartSubREQCliCommandCont: true, - StartSubREQToConsole: true, - StartSubREQToFileAppend: true, - StartSubREQToFile: true, - StartSubREQHello: true, - StartSubREQErrorLog: true, - StartSubREQHttpGet: true, - StartSubREQTailFile: true, - // StartSubREQToSocket: flagNodeSlice{OK: true, Values: []Node{"*"}}, - } - stewardServer, err := NewServer(conf, "test") + stewardServer, err := NewServer(&conf, "test") if err != nil { t.Fatalf(" * failed: could not start the Steward instance %v\n", err) } @@ -72,9 +55,15 @@ func newNatsServerForTesting(t *testing.T) *natsserver.Server { } func TestRequest(t *testing.T) { + type containsOrEquals int + const contains containsOrEquals = 1 + const equals containsOrEquals = 2 + type test struct { + info string message Message want []byte + containsOrEquals } ns := newNatsServerForTesting(t) @@ -87,20 +76,70 @@ func TestRequest(t *testing.T) { srv.Start() defer srv.Stop() + // 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{ - {message: Message{ - ToNode: "central", - FromNode: "central", - Method: REQCliCommand, - MethodArgs: []string{"bash", "-c", "echo apekatt"}, - MethodTimeout: 5, - ReplyMethod: REQTest, - }, want: []byte("apekatt"), + { + info: "REQCliCommand test gris", + message: Message{ + ToNode: "central", + FromNode: "central", + Method: REQCliCommand, + MethodArgs: []string{"bash", "-c", "echo gris"}, + MethodTimeout: 5, + ReplyMethod: REQTest, + }, want: []byte("gris"), + containsOrEquals: equals, + }, + { + info: "REQCliCommand test sau", + message: Message{ + ToNode: "central", + FromNode: "central", + Method: REQCliCommand, + MethodArgs: []string{"bash", "-c", "echo sau"}, + MethodTimeout: 5, + ReplyMethod: REQTest, + }, want: []byte("sau"), + containsOrEquals: equals, + }, + { + info: "REQHttpGet test edgeos.raalabs.tech", + message: Message{ + ToNode: "central", + FromNode: "central", + Method: REQHttpGet, + MethodArgs: []string{"http://localhost:10080"}, + MethodTimeout: 5, + ReplyMethod: REQTest, + }, want: []byte("web page content"), + containsOrEquals: contains, + }, + { + info: "REQOpProcessList test", + message: Message{ + ToNode: "central", + FromNode: "central", + Method: REQOpProcessList, + MethodArgs: []string{}, + MethodTimeout: 5, + ReplyMethod: REQTest, + }, want: []byte("central.REQHttpGet.EventACK"), + containsOrEquals: contains, }, } for _, tt := range tests { - sam, err := newSubjectAndMessage(tt.message) if err != nil { t.Fatalf("newSubjectAndMessage failed: %v\n", err) @@ -109,12 +148,24 @@ func TestRequest(t *testing.T) { srv.toRingBufferCh <- []subjectAndMessage{sam} result := <-srv.errorKernel.testCh + resStr := string(result) resStr = strings.TrimSuffix(resStr, "\n") result = []byte(resStr) - if !bytes.Equal(result, tt.want) { - t.Fatalf("\n **** want: %v, got: %v\n", tt.want, result) + switch tt.containsOrEquals { + + case equals: + 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) + + case contains: + 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) } } } diff --git a/steward_test.go b/steward_test.go index be9329d..d1b1810 100644 --- a/steward_test.go +++ b/steward_test.go @@ -11,7 +11,6 @@ import ( "io" "log" "net" - "net/http" "os" "path/filepath" "strings" @@ -66,7 +65,6 @@ func TestStewardServer(t *testing.T) { StartSubREQToFile: true, StartSubREQHello: true, StartSubREQErrorLog: true, - StartSubREQHttpGet: true, StartSubREQTailFile: true, // StartSubREQToSocket: flagNodeSlice{OK: true, Values: []Node{"*"}}, } @@ -84,7 +82,6 @@ func TestStewardServer(t *testing.T) { // Specify all the test funcs to run in the slice. funcs := []testFunc{ - checkREQOpProcessListTest, checkREQCliCommandTest, checkREQCliCommandContTest, // checkREQToConsoleTest(conf, t), NB: No tests will be made for console ouput. @@ -92,16 +89,9 @@ func TestStewardServer(t *testing.T) { // checkREQToFileTest(conf, t), NB: Already tested via others checkREQHelloTest, checkREQErrorLogTest, - // checkREQPingTest(conf, t) - // checkREQPongTest(conf, t) - checkREQHttpGetTest, checkREQTailFileTest, - // checkREQToSocketTest(conf, t) - checkErrorKernelMalformedJSONtest, checkMetricValuesTest, - - // checkREQRelay } for _, f := range funcs { @@ -120,36 +110,6 @@ func TestStewardServer(t *testing.T) { // Check REQ types // ---------------------------------------------------------------------------- -// Testing op (operator) Commands. -func checkREQOpProcessListTest(stewardServer *server, conf *Configuration, t *testing.T) error { - m := `[ - { - "directory":"opCommand", - "fileName": "fileName.result", - "toNode": "central", - "data": [], - "method":"REQOpProcessList", - "replyMethod":"REQToFile", - "ACKTimeout":3, - "retries":3, - "replyACKTimeout":3, - "replyRetries":3, - "MethodTimeout": 7 - } - ]` - - writeToSocketTest(conf, m, t) - - resultFile := filepath.Join(conf.SubscribersDataFolder, "opCommand", "central", "fileName.result") - _, err := findStringInFileTest("central.REQHttpGet.CommandACK", resultFile, conf, t) - if err != nil { - return fmt.Errorf(" \U0001F631 [FAILED] : checkREQOpProcessListTest: %v", err) - } - - t.Logf(" \U0001f600 [SUCCESS] : checkREQOpCommandTest\n") - return nil -} - // Sending of CLI Commands. func checkREQCliCommandTest(stewardServer *server, conf *Configuration, t *testing.T) error { m := `[ @@ -250,45 +210,6 @@ func checkREQErrorLogTest(stewardServer *server, conf *Configuration, t *testing return nil } -// Check http get method. -func checkREQHttpGetTest(stewardServer *server, conf *Configuration, t *testing.T) error { - // 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) - }() - } - - m := `[ - { - "directory": "httpget", - "fileName":"fileName.result", - "toNode": "central", - "methodArgs": ["http://127.0.0.1:10080/"], - "method": "REQHttpGet", - "replyMethod":"REQToFile", - "ACKTimeout":5, - "methodTimeout": 5 - } - ]` - - writeToSocketTest(conf, m, t) - - resultFile := filepath.Join(conf.SubscribersDataFolder, "httpget", "central", "fileName.result") - _, err := findStringInFileTest("web page content", resultFile, conf, t) - if err != nil { - return fmt.Errorf(" \U0001F631 [FAILED] : checkREQHttpGetTest: %v", err) - } - - t.Logf(" \U0001f600 [SUCCESS] : checkREQHttpGetTest\n") - return nil -} - // Check the tailing of files type. func checkREQTailFileTest(stewardServer *server, conf *Configuration, t *testing.T) error { // Create a file with some content.