1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2025-01-07 04:49:17 +00:00

added table tests for reqeusts

This commit is contained in:
postmannen 2022-05-23 05:51:06 +02:00
parent 090a253c94
commit dbe87bcca5
2 changed files with 22 additions and 55 deletions

View file

@ -9,7 +9,7 @@ import (
natsserver "github.com/nats-io/nats-server/v2/server" natsserver "github.com/nats-io/nats-server/v2/server"
) )
func newServerForTesting(t *testing.T) *server { func newServerForTesting(t *testing.T, addressAndPort string) (*server, *Configuration) {
//if !*logging { //if !*logging {
// log.SetOutput(io.Discard) // log.SetOutput(io.Discard)
//} //}
@ -21,7 +21,7 @@ func newServerForTesting(t *testing.T) *server {
// Create the config to run a steward instance. // Create the config to run a steward instance.
//tempdir := "./tmp" //tempdir := "./tmp"
conf := newConfigurationDefaults() conf := newConfigurationDefaults()
conf.BrokerAddress = "127.0.0.1:42222" conf.BrokerAddress = addressAndPort
conf.NodeName = "central" conf.NodeName = "central"
conf.CentralNodeName = "central" conf.CentralNodeName = "central"
conf.ConfigFolder = "tmp" conf.ConfigFolder = "tmp"
@ -29,21 +29,22 @@ func newServerForTesting(t *testing.T) *server {
conf.SocketFolder = "tmp" conf.SocketFolder = "tmp"
conf.SubscribersDataFolder = "tmp" conf.SubscribersDataFolder = "tmp"
conf.DatabaseFolder = "tmp" conf.DatabaseFolder = "tmp"
conf.StartSubREQErrorLog = true
stewardServer, err := NewServer(&conf, "test") 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)
} }
return stewardServer return stewardServer, &conf
} }
// Start up the nats-server message broker for testing purposes. // Start up the nats-server message broker for testing purposes.
func newNatsServerForTesting(t *testing.T) *natsserver.Server { func newNatsServerForTesting(t *testing.T, port int) *natsserver.Server {
// Start up the nats-server message broker. // Start up the nats-server message broker.
nsOpt := &natsserver.Options{ nsOpt := &natsserver.Options{
Host: "127.0.0.1", Host: "127.0.0.1",
Port: 42222, Port: port,
} }
ns, err := natsserver.NewServer(nsOpt) ns, err := natsserver.NewServer(nsOpt)
@ -66,13 +67,13 @@ func TestRequest(t *testing.T) {
containsOrEquals containsOrEquals
} }
ns := newNatsServerForTesting(t) ns := newNatsServerForTesting(t, 42222)
if err := natsserver.Run(ns); err != nil { if err := natsserver.Run(ns); err != nil {
natsserver.PrintAndDie(err.Error()) natsserver.PrintAndDie(err.Error())
} }
defer ns.Shutdown() defer ns.Shutdown()
srv := newServerForTesting(t) srv, _ := newServerForTesting(t, "127.0.0.1:42222")
srv.Start() srv.Start()
defer srv.Stop() defer srv.Stop()

View file

@ -29,50 +29,19 @@ var logging = flag.Bool("logging", false, "set to true to enable the normal logg
// Message passing. // Message passing.
// The different REQ types. // The different REQ types.
func TestStewardServer(t *testing.T) { func TestStewardServer(t *testing.T) {
if !*logging { // if !*logging {
log.SetOutput(io.Discard) // log.SetOutput(io.Discard)
// }
ns := newNatsServerForTesting(t, 40222)
if err := natsserver.Run(ns); err != nil {
natsserver.PrintAndDie(err.Error())
} }
defer ns.Shutdown()
// Start the nats-server message broker. srv, conf := newServerForTesting(t, "127.0.0.1:40222")
startNatsServerTest(t) srv.Start()
defer srv.Stop()
// Start Steward instance
// ---------------------------------------
// 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:40222",
PromHostAndPort: ":2112",
NodeName: "central",
CentralNodeName: "central",
DefaultMessageRetries: 1,
DefaultMessageTimeout: 3,
EnableSocket: true,
// AllowEmptySignature: true,
EnableDebug: true,
StartSubREQCliCommand: true,
StartSubREQCliCommandCont: true,
StartSubREQToConsole: true,
StartSubREQToFileAppend: true,
StartSubREQToFile: true,
StartSubREQHello: true,
StartSubREQErrorLog: true,
StartSubREQTailFile: true,
// StartSubREQToSocket: flagNodeSlice{OK: true, Values: []Node{"*"}},
}
stewardServer, err := NewServer(conf, "test")
if err != nil {
t.Fatalf(" * failed: could not start the Steward instance %v\n", err)
}
stewardServer.Start()
// Run the message tests // Run the message tests
// //
@ -95,14 +64,11 @@ func TestStewardServer(t *testing.T) {
} }
for _, f := range funcs { for _, f := range funcs {
err := f(stewardServer, conf, t) err := f(srv, conf, t)
if err != nil { if err != nil {
t.Errorf("%v\n", err) t.Errorf("%v\n", err)
} }
} }
// ---------------------------------------
stewardServer.Stop()
} }
@ -213,7 +179,7 @@ func checkREQErrorLogTest(stewardServer *server, conf *Configuration, t *testing
// 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.
fh, err := os.OpenFile("test.file", os.O_APPEND|os.O_RDWR|os.O_CREATE|os.O_SYNC, 0600) fh, err := os.OpenFile("tmp/test.file", os.O_APPEND|os.O_RDWR|os.O_CREATE|os.O_SYNC, 0600)
if err != nil { if err != nil {
return fmt.Errorf(" * failed: unable to open temporary file: %v", err) return fmt.Errorf(" * failed: unable to open temporary file: %v", err)
} }
@ -248,7 +214,7 @@ func checkREQTailFileTest(stewardServer *server, conf *Configuration, t *testing
return fmt.Errorf(" \U0001F631 [FAILED] : checkREQTailFileTest: : getting current working directory: %v", err) return fmt.Errorf(" \U0001F631 [FAILED] : checkREQTailFileTest: : getting current working directory: %v", err)
} }
file := filepath.Join(wd, "test.file") file := filepath.Join(wd, "tmp/test.file")
s := `[ s := `[
{ {