2021-01-13 12:45:35 -05:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/cli/internal/test"
|
2021-08-09 13:15:46 -04:00
|
|
|
"github.com/moby/sys/signal"
|
2021-01-13 12:45:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestForwardSignals(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
called := make(chan struct{})
|
|
|
|
client := &fakeClient{containerKillFunc: func(ctx context.Context, container, signal string) error {
|
|
|
|
close(called)
|
|
|
|
return nil
|
|
|
|
}}
|
|
|
|
|
|
|
|
cli := test.NewFakeCli(client)
|
|
|
|
sigc := make(chan os.Signal)
|
|
|
|
defer close(sigc)
|
|
|
|
|
|
|
|
go ForwardAllSignals(ctx, cli, t.Name(), sigc)
|
|
|
|
|
|
|
|
timer := time.NewTimer(30 * time.Second)
|
|
|
|
defer timer.Stop()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
t.Fatal("timeout waiting to send signal")
|
|
|
|
case sigc <- signal.SignalMap["TERM"]:
|
|
|
|
}
|
|
|
|
if !timer.Stop() {
|
|
|
|
<-timer.C
|
|
|
|
}
|
|
|
|
timer.Reset(30 * time.Second)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-called:
|
|
|
|
case <-timer.C:
|
|
|
|
t.Fatal("timeout waiting for signal to be processed")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|