Skip to content

Commit

Permalink
Remove RecordLister from handler.Server
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-soltesz committed Jul 24, 2024
1 parent 8eadcad commit c0a776e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
10 changes: 2 additions & 8 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ type Server struct {
ASN ASNFinder
DNS dnsiface.Service

Nodes RecordLister
dnsTracker DNSTracker
}

Expand All @@ -66,23 +65,18 @@ type IataFinder interface {
type DNSTracker interface {
Update(string) error
Delete(string) error
}

// RecordLister lists known nodes from backingstore, e.g. file or Memorystore.
type RecordLister interface {
List() ([]string, error)
}

// NewServer creates a new Server instance for request handling.
func NewServer(project string, finder IataFinder, maxmind MaxmindFinder, asn ASNFinder,
ds dnsiface.Service, tracker DNSTracker, rl RecordLister) *Server {
ds dnsiface.Service, tracker DNSTracker) *Server {
return &Server{
Project: project,
Iata: finder,
Maxmind: maxmind,
ASN: asn,
DNS: ds,
Nodes: rl,

dnsTracker: tracker,
}
Expand Down Expand Up @@ -315,7 +309,7 @@ func (s *Server) Delete(rw http.ResponseWriter, req *http.Request) {
func (s *Server) List(rw http.ResponseWriter, req *http.Request) {
configs := []discovery.StaticConfig{}
resp := v0.ListResponse{}
hosts, err := s.Nodes.List()
hosts, err := s.dnsTracker.List()
if err != nil {
resp.Error = &v2.Error{
Type: "list",
Expand Down
38 changes: 17 additions & 21 deletions handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ func (f *fakeDNS) ChangeCreate(ctx context.Context, project string, zone string,
type fakeStatusTracker struct {
updateErr error
deleteErr error
nodes []string
listErr error
}

func (f *fakeStatusTracker) Update(string) error {
Expand All @@ -89,6 +91,10 @@ func (f *fakeStatusTracker) Delete(string) error {
return f.deleteErr
}

func (f *fakeStatusTracker) List() ([]string, error) {
return f.nodes, f.listErr
}

func TestServer_Lookup(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -218,7 +224,7 @@ func TestServer_Lookup(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewServer("mlab-sandbox", tt.iata, tt.maxmind, &fakeAsn{}, &fakeDNS{}, &fakeStatusTracker{}, nil)
s := NewServer("mlab-sandbox", tt.iata, tt.maxmind, &fakeAsn{}, &fakeDNS{}, &fakeStatusTracker{})
rw := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/autojoin/v0/lookup"+tt.request, nil)
for key, value := range tt.headers {
Expand All @@ -240,7 +246,7 @@ func TestServer_Lookup(t *testing.T) {
func TestServer_Reload(t *testing.T) {
t.Run("success", func(t *testing.T) {
f := &fakeIataFinder{}
s := NewServer("mlab-sandbox", f, &fakeMaxmind{}, &fakeAsn{}, &fakeDNS{}, &fakeStatusTracker{}, nil)
s := NewServer("mlab-sandbox", f, &fakeMaxmind{}, &fakeAsn{}, &fakeDNS{}, &fakeStatusTracker{})
s.Reload(context.Background())
if f.loads != 1 {
t.Errorf("Reload failed to call iata loader")
Expand All @@ -250,7 +256,7 @@ func TestServer_Reload(t *testing.T) {

func TestServer_LiveAndReady(t *testing.T) {
t.Run("success", func(t *testing.T) {
s := NewServer("mlab-sandbox", &fakeIataFinder{}, &fakeMaxmind{}, &fakeAsn{}, &fakeDNS{}, &fakeStatusTracker{}, nil)
s := NewServer("mlab-sandbox", &fakeIataFinder{}, &fakeMaxmind{}, &fakeAsn{}, &fakeDNS{}, &fakeStatusTracker{})
rw := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/", nil)
s.Live(rw, req)
Expand Down Expand Up @@ -399,7 +405,7 @@ func TestServer_Register(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewServer("mlab-sandbox", tt.Iata, tt.Maxmind, tt.ASN, tt.DNS, tt.Tracker, nil)
s := NewServer("mlab-sandbox", tt.Iata, tt.Maxmind, tt.ASN, tt.DNS, tt.Tracker)
rw := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/autojoin/v0/node/register"+tt.params, nil)

Expand Down Expand Up @@ -482,7 +488,7 @@ func TestServer_Delete(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewServer("mlab-sandbox", nil, nil, nil, tt.DNS, tt.Tracker, nil)
s := NewServer("mlab-sandbox", nil, nil, nil, tt.DNS, tt.Tracker)
rw := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/autojoin/v0/node/delete"+tt.qs, nil)
s.Delete(rw, req)
Expand All @@ -494,28 +500,18 @@ func TestServer_Delete(t *testing.T) {
}
}

type fakeLister struct {
nodes []string
err error
}

func (f *fakeLister) List() ([]string, error) {
return f.nodes, f.err
}

func TestServer_List(t *testing.T) {
tests := []struct {
name string
params string
lister *fakeLister
lister DNSTracker
wantCode int
wantLength int
}{
// TODO: Add test cases.
{
name: "success",
params: "",
lister: &fakeLister{
lister: &fakeStatusTracker{
nodes: []string{"test1"},
},
wantCode: http.StatusOK,
Expand All @@ -524,7 +520,7 @@ func TestServer_List(t *testing.T) {
{
name: "success-prometheus",
params: "?format=prometheus",
lister: &fakeLister{
lister: &fakeStatusTracker{
nodes: []string{"test1"},
},
wantCode: http.StatusOK,
Expand All @@ -533,16 +529,16 @@ func TestServer_List(t *testing.T) {
{
name: "error-internal",
params: "",
lister: &fakeLister{
err: errors.New("fake list error"),
lister: &fakeStatusTracker{
listErr: errors.New("fake list error"),
},
wantCode: http.StatusInternalServerError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Nodes: tt.fields.Nodes,
s := NewServer("mlab-sandbox", nil, nil, nil, nil, nil, tt.lister)
s := NewServer("mlab-sandbox", nil, nil, nil, nil, tt.lister)
rw := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/autojoin/v0/node/list"+tt.params, nil)

Expand Down

0 comments on commit c0a776e

Please sign in to comment.