mirror of
https://github.com/postmannen/ctrl.git
synced 2024-12-14 12:37:31 +00:00
added table tests for reqeusts
This commit is contained in:
parent
4f09dd406d
commit
090a253c94
3 changed files with 92 additions and 120 deletions
|
@ -444,7 +444,7 @@ func (m methodREQTest) getKind() Event {
|
||||||
// handler to be used as a reply method when testing requests.
|
// handler to be used as a reply method when testing requests.
|
||||||
// We can then within the test listen on the testCh for received
|
// We can then within the test listen on the testCh for received
|
||||||
// data and validate it.
|
// 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) {
|
func (m methodREQTest) handler(proc process, message Message, node string) ([]byte, error) {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
|
119
requests_test.go
119
requests_test.go
|
@ -2,7 +2,7 @@ package steward
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"path/filepath"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -19,35 +19,18 @@ func newServerForTesting(t *testing.T) *server {
|
||||||
// tempdir := t.TempDir()
|
// tempdir := t.TempDir()
|
||||||
|
|
||||||
// Create the config to run a steward instance.
|
// Create the config to run a steward instance.
|
||||||
tempdir := "./tmp"
|
//tempdir := "./tmp"
|
||||||
conf := &Configuration{
|
conf := newConfigurationDefaults()
|
||||||
SocketFolder: filepath.Join(tempdir, "tmp"),
|
conf.BrokerAddress = "127.0.0.1:42222"
|
||||||
DatabaseFolder: filepath.Join(tempdir, "var/lib"),
|
conf.NodeName = "central"
|
||||||
//SubscribersDataFolder: filepath.Join(tempdir, "data"),
|
conf.CentralNodeName = "central"
|
||||||
SubscribersDataFolder: "./tmp/",
|
conf.ConfigFolder = "tmp"
|
||||||
ConfigFolder: "./tmp/etc",
|
conf.SubscribersDataFolder = "tmp"
|
||||||
BrokerAddress: "127.0.0.1:42222",
|
conf.SocketFolder = "tmp"
|
||||||
PromHostAndPort: ":2112",
|
conf.SubscribersDataFolder = "tmp"
|
||||||
NodeName: "central",
|
conf.DatabaseFolder = "tmp"
|
||||||
CentralNodeName: "central",
|
|
||||||
DefaultMessageRetries: 1,
|
|
||||||
DefaultMessageTimeout: 3,
|
|
||||||
EnableSocket: true,
|
|
||||||
// AllowEmptySignature: true,
|
|
||||||
EnableDebug: true,
|
|
||||||
|
|
||||||
StartSubREQCliCommand: true,
|
stewardServer, err := NewServer(&conf, "test")
|
||||||
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")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(" * failed: could not start the Steward instance %v\n", err)
|
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) {
|
func TestRequest(t *testing.T) {
|
||||||
|
type containsOrEquals int
|
||||||
|
const contains containsOrEquals = 1
|
||||||
|
const equals containsOrEquals = 2
|
||||||
|
|
||||||
type test struct {
|
type test struct {
|
||||||
|
info string
|
||||||
message Message
|
message Message
|
||||||
want []byte
|
want []byte
|
||||||
|
containsOrEquals
|
||||||
}
|
}
|
||||||
|
|
||||||
ns := newNatsServerForTesting(t)
|
ns := newNatsServerForTesting(t)
|
||||||
|
@ -87,20 +76,70 @@ func TestRequest(t *testing.T) {
|
||||||
srv.Start()
|
srv.Start()
|
||||||
defer srv.Stop()
|
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{
|
tests := []test{
|
||||||
{message: Message{
|
{
|
||||||
|
info: "REQCliCommand test gris",
|
||||||
|
message: Message{
|
||||||
ToNode: "central",
|
ToNode: "central",
|
||||||
FromNode: "central",
|
FromNode: "central",
|
||||||
Method: REQCliCommand,
|
Method: REQCliCommand,
|
||||||
MethodArgs: []string{"bash", "-c", "echo apekatt"},
|
MethodArgs: []string{"bash", "-c", "echo gris"},
|
||||||
MethodTimeout: 5,
|
MethodTimeout: 5,
|
||||||
ReplyMethod: REQTest,
|
ReplyMethod: REQTest,
|
||||||
}, want: []byte("apekatt"),
|
}, 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 {
|
for _, tt := range tests {
|
||||||
|
|
||||||
sam, err := newSubjectAndMessage(tt.message)
|
sam, err := newSubjectAndMessage(tt.message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("newSubjectAndMessage failed: %v\n", err)
|
t.Fatalf("newSubjectAndMessage failed: %v\n", err)
|
||||||
|
@ -109,12 +148,24 @@ func TestRequest(t *testing.T) {
|
||||||
srv.toRingBufferCh <- []subjectAndMessage{sam}
|
srv.toRingBufferCh <- []subjectAndMessage{sam}
|
||||||
|
|
||||||
result := <-srv.errorKernel.testCh
|
result := <-srv.errorKernel.testCh
|
||||||
|
|
||||||
resStr := string(result)
|
resStr := string(result)
|
||||||
resStr = strings.TrimSuffix(resStr, "\n")
|
resStr = strings.TrimSuffix(resStr, "\n")
|
||||||
result = []byte(resStr)
|
result = []byte(resStr)
|
||||||
|
|
||||||
|
switch tt.containsOrEquals {
|
||||||
|
|
||||||
|
case equals:
|
||||||
if !bytes.Equal(result, tt.want) {
|
if !bytes.Equal(result, tt.want) {
|
||||||
t.Fatalf("\n **** want: %v, got: %v\n", tt.want, result)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,6 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -66,7 +65,6 @@ func TestStewardServer(t *testing.T) {
|
||||||
StartSubREQToFile: true,
|
StartSubREQToFile: true,
|
||||||
StartSubREQHello: true,
|
StartSubREQHello: true,
|
||||||
StartSubREQErrorLog: true,
|
StartSubREQErrorLog: true,
|
||||||
StartSubREQHttpGet: true,
|
|
||||||
StartSubREQTailFile: true,
|
StartSubREQTailFile: true,
|
||||||
// StartSubREQToSocket: flagNodeSlice{OK: true, Values: []Node{"*"}},
|
// 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.
|
// Specify all the test funcs to run in the slice.
|
||||||
funcs := []testFunc{
|
funcs := []testFunc{
|
||||||
checkREQOpProcessListTest,
|
|
||||||
checkREQCliCommandTest,
|
checkREQCliCommandTest,
|
||||||
checkREQCliCommandContTest,
|
checkREQCliCommandContTest,
|
||||||
// checkREQToConsoleTest(conf, t), NB: No tests will be made for console ouput.
|
// 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
|
// checkREQToFileTest(conf, t), NB: Already tested via others
|
||||||
checkREQHelloTest,
|
checkREQHelloTest,
|
||||||
checkREQErrorLogTest,
|
checkREQErrorLogTest,
|
||||||
// checkREQPingTest(conf, t)
|
|
||||||
// checkREQPongTest(conf, t)
|
|
||||||
checkREQHttpGetTest,
|
|
||||||
checkREQTailFileTest,
|
checkREQTailFileTest,
|
||||||
// checkREQToSocketTest(conf, t)
|
|
||||||
|
|
||||||
checkErrorKernelMalformedJSONtest,
|
checkErrorKernelMalformedJSONtest,
|
||||||
checkMetricValuesTest,
|
checkMetricValuesTest,
|
||||||
|
|
||||||
// checkREQRelay
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, f := range funcs {
|
for _, f := range funcs {
|
||||||
|
@ -120,36 +110,6 @@ func TestStewardServer(t *testing.T) {
|
||||||
// Check REQ types
|
// 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.
|
// Sending of CLI Commands.
|
||||||
func checkREQCliCommandTest(stewardServer *server, conf *Configuration, t *testing.T) error {
|
func checkREQCliCommandTest(stewardServer *server, conf *Configuration, t *testing.T) error {
|
||||||
m := `[
|
m := `[
|
||||||
|
@ -250,45 +210,6 @@ func checkREQErrorLogTest(stewardServer *server, conf *Configuration, t *testing
|
||||||
return nil
|
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.
|
// 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) error {
|
||||||
// Create a file with some content.
|
// Create a file with some content.
|
||||||
|
|
Loading…
Reference in a new issue