mirror of
https://github.com/fleetdm/fleet
synced 2026-05-24 09:28:54 +00:00
For #27927 Refactoring to speed up fleetctl tests, no functional changes. Mostly changing test files. fleetctl is no longer the long pole in CI, the long pole is mysql, followed by vuln. <img width="389" alt="image" src="https://github.com/user-attachments/assets/9ada64e2-b5e8-42e3-b120-4eb36183ae38" />
91 lines
2.3 KiB
Go
91 lines
2.3 KiB
Go
package fleetctl
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/fleetdm/fleet/v4/cmd/fleetctl/fleetctl/testing_utils"
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
"github.com/fleetdm/fleet/v4/server/service"
|
|
"github.com/fleetdm/fleet/v4/server/service/schedule"
|
|
kitlog "github.com/go-kit/log"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestTrigger(t *testing.T) {
|
|
const (
|
|
name = "test_sched"
|
|
instanceID = "test_instance"
|
|
interval = 5 * time.Minute
|
|
)
|
|
|
|
testCases := []struct {
|
|
args []string
|
|
delay time.Duration
|
|
expected string
|
|
}{
|
|
{
|
|
args: []string{"trigger"},
|
|
expected: fmt.Sprintf("[!] Name must be specified; supported trigger name is %s", name),
|
|
},
|
|
{
|
|
args: []string{"trigger", "--name", name},
|
|
expected: fmt.Sprintf("[+] Sent request to trigger %s schedule", name),
|
|
},
|
|
{
|
|
args: []string{"trigger", "--name", name},
|
|
delay: 10 * time.Millisecond,
|
|
expected: fmt.Sprintf("[!] Conflicts with current status of %s schedule: triggered run started", name),
|
|
},
|
|
{
|
|
args: []string{"trigger", "--name", "foo"},
|
|
expected: fmt.Sprintf("[!] Invalid name; supported trigger name is %s", name),
|
|
},
|
|
}
|
|
|
|
r, w, _ := os.Pipe()
|
|
oldStdout := os.Stdout
|
|
os.Stdout = w
|
|
|
|
_, _ = testing_utils.RunServerWithMockedDS(t, &service.TestServerOpts{
|
|
Logger: kitlog.NewNopLogger(),
|
|
StartCronSchedules: []service.TestNewScheduleFunc{
|
|
func(ctx context.Context, ds fleet.Datastore) fleet.NewCronScheduleFunc {
|
|
return func() (fleet.CronSchedule, error) {
|
|
s := schedule.New(ctx, name, instanceID, interval,
|
|
schedule.SetupMockLocker(name, instanceID, time.Now().Add(-1*time.Hour)),
|
|
schedule.SetUpMockStatsStore(name),
|
|
schedule.WithJob("test_job",
|
|
func(context.Context) error {
|
|
time.Sleep(100 * time.Millisecond)
|
|
return nil
|
|
}))
|
|
return s, nil
|
|
}
|
|
},
|
|
},
|
|
})
|
|
|
|
for _, c := range testCases {
|
|
if c.delay != 0 {
|
|
time.Sleep(c.delay)
|
|
}
|
|
assert.Equal(t, "", RunAppForTest(t, c.args))
|
|
}
|
|
|
|
os.Stdout = oldStdout
|
|
w.Close()
|
|
out, _ := io.ReadAll(r)
|
|
outlines := strings.Split(string(out), "\n")
|
|
require.Len(t, outlines, len(testCases)+1)
|
|
|
|
for i, c := range testCases {
|
|
require.True(t, strings.HasPrefix(outlines[i], c.expected))
|
|
}
|
|
}
|