/
fixture_test.go
112 lines (100 loc) · 2.43 KB
/
fixture_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
106
107
108
109
110
111
112
package matchbox
import (
"fmt"
"io/ioutil"
"net"
"github.com/poseidon/matchbox/matchbox/rpc"
"github.com/poseidon/matchbox/matchbox/server"
"github.com/poseidon/matchbox/matchbox/storage"
"github.com/poseidon/matchbox/matchbox/tlsutil"
"google.golang.org/grpc"
)
var (
fakeClientCert = mustReadFile("testdata/client.crt")
fakeClientKey = mustReadFile("testdata/client.key")
fakeCACert = mustReadFile("testdata/ca.crt")
clientTLSInfo = &TLSContents{
Cert: fakeClientCert,
Key: fakeClientKey,
CA: fakeCACert,
}
serverTLSInfo = &tlsutil.TLSInfo{
CAFile: "testdata/ca.crt",
CertFile: "testdata/server.crt",
KeyFile: "testdata/server.key",
}
)
type FixtureServer struct {
Store storage.Store
Server *grpc.Server
Listener net.Listener
// TLS server certificates (files) which will be used
ServerTLS *tlsutil.TLSInfo
// TLS client credentials which should be used
ClientTLS *TLSContents
}
// TODO: Merge into matchbox tlsutil TLSInfo to allow raw contents.
type TLSContents struct {
Cert []byte
Key []byte
CA []byte
}
func NewFixtureServer(clientTLS *TLSContents, serverTLS *tlsutil.TLSInfo, s storage.Store) *FixtureServer {
// Address close (i.e. release) is effectively asynchronous. Test server
// instances should reserve a random address upfront.
lis, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
panic(fmt.Errorf("failed to start listening: %v", err))
}
return &FixtureServer{
Store: s,
Listener: lis,
ServerTLS: serverTLS,
ClientTLS: clientTLS,
}
}
func (s *FixtureServer) Start() error {
cfg, err := s.ServerTLS.ServerConfig()
if err != nil {
return fmt.Errorf("Invalid TLS credentials: %v", err)
}
srv := server.NewServer(&server.Config{Store: s.Store})
s.Server = rpc.NewServer(srv, cfg)
return s.Server.Serve(s.Listener)
}
func (s *FixtureServer) Stop() {
if s.Server != nil {
s.Server.Stop()
}
}
func (s *FixtureServer) AddProviderConfig(hcl string) string {
provider := `
provider "matchbox" {
endpoint = "%s"
client_cert = <<CERT
%s
CERT
client_key = <<KEY
%s
KEY
ca = <<CA
%s
CA
}
%s
`
return fmt.Sprintf(provider,
s.Listener.Addr().String(),
s.ClientTLS.Cert,
s.ClientTLS.Key,
s.ClientTLS.CA,
hcl)
}
// mustFile wraps a call to ioutil.ReadFile and panics if the error is non-nil.
func mustReadFile(filename string) []byte {
contents, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
return contents
}