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)
96 GitCommand: "/usr/bin/git",
99 err = s.testServer.Start()
100 c.Assert(err, check.Equals, nil)
102 // Clear ARVADOS_API_TOKEN after starting up the server, to
103 // make sure arv-git-httpd doesn't use it.
104 os.Setenv("ARVADOS_API_TOKEN", "unused-token-placates-client-library")
107 func (s *IntegrationSuite) TearDownTest(c *check.C) {
109 if s.testServer != nil {
110 err = s.testServer.Close()
112 c.Check(err, check.Equals, nil)
113 if s.tmpRepoRoot != "" {
114 err = os.RemoveAll(s.tmpRepoRoot)
115 c.Check(err, check.Equals, nil)
117 if s.tmpWorkdir != "" {
118 err = os.RemoveAll(s.tmpWorkdir)
119 c.Check(err, check.Equals, nil)
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://none:" + token + "@" + s.testServer.Addr + "/" + repo,
132 cmd := exec.Command("git", gitargs...)
133 w, err := cmd.StdinPipe()
134 c.Assert(err, check.Equals, nil)
136 output, err := cmd.CombinedOutput()
137 c.Log("git ", gitargs, " => ", err)
138 c.Log(string(output))
139 if err != nil && len(output) > 0 {
140 // If messages appeared on stderr, they are more
141 // helpful than the err returned by CombinedOutput().
143 // Easier to match error strings without newlines:
144 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))
149 // Make a bare arvados repo at {tmpRepoRoot}/arvados.git
150 func (s *IntegrationSuite) makeArvadosRepo(c *check.C) {
151 msg, err := exec.Command("git", "init", "--bare", s.tmpRepoRoot+"/zzzzz-s0uqq-arvadosrepo0123.git").CombinedOutput()
153 c.Assert(err, check.Equals, nil)
154 msg, err = exec.Command("git", "--git-dir", s.tmpRepoRoot+"/zzzzz-s0uqq-arvadosrepo0123.git", "fetch", "../../.git", "HEAD:master").CombinedOutput()
156 c.Assert(err, check.Equals, nil)
159 // Gocheck boilerplate
160 func Test(t *testing.T) {