refactor and add error handling for invalid context type

This commit is contained in:
Chelsea Holland Komlo 2017-08-03 14:47:20 +00:00
parent 98fe705dff
commit 312bb19e1c
3 changed files with 37 additions and 12 deletions

View file

@ -36,7 +36,7 @@ func createJobForTest(jobID string, s *TestAgent, t *testing.T) {
}
}
func TestHTTP_ResourcesWithSingleJob(t *testing.T) {
func TestHTTP_Resources_SingleJob(t *testing.T) {
testJob := "aaaaaaaa-e8f7-fd38-c855-ab94ceb89706"
testJobPrefix := "aaaaaaaa-e8f7-fd38"
t.Parallel()
@ -71,7 +71,7 @@ func TestHTTP_ResourcesWithSingleJob(t *testing.T) {
})
}
func TestHTTP_ResourcesWithMultipleJobs(t *testing.T) {
func TestHTTP_Resources_MultipleJobs(t *testing.T) {
testJobA := "aaaaaaaa-e8f7-fd38-c855-ab94ceb89706"
testJobB := "aaaaaaaa-e8f7-fd38-c855-ab94ceb89707"
testJobC := "bbbbbbbb-e8f7-fd38-c855-ab94ceb89707"
@ -113,7 +113,7 @@ func TestHTTP_ResourcesWithMultipleJobs(t *testing.T) {
})
}
func TestHTTP_ResoucesListForEvaluations(t *testing.T) {
func TestHTTP_ResoucesList_Evaluation(t *testing.T) {
t.Parallel()
httpTest(t, nil, func(s *TestAgent) {
state := s.Agent.server.State()

View file

@ -1,6 +1,7 @@
package nomad
import (
"fmt"
"github.com/hashicorp/go-memdb"
"github.com/hashicorp/nomad/nomad/state"
"github.com/hashicorp/nomad/nomad/structs"
@ -69,14 +70,17 @@ func (r *Resources) List(args *structs.ResourcesRequest,
res := make([]string, 0)
isTrunc := false
if args.Context == "job" {
switch args.Context {
case "job":
iter, err = state.JobsByIDPrefix(ws, args.Prefix)
} else if args.Context == "eval" {
case "eval":
iter, err = state.EvalsByIDPrefix(ws, args.Prefix)
} else if args.Context == "alloc" {
case "alloc":
iter, err = state.AllocsByIDPrefix(ws, args.Prefix)
} else if args.Context == "node" {
case "node":
iter, err = state.NodesByIDPrefix(ws, args.Prefix)
default:
return fmt.Errorf("invalid context")
}
if err != nil {

View file

@ -29,7 +29,7 @@ func TestResourcesEndpoint_List(t *testing.T) {
t.Parallel()
s := testServer(t, func(c *Config) {
c.NumSchedulers = 0 // Prevent automatic dequeue
c.NumSchedulers = 0
})
defer s.Shutdown()
@ -56,12 +56,13 @@ func TestResourcesEndpoint_List(t *testing.T) {
assert.Equal(t, jobID, resp.Matches["job"][0])
}
func TestResourcesEndpoint_List_ShouldTruncateResultsToUnder20(t *testing.T) {
// truncate should limit results to 20
func TestResourcesEndpoint_List_Truncate(t *testing.T) {
prefix := "aaaaaaaa-e8f7-fd38-c855-ab94ceb8970"
t.Parallel()
s := testServer(t, func(c *Config) {
c.NumSchedulers = 0 // Prevent automatic dequeue
c.NumSchedulers = 0
})
defer s.Shutdown()
@ -90,10 +91,10 @@ func TestResourcesEndpoint_List_ShouldTruncateResultsToUnder20(t *testing.T) {
assert.Equal(t, resp.Truncations["job"], true)
}
func TestResourcesEndpoint_List_ShouldReturnEvals(t *testing.T) {
func TestResourcesEndpoint_List_Evals(t *testing.T) {
t.Parallel()
s := testServer(t, func(c *Config) {
c.NumSchedulers = 0 // Prevent automatic dequeue
c.NumSchedulers = 0
})
defer s.Shutdown()
@ -215,3 +216,23 @@ func TestResourcesEndpoint_List_Node(t *testing.T) {
assert.Equal(t, resp.Truncations["node"], false)
}
func TestResourcesEndpoint_List_InvalidContext(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)
req := &structs.ResourcesRequest{
Prefix: "anyPrefix",
Context: "invalid",
}
var resp structs.ResourcesResponse
err := msgpackrpc.CallWithCodec(codec, "Resources.List", req, &resp)
assert.Equal(t, err.Error(), "invalid context")
}