Skip to content

Commit

Permalink
nomad: testing region list
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanuber committed Nov 24, 2015
1 parent c6d7313 commit 0bee0ea
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 2 deletions.
2 changes: 1 addition & 1 deletion command/agent/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (s *HTTPServer) registerHandlers(enableDebug bool) {
s.mux.HandleFunc("/v1/agent/force-leave", s.wrap(s.AgentForceLeaveRequest))
s.mux.HandleFunc("/v1/agent/servers", s.wrap(s.AgentServersRequest))

s.mux.HandleFunc("/v1/regions", s.wrap(s.RegionsListRequest))
s.mux.HandleFunc("/v1/regions", s.wrap(s.RegionListRequest))

s.mux.HandleFunc("/v1/status/leader", s.wrap(s.StatusLeaderRequest))
s.mux.HandleFunc("/v1/status/peers", s.wrap(s.StatusPeersRequest))
Expand Down
29 changes: 29 additions & 0 deletions command/agent/region_endpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package agent

import (
"net/http"
"net/http/httptest"
"testing"
)

func TestHTTP_RegionList(t *testing.T) {
httpTest(t, nil, func(s *TestServer) {
// Make the HTTP request
req, err := http.NewRequest("GET", "/v1/regions", nil)
if err != nil {
t.Fatalf("err: %v", err)
}
respW := httptest.NewRecorder()

// Make the request
obj, err := s.Server.RegionListRequest(respW, req)
if err != nil {
t.Fatalf("err: %v", err)
}

out := obj.([]string)
if len(out) != 1 || out[0] != "global" {
t.Fatalf("unexpected regions: %#v", out)
}
})
}
2 changes: 1 addition & 1 deletion command/agent/regions_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/hashicorp/nomad/nomad/structs"
)

func (s *HTTPServer) RegionsListRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
func (s *HTTPServer) RegionListRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
if req.Method != "GET" {
return nil, CodedError(405, ErrInvalidMethod)
}
Expand Down
46 changes: 46 additions & 0 deletions nomad/regions_endpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package nomad

import (
"fmt"
"testing"

"github.com/hashicorp/net-rpc-msgpackrpc"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/testutil"
)

func TestRegionList(t *testing.T) {
// Make the servers
s1 := testServer(t, func(c *Config) {
c.Region = "region1"
})
defer s1.Shutdown()
codec := rpcClient(t, s1)

s2 := testServer(t, func(c *Config) {
c.Region = "region2"
})
defer s2.Shutdown()

// Join the servers
s2Addr := fmt.Sprintf("127.0.0.1:%d",
s2.config.SerfConfig.MemberlistConfig.BindPort)
if n, err := s1.Join([]string{s2Addr}); err != nil || n != 1 {
t.Fatalf("Failed joining: %v (%d joined)", err, n)
}

// Query the regions list
testutil.WaitForResult(func() (bool, error) {
var arg structs.GenericRequest
var out []string
if err := msgpackrpc.CallWithCodec(codec, "Region.List", &arg, &out); err != nil {
t.Fatalf("err: %v", err)
}
if len(out) != 2 || out[0] != "region1" || out[1] != "region2" {
t.Fatalf("unexpected regions: %v", out)
}
return true, nil
}, func(err error) {
t.Fatalf("err: %v", err)
})
}
33 changes: 33 additions & 0 deletions nomad/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sync/atomic"
"testing"
"time"

"github.com/hashicorp/nomad/testutil"
)

var nextPort uint32 = 15000
Expand Down Expand Up @@ -86,3 +88,34 @@ func TestServer_RPC(t *testing.T) {
t.Fatalf("err: %v", err)
}
}

func TestServer_Regions(t *testing.T) {
// Make the servers
s1 := testServer(t, func(c *Config) {
c.Region = "region1"
})
defer s1.Shutdown()

s2 := testServer(t, func(c *Config) {
c.Region = "region2"
})
defer s2.Shutdown()

// Join them together
s2Addr := fmt.Sprintf("127.0.0.1:%d",
s2.config.SerfConfig.MemberlistConfig.BindPort)
if n, err := s1.Join([]string{s2Addr}); err != nil || n != 1 {
t.Fatalf("Failed joining: %v (%d joined)", err, n)
}

// Try listing the regions
testutil.WaitForResult(func() (bool, error) {
out := s1.Regions()
if len(out) != 2 || out[0] != "region1" || out[1] != "region2" {
return false, fmt.Errorf("unexpected regions: %v", out)
}
return true, nil
}, func(err error) {
t.Fatalf("err: %v", err)
})
}

0 comments on commit 0bee0ea

Please sign in to comment.