1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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"
22 // Gocheck boilerplate
23 func Test(t *testing.T) {
27 // IntegrationSuite tests need an API server and an arv-git-httpd
28 // server. See GitSuite and GitoliteSuite.
29 type IntegrationSuite struct {
33 cluster *arvados.Cluster
36 func (s *IntegrationSuite) SetUpTest(c *check.C) {
37 arvadostest.ResetEnv()
40 if s.tmpRepoRoot == "" {
41 s.tmpRepoRoot, err = ioutil.TempDir("", "arv-git-httpd")
42 c.Assert(err, check.Equals, nil)
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)
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)
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
73 s.testServer = &server{cluster: s.cluster}
74 err = s.testServer.Start()
75 c.Assert(err, check.Equals, nil)
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",
86 c.Assert(err, check.Equals, nil)
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")
96 func (s *IntegrationSuite) TearDownTest(c *check.C) {
98 if s.testServer != nil {
99 err = s.testServer.Close()
101 c.Check(err, check.Equals, nil)
104 if s.tmpRepoRoot != "" {
105 err = os.RemoveAll(s.tmpRepoRoot)
106 c.Check(err, check.Equals, nil)
110 if s.tmpWorkdir != "" {
111 err = os.RemoveAll(s.tmpWorkdir)
112 c.Check(err, check.Equals, nil)
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)
123 os.Chdir(s.tmpWorkdir)
125 gitargs := append([]string{
126 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
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)
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().
140 // Easier to match error strings without newlines:
141 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))