github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/container/signals_unix_test.go (about)

     1  // +build !windows
     2  
     3  package container
     4  
     5  import (
     6  	"context"
     7  	"os"
     8  	"syscall"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/docker/cli/internal/test"
    13  	"golang.org/x/sys/unix"
    14  	"gotest.tools/v3/assert"
    15  )
    16  
    17  func TestIgnoredSignals(t *testing.T) {
    18  	ignoredSignals := []syscall.Signal{unix.SIGPIPE, unix.SIGCHLD, unix.SIGURG}
    19  
    20  	for _, s := range ignoredSignals {
    21  		t.Run(unix.SignalName(s), func(t *testing.T) {
    22  			ctx, cancel := context.WithCancel(context.Background())
    23  			defer cancel()
    24  
    25  			var called bool
    26  			client := &fakeClient{containerKillFunc: func(ctx context.Context, container, signal string) error {
    27  				called = true
    28  				return nil
    29  			}}
    30  
    31  			cli := test.NewFakeCli(client)
    32  			sigc := make(chan os.Signal)
    33  			defer close(sigc)
    34  
    35  			done := make(chan struct{})
    36  			go func() {
    37  				ForwardAllSignals(ctx, cli, t.Name(), sigc)
    38  				close(done)
    39  			}()
    40  
    41  			timer := time.NewTimer(30 * time.Second)
    42  			defer timer.Stop()
    43  
    44  			select {
    45  			case <-timer.C:
    46  				t.Fatal("timeout waiting to send signal")
    47  			case sigc <- s:
    48  			case <-done:
    49  			}
    50  
    51  			// cancel the context so ForwardAllSignals will exit after it has processed the signal we sent.
    52  			// This is how we know the signal was actually processed and are not introducing a flakey test.
    53  			cancel()
    54  			<-done
    55  
    56  			assert.Assert(t, !called, "kill was called")
    57  		})
    58  	}
    59  }