Skip to content

Commit d70bd7f

Browse files
committed
Add more tests
1 parent d560f1c commit d70bd7f

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

lib/services/local/desktops.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func NewWindowsDesktopService(backend backend.Backend) *WindowsDesktopService {
3939
}
4040

4141
func (s *WindowsDesktopService) getWindowsDesktop(ctx context.Context, name, hostID string) (types.WindowsDesktop, error) {
42-
key := backend.ExactKey(windowsDesktopsPrefix, hostID, name)
42+
key := backend.NewKey(windowsDesktopsPrefix, hostID, name)
4343
item, err := s.Get(ctx, key)
4444
if err != nil {
4545
return nil, trace.Wrap(err)

lib/services/local/desktops_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,60 @@ import (
3030
"github.com/gravitational/teleport/api/types"
3131
"github.com/gravitational/teleport/lib/backend"
3232
"github.com/gravitational/teleport/lib/backend/memory"
33+
"github.com/gravitational/trace"
3334
)
3435

36+
func TestGetWindowsDesktops(t *testing.T) {
37+
t.Parallel()
38+
39+
ctx := context.Background()
40+
clock := clockwork.NewFakeClock()
41+
42+
bk, err := memory.New(memory.Config{
43+
Context: ctx,
44+
Clock: clock,
45+
})
46+
require.NoError(t, err)
47+
48+
service := NewWindowsDesktopService(bk)
49+
50+
d1, err := types.NewWindowsDesktopV3("apple", nil, types.WindowsDesktopSpecV3{Addr: "_", HostID: "test-host-id-1"})
51+
require.NoError(t, err)
52+
require.NoError(t, service.CreateWindowsDesktop(ctx, d1))
53+
54+
d2, err := types.NewWindowsDesktopV3("apple", nil, types.WindowsDesktopSpecV3{Addr: "_", HostID: "test-host-id-2"})
55+
require.NoError(t, err)
56+
require.NoError(t, service.CreateWindowsDesktop(ctx, d2))
57+
58+
d3, err := types.NewWindowsDesktopV3("carrot", nil, types.WindowsDesktopSpecV3{Addr: "_", HostID: "test-host-id-2"})
59+
require.NoError(t, err)
60+
require.NoError(t, service.CreateWindowsDesktop(ctx, d3))
61+
62+
// search by name and host ID
63+
result, err := service.GetWindowsDesktops(ctx, types.WindowsDesktopFilter{Name: "apple", HostID: "test-host-id-2"})
64+
require.NoError(t, err)
65+
require.Len(t, result, 1)
66+
require.Equal(t, d1.GetName(), result[0].GetName())
67+
68+
// search by host ID
69+
result, err = service.GetWindowsDesktops(ctx, types.WindowsDesktopFilter{HostID: "test-host-id-2"})
70+
require.NoError(t, err)
71+
require.Len(t, result, 2)
72+
require.Equal(t, d2.GetName(), result[0].GetName())
73+
require.Equal(t, d3.GetName(), result[1].GetName())
74+
75+
// no filter
76+
result, err = service.GetWindowsDesktops(ctx, types.WindowsDesktopFilter{})
77+
require.NoError(t, err)
78+
require.Len(t, result, 3)
79+
80+
// non-matching filter
81+
result, err = service.GetWindowsDesktops(ctx, types.WindowsDesktopFilter{Name: "foo", HostID: "bar"})
82+
require.Error(t, err)
83+
require.True(t, trace.IsNotFound(err))
84+
require.Empty(t, result)
85+
}
86+
3587
func TestListWindowsDesktops(t *testing.T) {
3688
t.Parallel()
3789

0 commit comments

Comments
 (0)