-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcmds.go
More file actions
164 lines (131 loc) · 3.96 KB
/
cmds.go
File metadata and controls
164 lines (131 loc) · 3.96 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
package cmdtest
import (
"bytes"
"context"
"database/sql"
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"
_ "github.com/lib/pq"
"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/lookout"
// function to stop running commands
// redefined in StoppableCtx
var stop func()
type IntegrationSuite struct {
suite.Suite
Ctx context.Context
Stop func()
}
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")
time.Sleep(time.Second) // go needs a bit of time to kill process
}
}
// 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...)
cmd := exec.CommandContext(suite.Ctx, dummyBin, args...)
cmd.Stdout = outputWriter
cmd.Stderr = outputWriter
err := cmd.Start()
suite.Require().NoError(err, "can't start analyzer")
go func() {
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
failExit()
}
}
}()
return tee
}
// StartServe starts lookout server with context and optional arguments
func (suite *IntegrationSuite) StartServe(args ...string) (io.Reader, io.WriteCloser) {
require := suite.Require()
r, outputWriter := io.Pipe()
buf := &bytes.Buffer{}
tee := io.TeeReader(r, buf)
args = append([]string{"serve"}, args...)
cmd := exec.CommandContext(suite.Ctx, lookoutBin, args...)
cmd.Stdout = outputWriter
cmd.Stderr = outputWriter
w, err := cmd.StdinPipe()
require.NoError(err, "can't start server")
err = cmd.Start()
require.NoError(err, "can't start server")
go func() {
if err := cmd.Wait(); err != nil {
// don't print error if analyzer was killed by cancel
if suite.Ctx.Err() != context.Canceled {
fmt.Println("server exited with error:", err)
fmt.Printf("output:\n%s", buf.String())
// T.Fail cannot be called from a goroutine
failExit()
}
}
}()
return tee, w
}
// RunCli runs lookout subcommand (not a server)
func (suite *IntegrationSuite) RunCli(cmd string, args ...string) io.Reader {
args = append([]string{cmd}, args...)
var out bytes.Buffer
cliCmd := exec.CommandContext(suite.Ctx, lookoutBin, args...)
cliCmd.Stdout = &out
cliCmd.Stderr = &out
err := cliCmd.Run()
suite.Require().NoErrorf(err,
"'lookout %s' command returned error. output:\n%s",
strings.Join(args, " "), out.String())
return &out
}
// 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;")
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)
}
func failExit() {
stop()
os.Exit(1)
}