This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
generated from ContainerSSH/library-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
105 lines (91 loc) · 2.49 KB
/
integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package backend_test
import (
"context"
"net"
"sync"
"testing"
"github.com/containerssh/configuration/v2"
"github.com/containerssh/geoip"
"github.com/containerssh/log"
"github.com/containerssh/metrics"
"github.com/containerssh/service"
"github.com/containerssh/sshserver"
"github.com/containerssh/structutils"
"github.com/stretchr/testify/assert"
"golang.org/x/crypto/ssh"
"github.com/containerssh/backend"
)
func TestSimpleContainerLaunch(t *testing.T) {
t.Parallel()
lock := &sync.Mutex{}
for _, backendName := range []string{"docker", "dockerrun"} {
t.Run("backend="+backendName, func(t *testing.T) {
lock.Lock()
defer lock.Unlock()
config := configuration.AppConfig{}
structutils.Defaults(&config)
config.Backend = backendName
config.Auth.URL = "http://localhost:8080"
err := config.SSH.GenerateHostKey()
assert.NoError(t, err)
backendLogger := log.NewTestLogger(t)
geoIPLookupProvider, err := geoip.New(
geoip.Config{
Provider: geoip.DummyProvider,
},
)
assert.NoError(t, err)
metricsCollector := metrics.New(
geoIPLookupProvider,
)
b, err := backend.New(
config,
backendLogger,
metricsCollector,
sshserver.AuthResponseSuccess,
)
assert.NoError(t, err)
sshServerLogger := log.NewTestLogger(t)
sshServer, err := sshserver.New(config.SSH, b, sshServerLogger)
assert.NoError(t, err)
lifecycle := service.NewLifecycle(sshServer)
running := make(chan struct{})
lifecycle.OnRunning(
func(s service.Service, l service.Lifecycle) {
running <- struct{}{}
})
go func() {
_ = lifecycle.Run()
}()
<-running
processClientInteraction(t, config)
lifecycle.Stop(context.Background())
err = lifecycle.Wait()
assert.NoError(t, err)
})
}
}
func processClientInteraction(t *testing.T, config configuration.AppConfig) {
clientConfig := &ssh.ClientConfig{
User: "foo",
Auth: []ssh.AuthMethod{ssh.Password("bar")},
}
clientConfig.HostKeyCallback = func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
}
sshConnection, err := ssh.Dial("tcp", config.SSH.Listen, clientConfig)
if !assert.NoError(t, err) {
return
}
defer func() {
if sshConnection != nil {
_ = sshConnection.Close()
}
}()
session, err := sshConnection.NewSession()
assert.NoError(t, err)
output, err := session.CombinedOutput("echo 'Hello world!'")
assert.NoError(t, err)
assert.NoError(t, sshConnection.Close())
assert.EqualValues(t, []byte("Hello world!\n"), output)
}