17119: Merge branch 'master' into 17119-add-filter-groups
[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         _, err = exec.Command("git", "init", s.tmpWorkdir).Output()
57         c.Assert(err, check.Equals, nil)
58         _, 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()
59         c.Assert(err, check.Equals, nil)
60         _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && git push "+s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git master:master").CombinedOutput()
61         c.Assert(err, check.Equals, nil)
62         _, 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()
63         c.Assert(err, check.Equals, nil)
64
65         if s.cluster == nil {
66                 cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
67                 c.Assert(err, check.Equals, nil)
68                 s.cluster, err = cfg.GetCluster("")
69                 c.Assert(err, check.Equals, nil)
70
71                 s.cluster.Services.GitHTTP.InternalURLs = map[arvados.URL]arvados.ServiceInstance{{Host: "localhost:0"}: {}}
72                 s.cluster.TLS.Insecure = true
73                 s.cluster.Git.GitCommand = "/usr/bin/git"
74                 s.cluster.Git.Repositories = s.tmpRepoRoot
75                 s.cluster.ManagementToken = arvadostest.ManagementToken
76         }
77
78         s.testServer = &server{cluster: s.cluster}
79         err = s.testServer.Start()
80         c.Assert(err, check.Equals, nil)
81
82         _, err = exec.Command("git", "config",
83                 "--file", s.tmpWorkdir+"/.git/config",
84                 "credential.http://"+s.testServer.Addr+"/.helper",
85                 "!cred(){ cat >/dev/null; if [ \"$1\" = get ]; then echo password=$ARVADOS_API_TOKEN; fi; };cred").Output()
86         c.Assert(err, check.Equals, nil)
87         _, err = exec.Command("git", "config",
88                 "--file", s.tmpWorkdir+"/.git/config",
89                 "credential.http://"+s.testServer.Addr+"/.username",
90                 "none").Output()
91         c.Assert(err, check.Equals, nil)
92
93         // Clear ARVADOS_API_* env vars before starting up the server,
94         // to make sure arv-git-httpd doesn't use them or complain
95         // about them being missing.
96         os.Unsetenv("ARVADOS_API_HOST")
97         os.Unsetenv("ARVADOS_API_HOST_INSECURE")
98         os.Unsetenv("ARVADOS_API_TOKEN")
99 }
100
101 func (s *IntegrationSuite) TearDownTest(c *check.C) {
102         var err error
103         if s.testServer != nil {
104                 err = s.testServer.Close()
105         }
106         c.Check(err, check.Equals, nil)
107         s.testServer = nil
108
109         if s.tmpRepoRoot != "" {
110                 err = os.RemoveAll(s.tmpRepoRoot)
111                 c.Check(err, check.Equals, nil)
112         }
113         s.tmpRepoRoot = ""
114
115         if s.tmpWorkdir != "" {
116                 err = os.RemoveAll(s.tmpWorkdir)
117                 c.Check(err, check.Equals, nil)
118         }
119         s.tmpWorkdir = ""
120
121         s.cluster = nil
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 }