drop separate ID method from providers, fill directly

This commit is contained in:
Umputun 2021-04-11 23:41:23 -05:00
parent 5a98338f84
commit 9c659afb0f
6 changed files with 21 additions and 83 deletions

View File

@ -36,7 +36,6 @@ type URLMapper struct {
type Provider interface {
Events(ctx context.Context) (res <-chan struct{})
List() (res []URLMapper, err error)
ID() ProviderID
}
// ProviderID holds provider identifier to emulate enum of them
@ -133,7 +132,6 @@ func (s *Service) mergeLists() (res []URLMapper) {
}
for i := range lst {
lst[i] = s.extendMapper(lst[i])
lst[i].ProviderID = p.ID()
}
res = append(res, lst...)
}

View File

@ -20,13 +20,12 @@ func TestService_Do(t *testing.T) {
},
ListFunc: func() ([]URLMapper, error) {
return []URLMapper{
{Server: "*", SrcMatch: *regexp.MustCompile("^/api/svc1/(.*)"), Dst: "http://127.0.0.1:8080/blah1/$1"},
{Server: "*", SrcMatch: *regexp.MustCompile("^/api/svc2/(.*)"), Dst: "http://127.0.0.2:8080/blah2/@1/abc"},
{Server: "*", SrcMatch: *regexp.MustCompile("^/api/svc1/(.*)"), Dst: "http://127.0.0.1:8080/blah1/$1",
ProviderID: PIFile},
{Server: "*", SrcMatch: *regexp.MustCompile("^/api/svc2/(.*)"),
Dst: "http://127.0.0.2:8080/blah2/@1/abc", ProviderID: PIFile},
}, nil
},
IDFunc: func() ProviderID {
return PIFile
},
}
p2 := &ProviderMock{
EventsFunc: func(ctx context.Context) <-chan struct{} {
@ -34,12 +33,10 @@ func TestService_Do(t *testing.T) {
},
ListFunc: func() ([]URLMapper, error) {
return []URLMapper{
{Server: "localhost", SrcMatch: *regexp.MustCompile("/api/svc3/xyz"), Dst: "http://127.0.0.3:8080/blah3/xyz"},
{Server: "localhost", SrcMatch: *regexp.MustCompile("/api/svc3/xyz"),
Dst: "http://127.0.0.3:8080/blah3/xyz", ProviderID: PIDocker},
}, nil
},
IDFunc: func() ProviderID {
return PIDocker
},
}
svc := NewService([]Provider{p1, p2})
@ -61,9 +58,6 @@ func TestService_Do(t *testing.T) {
assert.Equal(t, 1, len(p1.ListCalls()))
assert.Equal(t, 1, len(p2.ListCalls()))
assert.Equal(t, 2, len(p1.IDCalls()))
assert.Equal(t, 1, len(p2.IDCalls()))
}
func TestService_Match(t *testing.T) {
@ -75,14 +69,11 @@ func TestService_Match(t *testing.T) {
},
ListFunc: func() ([]URLMapper, error) {
return []URLMapper{
{SrcMatch: *regexp.MustCompile("^/api/svc1/(.*)"), Dst: "http://127.0.0.1:8080/blah1/$1"},
{SrcMatch: *regexp.MustCompile("^/api/svc1/(.*)"), Dst: "http://127.0.0.1:8080/blah1/$1", ProviderID: PIFile},
{Server: "m.example.com", SrcMatch: *regexp.MustCompile("^/api/svc2/(.*)"),
Dst: "http://127.0.0.2:8080/blah2/$1/abc"},
Dst: "http://127.0.0.2:8080/blah2/$1/abc", ProviderID: PIFile},
}, nil
},
IDFunc: func() ProviderID {
return PIFile
},
}
p2 := &ProviderMock{
EventsFunc: func(ctx context.Context) <-chan struct{} {
@ -90,12 +81,9 @@ func TestService_Match(t *testing.T) {
},
ListFunc: func() ([]URLMapper, error) {
return []URLMapper{
{SrcMatch: *regexp.MustCompile("/api/svc3/xyz"), Dst: "http://127.0.0.3:8080/blah3/xyz"},
{SrcMatch: *regexp.MustCompile("/api/svc3/xyz"), Dst: "http://127.0.0.3:8080/blah3/xyz", ProviderID: PIDocker},
}, nil
},
IDFunc: func() ProviderID {
return PIDocker
},
}
svc := NewService([]Provider{p1, p2})
@ -139,14 +127,11 @@ func TestService_Servers(t *testing.T) {
},
ListFunc: func() ([]URLMapper, error) {
return []URLMapper{
{SrcMatch: *regexp.MustCompile("^/api/svc1/(.*)"), Dst: "http://127.0.0.1:8080/blah1/$1"},
{SrcMatch: *regexp.MustCompile("^/api/svc1/(.*)"), Dst: "http://127.0.0.1:8080/blah1/$1", ProviderID: PIFile},
{Server: "m.example.com", SrcMatch: *regexp.MustCompile("^/api/svc2/(.*)"),
Dst: "http://127.0.0.2:8080/blah2/$1/abc"},
Dst: "http://127.0.0.2:8080/blah2/$1/abc", ProviderID: PIFile},
}, nil
},
IDFunc: func() ProviderID {
return PIFile
},
}
p2 := &ProviderMock{
EventsFunc: func(ctx context.Context) <-chan struct{} {
@ -154,12 +139,10 @@ func TestService_Servers(t *testing.T) {
},
ListFunc: func() ([]URLMapper, error) {
return []URLMapper{
{Server: "xx.reproxy.io", SrcMatch: *regexp.MustCompile("/api/svc3/xyz"), Dst: "http://127.0.0.3:8080/blah3/xyz"},
{Server: "xx.reproxy.io", SrcMatch: *regexp.MustCompile("/api/svc3/xyz"),
Dst: "http://127.0.0.3:8080/blah3/xyz", ProviderID: PIDocker},
}, nil
},
IDFunc: func() ProviderID {
return PIDocker
},
}
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)

View File

@ -95,14 +95,12 @@ func (d *Docker) List() ([]discovery.URLMapper, error) {
return nil, errors.Wrapf(err, "invalid src regex %s", srcURL)
}
res = append(res, discovery.URLMapper{Server: server, SrcMatch: *srcRegex, Dst: destURL, PingURL: pingURL})
res = append(res, discovery.URLMapper{Server: server, SrcMatch: *srcRegex, Dst: destURL,
PingURL: pingURL, ProviderID: discovery.PIDocker})
}
return res, nil
}
// ID returns providers id
func (d *Docker) ID() discovery.ProviderID { return discovery.PIDocker }
// activate starts blocking listener for all docker events
// filters everything except "container" type, detects stop/start events and publishes signals to eventsCh
func (d *Docker) events(ctx context.Context, client DockerClient, eventsCh chan struct{}) error {

View File

@ -94,7 +94,7 @@ func (d *File) List() (res []discovery.URLMapper, err error) {
if srv == "default" {
srv = "*"
}
mapper := discovery.URLMapper{Server: srv, SrcMatch: *rx, Dst: f.Dest, PingURL: f.Ping}
mapper := discovery.URLMapper{Server: srv, SrcMatch: *rx, Dst: f.Dest, PingURL: f.Ping, ProviderID: discovery.PIFile}
res = append(res, mapper)
}
}
@ -108,6 +108,3 @@ func (d *File) List() (res []discovery.URLMapper, err error) {
err = fh.Close()
return res, err
}
// ID returns providers id
func (d *File) ID() discovery.ProviderID { return discovery.PIFile }

View File

@ -36,10 +36,11 @@ func (s *Static) List() (res []discovery.URLMapper, err error) {
}
return discovery.URLMapper{
Server: strings.TrimSpace(elems[0]),
SrcMatch: *rx,
Dst: strings.TrimSpace(elems[2]),
PingURL: strings.TrimSpace(elems[3]),
Server: strings.TrimSpace(elems[0]),
SrcMatch: *rx,
Dst: strings.TrimSpace(elems[2]),
PingURL: strings.TrimSpace(elems[3]),
ProviderID: discovery.PIStatic,
}, nil
}
@ -52,6 +53,3 @@ func (s *Static) List() (res []discovery.URLMapper, err error) {
}
return res, nil
}
// ID returns providers id
func (s *Static) ID() discovery.ProviderID { return discovery.PIStatic }

View File

@ -21,9 +21,6 @@ var _ Provider = &ProviderMock{}
// EventsFunc: func(ctx context.Context) <-chan struct{} {
// panic("mock out the Events method")
// },
// IDFunc: func() ProviderID {
// panic("mock out the ID method")
// },
// ListFunc: func() ([]URLMapper, error) {
// panic("mock out the List method")
// },
@ -37,9 +34,6 @@ type ProviderMock struct {
// EventsFunc mocks the Events method.
EventsFunc func(ctx context.Context) <-chan struct{}
// IDFunc mocks the ID method.
IDFunc func() ProviderID
// ListFunc mocks the List method.
ListFunc func() ([]URLMapper, error)
@ -50,15 +44,11 @@ type ProviderMock struct {
// Ctx is the ctx argument value.
Ctx context.Context
}
// ID holds details about calls to the ID method.
ID []struct {
}
// List holds details about calls to the List method.
List []struct {
}
}
lockEvents sync.RWMutex
lockID sync.RWMutex
lockList sync.RWMutex
}
@ -93,32 +83,6 @@ func (mock *ProviderMock) EventsCalls() []struct {
return calls
}
// ID calls IDFunc.
func (mock *ProviderMock) ID() ProviderID {
if mock.IDFunc == nil {
panic("ProviderMock.IDFunc: method is nil but Provider.ID was just called")
}
callInfo := struct {
}{}
mock.lockID.Lock()
mock.calls.ID = append(mock.calls.ID, callInfo)
mock.lockID.Unlock()
return mock.IDFunc()
}
// IDCalls gets all the calls that were made to ID.
// Check the length with:
// len(mockedProvider.IDCalls())
func (mock *ProviderMock) IDCalls() []struct {
} {
var calls []struct {
}
mock.lockID.RLock()
calls = mock.calls.ID
mock.lockID.RUnlock()
return calls
}
// List calls ListFunc.
func (mock *ProviderMock) List() ([]URLMapper, error) {
if mock.ListFunc == nil {