5416: Add integration tests.
[arvados.git] / services / arv-git-httpd / server_test.go
1 package main
2
3 import (
4         "errors"
5         "io/ioutil"
6         "os"
7         "os/exec"
8         "strings"
9         "testing"
10
11         check "gopkg.in/check.v1"
12         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
13 )
14
15 var _ = check.Suite(&IntegrationSuite{})
16
17 // IntegrationSuite tests need an API server and an arv-git-httpd server
18 type IntegrationSuite struct {
19         tmpRepoRoot string
20         tmpWorkdir  string
21         testServer  *server
22 }
23
24 func (s *IntegrationSuite) TestReadonly(c *check.C) {
25         // Spectator token
26         os.Setenv("ARVADOS_API_TOKEN", "zw2f4gwx8hw8cjre7yp6v1zylhrhn3m5gvjq73rtpwhmknrybu")
27         err := s.runGit(c, "fetch", "foo.git")
28         c.Assert(err, check.Equals, nil)
29         err = s.runGit(c, "push", "foo.git", "master:newbranchfail")
30         c.Assert(err, check.ErrorMatches, `.*HTTP code = 403.*`)
31         _, err = os.Stat(s.tmpRepoRoot + "/.git/refs/heads/newbranchfail")
32         c.Assert(err, check.FitsTypeOf, &os.PathError{})
33 }
34
35 func (s *IntegrationSuite) TestReadwrite(c *check.C) {
36         // Active user token
37         os.Setenv("ARVADOS_API_TOKEN", "3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi")
38         err := s.runGit(c, "fetch", "foo.git")
39         c.Assert(err, check.Equals, nil)
40         err = s.runGit(c, "push", "foo.git", "master:newbranch")
41         c.Assert(err, check.Equals, nil)
42         _, err = os.Stat(s.tmpRepoRoot + "/foo/.git/refs/heads/newbranch")
43         c.Assert(err, check.Equals, nil)
44 }
45
46 func (s *IntegrationSuite) TestNonexistent(c *check.C) {
47         // Spectator token
48         os.Setenv("ARVADOS_API_TOKEN", "zw2f4gwx8hw8cjre7yp6v1zylhrhn3m5gvjq73rtpwhmknrybu")
49         err := s.runGit(c, "fetch", "thisrepodoesnotexist.git")
50         c.Assert(err, check.ErrorMatches, `.* not found:.*`)
51 }
52
53 func (s *IntegrationSuite) TestNoPermission(c *check.C) {
54         // Anonymous token
55         os.Setenv("ARVADOS_API_TOKEN", "4kg6k6lzmp9kj4cpkcoxie964cmvjahbt4fod9zru44k4jqdmi")
56         for _, repo := range []string{"foo.git", "foo/.git", "foo/bar.git", "foo/bar/.git"} {
57                 err := s.runGit(c, "fetch", repo)
58                 c.Assert(err, check.ErrorMatches, `.* not found:.*`)
59         }
60 }
61
62 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
63         arvadostest.StartAPI()
64 }
65
66 func (s *IntegrationSuite) SetUpTest(c *check.C) {
67         arvadostest.ResetEnv()
68         s.testServer = &server{}
69         var err error
70         s.tmpRepoRoot, err = ioutil.TempDir("", "arv-git-httpd")
71         c.Assert(err, check.Equals, nil)
72         s.tmpWorkdir, err = ioutil.TempDir("", "arv-git-httpd")
73         c.Assert(err, check.Equals, nil)
74         _, err = exec.Command("git", "init", "--bare", s.tmpRepoRoot + "/arvados.git").Output()
75         c.Assert(err, check.Equals, nil)
76         _, err = exec.Command("git", "--git-dir", s.tmpRepoRoot + "/arvados.git", "fetch", "../../.git", "master:master").Output()
77         c.Assert(err, check.Equals, nil)
78         _, err = exec.Command("git", "init", s.tmpRepoRoot + "/foo").Output()
79         c.Assert(err, check.Equals, nil)
80         _, err = exec.Command("sh", "-c", "cd " + s.tmpRepoRoot + "/foo && echo test >test && git add test && git commit -am 'foo: test'").CombinedOutput()
81         c.Assert(err, check.Equals, nil)
82         _, err = exec.Command("git", "init", s.tmpWorkdir).Output()
83         c.Assert(err, check.Equals, nil)
84         _, err = exec.Command("sh", "-c", "cd " + s.tmpWorkdir + " && echo work >work && git add work && git commit -am 'workdir: test'").CombinedOutput()
85         c.Assert(err, check.Equals, nil)
86
87         theConfig = &config{
88                 Addr: ":",
89                 GitCommand: "/usr/bin/git",
90                 Root: s.tmpRepoRoot,
91         }
92         err = s.testServer.Start()
93         c.Assert(err, check.Equals, nil)
94
95         // Clear ARVADOS_API_TOKEN after starting up the server, to
96         // make sure arv-git-httpd doesn't use it.
97         os.Setenv("ARVADOS_API_TOKEN", "")
98
99         _, err = exec.Command("git", "config",
100                 "--file", s.tmpWorkdir + "/.git/config",
101                 "credential.http://" + s.testServer.Addr + "/.helper",
102                 "!foo(){ echo password=$ARVADOS_API_TOKEN; };foo").Output()
103         c.Assert(err, check.Equals, nil)
104         _, err = exec.Command("git", "config",
105                 "--file", s.tmpWorkdir + "/.git/config",
106                 "credential.http://" + s.testServer.Addr + "/.username",
107                 "none").Output()
108         c.Assert(err, check.Equals, nil)
109 }
110
111 func (s *IntegrationSuite) TearDownTest(c *check.C) {
112         var err error
113         if s.testServer != nil {
114                 err = s.testServer.Close()
115         }
116         c.Check(err, check.Equals, nil)
117         if s.tmpRepoRoot != "" {
118                 err = os.RemoveAll(s.tmpRepoRoot)
119                 c.Check(err, check.Equals, nil)
120         }
121         if s.tmpWorkdir != "" {
122                 err = os.RemoveAll(s.tmpWorkdir)
123                 c.Check(err, check.Equals, nil)
124         }
125 }
126
127 func (s *IntegrationSuite) runGit(c *check.C, gitCmd, repo string, args ...string) error {
128         cwd, err := os.Getwd()
129         c.Assert(err, check.Equals, nil)
130         defer os.Chdir(cwd)
131         os.Chdir(s.tmpWorkdir)
132
133         gitargs := append([]string{
134                 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
135         }, args...)
136         cmd := exec.Command("git", gitargs...)
137         w, err := cmd.StdinPipe()
138         c.Assert(err, check.Equals, nil)
139         go w.Close()
140         output, err := cmd.CombinedOutput()
141         c.Log("git ", gitargs, " => ", err)
142         if err != nil {
143                 // Easier to match error strings without newlines.
144                 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))
145         }
146         return err
147 }
148
149 // Gocheck boilerplate
150 func Test(t *testing.T) {
151         check.TestingT(t)
152 }