Stub out controllers and routes. Add basic workbench support.
[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/arvadostest"
12         check "gopkg.in/check.v1"
13 )
14
15 // Gocheck boilerplate
16 func Test(t *testing.T) {
17         check.TestingT(t)
18 }
19
20 // IntegrationSuite tests need an API server and an arv-git-httpd
21 // server. See GitSuite and GitoliteSuite.
22 type IntegrationSuite struct {
23         tmpRepoRoot string
24         tmpWorkdir  string
25         testServer  *server
26         Config      *config
27 }
28
29 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
30         arvadostest.StartAPI()
31 }
32
33 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
34         arvadostest.StopAPI()
35 }
36
37 func (s *IntegrationSuite) SetUpTest(c *check.C) {
38         arvadostest.ResetEnv()
39         s.testServer = &server{}
40         var err error
41         if s.tmpRepoRoot == "" {
42                 s.tmpRepoRoot, err = ioutil.TempDir("", "arv-git-httpd")
43                 c.Assert(err, check.Equals, nil)
44         }
45         s.tmpWorkdir, err = ioutil.TempDir("", "arv-git-httpd")
46         c.Assert(err, check.Equals, nil)
47         _, err = exec.Command("git", "init", "--bare", s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git").Output()
48         c.Assert(err, check.Equals, nil)
49         _, err = exec.Command("git", "init", s.tmpWorkdir).Output()
50         c.Assert(err, check.Equals, nil)
51         _, 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()
52         c.Assert(err, check.Equals, nil)
53         _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && git push "+s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git master:master").CombinedOutput()
54         c.Assert(err, check.Equals, nil)
55         _, 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()
56         c.Assert(err, check.Equals, nil)
57
58         _, err = exec.Command("git", "config",
59                 "--file", s.tmpWorkdir+"/.git/config",
60                 "credential.http://"+s.testServer.Addr+"/.helper",
61                 "!cred(){ cat >/dev/null; if [ \"$1\" = get ]; then echo password=$ARVADOS_API_TOKEN; fi; };cred").Output()
62         c.Assert(err, check.Equals, nil)
63         _, err = exec.Command("git", "config",
64                 "--file", s.tmpWorkdir+"/.git/config",
65                 "credential.http://"+s.testServer.Addr+"/.username",
66                 "none").Output()
67         c.Assert(err, check.Equals, nil)
68
69         if s.Config == nil {
70                 s.Config = &config{
71                         Addr:       ":0",
72                         GitCommand: "/usr/bin/git",
73                         Root:       s.tmpRepoRoot,
74                 }
75         }
76         theConfig = s.Config
77         err = s.testServer.Start()
78         c.Assert(err, check.Equals, nil)
79
80         // Clear ARVADOS_API_TOKEN after starting up the server, to
81         // make sure arv-git-httpd doesn't use it.
82         os.Setenv("ARVADOS_API_TOKEN", "unused-token-placates-client-library")
83 }
84
85 func (s *IntegrationSuite) TearDownTest(c *check.C) {
86         var err error
87         if s.testServer != nil {
88                 err = s.testServer.Close()
89         }
90         c.Check(err, check.Equals, nil)
91         s.testServer = nil
92
93         if s.tmpRepoRoot != "" {
94                 err = os.RemoveAll(s.tmpRepoRoot)
95                 c.Check(err, check.Equals, nil)
96         }
97         s.tmpRepoRoot = ""
98
99         if s.tmpWorkdir != "" {
100                 err = os.RemoveAll(s.tmpWorkdir)
101                 c.Check(err, check.Equals, nil)
102         }
103         s.tmpWorkdir = ""
104
105         s.Config = nil
106 }
107
108 func (s *IntegrationSuite) RunGit(c *check.C, token, gitCmd, repo string, args ...string) error {
109         cwd, err := os.Getwd()
110         c.Assert(err, check.Equals, nil)
111         defer os.Chdir(cwd)
112         os.Chdir(s.tmpWorkdir)
113
114         gitargs := append([]string{
115                 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
116         }, args...)
117         cmd := exec.Command("git", gitargs...)
118         cmd.Env = append(os.Environ(), "ARVADOS_API_TOKEN="+token)
119         w, err := cmd.StdinPipe()
120         c.Assert(err, check.Equals, nil)
121         w.Close()
122         output, err := cmd.CombinedOutput()
123         c.Log("git ", gitargs, " => ", err)
124         c.Log(string(output))
125         if err != nil && len(output) > 0 {
126                 // If messages appeared on stderr, they are more
127                 // helpful than the err returned by CombinedOutput().
128                 //
129                 // Easier to match error strings without newlines:
130                 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))
131         }
132         return err
133 }