Merge branch '8784-dir-listings'
[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:     ":0",
81                         GitCommand: "/usr/bin/git",
82                         RepoRoot:   s.tmpRepoRoot,
83                 }
84         }
85
86         // Clear ARVADOS_API_* env vars before starting up the server,
87         // to make sure arv-git-httpd doesn't use them or complain
88         // about them being missing.
89         os.Unsetenv("ARVADOS_API_HOST")
90         os.Unsetenv("ARVADOS_API_HOST_INSECURE")
91         os.Unsetenv("ARVADOS_API_TOKEN")
92
93         theConfig = s.Config
94         err = s.testServer.Start()
95         c.Assert(err, check.Equals, nil)
96 }
97
98 func (s *IntegrationSuite) TearDownTest(c *check.C) {
99         var err error
100         if s.testServer != nil {
101                 err = s.testServer.Close()
102         }
103         c.Check(err, check.Equals, nil)
104         s.testServer = nil
105
106         if s.tmpRepoRoot != "" {
107                 err = os.RemoveAll(s.tmpRepoRoot)
108                 c.Check(err, check.Equals, nil)
109         }
110         s.tmpRepoRoot = ""
111
112         if s.tmpWorkdir != "" {
113                 err = os.RemoveAll(s.tmpWorkdir)
114                 c.Check(err, check.Equals, nil)
115         }
116         s.tmpWorkdir = ""
117
118         s.Config = nil
119
120         theConfig = defaultConfig()
121 }
122
123 func (s *IntegrationSuite) RunGit(c *check.C, token, gitCmd, repo string, args ...string) error {
124         cwd, err := os.Getwd()
125         c.Assert(err, check.Equals, nil)
126         defer os.Chdir(cwd)
127         os.Chdir(s.tmpWorkdir)
128
129         gitargs := append([]string{
130                 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
131         }, args...)
132         cmd := exec.Command("git", gitargs...)
133         cmd.Env = append(os.Environ(), "ARVADOS_API_TOKEN="+token)
134         w, err := cmd.StdinPipe()
135         c.Assert(err, check.Equals, nil)
136         w.Close()
137         output, err := cmd.CombinedOutput()
138         c.Log("git ", gitargs, " => ", err)
139         c.Log(string(output))
140         if err != nil && len(output) > 0 {
141                 // If messages appeared on stderr, they are more
142                 // helpful than the err returned by CombinedOutput().
143                 //
144                 // Easier to match error strings without newlines:
145                 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))
146         }
147         return err
148 }