11 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
12 check "gopkg.in/check.v1"
15 var _ = check.Suite(&IntegrationSuite{})
18 spectatorToken = "zw2f4gwx8hw8cjre7yp6v1zylhrhn3m5gvjq73rtpwhmknrybu"
19 activeToken = "3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi"
20 anonymousToken = "4kg6k6lzmp9kj4cpkcoxie964cmvjahbt4fod9zru44k4jqdmi"
23 // IntegrationSuite tests need an API server and an arv-git-httpd server
24 type IntegrationSuite struct {
30 func (s *IntegrationSuite) TestPathVariants(c *check.C) {
32 for _, repo := range []string{"active/foo.git", "active/foo/.git", "arvados.git", "arvados/.git"} {
33 err := s.runGit(c, spectatorToken, "fetch", repo)
34 c.Assert(err, check.Equals, nil)
38 func (s *IntegrationSuite) TestReadonly(c *check.C) {
39 err := s.runGit(c, spectatorToken, "fetch", "active/foo.git")
40 c.Assert(err, check.Equals, nil)
41 err = s.runGit(c, spectatorToken, "push", "active/foo.git", "master:newbranchfail")
42 c.Assert(err, check.ErrorMatches, `.*HTTP code = 403.*`)
43 _, err = os.Stat(s.tmpRepoRoot + "/zzzzz-s0uqq-382brsig8rp3666/.git/refs/heads/newbranchfail")
44 c.Assert(err, check.FitsTypeOf, &os.PathError{})
47 func (s *IntegrationSuite) TestReadwrite(c *check.C) {
48 err := s.runGit(c, activeToken, "fetch", "active/foo.git")
49 c.Assert(err, check.Equals, nil)
50 err = s.runGit(c, activeToken, "push", "active/foo.git", "master:newbranch")
51 c.Assert(err, check.Equals, nil)
52 _, err = os.Stat(s.tmpRepoRoot + "/zzzzz-s0uqq-382brsig8rp3666/.git/refs/heads/newbranch")
53 c.Assert(err, check.Equals, nil)
56 func (s *IntegrationSuite) TestNonexistent(c *check.C) {
57 err := s.runGit(c, spectatorToken, "fetch", "thisrepodoesnotexist.git")
58 c.Assert(err, check.ErrorMatches, `.* not found.*`)
61 func (s *IntegrationSuite) TestMissingGitdirReadableRepository(c *check.C) {
62 err := s.runGit(c, activeToken, "fetch", "active/foo2.git")
63 c.Assert(err, check.ErrorMatches, `.* not found.*`)
66 func (s *IntegrationSuite) TestNoPermission(c *check.C) {
67 for _, repo := range []string{"active/foo.git", "active/foo/.git"} {
68 err := s.runGit(c, anonymousToken, "fetch", repo)
69 c.Assert(err, check.ErrorMatches, `.* not found.*`)
73 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
74 arvadostest.StartAPI()
77 func (s *IntegrationSuite) SetUpTest(c *check.C) {
78 arvadostest.ResetEnv()
79 s.testServer = &server{}
81 s.tmpRepoRoot, err = ioutil.TempDir("", "arv-git-httpd")
82 c.Assert(err, check.Equals, nil)
83 s.tmpWorkdir, err = ioutil.TempDir("", "arv-git-httpd")
84 c.Assert(err, check.Equals, nil)
85 _, err = exec.Command("git", "init", s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666").Output()
86 c.Assert(err, check.Equals, nil)
87 _, err = exec.Command("sh", "-c", "cd "+s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666 && echo test >test && git add test && git -c user.name=Foo -c user.email=Foo commit -am 'foo: test'").CombinedOutput()
88 c.Assert(err, check.Equals, nil)
89 _, err = exec.Command("git", "init", s.tmpWorkdir).Output()
90 c.Assert(err, check.Equals, nil)
91 _, 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()
92 c.Assert(err, check.Equals, nil)
94 _, err = exec.Command("git", "config",
95 "--file", s.tmpWorkdir+"/.git/config",
96 "credential.http://"+s.testServer.Addr+"/.helper",
97 "!cred(){ cat >/dev/null; if [ \"$1\" = get ]; then echo password=$ARVADOS_API_TOKEN; fi; };cred").Output()
98 c.Assert(err, check.Equals, nil)
99 _, err = exec.Command("git", "config",
100 "--file", s.tmpWorkdir+"/.git/config",
101 "credential.http://"+s.testServer.Addr+"/.username",
103 c.Assert(err, check.Equals, nil)
107 GitCommand: "/usr/bin/git",
110 err = s.testServer.Start()
111 c.Assert(err, check.Equals, nil)
113 // Clear ARVADOS_API_TOKEN after starting up the server, to
114 // make sure arv-git-httpd doesn't use it.
115 os.Setenv("ARVADOS_API_TOKEN", "unused-token-placates-client-library")
118 func (s *IntegrationSuite) TearDownTest(c *check.C) {
120 if s.testServer != nil {
121 err = s.testServer.Close()
123 c.Check(err, check.Equals, nil)
124 if s.tmpRepoRoot != "" {
125 err = os.RemoveAll(s.tmpRepoRoot)
126 c.Check(err, check.Equals, nil)
128 if s.tmpWorkdir != "" {
129 err = os.RemoveAll(s.tmpWorkdir)
130 c.Check(err, check.Equals, nil)
134 func (s *IntegrationSuite) runGit(c *check.C, token, gitCmd, repo string, args ...string) error {
135 cwd, err := os.Getwd()
136 c.Assert(err, check.Equals, nil)
138 os.Chdir(s.tmpWorkdir)
140 gitargs := append([]string{
141 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
143 cmd := exec.Command("git", gitargs...)
144 cmd.Env = append(os.Environ(), "ARVADOS_API_TOKEN="+token)
145 w, err := cmd.StdinPipe()
146 c.Assert(err, check.Equals, nil)
148 output, err := cmd.CombinedOutput()
149 c.Log("git ", gitargs, " => ", err)
150 c.Log(string(output))
151 if err != nil && len(output) > 0 {
152 // If messages appeared on stderr, they are more
153 // helpful than the err returned by CombinedOutput().
155 // Easier to match error strings without newlines:
156 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))
161 // Make a bare arvados repo at {tmpRepoRoot}/arvados.git
162 func (s *IntegrationSuite) makeArvadosRepo(c *check.C) {
163 msg, err := exec.Command("git", "init", "--bare", s.tmpRepoRoot+"/zzzzz-s0uqq-arvadosrepo0123.git").CombinedOutput()
165 c.Assert(err, check.Equals, nil)
166 msg, err = exec.Command("git", "--git-dir", s.tmpRepoRoot+"/zzzzz-s0uqq-arvadosrepo0123.git", "fetch", "../../.git", "HEAD:master").CombinedOutput()
168 c.Assert(err, check.Equals, nil)
171 // Gocheck boilerplate
172 func Test(t *testing.T) {