Merge branch '18696-rnaseq-training' refs #18696
[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) SetUpTest(c *check.C) {
37         arvadostest.ResetEnv()
38
39         var err error
40         if s.tmpRepoRoot == "" {
41                 s.tmpRepoRoot, err = ioutil.TempDir("", "arv-git-httpd")
42                 c.Assert(err, check.Equals, nil)
43         }
44         s.tmpWorkdir, err = ioutil.TempDir("", "arv-git-httpd")
45         c.Assert(err, check.Equals, nil)
46         _, err = exec.Command("git", "init", "--bare", s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git").Output()
47         c.Assert(err, check.Equals, nil)
48         // we need git 2.28 to specify the initial branch with -b; Buster only has 2.20; so we do it in 2 steps
49         _, err = exec.Command("git", "init", s.tmpWorkdir).Output()
50         c.Assert(err, check.Equals, nil)
51         _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && git checkout -b main").Output()
52         c.Assert(err, check.Equals, nil)
53         _, 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()
54         c.Assert(err, check.Equals, nil)
55         _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && git push "+s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git main:main").CombinedOutput()
56         c.Assert(err, check.Equals, nil)
57         _, 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()
58         c.Assert(err, check.Equals, nil)
59
60         if s.cluster == nil {
61                 cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
62                 c.Assert(err, check.Equals, nil)
63                 s.cluster, err = cfg.GetCluster("")
64                 c.Assert(err, check.Equals, nil)
65
66                 s.cluster.Services.GitHTTP.InternalURLs = map[arvados.URL]arvados.ServiceInstance{{Host: "localhost:0"}: {}}
67                 s.cluster.TLS.Insecure = true
68                 s.cluster.Git.GitCommand = "/usr/bin/git"
69                 s.cluster.Git.Repositories = s.tmpRepoRoot
70                 s.cluster.ManagementToken = arvadostest.ManagementToken
71         }
72
73         s.testServer = &server{cluster: s.cluster}
74         err = s.testServer.Start()
75         c.Assert(err, check.Equals, nil)
76
77         _, err = exec.Command("git", "config",
78                 "--file", s.tmpWorkdir+"/.git/config",
79                 "credential.http://"+s.testServer.Addr+"/.helper",
80                 "!cred(){ cat >/dev/null; if [ \"$1\" = get ]; then echo password=$ARVADOS_API_TOKEN; fi; };cred").Output()
81         c.Assert(err, check.Equals, nil)
82         _, err = exec.Command("git", "config",
83                 "--file", s.tmpWorkdir+"/.git/config",
84                 "credential.http://"+s.testServer.Addr+"/.username",
85                 "none").Output()
86         c.Assert(err, check.Equals, nil)
87
88         // Clear ARVADOS_API_* env vars before starting up the server,
89         // to make sure arv-git-httpd doesn't use them or complain
90         // about them being missing.
91         os.Unsetenv("ARVADOS_API_HOST")
92         os.Unsetenv("ARVADOS_API_HOST_INSECURE")
93         os.Unsetenv("ARVADOS_API_TOKEN")
94 }
95
96 func (s *IntegrationSuite) TearDownTest(c *check.C) {
97         var err error
98         if s.testServer != nil {
99                 err = s.testServer.Close()
100         }
101         c.Check(err, check.Equals, nil)
102         s.testServer = nil
103
104         if s.tmpRepoRoot != "" {
105                 err = os.RemoveAll(s.tmpRepoRoot)
106                 c.Check(err, check.Equals, nil)
107         }
108         s.tmpRepoRoot = ""
109
110         if s.tmpWorkdir != "" {
111                 err = os.RemoveAll(s.tmpWorkdir)
112                 c.Check(err, check.Equals, nil)
113         }
114         s.tmpWorkdir = ""
115
116         s.cluster = nil
117 }
118
119 func (s *IntegrationSuite) RunGit(c *check.C, token, gitCmd, repo string, args ...string) error {
120         cwd, err := os.Getwd()
121         c.Assert(err, check.Equals, nil)
122         defer os.Chdir(cwd)
123         os.Chdir(s.tmpWorkdir)
124
125         gitargs := append([]string{
126                 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
127         }, args...)
128         cmd := exec.Command("git", gitargs...)
129         cmd.Env = append(os.Environ(), "ARVADOS_API_TOKEN="+token)
130         w, err := cmd.StdinPipe()
131         c.Assert(err, check.Equals, nil)
132         w.Close()
133         output, err := cmd.CombinedOutput()
134         c.Log("git ", gitargs, " => ", err)
135         c.Log(string(output))
136         if err != nil && len(output) > 0 {
137                 // If messages appeared on stderr, they are more
138                 // helpful than the err returned by CombinedOutput().
139                 //
140                 // Easier to match error strings without newlines:
141                 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))
142         }
143         return err
144 }