Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
goSDN
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
danet
goSDN
Commits
d232a8e1
Commit
d232a8e1
authored
4 years ago
by
Manuel Kieweg
Browse files
Options
Downloads
Patches
Plain Diff
test for controller init and run
parent
0e861ab6
No related branches found
No related tags found
3 merge requests
!120
Resolve "Code Quality"
,
!119
Draft: Resolve "Tests for HTTP API and CLI"
,
!90
Develop
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
nucleus/controller.go
+27
-8
27 additions, 8 deletions
nucleus/controller.go
nucleus/controller_test.go
+51
-0
51 additions, 0 deletions
nucleus/controller_test.go
nucleus/http.go
+22
-3
22 additions, 3 deletions
nucleus/http.go
with
100 additions
and
11 deletions
nucleus/controller.go
+
27
−
8
View file @
d232a8e1
...
@@ -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
This diff is collapsed.
Click to expand it.
nucleus/controller_test.go
+
51
−
0
View file @
d232a8e1
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
)
})
}
}
This diff is collapsed.
Click to expand it.
nucleus/http.go
+
22
−
3
View file @
d232a8e1
...
@@ -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
.
http
Server
.
ListenAndServe
()
if
err
!=
nil
{
if
err
!=
nil
{
return
return
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment