From 312bb19e1c4a083d6f523cc2fd4f8de858df130d Mon Sep 17 00:00:00 2001 From: Chelsea Holland Komlo Date: Thu, 3 Aug 2017 14:47:20 +0000 Subject: [PATCH] refactor and add error handling for invalid context type --- command/agent/resources_endpoint_test.go | 6 ++--- nomad/resources_endpoint.go | 12 ++++++--- nomad/resources_endpoint_test.go | 31 ++++++++++++++++++++---- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/command/agent/resources_endpoint_test.go b/command/agent/resources_endpoint_test.go index 5bed0dd0c..b07a192af 100644 --- a/command/agent/resources_endpoint_test.go +++ b/command/agent/resources_endpoint_test.go @@ -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() diff --git a/nomad/resources_endpoint.go b/nomad/resources_endpoint.go index 1d459071a..4056bb48d 100644 --- a/nomad/resources_endpoint.go +++ b/nomad/resources_endpoint.go @@ -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 { diff --git a/nomad/resources_endpoint_test.go b/nomad/resources_endpoint_test.go index 75e90a42c..5dfeb5978 100644 --- a/nomad/resources_endpoint_test.go +++ b/nomad/resources_endpoint_test.go @@ -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") +}