probe/discovery/discoverer_test.go
crusader 126472bc2d ing
2018-09-04 01:11:58 +09:00

476 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
}
switch msg.Type() {
case types.DiscoveryMessageTypeHost:
log.Print("Discovered Host: ", msg.Data())
case types.DiscoveryMessageTypePort:
log.Print("Discovered Port: ", msg.Data(), " Host: ", msg.Data().(*omd.Port).Host)
case types.DiscoveryMessageTypeService:
log.Print("Discovered Service: ", msg.Data(), " Port: ", msg.Data().(*omd.Service).Port, " Host: ", msg.Data().(*omd.Service).Port.Host)
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())
}
}
}
}()
// 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
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)
})
}
}