15003: Move keys from RequestLimits to API section.
[arvados.git] / services / arv-git-httpd / integration_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "errors"
9         "io/ioutil"
10         "os"
11         "os/exec"
12         "strings"
13         "testing"
14
15         "git.curoverse.com/arvados.git/sdk/go/arvados"
16         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
17         check "gopkg.in/check.v1"
18 )
19
20 // Gocheck boilerplate
21 func Test(t *testing.T) {
22         check.TestingT(t)
23 }
24
25 // IntegrationSuite tests need an API server and an arv-git-httpd
26 // server. See GitSuite and GitoliteSuite.
27 type IntegrationSuite struct {
28         tmpRepoRoot string
29         tmpWorkdir  string
30         testServer  *server
31         Config      *Config
32 }
33
34 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
35         arvadostest.StartAPI()
36 }
37
38 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
39         arvadostest.StopAPI()
40 }
41
42 func (s *IntegrationSuite) SetUpTest(c *check.C) {
43         arvadostest.ResetEnv()
44         s.testServer = &server{}
45         var err error
46         if s.tmpRepoRoot == "" {
47                 s.tmpRepoRoot, err = ioutil.TempDir("", "arv-git-httpd")
48                 c.Assert(err, check.Equals, nil)
49         }
50         s.tmpWorkdir, err = ioutil.TempDir("", "arv-git-httpd")
51         c.Assert(err, check.Equals, nil)
52         _, err = exec.Command("git", "init", "--bare", s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git").Output()
53         c.Assert(err, check.Equals, nil)
54         _, err = exec.Command("git", "init", s.tmpWorkdir).Output()
55         c.Assert(err, check.Equals, nil)
56         _, 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()
57         c.Assert(err, check.Equals, nil)
58         _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && git push "+s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git master:master").CombinedOutput()
59         c.Assert(err, check.Equals, nil)
60         _, 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()
61         c.Assert(err, check.Equals, nil)
62
63         _, err = exec.Command("git", "config",
64                 "--file", s.tmpWorkdir+"/.git/config",
65                 "credential.http://"+s.testServer.Addr+"/.helper",
66                 "!cred(){ cat >/dev/null; if [ \"$1\" = get ]; then echo password=$ARVADOS_API_TOKEN; fi; };cred").Output()
67         c.Assert(err, check.Equals, nil)
68         _, err = exec.Command("git", "config",
69                 "--file", s.tmpWorkdir+"/.git/config",
70                 "credential.http://"+s.testServer.Addr+"/.username",
71                 "none").Output()
72         c.Assert(err, check.Equals, nil)
73
74         if s.Config == nil {
75                 s.Config = &Config{
76                         Client: arvados.Client{
77                                 APIHost:  arvadostest.APIHost(),
78                                 Insecure: true,
79                         },
80                         Listen:          "localhost:0",
81                         GitCommand:      "/usr/bin/git",
82                         RepoRoot:        s.tmpRepoRoot,
83                         ManagementToken: arvadostest.ManagementToken,
84                 }
85         }
86
87         // Clear ARVADOS_API_* env vars before starting up the server,
88         // to make sure arv-git-httpd doesn't use them or complain
89         // about them being missing.
90         os.Unsetenv("ARVADOS_API_HOST")
91         os.Unsetenv("ARVADOS_API_HOST_INSECURE")
92         os.Unsetenv("ARVADOS_API_TOKEN")
93
94         theConfig = s.Config
95         err = s.testServer.Start()
96         c.Assert(err, check.Equals, nil)
97 }
98
99 func (s *IntegrationSuite) TearDownTest(c *check.C) {
100         var err error
101         if s.testServer != nil {
102                 err = s.testServer.Close()
103         }
104         c.Check(err, check.Equals, nil)
105         s.testServer = nil
106
107         if s.tmpRepoRoot != "" {
108                 err = os.RemoveAll(s.tmpRepoRoot)
109                 c.Check(err, check.Equals, nil)
110         }
111         s.tmpRepoRoot = ""
112
113         if s.tmpWorkdir != "" {
114                 err = os.RemoveAll(s.tmpWorkdir)
115                 c.Check(err, check.Equals, nil)
116         }
117         s.tmpWorkdir = ""
118
119         s.Config = nil
120
121         theConfig = defaultConfig()
122 }
123
124 func (s *IntegrationSuite) RunGit(c *check.C, token, gitCmd, repo string, args ...string) error {
125         cwd, err := os.Getwd()
126         c.Assert(err, check.Equals, nil)
127         defer os.Chdir(cwd)
128         os.Chdir(s.tmpWorkdir)
129
130         gitargs := append([]string{
131                 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
132         }, args...)
133         cmd := exec.Command("git", gitargs...)
134         cmd.Env = append(os.Environ(), "ARVADOS_API_TOKEN="+token)
135         w, err := cmd.StdinPipe()
136         c.Assert(err, check.Equals, nil)
137         w.Close()
138         output, err := cmd.CombinedOutput()
139         c.Log("git ", gitargs, " => ", err)
140         c.Log(string(output))
141         if err != nil && len(output) > 0 {
142                 // If messages appeared on stderr, they are more
143                 // helpful than the err returned by CombinedOutput().
144                 //
145                 // Easier to match error strings without newlines:
146                 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))
147         }
148         return err
149 }