Merge branch '14712-git-httpd-config'
[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/lib/config"
16         "git.curoverse.com/arvados.git/sdk/go/arvados"
17         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
18         check "gopkg.in/check.v1"
19 )
20
21 // Gocheck boilerplate
22 func Test(t *testing.T) {
23         check.TestingT(t)
24 }
25
26 // IntegrationSuite tests need an API server and an arv-git-httpd
27 // server. See GitSuite and GitoliteSuite.
28 type IntegrationSuite struct {
29         tmpRepoRoot string
30         tmpWorkdir  string
31         testServer  *server
32         cluster     *arvados.Cluster
33 }
34
35 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
36         arvadostest.StartAPI()
37 }
38
39 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
40         arvadostest.StopAPI()
41 }
42
43 func (s *IntegrationSuite) SetUpTest(c *check.C) {
44         arvadostest.ResetEnv()
45
46         var err error
47         if s.tmpRepoRoot == "" {
48                 s.tmpRepoRoot, err = ioutil.TempDir("", "arv-git-httpd")
49                 c.Assert(err, check.Equals, nil)
50         }
51         s.tmpWorkdir, err = ioutil.TempDir("", "arv-git-httpd")
52         c.Assert(err, check.Equals, nil)
53         _, err = exec.Command("git", "init", "--bare", s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git").Output()
54         c.Assert(err, check.Equals, nil)
55         _, err = exec.Command("git", "init", s.tmpWorkdir).Output()
56         c.Assert(err, check.Equals, nil)
57         _, 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()
58         c.Assert(err, check.Equals, nil)
59         _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && git push "+s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git master:master").CombinedOutput()
60         c.Assert(err, check.Equals, nil)
61         _, 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()
62         c.Assert(err, check.Equals, nil)
63
64         if s.cluster == nil {
65                 cfg, err := config.NewLoader(nil, nil).Load()
66                 c.Assert(err, check.Equals, nil)
67                 s.cluster, err = cfg.GetCluster("")
68                 c.Assert(err, check.Equals, nil)
69
70                 s.cluster.Services.GitHTTP.InternalURLs = map[arvados.URL]arvados.ServiceInstance{arvados.URL{Host: "localhost:0"}: arvados.ServiceInstance{}}
71                 s.cluster.TLS.Insecure = true
72                 s.cluster.Git.GitCommand = "/usr/bin/git"
73                 s.cluster.Git.Repositories = s.tmpRepoRoot
74                 s.cluster.ManagementToken = arvadostest.ManagementToken
75         }
76
77         s.testServer = &server{cluster: s.cluster}
78         err = s.testServer.Start()
79         c.Assert(err, check.Equals, nil)
80
81         _, err = exec.Command("git", "config",
82                 "--file", s.tmpWorkdir+"/.git/config",
83                 "credential.http://"+s.testServer.Addr+"/.helper",
84                 "!cred(){ cat >/dev/null; if [ \"$1\" = get ]; then echo password=$ARVADOS_API_TOKEN; fi; };cred").Output()
85         c.Assert(err, check.Equals, nil)
86         _, err = exec.Command("git", "config",
87                 "--file", s.tmpWorkdir+"/.git/config",
88                 "credential.http://"+s.testServer.Addr+"/.username",
89                 "none").Output()
90         c.Assert(err, check.Equals, nil)
91
92         // Clear ARVADOS_API_* env vars before starting up the server,
93         // to make sure arv-git-httpd doesn't use them or complain
94         // about them being missing.
95         os.Unsetenv("ARVADOS_API_HOST")
96         os.Unsetenv("ARVADOS_API_HOST_INSECURE")
97         os.Unsetenv("ARVADOS_API_TOKEN")
98 }
99
100 func (s *IntegrationSuite) TearDownTest(c *check.C) {
101         var err error
102         if s.testServer != nil {
103                 err = s.testServer.Close()
104         }
105         c.Check(err, check.Equals, nil)
106         s.testServer = nil
107
108         if s.tmpRepoRoot != "" {
109                 err = os.RemoveAll(s.tmpRepoRoot)
110                 c.Check(err, check.Equals, nil)
111         }
112         s.tmpRepoRoot = ""
113
114         if s.tmpWorkdir != "" {
115                 err = os.RemoveAll(s.tmpWorkdir)
116                 c.Check(err, check.Equals, nil)
117         }
118         s.tmpWorkdir = ""
119
120         s.cluster = nil
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 }