2021-01-13 17:07:04 +00:00
|
|
|
package endpoint
|
2020-04-13 22:14:56 +00:00
|
|
|
|
|
|
|
import (
|
2020-06-15 10:04:08 +00:00
|
|
|
"reflect"
|
2020-04-13 22:14:56 +00:00
|
|
|
"testing"
|
2020-12-26 13:11:49 +00:00
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
api_mock "gitlab.com/inetmock/inetmock/internal/mock/api"
|
|
|
|
logging_mock "gitlab.com/inetmock/inetmock/internal/mock/logging"
|
|
|
|
plugins_mock "gitlab.com/inetmock/inetmock/internal/mock/plugins"
|
|
|
|
"gitlab.com/inetmock/inetmock/pkg/api"
|
|
|
|
"gitlab.com/inetmock/inetmock/pkg/config"
|
|
|
|
"gitlab.com/inetmock/inetmock/pkg/health"
|
|
|
|
"gitlab.com/inetmock/inetmock/pkg/logging"
|
2020-04-13 22:14:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_endpointManager_CreateEndpoint(t *testing.T) {
|
|
|
|
type fields struct {
|
2020-12-26 13:11:49 +00:00
|
|
|
logger logging.Logger
|
|
|
|
registry api.HandlerRegistry
|
2020-04-13 22:14:56 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
name string
|
2020-12-26 13:11:49 +00:00
|
|
|
multiHandlerConfig config.EndpointConfig
|
2020-04-13 22:14:56 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
wantEndpoints int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test add endpoint",
|
|
|
|
wantErr: false,
|
|
|
|
wantEndpoints: 1,
|
|
|
|
fields: fields{
|
|
|
|
logger: func() logging.Logger {
|
2020-04-25 22:22:45 +00:00
|
|
|
return logging_mock.NewMockLogger(gomock.NewController(t))
|
2020-04-13 22:14:56 +00:00
|
|
|
}(),
|
2020-06-15 10:04:08 +00:00
|
|
|
registry: func() api.HandlerRegistry {
|
2020-04-25 22:22:45 +00:00
|
|
|
registry := plugins_mock.NewMockHandlerRegistry(gomock.NewController(t))
|
2020-04-13 22:14:56 +00:00
|
|
|
registry.
|
|
|
|
EXPECT().
|
|
|
|
HandlerForName("sampleHandler").
|
|
|
|
MinTimes(1).
|
|
|
|
MaxTimes(1).
|
2020-04-25 22:22:45 +00:00
|
|
|
Return(api_mock.NewMockProtocolHandler(gomock.NewController(t)), true)
|
2020-04-13 22:14:56 +00:00
|
|
|
return registry
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
name: "sampleEndpoint",
|
2020-12-26 13:11:49 +00:00
|
|
|
multiHandlerConfig: config.EndpointConfig{
|
2020-04-27 22:26:15 +00:00
|
|
|
Handler: "sampleHandler",
|
|
|
|
Ports: []uint16{80},
|
|
|
|
ListenAddress: "0.0.0.0",
|
|
|
|
},
|
2020-04-13 22:14:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test add unknown handler",
|
|
|
|
wantErr: true,
|
|
|
|
wantEndpoints: 0,
|
|
|
|
fields: fields{
|
|
|
|
logger: func() logging.Logger {
|
2020-04-25 22:22:45 +00:00
|
|
|
return logging_mock.NewMockLogger(gomock.NewController(t))
|
2020-04-13 22:14:56 +00:00
|
|
|
}(),
|
2020-06-15 10:04:08 +00:00
|
|
|
registry: func() api.HandlerRegistry {
|
2020-04-25 22:22:45 +00:00
|
|
|
registry := plugins_mock.NewMockHandlerRegistry(gomock.NewController(t))
|
2020-04-13 22:14:56 +00:00
|
|
|
registry.
|
|
|
|
EXPECT().
|
|
|
|
HandlerForName("sampleHandler").
|
|
|
|
MinTimes(1).
|
|
|
|
MaxTimes(1).
|
|
|
|
Return(nil, false)
|
|
|
|
return registry
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
name: "sampleEndpoint",
|
2020-12-26 13:11:49 +00:00
|
|
|
multiHandlerConfig: config.EndpointConfig{
|
2020-04-27 22:26:15 +00:00
|
|
|
Handler: "sampleHandler",
|
|
|
|
Ports: []uint16{80},
|
|
|
|
ListenAddress: "0.0.0.0",
|
|
|
|
},
|
2020-04-13 22:14:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-12-26 13:11:49 +00:00
|
|
|
e := NewEndpointManager(tt.fields.registry, tt.fields.logger, health.New(), nil)
|
2020-04-13 22:14:56 +00:00
|
|
|
if err := e.CreateEndpoint(tt.args.name, tt.args.multiHandlerConfig); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreateEndpoint() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(e.RegisteredEndpoints()) != tt.wantEndpoints {
|
|
|
|
t.Errorf("RegisteredEndpoints() = %d, want = 1", len(e.RegisteredEndpoints()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(e.RegisteredEndpoints()) > 0 && e.RegisteredEndpoints()[0].Name() != tt.args.name {
|
|
|
|
t.Errorf("Name() = %s, want = %s", e.RegisteredEndpoints()[0].Name(), tt.args.name)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-06-15 10:04:08 +00:00
|
|
|
|
|
|
|
func Test_endpointManager_StartedEndpoints(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
logger logging.Logger
|
|
|
|
registeredEndpoints []Endpoint
|
|
|
|
properlyStartedEndpoints []Endpoint
|
|
|
|
registry api.HandlerRegistry
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
want []Endpoint
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "",
|
|
|
|
fields: fields{},
|
|
|
|
want: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
e := endpointManager{
|
|
|
|
logger: tt.fields.logger,
|
|
|
|
registeredEndpoints: tt.fields.registeredEndpoints,
|
|
|
|
properlyStartedEndpoints: tt.fields.properlyStartedEndpoints,
|
|
|
|
}
|
|
|
|
if got := e.StartedEndpoints(); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("StartedEndpoints() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|