1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.curoverse.com/arvados.git/sdk/go/arvados"
16 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
17 check "gopkg.in/check.v1"
20 // Gocheck boilerplate
21 func Test(t *testing.T) {
25 // IntegrationSuite tests need an API server and an arv-git-httpd
26 // server. See GitSuite and GitoliteSuite.
27 type IntegrationSuite struct {
34 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
35 arvadostest.StartAPI()
38 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
42 func (s *IntegrationSuite) SetUpTest(c *check.C) {
43 arvadostest.ResetEnv()
44 s.testServer = &server{}
46 if s.tmpRepoRoot == "" {
47 s.tmpRepoRoot, err = ioutil.TempDir("", "arv-git-httpd")
48 c.Assert(err, check.Equals, nil)
50 s.tmpWorkdir, err = ioutil.TempDir("", "arv-git-httpd")
51 c.Assert(err, check.Equals, nil)
52 _, err = exec.Command("git", "init", "--bare", s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git").Output()
53 c.Assert(err, check.Equals, nil)
54 _, err = exec.Command("git", "init", s.tmpWorkdir).Output()
55 c.Assert(err, check.Equals, nil)
56 _, 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()
57 c.Assert(err, check.Equals, nil)
58 _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && git push "+s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git master:master").CombinedOutput()
59 c.Assert(err, check.Equals, nil)
60 _, 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()
61 c.Assert(err, check.Equals, nil)
63 _, err = exec.Command("git", "config",
64 "--file", s.tmpWorkdir+"/.git/config",
65 "credential.http://"+s.testServer.Addr+"/.helper",
66 "!cred(){ cat >/dev/null; if [ \"$1\" = get ]; then echo password=$ARVADOS_API_TOKEN; fi; };cred").Output()
67 c.Assert(err, check.Equals, nil)
68 _, err = exec.Command("git", "config",
69 "--file", s.tmpWorkdir+"/.git/config",
70 "credential.http://"+s.testServer.Addr+"/.username",
72 c.Assert(err, check.Equals, nil)
76 Client: arvados.Client{
77 APIHost: arvadostest.APIHost(),
80 Listen: "localhost:0",
81 GitCommand: "/usr/bin/git",
82 RepoRoot: s.tmpRepoRoot,
83 ManagementToken: arvadostest.ManagementToken,
87 // Clear ARVADOS_API_* env vars before starting up the server,
88 // to make sure arv-git-httpd doesn't use them or complain
89 // about them being missing.
90 os.Unsetenv("ARVADOS_API_HOST")
91 os.Unsetenv("ARVADOS_API_HOST_INSECURE")
92 os.Unsetenv("ARVADOS_API_TOKEN")
95 err = s.testServer.Start()
96 c.Assert(err, check.Equals, nil)
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)
121 theConfig = defaultConfig()
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)
128 os.Chdir(s.tmpWorkdir)
130 gitargs := append([]string{
131 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
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)
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().
145 // Easier to match error strings without newlines:
146 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))