1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 "git.arvados.org/arvados.git/lib/config"
17 "git.arvados.org/arvados.git/sdk/go/arvados"
18 "git.arvados.org/arvados.git/sdk/go/arvadostest"
19 "git.arvados.org/arvados.git/sdk/go/ctxlog"
20 "git.arvados.org/arvados.git/sdk/go/httpserver"
21 check "gopkg.in/check.v1"
24 // Gocheck boilerplate
25 func Test(t *testing.T) {
29 // IntegrationSuite tests need an API server and an arvados-git-httpd
30 // server. See GitSuite and GitoliteSuite.
31 type IntegrationSuite struct {
34 testServer *httpserver.Server
35 cluster *arvados.Cluster
38 func (s *IntegrationSuite) SetUpTest(c *check.C) {
39 arvadostest.ResetEnv()
42 if s.tmpRepoRoot == "" {
43 s.tmpRepoRoot, err = ioutil.TempDir("", "githttp")
44 c.Assert(err, check.Equals, nil)
46 s.tmpWorkdir, err = ioutil.TempDir("", "githttp")
47 c.Assert(err, check.Equals, nil)
48 _, err = exec.Command("git", "init", "--bare", s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git").Output()
49 c.Assert(err, check.Equals, nil)
50 // we need git 2.28 to specify the initial branch with -b; Buster only has 2.20; so we do it in 2 steps
51 _, err = exec.Command("git", "init", s.tmpWorkdir).Output()
52 c.Assert(err, check.Equals, nil)
53 _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && git checkout -b main").Output()
54 c.Assert(err, check.Equals, nil)
55 _, 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()
56 c.Assert(err, check.Equals, nil)
57 _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && git push "+s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git main:main").CombinedOutput()
58 c.Assert(err, check.Equals, nil)
59 _, 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()
60 c.Assert(err, check.Equals, nil)
63 cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
64 c.Assert(err, check.Equals, nil)
65 s.cluster, err = cfg.GetCluster("")
66 c.Assert(err, check.Equals, nil)
68 s.cluster.Services.GitHTTP.InternalURLs = map[arvados.URL]arvados.ServiceInstance{{Host: "localhost:0"}: {}}
69 s.cluster.TLS.Insecure = true
70 s.cluster.Git.GitCommand = "/usr/bin/git"
71 s.cluster.Git.Repositories = s.tmpRepoRoot
72 s.cluster.ManagementToken = arvadostest.ManagementToken
75 s.testServer = &httpserver.Server{}
76 s.testServer.Handler = httpserver.LogRequests(newHandler(context.Background(), s.cluster, "", nil))
77 err = s.testServer.Start()
78 c.Assert(err, check.Equals, nil)
80 _, err = exec.Command("git", "config",
81 "--file", s.tmpWorkdir+"/.git/config",
82 "credential.http://"+s.testServer.Addr+"/.helper",
83 "!cred(){ cat >/dev/null; if [ \"$1\" = get ]; then echo password=$ARVADOS_API_TOKEN; fi; };cred").Output()
84 c.Assert(err, check.Equals, nil)
85 _, err = exec.Command("git", "config",
86 "--file", s.tmpWorkdir+"/.git/config",
87 "credential.http://"+s.testServer.Addr+"/.username",
89 c.Assert(err, check.Equals, nil)
91 // Clear ARVADOS_API_* env vars before starting up the server,
92 // to make sure arvados-git-httpd doesn't use them or complain
93 // about them being missing.
94 os.Unsetenv("ARVADOS_API_HOST")
95 os.Unsetenv("ARVADOS_API_HOST_INSECURE")
96 os.Unsetenv("ARVADOS_API_TOKEN")
99 func (s *IntegrationSuite) TearDownTest(c *check.C) {
101 if s.testServer != nil {
102 err = s.testServer.Close()
104 c.Check(err, check.Equals, nil)
107 if s.tmpRepoRoot != "" {
108 err = os.RemoveAll(s.tmpRepoRoot)
109 c.Check(err, check.Equals, nil)
113 if s.tmpWorkdir != "" {
114 err = os.RemoveAll(s.tmpWorkdir)
115 c.Check(err, check.Equals, nil)
122 func (s *IntegrationSuite) RunGit(c *check.C, token, gitCmd, repo string, args ...string) error {
123 cwd, err := os.Getwd()
124 c.Assert(err, check.Equals, nil)
126 os.Chdir(s.tmpWorkdir)
128 gitargs := append([]string{
129 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
131 cmd := exec.Command("git", gitargs...)
132 cmd.Env = append(os.Environ(), "ARVADOS_API_TOKEN="+token)
133 w, err := cmd.StdinPipe()
134 c.Assert(err, check.Equals, nil)
136 output, err := cmd.CombinedOutput()
137 c.Log("git ", gitargs, " => ", err)
138 c.Log(string(output))
139 if err != nil && len(output) > 0 {
140 // If messages appeared on stderr, they are more
141 // helpful than the err returned by CombinedOutput().
143 // Easier to match error strings without newlines:
144 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))