1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2024-12-14 12:37:31 +00:00

added via socket test for refactored tests

This commit is contained in:
postmannen 2022-05-23 07:43:34 +02:00
parent 3d005b7a09
commit 444c04ac3b
2 changed files with 73 additions and 15 deletions

View file

@ -176,7 +176,7 @@ func (s *server) readSocket() {
b := make([]byte, 1500) b := make([]byte, 1500)
_, err = conn.Read(b) _, err = conn.Read(b)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
er := fmt.Errorf("error: failed to read data from tcp listener: %v", err) er := fmt.Errorf("error: failed to read data from socket: %v", err)
s.errorKernel.errSend(s.processInitial, Message{}, er) s.errorKernel.errSend(s.processInitial, Message{}, er)
return return
} }

View file

@ -2,6 +2,8 @@ package steward
import ( import (
"bytes" "bytes"
"encoding/json"
"net"
"net/http" "net/http"
"path/filepath" "path/filepath"
"strings" "strings"
@ -56,17 +58,46 @@ func newNatsServerForTesting(t *testing.T, port int) *natsserver.Server {
return ns return ns
} }
// Write message to socket for testing purposes.
func writeMsgsToSocketTest(conf *Configuration, messages []Message, t *testing.T) {
js, err := json.Marshal(messages)
if err != nil {
t.Fatalf("writeMsgsToSocketTest: %v\n ", err)
}
socket, err := net.Dial("unix", filepath.Join(conf.SocketFolder, "steward.sock"))
if err != nil {
t.Fatalf(" * failed: could to open socket file for writing: %v\n", err)
}
defer socket.Close()
_, err = socket.Write(js)
if err != nil {
t.Fatalf(" * failed: could not write to socket: %v\n", err)
}
}
func TestRequest(t *testing.T) { func TestRequest(t *testing.T) {
type containsOrEquals int type containsOrEquals int
const REQTestContains containsOrEquals = 1 const (
const REQTestEquals containsOrEquals = 2 REQTestContains containsOrEquals = iota
const fileContains containsOrEquals = 3 REQTestEquals containsOrEquals = iota
fileContains containsOrEquals = iota
)
type viaSocketOrCh int
const (
viaSocket viaSocketOrCh = iota
viaCh viaSocketOrCh = iota
)
type test struct { type test struct {
info string info string
message Message message Message
want []byte want []byte
containsOrEquals containsOrEquals
viaSocketOrCh
} }
ns := newNatsServerForTesting(t, 42222) ns := newNatsServerForTesting(t, 42222)
@ -93,7 +124,7 @@ func TestRequest(t *testing.T) {
tests := []test{ tests := []test{
{ {
info: "REQCliCommand test gris", info: "REQCliCommand test, echo gris",
message: Message{ message: Message{
ToNode: "central", ToNode: "central",
FromNode: "central", FromNode: "central",
@ -103,9 +134,10 @@ func TestRequest(t *testing.T) {
ReplyMethod: REQTest, ReplyMethod: REQTest,
}, want: []byte("gris"), }, want: []byte("gris"),
containsOrEquals: REQTestEquals, containsOrEquals: REQTestEquals,
viaSocketOrCh: viaCh,
}, },
{ {
info: "REQCliCommand test sau", info: "REQCliCommand test via socket, echo sau",
message: Message{ message: Message{
ToNode: "central", ToNode: "central",
FromNode: "central", FromNode: "central",
@ -115,9 +147,10 @@ func TestRequest(t *testing.T) {
ReplyMethod: REQTest, ReplyMethod: REQTest,
}, want: []byte("sau"), }, want: []byte("sau"),
containsOrEquals: REQTestEquals, containsOrEquals: REQTestEquals,
viaSocketOrCh: viaSocket,
}, },
{ {
info: "REQCliCommand test result in file", info: "REQCliCommand test, echo sau, result in file",
message: Message{ message: Message{
ToNode: "central", ToNode: "central",
FromNode: "central", FromNode: "central",
@ -126,12 +159,28 @@ func TestRequest(t *testing.T) {
MethodTimeout: 5, MethodTimeout: 5,
ReplyMethod: REQToFile, ReplyMethod: REQToFile,
Directory: "test", Directory: "test",
FileName: "clicommand.result", FileName: "file1.result",
}, want: []byte("sau"), }, want: []byte("sau"),
containsOrEquals: fileContains, containsOrEquals: fileContains,
viaSocketOrCh: viaCh,
}, },
{ {
info: "REQHttpGet test edgeos.raalabs.tech", info: "REQCliCommand test, echo several, result in file continous",
message: Message{
ToNode: "central",
FromNode: "central",
Method: REQCliCommand,
MethodArgs: []string{"bash", "-c", "echo giraff && echo sau && echo apekatt"},
MethodTimeout: 5,
ReplyMethod: REQToFile,
Directory: "test",
FileName: "file2.result",
}, want: []byte("sau"),
containsOrEquals: fileContains,
viaSocketOrCh: viaCh,
},
{
info: "REQHttpGet test, localhost:10080",
message: Message{ message: Message{
ToNode: "central", ToNode: "central",
FromNode: "central", FromNode: "central",
@ -141,6 +190,7 @@ func TestRequest(t *testing.T) {
ReplyMethod: REQTest, ReplyMethod: REQTest,
}, want: []byte("web page content"), }, want: []byte("web page content"),
containsOrEquals: REQTestContains, containsOrEquals: REQTestContains,
viaSocketOrCh: viaCh,
}, },
{ {
info: "REQOpProcessList test", info: "REQOpProcessList test",
@ -153,19 +203,27 @@ func TestRequest(t *testing.T) {
ReplyMethod: REQTest, ReplyMethod: REQTest,
}, want: []byte("central.REQHttpGet.EventACK"), }, want: []byte("central.REQHttpGet.EventACK"),
containsOrEquals: REQTestContains, containsOrEquals: REQTestContains,
viaSocketOrCh: viaCh,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
sam, err := newSubjectAndMessage(tt.message) switch tt.viaSocketOrCh {
if err != nil { case viaCh:
t.Fatalf("newSubjectAndMessage failed: %v\n", err) sam, err := newSubjectAndMessage(tt.message)
if err != nil {
t.Fatalf("newSubjectAndMessage failed: %v\n", err)
}
srv.toRingBufferCh <- []subjectAndMessage{sam}
case viaSocket:
msgs := []Message{tt.message}
writeMsgsToSocketTest(conf, msgs, t)
} }
srv.toRingBufferCh <- []subjectAndMessage{sam}
switch tt.containsOrEquals { switch tt.containsOrEquals {
case REQTestEquals: case REQTestEquals:
result := <-srv.errorKernel.testCh result := <-srv.errorKernel.testCh
resStr := string(result) resStr := string(result)