probe/discovery/discoverer_test.go

450 lines
9.9 KiB
Go
Raw Normal View History

2018-09-01 13:09:33 +00:00
package discovery
import (
"log"
"reflect"
"sync"
"testing"
omd "git.loafle.net/overflow/model/discovery"
"git.loafle.net/overflow_scanner/probe/__test"
"git.loafle.net/overflow_scanner/probe/discovery/session"
"git.loafle.net/overflow_scanner/probe/discovery/types"
)
func TestInstance(t *testing.T) {
i := Instance()
tests := []struct {
name string
want Discoverer
}{
{
name: "first",
want: i,
},
{
name: "second",
want: i,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Instance(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Instance() = %v, want %v", got, tt.want)
}
})
}
}
func Test_ofDiscoverer_DiscoverHost(t *testing.T) {
i := Instance()
defer i.Shutdown()
stopChan := make(chan struct{})
go func() {
for {
select {
case msg, ok := <-i.Message():
if !ok {
close(stopChan)
}
switch msg.Type() {
case types.DiscoveryMessageTypeHost:
log.Print("Discovered Host: ", msg.Data())
case types.DiscoveryMessageTypePort:
log.Print("Discovered Port: ", msg.Data())
case types.DiscoveryMessageTypeService:
2018-09-03 06:03:57 +00:00
log.Print("Discovered Service: ", msg.Data(), " \n Port: ", msg.Data().(*omd.Service).Port, " \n Host: ", msg.Data().(*omd.Service).Port.Host)
2018-09-01 13:09:33 +00:00
case types.DiscoveryMessageTypeStart:
log.Print("Discovery start: ", msg.Data())
case types.DiscoveryMessageTypeStop:
log.Print("Discovery stop: ", msg.Data())
close(stopChan)
default:
// log.Print(msg.Data())
}
}
}
}()
i.DiscoverHost(
"testRequesterID",
__test.Zone(),
__test.DiscoverHost(
__test.DiscoveryConfig(),
1,
254,
__test.DiscoverPort(
nil,
1,
65535,
true,
false,
__test.DiscoverService(
nil,
),
),
),
)
select {
case <-stopChan:
}
// type fields struct {
// stopChan chan struct{}
// stopWg sync.WaitGroup
// requestQueue chan types.DiscoveryRequest
// messageChan chan types.DiscoveryMessage
// }
// type args struct {
// requesterID string
// zone *omd.Zone
// dh *omd.DiscoverHost
// }
// tests := []struct {
// name string
// fields fields
// args args
// }{
// // TODO: Add test cases.
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// d := &ofDiscoverer{
// stopChan: tt.fields.stopChan,
// stopWg: tt.fields.stopWg,
// requestQueue: tt.fields.requestQueue,
// messageChan: tt.fields.messageChan,
// }
// d.DiscoverHost(tt.args.requesterID, tt.args.zone, tt.args.dh)
// })
// }
}
func Test_ofDiscoverer_DiscoverPort(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
type args struct {
requesterID string
host *omd.Host
dp *omd.DiscoverPort
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.DiscoverPort(tt.args.requesterID, tt.args.host, tt.args.dp)
})
}
}
func Test_ofDiscoverer_DiscoverService(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
type args struct {
requesterID string
port *omd.Port
ds *omd.DiscoverService
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.DiscoverService(tt.args.requesterID, tt.args.port, tt.args.ds)
})
}
}
func Test_ofDiscoverer_Message(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
tests := []struct {
name string
fields fields
want <-chan types.DiscoveryMessage
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
if got := d.Message(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ofDiscoverer.Message() = %v, want %v", got, tt.want)
}
})
}
}
func Test_ofDiscoverer_SendMessage(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
type args struct {
discoveryRequest types.DiscoveryRequest
messageType types.DiscoveryMessageType
data interface{}
err error
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.SendMessage(tt.args.discoveryRequest, tt.args.messageType, tt.args.data, tt.args.err)
})
}
}
func Test_ofDiscoverer_Shutdown(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
tests := []struct {
name string
fields fields
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.Shutdown()
})
}
}
func Test_ofDiscoverer_start(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
tests := []struct {
name string
fields fields
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.start()
})
}
}
func Test_ofDiscoverer_enqueue(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
type args struct {
req *ofDiscoveryRequest
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.enqueue(tt.args.req)
})
}
}
func Test_ofDiscoverer_handleRequest(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
tests := []struct {
name string
fields fields
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.handleRequest()
})
}
}
func Test_ofDiscoverer_discover(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
type args struct {
req types.DiscoveryRequest
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.discover(tt.args.req)
})
}
}
func Test_ofDiscoverer_complexDiscover(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
type args struct {
s session.DiscoverySession
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.complexDiscover(tt.args.s)
})
}
}
func Test_ofDiscoverer_hierarchyDiscover(t *testing.T) {
type fields struct {
stopChan chan struct{}
stopWg sync.WaitGroup
requestQueue chan types.DiscoveryRequest
messageChan chan types.DiscoveryMessage
}
type args struct {
s session.DiscoverySession
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &ofDiscoverer{
stopChan: tt.fields.stopChan,
stopWg: tt.fields.stopWg,
requestQueue: tt.fields.requestQueue,
messageChan: tt.fields.messageChan,
}
d.hierarchyDiscover(tt.args.s)
})
}
}