9950: Load config from /etc/arvados/arv-git-httpd/config.json.
[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: os.Getenv("ARVADOS_API_HOST"),
74                         },
75                         Listen:     ":0",
76                         GitCommand: "/usr/bin/git",
77                         Root:       s.tmpRepoRoot,
78                 }
79         }
80         theConfig = s.Config
81         err = s.testServer.Start()
82         c.Assert(err, check.Equals, nil)
83
84         // Clear ARVADOS_API_TOKEN after starting up the server, to
85         // make sure arv-git-httpd doesn't use it.
86         os.Setenv("ARVADOS_API_TOKEN", "unused-token-placates-client-library")
87 }
88
89 func (s *IntegrationSuite) TearDownTest(c *check.C) {
90         var err error
91         if s.testServer != nil {
92                 err = s.testServer.Close()
93         }
94         c.Check(err, check.Equals, nil)
95         s.testServer = nil
96
97         if s.tmpRepoRoot != "" {
98                 err = os.RemoveAll(s.tmpRepoRoot)
99                 c.Check(err, check.Equals, nil)
100         }
101         s.tmpRepoRoot = ""
102
103         if s.tmpWorkdir != "" {
104                 err = os.RemoveAll(s.tmpWorkdir)
105                 c.Check(err, check.Equals, nil)
106         }
107         s.tmpWorkdir = ""
108
109         s.Config = nil
110 }
111
112 func (s *IntegrationSuite) RunGit(c *check.C, token, gitCmd, repo string, args ...string) error {
113         cwd, err := os.Getwd()
114         c.Assert(err, check.Equals, nil)
115         defer os.Chdir(cwd)
116         os.Chdir(s.tmpWorkdir)
117
118         gitargs := append([]string{
119                 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
120         }, args...)
121         cmd := exec.Command("git", gitargs...)
122         cmd.Env = append(os.Environ(), "ARVADOS_API_TOKEN="+token)
123         w, err := cmd.StdinPipe()
124         c.Assert(err, check.Equals, nil)
125         w.Close()
126         output, err := cmd.CombinedOutput()
127         c.Log("git ", gitargs, " => ", err)
128         c.Log(string(output))
129         if err != nil && len(output) > 0 {
130                 // If messages appeared on stderr, they are more
131                 // helpful than the err returned by CombinedOutput().
132                 //
133                 // Easier to match error strings without newlines:
134                 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))
135         }
136         return err
137 }