mirror of
https://github.com/fleetdm/fleet
synced 2026-05-23 08:58:41 +00:00
156 lines
3.1 KiB
Go
156 lines
3.1 KiB
Go
package kdialog
|
|
|
|
import (
|
|
"os/exec"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/fleetdm/fleet/v4/orbit/pkg/dialog"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type mockExecCmd struct {
|
|
output []byte
|
|
exitCode int
|
|
capturedArgs []string
|
|
err error
|
|
}
|
|
|
|
func (m *mockExecCmd) runWithOutput(timeout time.Duration, args ...string) ([]byte, int, error) {
|
|
m.capturedArgs = append(m.capturedArgs, args...)
|
|
|
|
if m.exitCode != 0 {
|
|
return nil, m.exitCode, &exec.ExitError{}
|
|
}
|
|
|
|
if m.err != nil {
|
|
return nil, m.exitCode, m.err
|
|
}
|
|
|
|
return m.output, m.exitCode, nil
|
|
}
|
|
|
|
func TestShowEntryArgs(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
opts dialog.EntryOptions
|
|
expectedArgs []string
|
|
}{
|
|
{
|
|
name: "Basic Entry",
|
|
opts: dialog.EntryOptions{
|
|
Title: "A Title",
|
|
Text: "Some text",
|
|
},
|
|
expectedArgs: []string{"--password", "Some text", "--title", "A Title"},
|
|
},
|
|
}
|
|
|
|
for _, tt := range testCases {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
mock := &mockExecCmd{
|
|
output: []byte("some output"),
|
|
}
|
|
k := &KDialog{
|
|
cmdWithOutput: mock.runWithOutput,
|
|
}
|
|
output, err := k.ShowEntry(tt.opts)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, tt.expectedArgs, mock.capturedArgs)
|
|
assert.Equal(t, []byte("some output"), output)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestShowEntryError(t *testing.T) {
|
|
mock := &mockExecCmd{
|
|
exitCode: 1,
|
|
}
|
|
k := &KDialog{
|
|
cmdWithOutput: mock.runWithOutput,
|
|
}
|
|
_, err := k.ShowEntry(dialog.EntryOptions{})
|
|
assert.Error(t, err)
|
|
assert.ErrorIs(t, err, dialog.ErrCanceled)
|
|
|
|
mock = &mockExecCmd{
|
|
exitCode: 124,
|
|
}
|
|
k = &KDialog{
|
|
cmdWithOutput: mock.runWithOutput,
|
|
}
|
|
_, err = k.ShowEntry(dialog.EntryOptions{})
|
|
assert.Error(t, err)
|
|
assert.ErrorIs(t, err, dialog.ErrTimeout)
|
|
|
|
mock = &mockExecCmd{
|
|
exitCode: 2,
|
|
}
|
|
k = &KDialog{
|
|
cmdWithOutput: mock.runWithOutput,
|
|
}
|
|
_, err = k.ShowEntry(dialog.EntryOptions{})
|
|
assert.Error(t, err)
|
|
assert.ErrorIs(t, err, dialog.ErrUnknown)
|
|
}
|
|
|
|
func TestShowInfoArgs(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
opts dialog.InfoOptions
|
|
expectedArgs []string
|
|
}{
|
|
{
|
|
name: "Basic Info",
|
|
opts: dialog.InfoOptions{
|
|
Title: "A Title",
|
|
Text: "Some text",
|
|
},
|
|
expectedArgs: []string{"--msgbox", "Some text", "--title", "A Title"},
|
|
},
|
|
}
|
|
|
|
for _, tt := range testCases {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
mock := &mockExecCmd{}
|
|
k := &KDialog{
|
|
cmdWithOutput: mock.runWithOutput,
|
|
}
|
|
err := k.ShowInfo(tt.opts)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, tt.expectedArgs, mock.capturedArgs)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestShowInfoError(t *testing.T) {
|
|
testcases := []struct {
|
|
name string
|
|
exitCode int
|
|
expectedErr error
|
|
}{
|
|
{
|
|
name: "Dialog Timed Out",
|
|
exitCode: 124,
|
|
expectedErr: dialog.ErrTimeout,
|
|
},
|
|
{
|
|
name: "Unknown Error",
|
|
exitCode: 99,
|
|
expectedErr: dialog.ErrUnknown,
|
|
},
|
|
}
|
|
|
|
for _, tt := range testcases {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
mock := &mockExecCmd{
|
|
exitCode: tt.exitCode,
|
|
}
|
|
k := &KDialog{
|
|
cmdWithOutput: mock.runWithOutput,
|
|
}
|
|
err := k.ShowInfo(dialog.InfoOptions{})
|
|
assert.ErrorIs(t, err, tt.expectedErr)
|
|
})
|
|
}
|
|
}
|