7da85ee7472af663a7307d7e91c46282689d98f7
[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.arvados.org/arvados.git/lib/config"
16         "git.arvados.org/arvados.git/sdk/go/arvados"
17         "git.arvados.org/arvados.git/sdk/go/arvadostest"
18         "git.arvados.org/arvados.git/sdk/go/ctxlog"
19         check "gopkg.in/check.v1"
20 )
21
22 // Gocheck boilerplate
23 func Test(t *testing.T) {
24         check.TestingT(t)
25 }
26
27 // IntegrationSuite tests need an API server and an arv-git-httpd
28 // server. See GitSuite and GitoliteSuite.
29 type IntegrationSuite struct {
30         tmpRepoRoot string
31         tmpWorkdir  string
32         testServer  *server
33         cluster     *arvados.Cluster
34 }
35
36 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
37         arvadostest.StartAPI()
38 }
39
40 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
41         arvadostest.StopAPI()
42 }
43
44 func (s *IntegrationSuite) SetUpTest(c *check.C) {
45         arvadostest.ResetEnv()
46
47         var err error
48         if s.tmpRepoRoot == "" {
49                 s.tmpRepoRoot, err = ioutil.TempDir("", "arv-git-httpd")
50                 c.Assert(err, check.Equals, nil)
51         }
52         s.tmpWorkdir, err = ioutil.TempDir("", "arv-git-httpd")
53         c.Assert(err, check.Equals, nil)
54         _, err = exec.Command("git", "init", "--bare", s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git").Output()
55         c.Assert(err, check.Equals, nil)
56         // we need git 2.28 to specify the initial branch with -b; Buster only has 2.20; so we do it in 2 steps
57         _, err = exec.Command("git", "init", s.tmpWorkdir).Output()
58         c.Assert(err, check.Equals, nil)
59         _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && git checkout -b main").Output()
60         c.Assert(err, check.Equals, nil)
61         _, 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()
62         c.Assert(err, check.Equals, nil)
63         _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && git push "+s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git main:main").CombinedOutput()
64         c.Assert(err, check.Equals, nil)
65         _, 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()
66         c.Assert(err, check.Equals, nil)
67
68         if s.cluster == nil {
69                 cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
70                 c.Assert(err, check.Equals, nil)
71                 s.cluster, err = cfg.GetCluster("")
72                 c.Assert(err, check.Equals, nil)
73
74                 s.cluster.Services.GitHTTP.InternalURLs = map[arvados.URL]arvados.ServiceInstance{{Host: "localhost:0"}: {}}
75                 s.cluster.TLS.Insecure = true
76                 s.cluster.Git.GitCommand = "/usr/bin/git"
77                 s.cluster.Git.Repositories = s.tmpRepoRoot
78                 s.cluster.ManagementToken = arvadostest.ManagementToken
79         }
80
81         s.testServer = &server{cluster: s.cluster}
82         err = s.testServer.Start()
83         c.Assert(err, check.Equals, nil)
84
85         _, err = exec.Command("git", "config",
86                 "--file", s.tmpWorkdir+"/.git/config",
87                 "credential.http://"+s.testServer.Addr+"/.helper",
88                 "!cred(){ cat >/dev/null; if [ \"$1\" = get ]; then echo password=$ARVADOS_API_TOKEN; fi; };cred").Output()
89         c.Assert(err, check.Equals, nil)
90         _, err = exec.Command("git", "config",
91                 "--file", s.tmpWorkdir+"/.git/config",
92                 "credential.http://"+s.testServer.Addr+"/.username",
93                 "none").Output()
94         c.Assert(err, check.Equals, nil)
95
96         // Clear ARVADOS_API_* env vars before starting up the server,
97         // to make sure arv-git-httpd doesn't use them or complain
98         // about them being missing.
99         os.Unsetenv("ARVADOS_API_HOST")
100         os.Unsetenv("ARVADOS_API_HOST_INSECURE")
101         os.Unsetenv("ARVADOS_API_TOKEN")
102 }
103
104 func (s *IntegrationSuite) TearDownTest(c *check.C) {
105         var err error
106         if s.testServer != nil {
107                 err = s.testServer.Close()
108         }
109         c.Check(err, check.Equals, nil)
110         s.testServer = nil
111
112         if s.tmpRepoRoot != "" {
113                 err = os.RemoveAll(s.tmpRepoRoot)
114                 c.Check(err, check.Equals, nil)
115         }
116         s.tmpRepoRoot = ""
117
118         if s.tmpWorkdir != "" {
119                 err = os.RemoveAll(s.tmpWorkdir)
120                 c.Check(err, check.Equals, nil)
121         }
122         s.tmpWorkdir = ""
123
124         s.cluster = nil
125 }
126
127 func (s *IntegrationSuite) RunGit(c *check.C, token, 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         cmd.Env = append(os.Environ(), "ARVADOS_API_TOKEN="+token)
138         w, err := cmd.StdinPipe()
139         c.Assert(err, check.Equals, nil)
140         w.Close()
141         output, err := cmd.CombinedOutput()
142         c.Log("git ", gitargs, " => ", err)
143         c.Log(string(output))
144         if err != nil && len(output) > 0 {
145                 // If messages appeared on stderr, they are more
146                 // helpful than the err returned by CombinedOutput().
147                 //
148                 // Easier to match error strings without newlines:
149                 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))
150         }
151         return err
152 }