478 lines
10 KiB
Go
478 lines
10 KiB
Go
package discovery
|
|
|
|
import (
|
|
"log"
|
|
"reflect"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
omd "git.loafle.net/overflow/model/discovery"
|
|
oub "git.loafle.net/overflow/util-go/benchmark"
|
|
"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) {
|
|
defer func(fnc func() time.Duration) {
|
|
log.Print(fnc())
|
|
}(oub.Elapsed())
|
|
|
|
i := Instance()
|
|
defer i.Shutdown()
|
|
|
|
stopChan := make(chan struct{})
|
|
|
|
go func() {
|
|
for {
|
|
select {
|
|
case msg, ok := <-i.Message():
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
_, messageType, data, _ := msg()
|
|
|
|
switch messageType {
|
|
case types.DiscoveryMessageTypeHost:
|
|
log.Print("Discovered Host: ", data)
|
|
case types.DiscoveryMessageTypePort:
|
|
log.Print("Discovered Port: ", data, " Host: ", data.(*omd.Port).Host)
|
|
case types.DiscoveryMessageTypeService:
|
|
log.Print("Discovered Service: ", data, " Port: ", data.(*omd.Service).Port, " Host: ", data.(*omd.Service).Port.Host)
|
|
case types.DiscoveryMessageTypeStart:
|
|
log.Print("Discovery start: ", data)
|
|
case types.DiscoveryMessageTypeStop:
|
|
log.Print("Discovery stop: ", data)
|
|
close(stopChan)
|
|
default:
|
|
// log.Print(data)
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
|
|
// b, _ := json.Marshal(__test.Zone())
|
|
// log.Print(string(b))
|
|
|
|
// b, _ = json.Marshal(__test.DiscoverHost(
|
|
// __test.DiscoveryConfig(),
|
|
// 1,
|
|
// 254,
|
|
// __test.DiscoverPort(
|
|
// nil,
|
|
// 1,
|
|
// 65535,
|
|
// true,
|
|
// false,
|
|
// __test.DiscoverService(
|
|
// nil,
|
|
// ),
|
|
// ),
|
|
// ))
|
|
// log.Print(string(b))
|
|
|
|
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
|
|
datas []interface{}
|
|
}
|
|
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.datas...)
|
|
})
|
|
}
|
|
}
|
|
|
|
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)
|
|
})
|
|
}
|
|
}
|