Merge branch '18947-githttpd'
[arvados.git] / services / githttpd / integration_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package githttpd
6
7 import (
8         "context"
9         "errors"
10         "io/ioutil"
11         "os"
12         "os/exec"
13         "strings"
14         "testing"
15
16         "git.arvados.org/arvados.git/lib/config"
17         "git.arvados.org/arvados.git/sdk/go/arvados"
18         "git.arvados.org/arvados.git/sdk/go/arvadostest"
19         "git.arvados.org/arvados.git/sdk/go/ctxlog"
20         "git.arvados.org/arvados.git/sdk/go/httpserver"
21         check "gopkg.in/check.v1"
22 )
23
24 // Gocheck boilerplate
25 func Test(t *testing.T) {
26         check.TestingT(t)
27 }
28
29 // IntegrationSuite tests need an API server and an arvados-git-httpd
30 // server. See GitSuite and GitoliteSuite.
31 type IntegrationSuite struct {
32         tmpRepoRoot string
33         tmpWorkdir  string
34         testServer  *httpserver.Server
35         cluster     *arvados.Cluster
36 }
37
38 func (s *IntegrationSuite) SetUpTest(c *check.C) {
39         arvadostest.ResetEnv()
40
41         var err error
42         if s.tmpRepoRoot == "" {
43                 s.tmpRepoRoot, err = ioutil.TempDir("", "githttp")
44                 c.Assert(err, check.Equals, nil)
45         }
46         s.tmpWorkdir, err = ioutil.TempDir("", "githttp")
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         // we need git 2.28 to specify the initial branch with -b; Buster only has 2.20; so we do it in 2 steps
51         _, err = exec.Command("git", "init", s.tmpWorkdir).Output()
52         c.Assert(err, check.Equals, nil)
53         _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && git checkout -b main").Output()
54         c.Assert(err, check.Equals, nil)
55         _, 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()
56         c.Assert(err, check.Equals, nil)
57         _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && git push "+s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git main:main").CombinedOutput()
58         c.Assert(err, check.Equals, nil)
59         _, 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()
60         c.Assert(err, check.Equals, nil)
61
62         if s.cluster == nil {
63                 cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
64                 c.Assert(err, check.Equals, nil)
65                 s.cluster, err = cfg.GetCluster("")
66                 c.Assert(err, check.Equals, nil)
67
68                 s.cluster.Services.GitHTTP.InternalURLs = map[arvados.URL]arvados.ServiceInstance{{Host: "localhost:0"}: {}}
69                 s.cluster.TLS.Insecure = true
70                 s.cluster.Git.GitCommand = "/usr/bin/git"
71                 s.cluster.Git.Repositories = s.tmpRepoRoot
72                 s.cluster.ManagementToken = arvadostest.ManagementToken
73         }
74
75         s.testServer = &httpserver.Server{}
76         s.testServer.Handler = httpserver.LogRequests(newHandler(context.Background(), s.cluster, "", nil))
77         err = s.testServer.Start()
78         c.Assert(err, check.Equals, nil)
79
80         _, err = exec.Command("git", "config",
81                 "--file", s.tmpWorkdir+"/.git/config",
82                 "credential.http://"+s.testServer.Addr+"/.helper",
83                 "!cred(){ cat >/dev/null; if [ \"$1\" = get ]; then echo password=$ARVADOS_API_TOKEN; fi; };cred").Output()
84         c.Assert(err, check.Equals, nil)
85         _, err = exec.Command("git", "config",
86                 "--file", s.tmpWorkdir+"/.git/config",
87                 "credential.http://"+s.testServer.Addr+"/.username",
88                 "none").Output()
89         c.Assert(err, check.Equals, nil)
90
91         // Clear ARVADOS_API_* env vars before starting up the server,
92         // to make sure arvados-git-httpd doesn't use them or complain
93         // about them being missing.
94         os.Unsetenv("ARVADOS_API_HOST")
95         os.Unsetenv("ARVADOS_API_HOST_INSECURE")
96         os.Unsetenv("ARVADOS_API_TOKEN")
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.cluster = nil
120 }
121
122 func (s *IntegrationSuite) RunGit(c *check.C, token, gitCmd, repo string, args ...string) error {
123         cwd, err := os.Getwd()
124         c.Assert(err, check.Equals, nil)
125         defer os.Chdir(cwd)
126         os.Chdir(s.tmpWorkdir)
127
128         gitargs := append([]string{
129                 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
130         }, args...)
131         cmd := exec.Command("git", gitargs...)
132         cmd.Env = append(os.Environ(), "ARVADOS_API_TOKEN="+token)
133         w, err := cmd.StdinPipe()
134         c.Assert(err, check.Equals, nil)
135         w.Close()
136         output, err := cmd.CombinedOutput()
137         c.Log("git ", gitargs, " => ", err)
138         c.Log(string(output))
139         if err != nil && len(output) > 0 {
140                 // If messages appeared on stderr, they are more
141                 // helpful than the err returned by CombinedOutput().
142                 //
143                 // Easier to match error strings without newlines:
144                 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))
145         }
146         return err
147 }