Debian Codegraph @

integration/server.go

raw symbols SCIP
1 package integration
2
3 import (
4 "context"
5 "fmt"
6 "io/ioutil"
7 "net/http"
8 "net/url"
9 "os"
10 "testing"
11 "time"
12
13 discoveryhttp "github.com/coreos/discovery.etcd.io/http"
14
15 "github.com/coreos/etcd/embed"
16 "github.com/coreos/etcd/etcdserver/api/v3client"
17 )
18
19 // Service contains test discovery server components.
20 type Service struct {
21 rootCtx context.Context
22 rootCancel func()
23
24 cfg *embed.Config
25 dataDir string
26 etcdCURL url.URL
27 etcd *embed.Etcd
28
29 httpEp string
30 httpServer *http.Server
31 httpErrc chan error
32 }
33
34 const testDiscoveryHost = "handler-test"
35
36 // NewService creates a new service.
37 func NewService(t *testing.T, etcdClientPort, etcdPeerPort, httpPort int) *Service {
38 dataDir, err := ioutil.TempDir(os.TempDir(), "test-data")
39 if err != nil {
40 t.Fatal(err)
41 }
42
43 cfg := embed.NewConfig()
44 cfg.ClusterState = embed.ClusterStateFlagNew
45 cfg.Name = "test-etcd"
46 cfg.Dir = dataDir
47 curl := url.URL{Scheme: "http", Host: fmt.Sprintf("localhost:%d", etcdClientPort)}
48 cfg.ACUrls, cfg.LCUrls = []url.URL{curl}, []url.URL{curl}
49 purl := url.URL{Scheme: "http", Host: fmt.Sprintf("localhost:%d", etcdPeerPort)}
50 cfg.APUrls, cfg.LPUrls = []url.URL{purl}, []url.URL{purl}
51 cfg.InitialCluster = fmt.Sprintf("%s=%s", cfg.Name, cfg.APUrls[0].String())
52 cfg.SnapCount = 1000 // single-node, keep minimum snapshot
53
54 // TODO: enable this with etcd v3.3+
55 // cfg.AutoCompactionMode = compactor.ModePeriodic
56 // cfg.AutoCompactionRetention = 1
57
58 ctx, cancel := context.WithCancel(context.Background())
59 return &Service{
60 rootCtx: ctx,
61 rootCancel: cancel,
62
63 cfg: cfg,
64 dataDir: dataDir,
65 etcdCURL: curl,
66
67 httpEp: fmt.Sprintf("http://localhost:%d", httpPort),
68 httpServer: &http.Server{
69 Addr: fmt.Sprintf("localhost:%d", httpPort),
70 Handler: discoveryhttp.RegisterHandlers(ctx, cfg.LCUrls[0].String(), testDiscoveryHost),
71 },
72 httpErrc: make(chan error),
73 }
74 }
75
76 // Start starts etcd server and http listener.
77 func (sv *Service) Start(t *testing.T) <-chan error {
78 srv, err := embed.StartEtcd(sv.cfg)
79 if err != nil {
80 t.Fatal(err)
81 }
82 select {
83 case <-srv.Server.ReadyNotify():
84 err = nil
85 case err = <-srv.Err():
86 case <-srv.Server.StopNotify():
87 err = fmt.Errorf("received from etcdserver.Server.StopNotify")
88 }
89 if err != nil {
90 t.Fatal(err)
91 }
92 sv.etcd = srv
93
94 // issue linearized read to ensure leader election
95 cli := v3client.New(srv.Server)
96 _, err = cli.Get(context.Background(), "foo")
97
98 go func() {
99 defer close(sv.httpErrc)
100 if err := sv.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
101 sv.httpErrc <- err
102 return
103 }
104 sv.httpErrc <- nil
105 }()
106 return sv.httpErrc
107 }
108
109 // Stop stops etcd server, removing the data directory, and http server.
110 func (sv *Service) Stop(t *testing.T) {
111 defer os.RemoveAll(sv.dataDir)
112
113 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
114 err := sv.httpServer.Shutdown(ctx)
115 cancel()
116 if err != nil && err != context.DeadlineExceeded {
117 t.Fatal(err)
118 }
119 err = <-sv.httpErrc
120 if err != nil {
121 t.Fatal(err)
122 }
123
124 sv.rootCancel()
125 sv.etcd.Close()
126 }