github.com/argoproj/argo-cd/v3@v3.2.1/cmd/argocd/commands/admin/dashboard_test.go (about) 1 package admin 2 3 import ( 4 "context" 5 "os" 6 "syscall" 7 "testing" 8 "time" 9 10 "github.com/stretchr/testify/require" 11 "k8s.io/client-go/tools/clientcmd" 12 13 "github.com/argoproj/argo-cd/v3/pkg/apiclient" 14 ) 15 16 func TestRun_SignalHandling_GracefulShutdown(t *testing.T) { 17 stopCalled := false 18 d := &dashboard{ 19 startLocalServer: func(_ context.Context, opts *apiclient.ClientOptions, _ string, _ *int, _ *string, _ clientcmd.ClientConfig) (func(), error) { 20 return func() { 21 stopCalled = true 22 require.True(t, opts.Core, "Core client option should be set to true") 23 }, nil 24 }, 25 } 26 27 var runErr error 28 doneCh := make(chan struct{}) 29 go func() { 30 runErr = d.Run(t.Context(), &DashboardConfig{ClientOpts: &apiclient.ClientOptions{}}) 31 close(doneCh) 32 }() 33 34 // Allow some time for the dashboard to register the signal handler 35 time.Sleep(50 * time.Millisecond) 36 37 proc, procErr := os.FindProcess(os.Getpid()) 38 require.NoErrorf(t, procErr, "failed to find process: %v", procErr) 39 sigErr := proc.Signal(syscall.SIGINT) 40 require.NoErrorf(t, sigErr, "failed to send SIGINT: %v", sigErr) 41 42 select { 43 case <-doneCh: 44 require.NoError(t, runErr) 45 case <-time.After(500 * time.Millisecond): 46 t.Fatal("timeout: dashboard.Run did not exit after SIGINT") 47 } 48 49 require.True(t, stopCalled) 50 }