Files
teleport/lib/utils/signal/stack_handler_test.go
T
Vadym Popov 9d2b5c5fe9 Client auto updates integration for {tctl,tsh} (#47815)
* Client auto updates integration for tctl/tsh

* Add version validation
Fix recursive version check for darwin platform
Fix cleanup for multi-package support

* Fix identifying tools removal from home directory

* Replace ToolsMode with ToolsAutoUpdate

* Reuse insecure flag for tests

* Fix CheckRemote with login

* Fix windows administrative access requirement
Update must be able to be canceled, re-execute with latest version or last updated
Show progress bar before request is made

* Fix update cancellation for login action
Address review comments

* Add signal handler with stack context cancellation

* Use copy instead of hard link for windows
Fix progress bar if we can't receive size of package

* Replace with list in order to support manual cancel

* Download archive package to temp directory

* Decrease timeout for client tools proxy call
2024-10-31 17:09:24 +00:00

89 lines
2.6 KiB
Go

/*
* Teleport
* Copyright (C) 2024 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package signal
import (
"context"
"os"
"syscall"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestGetSignalHandler verifies the cancellation stack order.
func TestGetSignalHandler(t *testing.T) {
testHandler := GetSignalHandler()
parent := context.Background()
ctx1, cancel1 := testHandler.NotifyContext(parent)
ctx2, cancel2 := testHandler.NotifyContext(parent)
ctx3, cancel3 := testHandler.NotifyContext(parent)
ctx4, cancel4 := testHandler.NotifyContext(parent)
t.Cleanup(func() {
cancel4()
cancel2()
cancel1()
})
// Verify that all context not canceled.
require.NoError(t, ctx4.Err())
require.NoError(t, ctx3.Err())
require.NoError(t, ctx2.Err())
require.NoError(t, ctx1.Err())
// Cancel context manually to ensure it was removed from stack in right order.
cancel3()
// Check that last added context is canceled.
require.NoError(t, syscall.Kill(os.Getpid(), syscall.SIGINT))
select {
case <-ctx4.Done():
assert.ErrorIs(t, ctx3.Err(), context.Canceled)
assert.NoError(t, ctx2.Err())
assert.NoError(t, ctx1.Err())
case <-time.After(time.Second):
assert.Fail(t, "context 3 must be canceled")
}
// Send interrupt signal to cancel next context in the stack.
require.NoError(t, syscall.Kill(os.Getpid(), syscall.SIGINT))
select {
case <-ctx2.Done():
assert.ErrorIs(t, ctx4.Err(), context.Canceled)
assert.ErrorIs(t, ctx3.Err(), context.Canceled)
assert.NoError(t, ctx1.Err())
case <-time.After(time.Second):
assert.Fail(t, "context 2 must be canceled")
}
// All context must be canceled.
require.NoError(t, syscall.Kill(os.Getpid(), syscall.SIGINT))
select {
case <-ctx1.Done():
assert.ErrorIs(t, ctx4.Err(), context.Canceled)
assert.ErrorIs(t, ctx3.Err(), context.Canceled)
assert.ErrorIs(t, ctx2.Err(), context.Canceled)
case <-time.After(time.Second):
assert.Fail(t, "context 1 must be canceled")
}
}