Skip to content

Commit 91f290d

Browse files
committed
add e2e tests against the built binary
Signed-off-by: Miguel Molina <[email protected]>
1 parent d903647 commit 91f290d

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ before_script:
2121
- docker exec -it bblfshd bblfshctl driver install python bblfsh/python-driver
2222
- docker exec -it bblfshd bblfshctl driver install php bblfsh/php-driver
2323
- docker exec -it bblfshd bblfshctl driver install go bblfsh/go-driver
24+
- go get -v github.com/go-sql-driver/mysql/...
2425

2526
script:
2627
- make test-coverage codecov
28+
- make ci-e2e
2729

2830
jobs:
2931
include:

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,9 @@ static-build: LD_FLAGS += -linkmode external -extldflags '-static -lz'
4141
static-build: GO_BUILD_ARGS += -tags oniguruma
4242
static-build:
4343
go install -v $(GO_BUILD_ARGS) github.com/src-d/gitbase/...
44+
45+
ci-e2e: packages
46+
go test ./e2e -gitbase-version="$(TRAVIS_TAG)" \
47+
-must-run \
48+
-gitbase-bin="$(TRAVIS_BUILD_DIR)/build/gitbase_linux_amd64/gitbase" \
49+
-gitbase-repos="$(TRAVIS_BUILD_DIR)/.." -v

e2e/e2e_test.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package e2e
2+
3+
import (
4+
"database/sql"
5+
"flag"
6+
"fmt"
7+
"os"
8+
"os/exec"
9+
"testing"
10+
"time"
11+
12+
_ "github.com/go-sql-driver/mysql"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
var (
17+
mustRun = flag.Bool("must-run", false, "ensures the tests are run")
18+
bin = flag.String("gitbase-bin", "", "path to the gitbase binary to test")
19+
repos = flag.String("gitbase-repos", "", "path to the gitbase repos to test")
20+
version = flag.String("gitbase-version", "", "(optional) version of the binary")
21+
)
22+
23+
func TestMain(m *testing.M) {
24+
flag.Parse()
25+
26+
if *repos == "" {
27+
fmt.Println("gitbase-repos not provided")
28+
if *mustRun {
29+
os.Exit(1)
30+
} else {
31+
os.Exit(0)
32+
}
33+
}
34+
35+
path, err := exec.LookPath(*bin)
36+
if err != nil {
37+
fmt.Println("gitbase-bin not provided")
38+
if *mustRun {
39+
os.Exit(1)
40+
} else {
41+
os.Exit(0)
42+
}
43+
}
44+
45+
var done = make(chan error)
46+
cmd := exec.Command(
47+
path,
48+
"server",
49+
"--directories="+*repos,
50+
"--host=127.0.0.1",
51+
"--port=3308",
52+
"--index=indexes",
53+
)
54+
55+
if err := cmd.Start(); err != nil {
56+
fmt.Println("unable to start gitbase binary:", err)
57+
os.Exit(1)
58+
}
59+
60+
go func() {
61+
switch err := cmd.Wait().(type) {
62+
case *exec.ExitError:
63+
done <- nil
64+
default:
65+
done <- err
66+
}
67+
}()
68+
69+
time.Sleep(500 * time.Millisecond)
70+
71+
code := m.Run()
72+
73+
if err := cmd.Process.Signal(os.Interrupt); err != nil {
74+
fmt.Println("problem stopping binary:", err)
75+
os.Exit(1)
76+
}
77+
78+
if err := <-done; err != nil {
79+
fmt.Println("problem executing binary:", err)
80+
os.Exit(1)
81+
}
82+
83+
os.Exit(code)
84+
}
85+
86+
func connect(t *testing.T) (*sql.DB, func()) {
87+
db, err := sql.Open("mysql", "root:@tcp(127.0.0.1:3308)/gitbase")
88+
if err != nil {
89+
t.Errorf("unexpected error connecting to gitbase: %s", err)
90+
}
91+
92+
return db, func() {
93+
require.NoError(t, db.Close())
94+
}
95+
}
96+
97+
func TestVersion(t *testing.T) {
98+
if *version == "" {
99+
t.Skip("no version provided, skipping")
100+
}
101+
db, cleanup := connect(t)
102+
defer cleanup()
103+
104+
require := require.New(t)
105+
106+
var v string
107+
require.NoError(db.QueryRow("SELECT VERSION()").Scan(&v))
108+
require.Equal(fmt.Sprintf("8.0.11-%s", *version), v)
109+
}

0 commit comments

Comments
 (0)