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 1
/
Copy pathhttp_test.go
91 lines (79 loc) · 2 KB
/
http_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
package configuration_test
import (
"context"
"net"
"testing"
"time"
"github.com/containerssh/geoip"
"github.com/containerssh/http"
"github.com/containerssh/log"
"github.com/containerssh/metrics"
"github.com/containerssh/service"
"github.com/docker/docker/api/types/container"
"github.com/stretchr/testify/assert"
"github.com/containerssh/configuration/v2"
)
func TestHTTP(t *testing.T) {
logger := log.NewTestLogger(t)
srv, err := configuration.NewServer(
http.ServerConfiguration{
Listen: "127.0.0.1:8080",
},
&myConfigReqHandler{},
logger,
)
assert.NoError(t, err)
lifecycle := service.NewLifecycle(srv)
ready := make(chan struct{})
lifecycle.OnRunning(
func(s service.Service, l service.Lifecycle) {
ready <- struct{}{}
})
go func() {
_ = lifecycle.Run()
}()
<-ready
client, err := configuration.NewClient(
configuration.ClientConfig{
ClientConfiguration: http.ClientConfiguration{
URL: "http://127.0.0.1:8080",
Timeout: 2 * time.Second,
},
}, logger, getMetricsCollector(t),
)
assert.NoError(t, err)
connectionID := "0123456789ABCDEF"
config, err := client.Get(
context.Background(),
"foo",
net.TCPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 2222,
},
connectionID,
)
assert.NoError(t, err)
assert.Equal(t, "yourcompany/yourimage", config.Docker.Execution.Launch.ContainerConfig.Image)
lifecycle.Stop(context.Background())
err = lifecycle.Wait()
assert.NoError(t, err)
}
func getMetricsCollector(t *testing.T) metrics.Collector {
geoIP, err := geoip.New(geoip.Config{
Provider: "dummy",
})
assert.NoError(t, err)
return metrics.New(geoIP)
}
type myConfigReqHandler struct {
}
func (m *myConfigReqHandler) OnConfig(
request configuration.ConfigRequest,
) (config configuration.AppConfig, err error) {
config.Backend = "docker"
config.Docker.Execution.Launch.ContainerConfig = &container.Config{}
if request.Username == "foo" {
config.Docker.Execution.Launch.ContainerConfig.Image = "yourcompany/yourimage"
}
return config, err
}