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(),
81 GitCommand: "/usr/bin/git",
82 RepoRoot: s.tmpRepoRoot,
86 // Clear ARVADOS_API_* env vars before starting up the server,
87 // to make sure arv-git-httpd doesn't use them or complain
88 // about them being missing.
89 os.Unsetenv("ARVADOS_API_HOST")
90 os.Unsetenv("ARVADOS_API_HOST_INSECURE")
91 os.Unsetenv("ARVADOS_API_TOKEN")
94 err = s.testServer.Start()
95 c.Assert(err, check.Equals, nil)
98 func (s *IntegrationSuite) TearDownTest(c *check.C) {
100 if s.testServer != nil {
101 err = s.testServer.Close()
103 c.Check(err, check.Equals, nil)
106 if s.tmpRepoRoot != "" {
107 err = os.RemoveAll(s.tmpRepoRoot)
108 c.Check(err, check.Equals, nil)
112 if s.tmpWorkdir != "" {
113 err = os.RemoveAll(s.tmpWorkdir)
114 c.Check(err, check.Equals, nil)
120 theConfig = defaultConfig()
123 func (s *IntegrationSuite) RunGit(c *check.C, token, gitCmd, repo string, args ...string) error {
124 cwd, err := os.Getwd()
125 c.Assert(err, check.Equals, nil)
127 os.Chdir(s.tmpWorkdir)
129 gitargs := append([]string{
130 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
132 cmd := exec.Command("git", gitargs...)
133 cmd.Env = append(os.Environ(), "ARVADOS_API_TOKEN="+token)
134 w, err := cmd.StdinPipe()
135 c.Assert(err, check.Equals, nil)
137 output, err := cmd.CombinedOutput()
138 c.Log("git ", gitargs, " => ", err)
139 c.Log(string(output))
140 if err != nil && len(output) > 0 {
141 // If messages appeared on stderr, they are more
142 // helpful than the err returned by CombinedOutput().
144 // Easier to match error strings without newlines:
145 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))