-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor HTTP server to make it easier to call agent methods
- Loading branch information
Showing
11 changed files
with
133 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package http | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestServer_agentReload(t *testing.T) { | ||
testCases := []struct { | ||
inputReq *http.Request | ||
expectedRespCode int | ||
name string | ||
}{ | ||
{ | ||
inputReq: httptest.NewRequest("PUT", "/v1/agent/reload", nil), | ||
expectedRespCode: 200, | ||
name: "successfully reload", | ||
}, | ||
{ | ||
inputReq: httptest.NewRequest("GET", "/v1/agent/reload", nil), | ||
expectedRespCode: 405, | ||
name: "incorrect request method", | ||
}, | ||
} | ||
|
||
srv, stopSrv := TestServer(t) | ||
defer stopSrv() | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
w := httptest.NewRecorder() | ||
srv.mux.ServeHTTP(w, tc.inputReq) | ||
assert.Equal(tc.expectedRespCode, w.Code) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package http | ||
|
||
import ( | ||
"testing" | ||
|
||
hclog "github.com/hashicorp/go-hclog" | ||
"github.com/hashicorp/nomad-autoscaler/agent" | ||
"github.com/hashicorp/nomad-autoscaler/agent/config" | ||
) | ||
|
||
func TestServer(t *testing.T) (*Server, func()) { | ||
cfg := &config.HTTP{ | ||
BindAddress: "127.0.0.1", | ||
BindPort: 0, // Use next available port. | ||
} | ||
|
||
s, err := NewHTTPServer(cfg, hclog.NewNullLogger(), &agent.MockAgentHTTP{}) | ||
if err != nil { | ||
t.Fatalf("failed to start test server: %v", err) | ||
} | ||
|
||
return s, func() { | ||
s.Stop() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,14 @@ | ||
package agent | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
import "net/http" | ||
|
||
"github.com/hashicorp/nomad-autoscaler/agent/http" | ||
) | ||
// The methods in this file implement in the http.AgentHTTP interface. | ||
|
||
func (a *Agent) handleHTTPRequests(ctx context.Context) { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case r := <-a.httpServer.AgentCh(): | ||
switch r.Type { | ||
case http.AgentRequestTypeReload: | ||
a.handleHTTPReload(r) | ||
default: | ||
r.ResponseCh <- fmt.Errorf("invalid request type %q", r.Type) | ||
} | ||
} | ||
} | ||
func (a *Agent) DisplayMetrics(resp http.ResponseWriter, req *http.Request) (interface{}, error) { | ||
return a.inMemSink.DisplayMetrics(resp, req) | ||
} | ||
|
||
func (a *Agent) handleHTTPReload(r http.AgentRequest) { | ||
func (a *Agent) Reload(resp http.ResponseWriter, req *http.Request) (interface{}, error) { | ||
a.reload() | ||
r.ResponseCh <- nil | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package agent | ||
|
||
import ( | ||
"net/http" | ||
|
||
metrics "github.com/armon/go-metrics" | ||
) | ||
|
||
type MockAgentHTTP struct{} | ||
|
||
func (m *MockAgentHTTP) DisplayMetrics(resp http.ResponseWriter, req *http.Request) (interface{}, error) { | ||
return metrics.MetricsSummary{ | ||
Timestamp: "2020-11-17 00:17:50 +0000 UTC", | ||
Counters: []metrics.SampledValue{}, | ||
Gauges: []metrics.GaugeValue{}, | ||
Points: []metrics.PointValue{}, | ||
Samples: []metrics.SampledValue{}, | ||
}, nil | ||
} | ||
func (m *MockAgentHTTP) Reload(resp http.ResponseWriter, req *http.Request) (interface{}, error) { | ||
return nil, nil | ||
} |
Oops, something went wrong.