1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.curoverse.com/arvados.git/lib/config"
16 "git.curoverse.com/arvados.git/sdk/go/arvados"
17 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
18 check "gopkg.in/check.v1"
21 // Gocheck boilerplate
22 func Test(t *testing.T) {
26 // IntegrationSuite tests need an API server and an arv-git-httpd
27 // server. See GitSuite and GitoliteSuite.
28 type IntegrationSuite struct {
32 cluster *arvados.Cluster
35 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
36 arvadostest.StartAPI()
39 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
43 func (s *IntegrationSuite) SetUpTest(c *check.C) {
44 arvadostest.ResetEnv()
47 if s.tmpRepoRoot == "" {
48 s.tmpRepoRoot, err = ioutil.TempDir("", "arv-git-httpd")
49 c.Assert(err, check.Equals, nil)
51 s.tmpWorkdir, err = ioutil.TempDir("", "arv-git-httpd")
52 c.Assert(err, check.Equals, nil)
53 _, err = exec.Command("git", "init", "--bare", s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git").Output()
54 c.Assert(err, check.Equals, nil)
55 _, err = exec.Command("git", "init", s.tmpWorkdir).Output()
56 c.Assert(err, check.Equals, nil)
57 _, 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()
58 c.Assert(err, check.Equals, nil)
59 _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && git push "+s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git master:master").CombinedOutput()
60 c.Assert(err, check.Equals, nil)
61 _, 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()
62 c.Assert(err, check.Equals, nil)
64 _, err = exec.Command("git", "config",
65 "--file", s.tmpWorkdir+"/.git/config",
66 "credential.http://"+s.testServer.Addr+"/.helper",
67 "!cred(){ cat >/dev/null; if [ \"$1\" = get ]; then echo password=$ARVADOS_API_TOKEN; fi; };cred").Output()
68 c.Assert(err, check.Equals, nil)
69 _, err = exec.Command("git", "config",
70 "--file", s.tmpWorkdir+"/.git/config",
71 "credential.http://"+s.testServer.Addr+"/.username",
73 c.Assert(err, check.Equals, nil)
75 // Clear ARVADOS_API_* env vars before starting up the server,
76 // to make sure arv-git-httpd doesn't use them or complain
77 // about them being missing.
78 os.Unsetenv("ARVADOS_API_HOST")
79 os.Unsetenv("ARVADOS_API_HOST_INSECURE")
80 os.Unsetenv("ARVADOS_API_TOKEN")
82 cfg, err := config.NewLoader(nil, nil).Load()
83 c.Assert(err, check.Equals, nil)
84 s.cluster, err = cfg.GetCluster("")
85 c.Assert(err, check.Equals, nil)
88 s.cluster.Services.GitHTTP.InternalURLs = map[arvados.URL]arvados.ServiceInstance{arvados.URL{Host: "localhost:0"}: arvados.ServiceInstance{}}
89 s.cluster.TLS.Insecure = true
90 s.cluster.Git.GitCommand = "/usr/bin/git"
91 s.cluster.Git.Repositories = s.tmpRepoRoot
94 println(s.cluster.Services.Controller.InternalURLs)
95 println(arvadostest.APIHost())
96 println(s.cluster.ManagementToken)
97 println(arvadostest.ManagementToken)
99 s.testServer = &server{cluster: s.cluster}
100 err = s.testServer.Start()
101 c.Assert(err, check.Equals, nil)
104 func (s *IntegrationSuite) TearDownTest(c *check.C) {
106 if s.testServer != nil {
107 err = s.testServer.Close()
109 c.Check(err, check.Equals, nil)
112 if s.tmpRepoRoot != "" {
113 err = os.RemoveAll(s.tmpRepoRoot)
114 c.Check(err, check.Equals, nil)
118 if s.tmpWorkdir != "" {
119 err = os.RemoveAll(s.tmpWorkdir)
120 c.Check(err, check.Equals, nil)
127 func (s *IntegrationSuite) RunGit(c *check.C, token, gitCmd, repo string, args ...string) error {
128 cwd, err := os.Getwd()
129 c.Assert(err, check.Equals, nil)
131 os.Chdir(s.tmpWorkdir)
133 gitargs := append([]string{
134 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
136 cmd := exec.Command("git", gitargs...)
137 cmd.Env = append(os.Environ(), "ARVADOS_API_TOKEN="+token)
138 w, err := cmd.StdinPipe()
139 c.Assert(err, check.Equals, nil)
141 output, err := cmd.CombinedOutput()
142 c.Log("git ", gitargs, " => ", err)
143 c.Log(string(output))
144 if err != nil && len(output) > 0 {
145 // If messages appeared on stderr, they are more
146 // helpful than the err returned by CombinedOutput().
148 // Easier to match error strings without newlines:
149 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))