11 check "gopkg.in/check.v1"
12 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
15 var _ = check.Suite(&IntegrationSuite{})
17 // IntegrationSuite tests need an API server and an arv-git-httpd server
18 type IntegrationSuite struct {
24 func (s *IntegrationSuite) TestReadonly(c *check.C) {
26 os.Setenv("ARVADOS_API_TOKEN", "zw2f4gwx8hw8cjre7yp6v1zylhrhn3m5gvjq73rtpwhmknrybu")
27 err := s.runGit(c, "fetch", "foo.git")
28 c.Assert(err, check.Equals, nil)
29 err = s.runGit(c, "push", "foo.git", "master:newbranchfail")
30 c.Assert(err, check.ErrorMatches, `.*HTTP code = 403.*`)
31 _, err = os.Stat(s.tmpRepoRoot + "/.git/refs/heads/newbranchfail")
32 c.Assert(err, check.FitsTypeOf, &os.PathError{})
35 func (s *IntegrationSuite) TestReadwrite(c *check.C) {
37 os.Setenv("ARVADOS_API_TOKEN", "3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi")
38 err := s.runGit(c, "fetch", "foo.git")
39 c.Assert(err, check.Equals, nil)
40 err = s.runGit(c, "push", "foo.git", "master:newbranch")
41 c.Assert(err, check.Equals, nil)
42 _, err = os.Stat(s.tmpRepoRoot + "/foo/.git/refs/heads/newbranch")
43 c.Assert(err, check.Equals, nil)
46 func (s *IntegrationSuite) TestNonexistent(c *check.C) {
48 os.Setenv("ARVADOS_API_TOKEN", "zw2f4gwx8hw8cjre7yp6v1zylhrhn3m5gvjq73rtpwhmknrybu")
49 err := s.runGit(c, "fetch", "thisrepodoesnotexist.git")
50 c.Assert(err, check.ErrorMatches, `.* not found:.*`)
53 func (s *IntegrationSuite) TestNoPermission(c *check.C) {
55 os.Setenv("ARVADOS_API_TOKEN", "4kg6k6lzmp9kj4cpkcoxie964cmvjahbt4fod9zru44k4jqdmi")
56 for _, repo := range []string{"foo.git", "foo/.git", "foo/bar.git", "foo/bar/.git"} {
57 err := s.runGit(c, "fetch", repo)
58 c.Assert(err, check.ErrorMatches, `.* not found:.*`)
62 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
63 arvadostest.StartAPI()
66 func (s *IntegrationSuite) SetUpTest(c *check.C) {
67 arvadostest.ResetEnv()
68 s.testServer = &server{}
70 s.tmpRepoRoot, err = ioutil.TempDir("", "arv-git-httpd")
71 c.Assert(err, check.Equals, nil)
72 s.tmpWorkdir, err = ioutil.TempDir("", "arv-git-httpd")
73 c.Assert(err, check.Equals, nil)
74 _, err = exec.Command("git", "init", "--bare", s.tmpRepoRoot + "/arvados.git").Output()
75 c.Assert(err, check.Equals, nil)
76 _, err = exec.Command("git", "--git-dir", s.tmpRepoRoot + "/arvados.git", "fetch", "../../.git", "master:master").Output()
77 c.Assert(err, check.Equals, nil)
78 _, err = exec.Command("git", "init", s.tmpRepoRoot + "/foo").Output()
79 c.Assert(err, check.Equals, nil)
80 _, err = exec.Command("sh", "-c", "cd " + s.tmpRepoRoot + "/foo && echo test >test && git add test && git commit -am 'foo: test'").CombinedOutput()
81 c.Assert(err, check.Equals, nil)
82 _, err = exec.Command("git", "init", s.tmpWorkdir).Output()
83 c.Assert(err, check.Equals, nil)
84 _, err = exec.Command("sh", "-c", "cd " + s.tmpWorkdir + " && echo work >work && git add work && git commit -am 'workdir: test'").CombinedOutput()
85 c.Assert(err, check.Equals, nil)
89 GitCommand: "/usr/bin/git",
92 err = s.testServer.Start()
93 c.Assert(err, check.Equals, nil)
95 // Clear ARVADOS_API_TOKEN after starting up the server, to
96 // make sure arv-git-httpd doesn't use it.
97 os.Setenv("ARVADOS_API_TOKEN", "")
99 _, err = exec.Command("git", "config",
100 "--file", s.tmpWorkdir + "/.git/config",
101 "credential.http://" + s.testServer.Addr + "/.helper",
102 "!foo(){ echo password=$ARVADOS_API_TOKEN; };foo").Output()
103 c.Assert(err, check.Equals, nil)
104 _, err = exec.Command("git", "config",
105 "--file", s.tmpWorkdir + "/.git/config",
106 "credential.http://" + s.testServer.Addr + "/.username",
108 c.Assert(err, check.Equals, nil)
111 func (s *IntegrationSuite) TearDownTest(c *check.C) {
113 if s.testServer != nil {
114 err = s.testServer.Close()
116 c.Check(err, check.Equals, nil)
117 if s.tmpRepoRoot != "" {
118 err = os.RemoveAll(s.tmpRepoRoot)
119 c.Check(err, check.Equals, nil)
121 if s.tmpWorkdir != "" {
122 err = os.RemoveAll(s.tmpWorkdir)
123 c.Check(err, check.Equals, nil)
127 func (s *IntegrationSuite) runGit(c *check.C, 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 w, err := cmd.StdinPipe()
138 c.Assert(err, check.Equals, nil)
140 output, err := cmd.CombinedOutput()
141 c.Log("git ", gitargs, " => ", err)
143 // Easier to match error strings without newlines.
144 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))
149 // Gocheck boilerplate
150 func Test(t *testing.T) {