Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zmb3 committed Feb 21, 2025
1 parent d560f1c commit d70bd7f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/services/local/desktops.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewWindowsDesktopService(backend backend.Backend) *WindowsDesktopService {
}

func (s *WindowsDesktopService) getWindowsDesktop(ctx context.Context, name, hostID string) (types.WindowsDesktop, error) {
key := backend.ExactKey(windowsDesktopsPrefix, hostID, name)
key := backend.NewKey(windowsDesktopsPrefix, hostID, name)
item, err := s.Get(ctx, key)
if err != nil {
return nil, trace.Wrap(err)
Expand Down
52 changes: 52 additions & 0 deletions lib/services/local/desktops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,60 @@ import (
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/backend"
"github.com/gravitational/teleport/lib/backend/memory"
"github.com/gravitational/trace"
)

func TestGetWindowsDesktops(t *testing.T) {
t.Parallel()

ctx := context.Background()
clock := clockwork.NewFakeClock()

bk, err := memory.New(memory.Config{
Context: ctx,
Clock: clock,
})
require.NoError(t, err)

service := NewWindowsDesktopService(bk)

d1, err := types.NewWindowsDesktopV3("apple", nil, types.WindowsDesktopSpecV3{Addr: "_", HostID: "test-host-id-1"})
require.NoError(t, err)
require.NoError(t, service.CreateWindowsDesktop(ctx, d1))

d2, err := types.NewWindowsDesktopV3("apple", nil, types.WindowsDesktopSpecV3{Addr: "_", HostID: "test-host-id-2"})
require.NoError(t, err)
require.NoError(t, service.CreateWindowsDesktop(ctx, d2))

d3, err := types.NewWindowsDesktopV3("carrot", nil, types.WindowsDesktopSpecV3{Addr: "_", HostID: "test-host-id-2"})
require.NoError(t, err)
require.NoError(t, service.CreateWindowsDesktop(ctx, d3))

// search by name and host ID
result, err := service.GetWindowsDesktops(ctx, types.WindowsDesktopFilter{Name: "apple", HostID: "test-host-id-2"})
require.NoError(t, err)
require.Len(t, result, 1)
require.Equal(t, d1.GetName(), result[0].GetName())

// search by host ID
result, err = service.GetWindowsDesktops(ctx, types.WindowsDesktopFilter{HostID: "test-host-id-2"})
require.NoError(t, err)
require.Len(t, result, 2)
require.Equal(t, d2.GetName(), result[0].GetName())
require.Equal(t, d3.GetName(), result[1].GetName())

// no filter
result, err = service.GetWindowsDesktops(ctx, types.WindowsDesktopFilter{})
require.NoError(t, err)
require.Len(t, result, 3)

// non-matching filter
result, err = service.GetWindowsDesktops(ctx, types.WindowsDesktopFilter{Name: "foo", HostID: "bar"})
require.Error(t, err)
require.True(t, trace.IsNotFound(err))
require.Empty(t, result)
}

func TestListWindowsDesktops(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit d70bd7f

Please sign in to comment.