|
| 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