10816: Release savepoint after rollback. Use exec_query instead of execute. Move...
[arvados.git] / services / arv-git-httpd / integration_test.go
1 package main
2
3 import (
4         "errors"
5         "io/ioutil"
6         "os"
7         "os/exec"
8         "strings"
9         "testing"
10
11         "git.curoverse.com/arvados.git/sdk/go/arvados"
12         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
13         check "gopkg.in/check.v1"
14 )
15
16 // Gocheck boilerplate
17 func Test(t *testing.T) {
18         check.TestingT(t)
19 }
20
21 // IntegrationSuite tests need an API server and an arv-git-httpd
22 // server. See GitSuite and GitoliteSuite.
23 type IntegrationSuite struct {
24         tmpRepoRoot string
25         tmpWorkdir  string
26         testServer  *server
27         Config      *Config
28 }
29
30 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
31         arvadostest.StartAPI()
32 }
33
34 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
35         arvadostest.StopAPI()
36 }
37
38 func (s *IntegrationSuite) SetUpTest(c *check.C) {
39         arvadostest.ResetEnv()
40         s.testServer = &server{}
41         var err error
42         if s.tmpRepoRoot == "" {
43                 s.tmpRepoRoot, err = ioutil.TempDir("", "arv-git-httpd")
44                 c.Assert(err, check.Equals, nil)
45         }
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)
58
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",
67                 "none").Output()
68         c.Assert(err, check.Equals, nil)
69
70         if s.Config == nil {
71                 s.Config = &Config{
72                         Client: arvados.Client{
73                                 APIHost:  arvadostest.APIHost(),
74                                 Insecure: true,
75                         },
76                         Listen:     ":0",
77                         GitCommand: "/usr/bin/git",
78                         RepoRoot:   s.tmpRepoRoot,
79                 }
80         }
81
82         // Clear ARVADOS_API_* env vars before starting up the server,
83         // to make sure arv-git-httpd doesn't use them or complain
84         // about them being missing.
85         os.Unsetenv("ARVADOS_API_HOST")
86         os.Unsetenv("ARVADOS_API_HOST_INSECURE")
87         os.Unsetenv("ARVADOS_API_TOKEN")
88
89         theConfig = s.Config
90         err = s.testServer.Start()
91         c.Assert(err, check.Equals, nil)
92 }
93
94 func (s *IntegrationSuite) TearDownTest(c *check.C) {
95         var err error
96         if s.testServer != nil {
97                 err = s.testServer.Close()
98         }
99         c.Check(err, check.Equals, nil)
100         s.testServer = nil
101
102         if s.tmpRepoRoot != "" {
103                 err = os.RemoveAll(s.tmpRepoRoot)
104                 c.Check(err, check.Equals, nil)
105         }
106         s.tmpRepoRoot = ""
107
108         if s.tmpWorkdir != "" {
109                 err = os.RemoveAll(s.tmpWorkdir)
110                 c.Check(err, check.Equals, nil)
111         }
112         s.tmpWorkdir = ""
113
114         s.Config = nil
115
116         theConfig = defaultConfig()
117 }
118
119 func (s *IntegrationSuite) RunGit(c *check.C, token, gitCmd, repo string, args ...string) error {
120         cwd, err := os.Getwd()
121         c.Assert(err, check.Equals, nil)
122         defer os.Chdir(cwd)
123         os.Chdir(s.tmpWorkdir)
124
125         gitargs := append([]string{
126                 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
127         }, args...)
128         cmd := exec.Command("git", gitargs...)
129         cmd.Env = append(os.Environ(), "ARVADOS_API_TOKEN="+token)
130         w, err := cmd.StdinPipe()
131         c.Assert(err, check.Equals, nil)
132         w.Close()
133         output, err := cmd.CombinedOutput()
134         c.Log("git ", gitargs, " => ", err)
135         c.Log(string(output))
136         if err != nil && len(output) > 0 {
137                 // If messages appeared on stderr, they are more
138                 // helpful than the err returned by CombinedOutput().
139                 //
140                 // Easier to match error strings without newlines:
141                 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))
142         }
143         return err
144 }