Files
threshold/internal/clientcache/cmd/search/search_test.go
T
Jim fba9d1cdf1 Add client daemon repo, server, and cmd
The commands added are:
boundary daemon start
boundary daemon stop
boundary daemon add-token
boundary daemon status
boundary search

If these actions are performed on a platform that doesn't support sqlite
natively in go an error is returned.

Besides some hooks into the cmd package all the significant logic
resides in internal/clientcache

feat (cmds/authenticate): output the token name when storing token

feat: update go-dbw which now uses pure go sqlite driver

refact (base.Command): export FlagOutputCurlString

Exporting FlagOutputCurlString allows other commands that embed
the base.Command to access the flag as needed

feat: add a cache Repository to manage cache storage

feat: add a "cache" CLI command

This new command will support both managing and searching the
local CLI cache daemon.

feat: start cache daemon as needed when issue CLI commands

Move files to new directories and add persona table (#3526)

* Add persona table and move files to new directories

Client daemon now uses unix sockets and update pathing (#3535)

* Add unix socket listen and dialers with file permissions applied

* Update the pathing to match RFC

Cache refreshing now uses all stored personas. (#3545)

Updating headers for LLB files

Decouple daemon and cache server (#3566)

* Separate daemon from server

* Add persona handler to daemon server (#3579)

Moved search to use mql and added tests for the search command (#3589)

* search uses hashicorp/mql

Windows support for client side daemon (#3618)

* exec.Command for background process

* Log to stderr when in foreground and file always

* Use pid files across platform for start and stop

Requests with version info must match the daemon's version (#3640)

* requests with version info must match daemons

Update dependencies and breakout cgo/pure go sqlite drivers  (#3657)

upgrade go-dbw deps and add cgo build tags

Enable graceful shutdown in windows (#3695)

* Enable graceful shutdown in windows

Co-authored-by: Jim <jlambert@hashicorp.com>

Targets delete when a persona changes address or user id (#3673)

* Handle cache refresh in repo

Add sessions to the client side cache (#3682)

* Add sessions to repository

* Search command supports sessions

* adding session related tests to db and refresh

Change cache schema from personas to tokens+users (#3737)

* Change from personas to tokens+users

* Separate keyring tokens from auth tokens

Add support for keyringless auth tokens (#3765)

* Add support for keyringless auth tokens

Remove expired auth tokens from cache (#3768)

* Remove expired auth tokens from cache

Complete the TODO of checking for inflight tx (#3779)

Delete tokens that the controller says are invalid (#3782)

fix search test with boundary token check (#3799)

Collect resource info in repository_<resource>s.go (#3769)

* Collect resource info in repository_<resource>s.go

* Move search logic into the domain (#3794)

Daemon and search restricted to supported platforms (#3801)

* Daemon and search restricted to supported platforms

Reorganize the clientcache packages (#3814)

* Refactor for self contained domain logic

* Put clientcache stuff under internal
2024-01-05 07:43:20 -08:00

253 lines
6.5 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package search
import (
"context"
"errors"
"sync"
"testing"
"time"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/authtokens"
"github.com/hashicorp/boundary/api/sessions"
"github.com/hashicorp/boundary/api/targets"
"github.com/hashicorp/boundary/internal/clientcache/internal/daemon"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type testCommander struct {
t *testing.T
at *authtokens.AuthToken
}
func (r *testCommander) keyring() string {
return r.at.Id
}
func (r *testCommander) tokenName() string {
return r.at.Id
}
func (r *testCommander) Client(opt ...base.Option) (*api.Client, error) {
client, err := api.NewClient(nil)
require.NoError(r.t, err)
return client, nil
}
func (r *testCommander) DiscoverKeyringTokenInfo() (string, string, error) {
return r.keyring(), r.tokenName(), nil
}
func (r *testCommander) ReadTokenFromKeyring(k, a string) *authtokens.AuthToken {
return r.at
}
func TestSearch(t *testing.T) {
ctx := context.Background()
at := &authtokens.AuthToken{
Id: "at_1",
UserId: "user_1",
Token: "at_1_token",
ExpirationTime: time.Now().Add(time.Minute),
}
cmd := &testCommander{t: t, at: at}
boundaryTokenReaderFn := func(ctx context.Context, addr, authToken string) (*authtokens.AuthToken, error) {
if authToken == at.Token {
return at, nil
}
return nil, errors.New("test not found error")
}
srv := daemon.NewTestServer(t, cmd)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
srv.Serve(t, daemon.WithBoundaryTokenReaderFunc(ctx, boundaryTokenReaderFn))
}()
// Give the store some time to get initialized
time.Sleep(100 * time.Millisecond)
errorCases := []struct {
name string
fb filterBy
apiErrContains string
}{
{
name: "no resource",
fb: filterBy{
flagQuery: "name=name",
authTokenId: at.Id,
},
apiErrContains: "resource is a required field but was empty",
},
{
name: "bad resource",
fb: filterBy{
authTokenId: at.Id,
flagQuery: "name=name",
resource: "hosts",
},
apiErrContains: "provided resource is not a valid searchable resource",
},
{
name: "unknown auth token id",
fb: filterBy{
authTokenId: "unknown",
flagQuery: "description % tar",
resource: "targets",
},
apiErrContains: "Forbidden",
},
{
name: "unsupported column",
fb: filterBy{
authTokenId: at.Id,
flagQuery: "item % 'tar'",
resource: "targets",
},
apiErrContains: "invalid column \"item\"",
},
}
for _, tc := range errorCases {
t.Run(tc.name, func(t *testing.T) {
resp, err := search(ctx, srv.BaseDotDir(), tc.fb)
require.NoError(t, err)
r := daemon.SearchResult{}
apiErr, err := resp.Decode(&r)
assert.NoError(t, err)
assert.NotNil(t, apiErr)
assert.Contains(t, apiErr.Message, tc.apiErrContains)
})
}
t.Run("empty response from list", func(t *testing.T) {
resp, err := search(ctx, srv.BaseDotDir(), filterBy{
authTokenId: at.Id,
resource: "targets",
})
require.NoError(t, err)
r := daemon.SearchResult{}
apiErr, err := resp.Decode(&r)
assert.NoError(t, err)
assert.Nil(t, apiErr)
assert.NotNil(t, r)
assert.EqualValues(t, r, daemon.SearchResult{})
})
t.Run("empty response from query", func(t *testing.T) {
resp, err := search(ctx, srv.BaseDotDir(), filterBy{
authTokenId: at.Id,
flagQuery: "name=name",
resource: "targets",
})
require.NoError(t, err)
r := daemon.SearchResult{}
apiErr, err := resp.Decode(&r)
assert.NoError(t, err)
assert.Nil(t, apiErr)
assert.NotNil(t, r)
assert.EqualValues(t, r, daemon.SearchResult{})
})
srv.AddResources(t, cmd.at, []*targets.Target{
{Id: "ttcp_1234567890", Name: "name1", Description: "description1"},
{Id: "ttcp_0987654321", Name: "name2", Description: "description2"},
}, []*sessions.Session{
{Id: "sess_1234567890", TargetId: "ttcp_1234567890", Status: "pending"},
{Id: "sess_0987654321", TargetId: "ttcp_0987654321", Status: "pending"},
}, boundaryTokenReaderFn)
t.Run("target response from list", func(t *testing.T) {
resp, err := search(ctx, srv.BaseDotDir(), filterBy{
authTokenId: at.Id,
resource: "targets",
})
require.NoError(t, err)
r := daemon.SearchResult{}
apiErr, err := resp.Decode(&r)
assert.NoError(t, err)
assert.Nil(t, apiErr)
assert.NotNil(t, r)
assert.Len(t, r.Targets, 2)
})
t.Run("full target response from query", func(t *testing.T) {
resp, err := search(ctx, srv.BaseDotDir(), filterBy{
authTokenId: at.Id,
flagQuery: "id % ttcp",
resource: "targets",
})
require.NoError(t, err)
r := daemon.SearchResult{}
apiErr, err := resp.Decode(&r)
assert.NoError(t, err)
assert.Nil(t, apiErr)
assert.NotNil(t, r)
assert.Len(t, r.Targets, 2)
})
t.Run("partial target response from query", func(t *testing.T) {
resp, err := search(ctx, srv.BaseDotDir(), filterBy{
authTokenId: at.Id,
flagQuery: "id % ttcp_1234567890",
resource: "targets",
})
require.NoError(t, err)
r := daemon.SearchResult{}
apiErr, err := resp.Decode(&r)
assert.NoError(t, err)
assert.Nil(t, apiErr)
assert.NotNil(t, r)
assert.Len(t, r.Sessions, 0)
assert.Len(t, r.Targets, 1)
})
t.Run("session response from list", func(t *testing.T) {
resp, err := search(ctx, srv.BaseDotDir(), filterBy{
authTokenId: at.Id,
resource: "sessions",
})
require.NoError(t, err)
r := daemon.SearchResult{}
apiErr, err := resp.Decode(&r)
assert.NoError(t, err)
assert.Nil(t, apiErr)
assert.NotNil(t, r)
assert.Len(t, r.Targets, 0)
assert.Len(t, r.Sessions, 2)
})
t.Run("full session response from query", func(t *testing.T) {
resp, err := search(ctx, srv.BaseDotDir(), filterBy{
authTokenId: at.Id,
flagQuery: "id % sess",
resource: "sessions",
})
require.NoError(t, err)
r := daemon.SearchResult{}
apiErr, err := resp.Decode(&r)
assert.NoError(t, err)
assert.Nil(t, apiErr)
assert.NotNil(t, r)
assert.Len(t, r.Sessions, 2)
})
t.Run("partial session response from query", func(t *testing.T) {
resp, err := search(ctx, srv.BaseDotDir(), filterBy{
authTokenId: at.Id,
flagQuery: "id % sess_1234567890",
resource: "sessions",
})
require.NoError(t, err)
r := daemon.SearchResult{}
apiErr, err := resp.Decode(&r)
assert.NoError(t, err)
assert.Nil(t, apiErr)
assert.NotNil(t, r)
assert.Len(t, r.Sessions, 1)
})
}