Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
probakowski committed Oct 17, 2024
1 parent 50627bd commit 8a75575
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tool/tctl/common/edit_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ func TestEditResources(t *testing.T) {
kind: types.KindAutoUpdateVersion,
edit: testEditAutoUpdateVersion,
},
{
kind: types.KindDynamicWindowsDesktop,
edit: testEditDynamicWindowsDesktop,
},
}

for _, test := range tests {
Expand Down Expand Up @@ -619,3 +623,35 @@ func testEditAutoUpdateVersion(t *testing.T, clt *authclient.Client) {
"tools_autoupdate should have been modified by edit")
assert.Equal(t, expected.GetSpec().GetToolsVersion(), actual.GetSpec().GetToolsVersion())
}

func testEditDynamicWindowsDesktop(t *testing.T, clt *authclient.Client) {
ctx := context.Background()

expected, err := types.NewDynamicWindowsDesktopV1("test", nil, types.DynamicWindowsDesktopSpecV1{
Addr: "test",
})
require.NoError(t, err)
created, err := clt.DynamicDesktopClient().CreateDynamicWindowsDesktop(ctx, expected)
require.NoError(t, err)

editor := func(name string) error {
f, err := os.Create(name)
if err != nil {
return trace.Wrap(err, "opening file to edit")
}

expected.SetRevision(created.GetRevision())
expected.Spec.Addr = "test2"

collection := &dynamicWindowsDesktopCollection{desktops: []types.DynamicWindowsDesktop{expected}}
return trace.NewAggregate(writeYAML(collection, f), f.Close())
}

_, err = runEditCommand(t, clt, []string{"edit", "dynamic_windows_desktop/test"}, withEditor(editor))
require.NoError(t, err)

actual, err := clt.DynamicDesktopClient().GetDynamicWindowsDesktop(ctx, expected.GetName())
require.NoError(t, err)
expected.SetRevision(actual.GetRevision())
require.Empty(t, cmp.Diff(expected, actual, protocmp.Transform()))
}
33 changes: 33 additions & 0 deletions tool/tctl/common/resource_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,10 @@ func TestCreateResources(t *testing.T) {
kind: types.KindAutoUpdateVersion,
create: testCreateAutoUpdateVersion,
},
{
kind: types.KindDynamicWindowsDesktop,
create: testCreateDynamicWindowsDesktop,
},
}

for _, test := range tests {
Expand Down Expand Up @@ -2357,6 +2361,35 @@ version: v1
))
}

func testCreateDynamicWindowsDesktop(t *testing.T, clt *authclient.Client) {
const resourceYAML = `kind: dynamic_windows_desktop
metadata:
name: test
revision: 3a43b44a-201e-4d7f-aef1-ae2f6d9811ed
spec:
addr: test
version: v1
`

// Create the resource.
resourceYAMLPath := filepath.Join(t.TempDir(), "resource.yaml")
require.NoError(t, os.WriteFile(resourceYAMLPath, []byte(resourceYAML), 0644))
_, err := runResourceCommand(t, clt, []string{"create", resourceYAMLPath})
require.NoError(t, err)

// Get the resource
buf, err := runResourceCommand(t, clt, []string{"get", types.KindDynamicWindowsDesktop, "--format=json"})
require.NoError(t, err)
resources := mustDecodeJSON[[]types.DynamicWindowsDesktopV1](t, buf)
require.Len(t, resources, 1)

var expected types.DynamicWindowsDesktopV1
require.NoError(t, yaml.Unmarshal([]byte(resourceYAML), &expected))
expected.SetRevision(resources[0].GetRevision())

require.Empty(t, cmp.Diff([]types.DynamicWindowsDesktopV1{expected}, resources, protocmp.Transform()))
}

func TestPluginResourceWrapper(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 8a75575

Please sign in to comment.