Debian Codegraph @

e2e/discovery.go

raw symbols SCIP
1 package e2e
2
3 import (
4 "fmt"
5 "net/http"
6 "strings"
7 "sync/atomic"
8 "time"
9
10 "github.com/coreos/etcd/pkg/expect"
11 )
12
13 type discoveryProcessConfig struct {
14 execPath string
15 etcdEp string
16 discoveryHost string
17 webPort int
18 }
19
20 type discoveryProcess struct {
21 cfg *discoveryProcessConfig
22 proc *expect.ExpectProcess
23 donec chan struct{} // closed when Interact() terminates
24 }
25
26 var discoveryBasePort int32 = 30000
27
28 // NewDiscoveryProcess creates a new 'discoveryProcess'.
29 func (cfg *discoveryProcessConfig) NewDiscoveryProcess() *discoveryProcess {
30 port := int(atomic.LoadInt32(&discoveryBasePort))
31 atomic.AddInt32(&discoveryBasePort, 2)
32
33 copied := *cfg
34 copied.webPort = port
35 copied.discoveryHost = fmt.Sprintf("http://localhost:%d", copied.webPort)
36
37 return &discoveryProcess{cfg: &copied}
38 }
39
40 func (dp *discoveryProcess) Stop(d time.Duration) (err error) {
41 errc := make(chan error, 1)
42 go func() { errc <- dp.proc.Stop() }()
43 select {
44 case err := <-errc:
45 return err
46 case <-time.After(d):
47 return fmt.Errorf("took longer than %v to Stop cluster", d)
48 }
49 }
50
51 func (dp *discoveryProcess) Start() error {
52 args := []string{
53 dp.cfg.execPath,
54 "--etcd", dp.cfg.etcdEp,
55 "--host", dp.cfg.discoveryHost,
56 "--addr", fmt.Sprintf(":%d", dp.cfg.webPort),
57 }
58 child, err := expect.NewExpect(args[0], args[1:]...)
59 if err != nil {
60 return err
61 }
62 dp.proc = child
63 dp.donec = make(chan struct{})
64
65 readyC := make(chan error)
66 go func() {
67 readyC <- dp.waitReady()
68 }()
69 select {
70 case err = <-readyC:
71 if err != nil {
72 return err
73 }
74 case <-time.After(10 * time.Second):
75 return fmt.Errorf("timed out waiting for discover server")
76 }
77 return nil
78 }
79
80 func (dp *discoveryProcess) waitReady() error {
81 defer close(dp.donec)
82 return waitReadyExpectProcDiscovery(dp.proc)
83 }
84
85 func waitReadyExpectProcDiscovery(exproc *expect.ExpectProcess) error {
86 readyStrs := []string{"discovery server started", "discovery serving on"}
87 c := 0
88 matchSet := func(l string) bool {
89 for _, s := range readyStrs {
90 if strings.Contains(l, s) {
91 c++
92 break
93 }
94 }
95 return c == len(readyStrs)
96 }
97 _, err := exproc.ExpectFunc(matchSet)
98 return err
99 }
100
101 type cURLReq struct {
102 timeout time.Duration
103 endpoint string
104 method string
105 data string
106 expFunc func(txt string) bool
107 }
108
109 func (req cURLReq) Send() (string, error) {
110 cmdArgs := []string{"curl"}
111 if req.timeout > 0 {
112 cmdArgs = append(cmdArgs, "--max-time", fmt.Sprint(int(req.timeout.Seconds())))
113 }
114 cmdArgs = append(cmdArgs, "-L", req.endpoint)
115 switch req.method {
116 case http.MethodPost, http.MethodPut:
117 dt := req.data
118 if !strings.HasPrefix(dt, "{") { // for non-JSON value
119 dt = "value=" + dt
120 }
121 cmdArgs = append(cmdArgs, "-X", req.method, "-d", dt)
122 }
123
124 proc, err := expect.NewExpect(cmdArgs[0], cmdArgs[1:]...)
125 if err != nil {
126 return "", err
127 }
128
129 var line string
130 for {
131 line, err = proc.ExpectFunc(req.expFunc)
132 if err != nil {
133 proc.Close()
134 return "", err
135 }
136 break
137 }
138 return line, proc.Close()
139
140 }