624ecab901
API queries can request pagination using the `NextToken` and `PerPage` fields of `QueryOptions`, when supported by the underlying API. Add a `NextToken` field to the `structs.QueryMeta` so that we have a common field across RPCs to tell the caller where to resume paging from on their next API call. Include this field on the `api.QueryMeta` as well so that it's available for future versions of List HTTP APIs that wrap the response with `QueryMeta` rather than returning a simple list of structs. In the meantime callers can get the `X-Nomad-NextToken`. Add pagination to the `Eval.List` RPC by checking for pagination token and page size in `QueryOptions`. This will allow resuming from the last ID seen so long as the query parameters and the state store itself are unchanged between requests. Add filtering by job ID or evaluation status over the results we get out of the state store. Parse the query parameters of the `Eval.List` API into the arguments expected for filtering in the RPC call.
113 lines
2.3 KiB
Go
113 lines
2.3 KiB
Go
package state
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
)
|
|
|
|
func TestPaginator(t *testing.T) {
|
|
t.Parallel()
|
|
ids := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
|
|
|
|
cases := []struct {
|
|
name string
|
|
perPage int32
|
|
nextToken string
|
|
expected []string
|
|
expectedNextToken string
|
|
}{
|
|
{
|
|
name: "size-3 page-1",
|
|
perPage: 3,
|
|
expected: []string{"0", "1", "2"},
|
|
expectedNextToken: "3",
|
|
},
|
|
{
|
|
name: "size-5 page-2 stop before end",
|
|
perPage: 5,
|
|
nextToken: "3",
|
|
expected: []string{"3", "4", "5", "6", "7"},
|
|
expectedNextToken: "8",
|
|
},
|
|
{
|
|
name: "page-2 reading off the end",
|
|
perPage: 10,
|
|
nextToken: "5",
|
|
expected: []string{"5", "6", "7", "8", "9"},
|
|
expectedNextToken: "",
|
|
},
|
|
{
|
|
name: "starting off the end",
|
|
perPage: 5,
|
|
nextToken: "a",
|
|
expected: []string{},
|
|
expectedNextToken: "",
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
iter := newTestIterator(ids)
|
|
results := []string{}
|
|
|
|
paginator := NewPaginator(iter,
|
|
structs.QueryOptions{
|
|
PerPage: tc.perPage, NextToken: tc.nextToken,
|
|
},
|
|
func(raw interface{}) {
|
|
result := raw.(*mockObject)
|
|
results = append(results, result.GetID())
|
|
},
|
|
)
|
|
|
|
nextToken := paginator.Page()
|
|
require.Equal(t, tc.expected, results)
|
|
require.Equal(t, tc.expectedNextToken, nextToken)
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
// helpers for pagination tests
|
|
|
|
// implements memdb.ResultIterator interface
|
|
type testResultIterator struct {
|
|
results chan interface{}
|
|
idx int
|
|
}
|
|
|
|
func (i testResultIterator) Next() interface{} {
|
|
select {
|
|
case result := <-i.results:
|
|
return result
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// not used, but required to implement memdb.ResultIterator
|
|
func (i testResultIterator) WatchCh() <-chan struct{} {
|
|
return make(<-chan struct{})
|
|
}
|
|
|
|
type mockObject struct {
|
|
id string
|
|
}
|
|
|
|
func (m *mockObject) GetID() string {
|
|
return m.id
|
|
}
|
|
|
|
func newTestIterator(ids []string) memdb.ResultIterator {
|
|
iter := testResultIterator{results: make(chan interface{}, 20)}
|
|
for _, id := range ids {
|
|
iter.results <- &mockObject{id: id}
|
|
}
|
|
return iter
|
|
}
|