Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/wfctl/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/GoCodeAlone/workflow/interfaces"
)

const logCaptureFollowCompletionGrace = 30 * time.Second

func runLogs(args []string) error {
return runLogsWithOutput(args, os.Stdout)
}
Expand Down Expand Up @@ -113,7 +115,7 @@ func runLogsCapture(args []string, out io.Writer) error {
ctx := context.Background()
if follow && duration > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, duration)
ctx, cancel = context.WithTimeout(ctx, duration+logCaptureFollowCompletionGrace)
defer cancel()
}
durationSeconds := int64(0)
Expand Down
60 changes: 58 additions & 2 deletions cmd/wfctl/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@ import (
"path/filepath"
"strings"
"testing"
"time"

"github.com/GoCodeAlone/workflow/config"
"github.com/GoCodeAlone/workflow/interfaces"
)

type fakeLogProvider struct {
applyCapture
req interfaces.LogCaptureRequest
req interfaces.LogCaptureRequest
ctxDeadline time.Time
ctxRemaining time.Duration
hasDeadline bool
}

func (p *fakeLogProvider) CaptureLogs(_ context.Context, req interfaces.LogCaptureRequest, sink interfaces.LogCaptureSink) error {
func (p *fakeLogProvider) CaptureLogs(ctx context.Context, req interfaces.LogCaptureRequest, sink interfaces.LogCaptureSink) error {
p.req = req
p.ctxDeadline, p.hasDeadline = ctx.Deadline()
if p.hasDeadline {
p.ctxRemaining = time.Until(p.ctxDeadline)
}
return sink.WriteLogChunk(interfaces.LogChunk{Data: []byte("line one\n"), Source: "historic"})
}

Expand Down Expand Up @@ -177,6 +185,54 @@ modules:
}
}

func TestLogsCaptureFollowContextAllowsProviderCompletionGrace(t *testing.T) {
tmp := t.TempDir()
cfg := filepath.Join(tmp, "app.yaml")
if err := os.WriteFile(cfg, []byte(`
version: "1"
modules:
- name: do
type: iac.provider
config:
provider: digitalocean
- name: web
type: infra.container_service
config:
provider: do
app_name: bmw-staging
`), 0o600); err != nil {
t.Fatal(err)
}

provider := &fakeLogProvider{}
orig := resolveIaCProvider
resolveIaCProvider = func(_ context.Context, _ string, _ map[string]any) (interfaces.IaCProvider, io.Closer, error) {
return provider, nil, nil
}
t.Cleanup(func() { resolveIaCProvider = orig })

var out bytes.Buffer
err := runLogsWithOutput([]string{
"capture",
"--config", cfg,
"--resource", "web",
"--follow",
"--duration", "5s",
}, &out)
if err != nil {
t.Fatalf("runLogsWithOutput: %v", err)
}
if provider.req.DurationSeconds != 5 {
t.Fatalf("DurationSeconds = %d, want 5", provider.req.DurationSeconds)
}
if !provider.hasDeadline {
t.Fatal("expected wfctl to retain a client-side timeout guard")
}
if got := provider.ctxRemaining; got <= 5*time.Second {
t.Fatalf("client context remaining = %v, want grace beyond requested 5s follow", got)
}
}

func TestLogsCaptureRejectsUnknownType(t *testing.T) {
tmp := t.TempDir()
cfg := filepath.Join(tmp, "app.yaml")
Expand Down
Loading