summaryrefslogtreecommitdiff
path: root/internal/sshd/sshd_test.go
diff options
context:
space:
mode:
authorIgor Drozdov <idrozdov@gitlab.com>2021-07-09 14:41:41 +0300
committerIgor Drozdov <idrozdov@gitlab.com>2021-07-15 14:39:33 +0300
commit569a0197cacc75270776217c27e9d709907a9dfa (patch)
treeb693a2244f3d715d48df83eac70bdf6630e51d3a /internal/sshd/sshd_test.go
parentd3711d8d7e781dbff01d8ae5c7a1d5b800c5c8a2 (diff)
downloadgitlab-shell-569a0197cacc75270776217c27e9d709907a9dfa.tar.gz
Shutdown sshd gracefully
When interruption signal is sent, we are closing ssh listener to prevent it from accepting new connections Then after configured grace period, we cancel the context to cancel all ongoing operations
Diffstat (limited to 'internal/sshd/sshd_test.go')
-rw-r--r--internal/sshd/sshd_test.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/internal/sshd/sshd_test.go b/internal/sshd/sshd_test.go
new file mode 100644
index 0000000..d1891ec
--- /dev/null
+++ b/internal/sshd/sshd_test.go
@@ -0,0 +1,49 @@
+package sshd
+
+import (
+ "testing"
+ "context"
+ "path"
+
+ "github.com/stretchr/testify/require"
+
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
+ "gitlab.com/gitlab-org/gitlab-shell/internal/config"
+ "gitlab.com/gitlab-org/gitlab-shell/internal/testhelper"
+)
+
+const serverUrl = "127.0.0.1:50000"
+
+func TestShutdown(t *testing.T) {
+ s := setupServer(t)
+
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
+ done := make(chan bool, 1)
+ go func() {
+ require.NoError(t, s.serve(ctx))
+ done <- true
+ }()
+
+ require.NoError(t, s.Shutdown())
+
+ require.True(t, <-done, "the accepting loop must be interrupted")
+}
+
+func setupServer(t *testing.T) *Server {
+ testhelper.PrepareTestRootDir(t)
+
+ url := testserver.StartSocketHttpServer(t, []testserver.TestRequestHandler{})
+ srvCfg := config.ServerConfig{
+ Listen: serverUrl,
+ HostKeyFiles: []string{path.Join(testhelper.TestRoot, "certs/valid/server.key")},
+ }
+
+ cfg := &config.Config{RootDir: "/tmp", GitlabUrl: url, Server: srvCfg}
+
+ s := &Server{Config: cfg}
+ require.NoError(t, s.listen())
+
+ return s
+}