-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcmds.go
More file actions
260 lines (207 loc) · 6.82 KB
/
cmds.go
File metadata and controls
260 lines (207 loc) · 6.82 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package cmdtest
import (
"bytes"
"context"
"database/sql"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"time"
_ "github.com/lib/pq"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
// CmdTimeout defines timeout for a command
var CmdTimeout = time.Minute
// default path to binaries
var dummyBin = "../../build/bin/dummy"
var lookoutBin = "../../build/bin/lookoutd"
type IntegrationSuite struct {
suite.Suite
Ctx context.Context
Stop func()
wg sync.WaitGroup
logBuf *bytes.Buffer
}
func init() {
if os.Getenv("DUMMY_BIN") != "" {
dummyBin = os.Getenv("DUMMY_BIN")
}
if os.Getenv("LOOKOUT_BIN") != "" {
lookoutBin = os.Getenv("LOOKOUT_BIN")
}
}
// StoppableCtx return ctx and stop function
func (suite *IntegrationSuite) StoppableCtx() {
ctx, timeoutCancel := context.WithTimeout(context.Background(), CmdTimeout)
var cancel context.CancelFunc
suite.Ctx, cancel = context.WithCancel(ctx)
suite.Stop = func() {
timeoutCancel()
cancel()
fmt.Println("stopping services")
suite.wg.Wait()
}
}
// StartDummy starts dummy analyzer with context and optional arguments
func (suite *IntegrationSuite) StartDummy(args ...string) io.Reader {
r, outputWriter := io.Pipe()
buf := &bytes.Buffer{}
tee := io.TeeReader(r, buf)
args = append([]string{"serve"}, args...)
fmt.Printf("starting dummy %s\n", strings.Join(args, " "))
cmd := exec.CommandContext(suite.Ctx, dummyBin, args...)
cmd.Stdout = outputWriter
cmd.Stderr = outputWriter
go func() {
// cmd.Wait() will not finish until stdout is closed
<-suite.Ctx.Done()
outputWriter.Close()
}()
err := cmd.Start()
suite.Require().NoError(err, "can't start analyzer")
suite.wg.Add(1)
go func() {
defer suite.wg.Done()
if err := cmd.Wait(); err != nil {
// don't print error if analyzer was killed by cancel
if suite.Ctx.Err() != context.Canceled {
fmt.Println("analyzer exited with error:", err)
fmt.Printf("output:\n%s", buf.String())
// T.Fail cannot be called from a goroutine
suite.Stop()
os.Exit(1)
}
}
}()
return tee
}
// StartLookoutd starts lookoutd serve, or watch and work if the queue testing
// is enabled
func (suite *IntegrationSuite) StartLookoutd(configFile string) (io.Reader, io.WriteCloser) {
if suite.IsQueueTested() {
watcherR, watcherW := suite.StartWatcher("--provider", "json",
"-c", configFile)
workerR, _ := suite.StartWorker("--provider", "json",
"-c", configFile, "--probes-addr", "0.0.0.0:8091")
// make sure watcher server started correctly
suite.GrepTrue(watcherR, "Starting watcher")
// make sure worker started correctly
suite.GrepTrue(workerR, "Worker started")
// Write json commands to watcher, write processed output from worker
return workerR, watcherW
} else {
r, w := suite.StartServe("--provider", "json",
"-c", configFile)
// make sure server started correctly
suite.GrepTrue(r, "Starting watcher")
return r, w
}
}
// StartServe starts lookout server with context and optional arguments
func (suite *IntegrationSuite) StartServe(args ...string) (io.Reader, io.WriteCloser) {
args = append([]string{"serve"}, args...)
return suite.startLookoutd(args...)
}
// StartWatcher starts lookoutd watch with context and optional arguments
func (suite *IntegrationSuite) StartWatcher(args ...string) (io.Reader, io.WriteCloser) {
args = append([]string{"watch"}, args...)
return suite.startLookoutd(args...)
}
// StartWorker starts lookoutd work with context and optional arguments
func (suite *IntegrationSuite) StartWorker(args ...string) (io.Reader, io.WriteCloser) {
args = append([]string{"work"}, args...)
return suite.startLookoutd(args...)
}
func (suite *IntegrationSuite) startLookoutd(args ...string) (io.Reader, io.WriteCloser) {
require := suite.Require()
r, outputWriter := io.Pipe()
suite.logBuf = &bytes.Buffer{}
tee := io.TeeReader(r, suite.logBuf)
cmd := exec.CommandContext(suite.Ctx, lookoutBin, args...)
cmd.Stdout = outputWriter
cmd.Stderr = outputWriter
go func() {
// cmd.Wait() will not finish until stdout is closed
<-suite.Ctx.Done()
outputWriter.Close()
}()
fmt.Printf("starting lookoutd %s\n", strings.Join(args, " "))
w, err := cmd.StdinPipe()
require.NoError(err, "can't start lookoutd")
err = cmd.Start()
require.NoError(err, "can't start lookoutd")
suite.wg.Add(1)
go func() {
defer suite.wg.Done()
if err := cmd.Wait(); err != nil {
// don't print error if killed by cancel
if suite.Ctx.Err() != context.Canceled {
fmt.Println("lookoutd exited with error:", err)
fmt.Printf("output:\n%s", suite.logBuf.String())
// T.Fail cannot be called from a goroutine
suite.Stop()
os.Exit(1)
}
}
}()
return tee, w
}
// IsQueueTested returns true if LOOKOUT_TEST_QUEUE env var is set to true
func (suite *IntegrationSuite) IsQueueTested() bool {
res := false
qEnv := os.Getenv("LOOKOUT_TEST_QUEUE")
if qEnv != "" {
var err error
res, err = strconv.ParseBool(qEnv)
require.NoError(suite.T(), err, "failed to parse env var LOOKOUT_TEST_QUEUE, it must be a boolean")
}
return res
}
// RunCli runs lookout subcommand (not a server)
func (suite *IntegrationSuite) RunCli(cmd string, args ...string) io.Reader {
out, err := suite.runCli(cmd, args...)
suite.Require().NoErrorf(err,
"'%s %s' command returned error. output:\n%s",
cmd, strings.Join(args, " "), out.String())
return out
}
// RunCliErr runs lookout subcommand that should fail
func (suite *IntegrationSuite) RunCliErr(cmd string, args ...string) io.Reader {
out, err := suite.runCli(cmd, args...)
suite.Require().Error(err, "'%s %s' command should return error", cmd, strings.Join(args, " "))
return out
}
func (suite *IntegrationSuite) runCli(cmd string, args ...string) (*bytes.Buffer, error) {
args = append([]string{cmd}, args...)
var out bytes.Buffer
cliCmd := exec.CommandContext(suite.Ctx, lookoutBin, args...)
cliCmd.Stdout = &out
cliCmd.Stderr = &out
return &out, cliCmd.Run()
}
// ResetDB recreates database for the test
func (suite *IntegrationSuite) ResetDB() {
require := suite.Require()
db, err := sql.Open("postgres", "postgres://postgres:postgres@localhost:5432/lookout?sslmode=disable")
require.NoError(err, "can't connect to DB")
suite.runQuery(db, "DROP SCHEMA public CASCADE;")
suite.runQuery(db, "CREATE SCHEMA public;")
suite.runQuery(db, "GRANT ALL ON SCHEMA public TO postgres;")
suite.runQuery(db, "GRANT ALL ON SCHEMA public TO public;")
fmt.Println("running lookoutd migrate")
err = exec.Command(lookoutBin, "migrate").Run()
require.NoError(err, "can't migrate DB")
}
func (suite *IntegrationSuite) runQuery(db *sql.DB, query string) {
_, err := db.Exec(query)
suite.Require().NoError(err, "can't execute SQL: %q", query)
}
// Output returns the output read so far from the reader returned in StartLookoutd
func (suite *IntegrationSuite) Output() string {
return suite.logBuf.String()
}