fleet/cmd/fleetctl/trigger_test.go
Zach Wasserman ca2e30e59c
Fix error writing coverage when running tests (#10278)
Intended to fix this error we are seeing in CI:

```
error generating coverage report: write |1: file already closed
```

It seems like perhaps a change in the way the test coverage is reported
in a recent Go version has interacted with the closing of stdout in
these tests.

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [x] Added/updated tests
2023-03-02 17:11:15 -08:00

91 lines
2.2 KiB
Go

package main
import (
"context"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"time"
"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/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
_, _ = 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, _ := ioutil.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))
}
}