-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: lwsanty <lwsanty@gmail.com>
- Loading branch information
Showing
6 changed files
with
336 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,9 @@ script: | |
- GO111MODULE=on | ||
- make ci-script | ||
|
||
services: | ||
- docker | ||
|
||
jobs: | ||
include: | ||
- os: linux | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package integration | ||
|
||
import ( | ||
"database/sql" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/src-d/gitcollector/cmd/gitcollector/subcmd" | ||
|
||
"github.com/ory/dockertest" | ||
) | ||
|
||
const ( | ||
dockerImage = "postgres" | ||
dockerVersion = "9.6" | ||
) | ||
|
||
type helper struct { | ||
address string | ||
cmd subcmd.DownloadCmd | ||
cli *sql.DB | ||
closers []func() | ||
} | ||
|
||
func NewHelper(orgs string) (*helper, error) { | ||
lib, err := ioutil.TempDir("", "gcol-lib") | ||
if err != nil { | ||
removePaths(lib) | ||
return nil, err | ||
} | ||
|
||
tmp, err := ioutil.TempDir("", "gcol-tmp") | ||
if err != nil { | ||
removePaths(tmp, lib) | ||
return nil, err | ||
} | ||
|
||
addr, contClose, err := preparePostgres() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cli, err := sql.Open("postgres", addr) | ||
if err != nil { | ||
contClose() | ||
return nil, err | ||
} | ||
|
||
return &helper{ | ||
address: addr, | ||
cmd: subcmd.DownloadCmd{ | ||
Orgs: orgs, | ||
LibPath: lib, | ||
TmpPath: tmp, | ||
// Tests may fail due to request rate limit until https://github.com/src-d/gitcollector/issues/74 is fixed | ||
Token: os.Getenv("GITHUB_TOKEN"), | ||
}, | ||
cli: cli, | ||
closers: []func(){ | ||
func() { | ||
cli.Close() | ||
contClose() | ||
}, | ||
contClose, | ||
}, | ||
}, nil | ||
} | ||
|
||
func (h *helper) Exec() error { return h.cmd.Execute(nil) } | ||
|
||
func (h *helper) Cleanup() { | ||
removeDirsContents(h.cmd.TmpPath, h.cmd.LibPath) | ||
} | ||
|
||
func (h *helper) Close() { | ||
for _, c := range h.closers { | ||
c() | ||
} | ||
removePaths(h.cmd.TmpPath, h.cmd.LibPath) | ||
} | ||
|
||
func preparePostgres() (string, func(), error) { | ||
pool, err := dockertest.NewPool("") | ||
if err != nil { | ||
return "", func() {}, err | ||
} | ||
|
||
cont, err := pool.Run(dockerImage, dockerVersion, []string{ | ||
"POSTGRES_PASSWORD=postgres", | ||
}) | ||
if err != nil { | ||
return "", func() {}, err | ||
} | ||
|
||
const port = "5432/tcp" | ||
addr := `postgres://postgres:postgres@` + cont.GetHostPort(port) + "?sslmode=disable" | ||
if err := pool.Retry(func() error { | ||
cli, err := sql.Open("postgres", addr) | ||
if err != nil { | ||
return err | ||
} | ||
defer cli.Close() | ||
return cli.Ping() | ||
}); err != nil { | ||
cont.Close() | ||
return "", func() {}, err | ||
} | ||
|
||
return addr, func() { | ||
cont.Close() | ||
}, nil | ||
} | ||
|
||
func removePaths(paths ...string) { | ||
for _, p := range paths { | ||
os.RemoveAll(p) | ||
} | ||
} | ||
|
||
func removeDirsContents(dirs ...string) { | ||
for _, d := range dirs { | ||
removeContents(d) | ||
} | ||
} | ||
|
||
func removeContents(dir string) error { | ||
d, err := os.Open(dir) | ||
if err != nil { | ||
return err | ||
} | ||
defer d.Close() | ||
names, err := d.Readdirnames(-1) | ||
if err != nil { | ||
return err | ||
} | ||
for _, name := range names { | ||
err = os.RemoveAll(filepath.Join(dir, name)) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.