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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Entries up to v2.1.1 were backfilled from the GitHub release notes.

## [Unreleased]

### Added
- `tomato ui` plays a run as a film. Starting a run from the UI folds the tree, the inspector and the console away and puts the run on the whole window, in four acts: the dependencies on the topology, each pulsing until its `wait_for` check answers and then holding green with the time it took; the app coming up the same way; then every scenario, with its traffic travelling over the topology step by step and a numbered box ticking the steps off beside the narration; and at the end a report — the verdict, scenarios and steps, where the time went, each feature's scenarios with the failed step and its error, the slowest scenarios, and what the app cost (its CPU time, peak memory and growth, and the time it spent waiting on each dependency, from the profile). The film follows the run a beat behind so every moment is seen, catches up when it falls far behind, and can be paused, stepped (`←`/`→`, `space`) and replayed; `m` brings it back, `esc` or Close leaves it. A past run can be watched again from the Runs tab (`GET /api/runs/{run}/events` rebuilds its feed from `tomato.log`), and `?watch=<run>&beat=<n>` opens one at a given moment. Behind it the `tomato` event format now says how the run comes up: `phase` (`containers`, `resources`, `app`, `tests`), `container` (`starting`, `ready`, `failed`, with `name` and `durationMs`) and `app` (`starting`, `listening`, `ready`, `failed`, with `url` and `durationMs`), and the console prints each container's ✓ as it comes up, with how long it took.

### Changed
- Building tomato needs Go 1.27, which the embedded Toxiproxy v2.13.0 requires. Go 1.27's `compress/zlib` writes different bytes, so the committed PDF fixtures were regenerated.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Tomato is a language-agnostic behavioral testing framework that manages your tes
- **Presets** - `preset: kafka` instead of a page of listener config, with MSK-style `AWS_MSK_IAM` authentication on the stock apache/kafka image
- **AWS identity** - an `aws` resource gives the app IRSA against an STS tomato serves
- **Avro + Schema Registry** - Publish and assert Kafka Avro messages as plain JSON
- **Web UI** - `tomato ui` browses the suite, draws each scenario's flow and the topology of what it touches, and runs scenarios from the browser
- **Web UI** - `tomato ui` browses the suite, draws each scenario's flow and the topology of what it touches, runs scenarios from the browser and plays a run as a film: the dependencies and the app coming up, every scenario over the topology, then the report
- **CI Reports** - JUnit XML and Cucumber JSON alongside console output
- **Step Coverage** - `tomato coverage` reports which resource steps your features use, with `--min` to gate CI
- **Auto-generated Documentation** - Keep step docs in sync with code
Expand Down
110 changes: 110 additions & 0 deletions command/interrupt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package command

import (
"fmt"
"io"
"os"
"os/signal"
"sync"
"syscall"
)

// interruptExitCode is what a shell reports for a process ended by Ctrl+C.
const interruptExitCode = 130

// interrupts stops what a run started when the run is interrupted. Without it
// Ctrl+C, or the UI's Stop, ended tomato before its defers ran: the containers
// were reaped by Docker, but the app runs in its own process group, so nothing
// reached it, and it held the app port and failed the next run.
//
// Scenarios do not stop on a cancelled context, so the run cannot simply
// return: the cleanup registered with onInterrupt runs here, last registered
// first like defers, and tomato exits. A second signal exits at once.
type interrupts struct {
mu sync.Mutex
cleanup []func()
// waiting is set while the run waits for Ctrl+C itself (keep-alive), which
// then shuts down on its own path.
waiting chan struct{}
signals chan os.Signal
done chan struct{}
log io.Writer
exit func(code int)
}

// handleInterrupts catches SIGINT and SIGTERM until stop is called. What it
// says on an interrupt goes to stderr and to log.
func handleInterrupts(log io.Writer) *interrupts {
in := &interrupts{
signals: make(chan os.Signal, 1),
done: make(chan struct{}),
log: log,
exit: os.Exit,
}
signal.Notify(in.signals, os.Interrupt, syscall.SIGTERM)
go in.loop()
return in
}

func (in *interrupts) loop() {
var sig os.Signal
select {
case sig = <-in.signals:
case <-in.done:
return
}

in.mu.Lock()
waiting := in.waiting
cleanup := in.cleanup
in.cleanup = nil
in.mu.Unlock()

if waiting == nil {
msg := fmt.Sprintf("\n%s: stopping the app and containers (again to quit now)...\n", sig)
fmt.Fprint(os.Stderr, msg)
if in.log != nil {
fmt.Fprint(in.log, msg)
}
}

// A second signal while shutting down quits without waiting for it.
go func() {
select {
case <-in.signals:
in.exit(interruptExitCode)
case <-in.done:
}
}()

if waiting != nil {
close(waiting)
return
}
for i := len(cleanup) - 1; i >= 0; i-- {
cleanup[i]()
}
in.exit(interruptExitCode)
}

// onInterrupt registers fn to run if the run is interrupted.
func (in *interrupts) onInterrupt(fn func()) {
in.mu.Lock()
defer in.mu.Unlock()
in.cleanup = append(in.cleanup, fn)
}

// wait blocks until the run is interrupted, leaving the shutdown to the caller.
func (in *interrupts) wait() {
in.mu.Lock()
in.waiting = make(chan struct{})
waiting := in.waiting
in.mu.Unlock()
<-waiting
}

// stop stops catching signals.
func (in *interrupts) stop() {
signal.Stop(in.signals)
close(in.done)
}
100 changes: 100 additions & 0 deletions command/interrupt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package command

import (
"bytes"
"os"
"reflect"
"sync"
"syscall"
"testing"
"time"
)

// exitRecorder stands in for os.Exit.
type exitRecorder struct {
once sync.Once
code chan int
}

func newExitRecorder() *exitRecorder { return &exitRecorder{code: make(chan int, 1)} }

func (e *exitRecorder) exit(code int) { e.once.Do(func() { e.code <- code }) }

func (e *exitRecorder) wait(t *testing.T) int {
t.Helper()
select {
case code := <-e.code:
return code
case <-time.After(10 * time.Second):
t.Fatal("tomato did not exit after the interrupt")
return 0
}
}

func TestInterrupts_CleansUpInReverseAndExits(t *testing.T) {
var log bytes.Buffer
in := handleInterrupts(&log)
defer in.stop()
exit := newExitRecorder()
in.exit = exit.exit

var mu sync.Mutex
var order []string
for _, name := range []string{"containers", "resources", "app"} {
in.onInterrupt(func() { mu.Lock(); order = append(order, name); mu.Unlock() })
}

if err := syscall.Kill(os.Getpid(), syscall.SIGTERM); err != nil {
t.Fatal(err)
}
if code := exit.wait(t); code != interruptExitCode {
t.Errorf("exit code %d, want %d", code, interruptExitCode)
}
mu.Lock()
defer mu.Unlock()
if want := []string{"app", "resources", "containers"}; !reflect.DeepEqual(order, want) {
t.Errorf("cleaned up %v, want %v", order, want)
}
if !bytes.Contains(log.Bytes(), []byte("stopping the app")) {
t.Errorf("the run log should say why the run ended, got %q", log.String())
}
}

// Keep-alive waits for Ctrl+C itself and shuts down on its own path.
func TestInterrupts_WaitReturnsWithoutExiting(t *testing.T) {
in := handleInterrupts(nil)
defer in.stop()
exit := newExitRecorder()
in.exit = exit.exit
cleaned := false
in.onInterrupt(func() { cleaned = true })

returned := make(chan struct{})
go func() { in.wait(); close(returned) }()
// Let wait register before the signal arrives.
for {
in.mu.Lock()
ready := in.waiting != nil
in.mu.Unlock()
if ready {
break
}
time.Sleep(10 * time.Millisecond)
}
if err := syscall.Kill(os.Getpid(), syscall.SIGINT); err != nil {
t.Fatal(err)
}
select {
case <-returned:
case <-time.After(10 * time.Second):
t.Fatal("wait did not return on the interrupt")
}
select {
case code := <-exit.code:
t.Errorf("exited with %d; keep-alive shuts down itself", code)
default:
}
if cleaned {
t.Error("the registered cleanup ran; keep-alive runs its own")
}
}
Loading
Loading