9950: Propagate Client.Insecure config to arvadosclient library via ARVADOS_API_HOST_...
[arvados.git] / services / arv-git-httpd / integration_test.go
1 package main
2
3 import (
4         "errors"
5         "io/ioutil"
6         "os"
7         "os/exec"
8         "strings"
9         "testing"
10
11         "git.curoverse.com/arvados.git/sdk/go/arvados"
12         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
13         check "gopkg.in/check.v1"
14 )
15
16 // Gocheck boilerplate
17 func Test(t *testing.T) {
18         check.TestingT(t)
19 }
20
21 // IntegrationSuite tests need an API server and an arv-git-httpd
22 // server. See GitSuite and GitoliteSuite.
23 type IntegrationSuite struct {
24         tmpRepoRoot string
25         tmpWorkdir  string
26         testServer  *server
27         Config      *Config
28 }
29
30 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
31         arvadostest.StartAPI()
32 }
33
34 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
35         arvadostest.StopAPI()
36 }
37
38 func (s *IntegrationSuite) SetUpTest(c *check.C) {
39         arvadostest.ResetEnv()
40         s.testServer = &server{}
41         var err error
42         if s.tmpRepoRoot == "" {
43                 s.tmpRepoRoot, err = ioutil.TempDir("", "arv-git-httpd")
44                 c.Assert(err, check.Equals, nil)
45         }
46         s.tmpWorkdir, err = ioutil.TempDir("", "arv-git-httpd")
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         _, err = exec.Command("git", "init", s.tmpWorkdir).Output()
51         c.Assert(err, check.Equals, nil)
52         _, 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()
53         c.Assert(err, check.Equals, nil)
54         _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && git push "+s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666.git master:master").CombinedOutput()
55         c.Assert(err, check.Equals, nil)
56         _, 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()
57         c.Assert(err, check.Equals, nil)
58
59         _, err = exec.Command("git", "config",
60                 "--file", s.tmpWorkdir+"/.git/config",
61                 "credential.http://"+s.testServer.Addr+"/.helper",
62                 "!cred(){ cat >/dev/null; if [ \"$1\" = get ]; then echo password=$ARVADOS_API_TOKEN; fi; };cred").Output()
63         c.Assert(err, check.Equals, nil)
64         _, err = exec.Command("git", "config",
65                 "--file", s.tmpWorkdir+"/.git/config",
66                 "credential.http://"+s.testServer.Addr+"/.username",
67                 "none").Output()
68         c.Assert(err, check.Equals, nil)
69
70         if s.Config == nil {
71                 s.Config = &Config{
72                         Client: arvados.Client{
73                                 APIHost:  os.Getenv("ARVADOS_API_HOST"),
74                                 Insecure: true,
75                         },
76                         Listen:     ":0",
77                         GitCommand: "/usr/bin/git",
78                         RepoRoot:   s.tmpRepoRoot,
79                 }
80         }
81         theConfig = s.Config
82         err = s.testServer.Start()
83         c.Assert(err, check.Equals, nil)
84
85         // Clear ARVADOS_API_TOKEN after starting up the server, to
86         // make sure arv-git-httpd doesn't use it.
87         os.Setenv("ARVADOS_API_TOKEN", "unused-token-placates-client-library")
88 }
89
90 func (s *IntegrationSuite) TearDownTest(c *check.C) {
91         var err error
92         if s.testServer != nil {
93                 err = s.testServer.Close()
94         }
95         c.Check(err, check.Equals, nil)
96         s.testServer = nil
97
98         if s.tmpRepoRoot != "" {
99                 err = os.RemoveAll(s.tmpRepoRoot)
100                 c.Check(err, check.Equals, nil)
101         }
102         s.tmpRepoRoot = ""
103
104         if s.tmpWorkdir != "" {
105                 err = os.RemoveAll(s.tmpWorkdir)
106                 c.Check(err, check.Equals, nil)
107         }
108         s.tmpWorkdir = ""
109
110         s.Config = nil
111 }
112
113 func (s *IntegrationSuite) RunGit(c *check.C, token, gitCmd, repo string, args ...string) error {
114         cwd, err := os.Getwd()
115         c.Assert(err, check.Equals, nil)
116         defer os.Chdir(cwd)
117         os.Chdir(s.tmpWorkdir)
118
119         gitargs := append([]string{
120                 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
121         }, args...)
122         cmd := exec.Command("git", gitargs...)
123         cmd.Env = append(os.Environ(), "ARVADOS_API_TOKEN="+token)
124         w, err := cmd.StdinPipe()
125         c.Assert(err, check.Equals, nil)
126         w.Close()
127         output, err := cmd.CombinedOutput()
128         c.Log("git ", gitargs, " => ", err)
129         c.Log(string(output))
130         if err != nil && len(output) > 0 {
131                 // If messages appeared on stderr, they are more
132                 // helpful than the err returned by CombinedOutput().
133                 //
134                 // Easier to match error strings without newlines:
135                 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))
136         }
137         return err
138 }