Skip to content
Snippets Groups Projects
Commit d232a8e1 authored by Manuel Kieweg's avatar Manuel Kieweg
Browse files

test for controller init and run

parent 0e861ab6
No related branches found
No related tags found
3 merge requests!120Resolve "Code Quality",!119Draft: Resolve "Tests for HTTP API and CLI",!90Develop
...@@ -5,6 +5,9 @@ import ( ...@@ -5,6 +5,9 @@ import (
"context" "context"
"github.com/google/uuid" "github.com/google/uuid"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"net/http"
"os"
"os/signal"
"time" "time"
) )
...@@ -13,19 +16,28 @@ type Core struct { ...@@ -13,19 +16,28 @@ type Core struct {
// deprecated // deprecated
database database.Database database database.Database
pndc pndStore pndc pndStore
sbic sbiStore sbic sbiStore
httpServer *http.Server
stopChan chan os.Signal
} }
var c *Core var c *Core
//Initialize does start-up housekeeping like reading controller config files func init() {
func initialize() error {
c = &Core{ c = &Core{
database: database.Database{}, database: database.Database{},
pndc: pndStore{}, pndc: pndStore{},
sbic: sbiStore{}, sbic: sbiStore{},
stopChan: make(chan os.Signal, 1),
} }
// Setting up signal capturing
signal.Notify(c.stopChan, os.Interrupt)
}
// initialize does start-up housekeeping like reading controller config files
func initialize() error {
c.sbic = sbiStore{ c.sbic = sbiStore{
store{}, store{},
} }
...@@ -83,10 +95,17 @@ func Run(ctx context.Context) error { ...@@ -83,10 +95,17 @@ func Run(ctx context.Context) error {
log.WithFields(log.Fields{}).Info("initialisation finished") log.WithFields(log.Fields{}).Info("initialisation finished")
for { for {
select { select {
case <-c.stopChan:
return shutdown()
case <-ctx.Done(): case <-ctx.Done():
return nil return shutdown()
case <-time.Tick(time.Minute): case <-time.Tick(time.Minute):
log.Debug("up and running") log.Debug("up and running")
} }
} }
} }
func shutdown()error{
log.Info("shutting down controller")
return stopHttpServer()
}
\ No newline at end of file
package nucleus package nucleus
import (
"context"
"net/http"
"testing"
"time"
)
func TestRun(t *testing.T) {
type args struct {
request string
}
tests := []struct {
name string
args args
want interface{}
wantErr bool
}{
{
name: "liveliness indicator",
args: args{request: apiEndpoint + "/livez"},
want: &http.Response{StatusCode: http.StatusOK},
wantErr: false,
},
{
name: "readyness indicator",
args: args{request: apiEndpoint + "/readyz"},
want: &http.Response{StatusCode: http.StatusOK},
wantErr: false,
},
{
name: "init",
args: args{request: apiEndpoint + "/api?q=init"},
want: &http.Response{StatusCode: http.StatusOK},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
go func() {
if err := Run(ctx); (err != nil) != tt.wantErr {
t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr)
}
}()
time.Sleep(time.Second)
cancel()
time.Sleep(time.Second)
})
}
}
...@@ -2,22 +2,41 @@ package nucleus ...@@ -2,22 +2,41 @@ package nucleus
import ( import (
"code.fbi.h-da.de/cocsn/gosdn/forks/goarista/gnmi" "code.fbi.h-da.de/cocsn/gosdn/forks/goarista/gnmi"
"context"
"fmt" "fmt"
"github.com/google/uuid" "github.com/google/uuid"
gpb "github.com/openconfig/gnmi/proto/gnmi" gpb "github.com/openconfig/gnmi/proto/gnmi"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"net/http" "net/http"
"net/url" "net/url"
"time"
) )
// deprecated func stopHttpServer() error {
func httpApi() (err error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
log.Info("shutting down http server")
return c.httpServer.Shutdown(ctx)
}
func registerHttpHandler(){
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
}
}()
http.HandleFunc("/api", httpHandler) http.HandleFunc("/api", httpHandler)
http.HandleFunc("/livez", healthCheck) http.HandleFunc("/livez", healthCheck)
http.HandleFunc("/readyz", readynessCheck) http.HandleFunc("/readyz", readynessCheck)
}
// deprecated
func httpApi() (err error) {
registerHttpHandler()
c.httpServer = &http.Server{Addr: ":8080"}
go func() { go func() {
err = http.ListenAndServe(":8080", nil) err = c.httpServer.ListenAndServe()
if err != nil { if err != nil {
return return
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment