adding allocations to resouces list endpoint

adding nodes to resources list endpoint
This commit is contained in:
Chelsea Holland Komlo 2017-08-03 14:28:38 +00:00
parent 2759ffe2d2
commit 98fe705dff
2 changed files with 97 additions and 0 deletions

View File

@ -26,6 +26,10 @@ func getMatches(iter memdb.ResultIterator, context, prefix string) ([]string, bo
return i.(*structs.Job).ID
case *structs.Evaluation:
return i.(*structs.Evaluation).ID
case *structs.Allocation:
return i.(*structs.Allocation).ID
case *structs.Node:
return i.(*structs.Node).ID
default:
return ""
}
@ -69,11 +73,16 @@ func (r *Resources) List(args *structs.ResourcesRequest,
iter, err = state.JobsByIDPrefix(ws, args.Prefix)
} else if args.Context == "eval" {
iter, err = state.EvalsByIDPrefix(ws, args.Prefix)
} else if args.Context == "alloc" {
iter, err = state.AllocsByIDPrefix(ws, args.Prefix)
} else if args.Context == "node" {
iter, err = state.NodesByIDPrefix(ws, args.Prefix)
}
if err != nil {
return err
}
res, isTrunc = getMatches(iter, args.Context, args.Prefix)
reply.Matches[args.Context] = res
reply.Truncations[args.Context] = isTrunc

View File

@ -127,3 +127,91 @@ func TestResourcesEndpoint_List_ShouldReturnEvals(t *testing.T) {
assert.Equal(t, resp.Truncations["job"], false)
}
func TestResourcesEndpoint_List_Allocation(t *testing.T) {
t.Parallel()
s := testServer(t, func(c *Config) {
c.NumSchedulers = 0
})
defer s.Shutdown()
codec := rpcClient(t, s)
testutil.WaitForLeader(t, s.RPC)
alloc := mock.Alloc()
summary := mock.JobSummary(alloc.JobID)
state := s.fsm.State()
if err := state.UpsertJobSummary(999, summary); err != nil {
t.Fatalf("err: %v", err)
}
if err := state.UpsertAllocs(1000, []*structs.Allocation{alloc}); err != nil {
t.Fatalf("err: %v", err)
}
prefix := alloc.ID[:len(alloc.ID)-2]
req := &structs.ResourcesRequest{
Prefix: prefix,
Context: "alloc",
}
var resp structs.ResourcesResponse
if err := msgpackrpc.CallWithCodec(codec, "Resources.List", req, &resp); err != nil {
t.Fatalf("err: %v", err)
}
numMatches := len(resp.Matches["alloc"])
if numMatches != 1 {
t.Fatalf(fmt.Sprintf("err: the number of allocations expected %d does not match the number expected %d", 1, numMatches))
}
recAlloc := resp.Matches["alloc"][0]
if recAlloc != alloc.ID {
t.Fatalf(fmt.Sprintf("err: expected %s allocation but received %s", alloc.ID, recAlloc))
}
assert.Equal(t, resp.Truncations["alloc"], false)
}
func TestResourcesEndpoint_List_Node(t *testing.T) {
t.Parallel()
s := testServer(t, func(c *Config) {
c.NumSchedulers = 0
})
defer s.Shutdown()
codec := rpcClient(t, s)
testutil.WaitForLeader(t, s.RPC)
state := s.fsm.State()
node := mock.Node()
if err := state.UpsertNode(100, node); err != nil {
t.Fatalf("err: %v", err)
}
prefix := node.ID[:len(node.ID)-2]
req := &structs.ResourcesRequest{
Prefix: prefix,
Context: "node",
}
var resp structs.ResourcesResponse
if err := msgpackrpc.CallWithCodec(codec, "Resources.List", req, &resp); err != nil {
t.Fatalf("err: %v", err)
}
numMatches := len(resp.Matches["node"])
if numMatches != 1 {
t.Fatalf(fmt.Sprintf("err: the number of nodes expected %d does not match the number expected %d", 1, numMatches))
}
recNode := resp.Matches["node"][0]
if recNode != node.ID {
t.Fatalf(fmt.Sprintf("err: expected %s node but received %s", node.ID, recNode))
}
assert.Equal(t, resp.Truncations["node"], false)
}