11 "git.curoverse.com/arvados.git/sdk/go/arvados"
12 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
13 check "gopkg.in/check.v1"
16 // Gocheck boilerplate
17 func Test(t *testing.T) {
21 // IntegrationSuite tests need an API server and an arv-git-httpd
22 // server. See GitSuite and GitoliteSuite.
23 type IntegrationSuite struct {
30 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
31 arvadostest.StartAPI()
34 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
38 func (s *IntegrationSuite) SetUpTest(c *check.C) {
39 arvadostest.ResetEnv()
40 s.testServer = &server{}
42 if s.tmpRepoRoot == "" {
43 s.tmpRepoRoot, err = ioutil.TempDir("", "arv-git-httpd")
44 c.Assert(err, check.Equals, nil)
46 s.tmpWorkdir, err = ioutil.TempDir("", "arv-git-httpd")
47 c.Assert(err, check.Equals, nil)
48 _, err = exec.Command("git", "init", "--bare", s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git").Output()
49 c.Assert(err, check.Equals, nil)
50 _, err = exec.Command("git", "init", s.tmpWorkdir).Output()
51 c.Assert(err, check.Equals, nil)
52 _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && echo initial >initial && git add initial && git -c user.name=Initial -c user.email=Initial commit -am 'foo: initial commit'").CombinedOutput()
53 c.Assert(err, check.Equals, nil)
54 _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && git push "+s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git master:master").CombinedOutput()
55 c.Assert(err, check.Equals, nil)
56 _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && echo work >work && git add work && git -c user.name=Foo -c user.email=Foo commit -am 'workdir: test'").CombinedOutput()
57 c.Assert(err, check.Equals, nil)
59 _, err = exec.Command("git", "config",
60 "--file", s.tmpWorkdir+"/.git/config",
61 "credential.http://"+s.testServer.Addr+"/.helper",
62 "!cred(){ cat >/dev/null; if [ \"$1\" = get ]; then echo password=$ARVADOS_API_TOKEN; fi; };cred").Output()
63 c.Assert(err, check.Equals, nil)
64 _, err = exec.Command("git", "config",
65 "--file", s.tmpWorkdir+"/.git/config",
66 "credential.http://"+s.testServer.Addr+"/.username",
68 c.Assert(err, check.Equals, nil)
72 Client: arvados.Client{
73 APIHost: os.Getenv("ARVADOS_API_HOST"),
77 GitCommand: "/usr/bin/git",
78 RepoRoot: s.tmpRepoRoot,
82 err = s.testServer.Start()
83 c.Assert(err, check.Equals, nil)
85 // Clear ARVADOS_API_TOKEN after starting up the server, to
86 // make sure arv-git-httpd doesn't use it.
87 os.Setenv("ARVADOS_API_TOKEN", "unused-token-placates-client-library")
90 func (s *IntegrationSuite) TearDownTest(c *check.C) {
92 if s.testServer != nil {
93 err = s.testServer.Close()
95 c.Check(err, check.Equals, nil)
98 if s.tmpRepoRoot != "" {
99 err = os.RemoveAll(s.tmpRepoRoot)
100 c.Check(err, check.Equals, nil)
104 if s.tmpWorkdir != "" {
105 err = os.RemoveAll(s.tmpWorkdir)
106 c.Check(err, check.Equals, nil)
113 func (s *IntegrationSuite) RunGit(c *check.C, token, gitCmd, repo string, args ...string) error {
114 cwd, err := os.Getwd()
115 c.Assert(err, check.Equals, nil)
117 os.Chdir(s.tmpWorkdir)
119 gitargs := append([]string{
120 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
122 cmd := exec.Command("git", gitargs...)
123 cmd.Env = append(os.Environ(), "ARVADOS_API_TOKEN="+token)
124 w, err := cmd.StdinPipe()
125 c.Assert(err, check.Equals, nil)
127 output, err := cmd.CombinedOutput()
128 c.Log("git ", gitargs, " => ", err)
129 c.Log(string(output))
130 if err != nil && len(output) > 0 {
131 // If messages appeared on stderr, they are more
132 // helpful than the err returned by CombinedOutput().
134 // Easier to match error strings without newlines:
135 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))