11 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
12 check "gopkg.in/check.v1"
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) TestPathVariants(c *check.C) {
27 os.Setenv("ARVADOS_API_TOKEN", "zw2f4gwx8hw8cjre7yp6v1zylhrhn3m5gvjq73rtpwhmknrybu")
28 for _, repo := range []string{"active/foo.git", "active/foo/.git", "arvados.git", "arvados/.git"} {
29 err := s.runGit(c, "fetch", repo)
30 c.Assert(err, check.Equals, nil)
34 func (s *IntegrationSuite) TestReadonly(c *check.C) {
36 os.Setenv("ARVADOS_API_TOKEN", "zw2f4gwx8hw8cjre7yp6v1zylhrhn3m5gvjq73rtpwhmknrybu")
37 err := s.runGit(c, "fetch", "active/foo.git")
38 c.Assert(err, check.Equals, nil)
39 err = s.runGit(c, "push", "active/foo.git", "master:newbranchfail")
40 c.Assert(err, check.ErrorMatches, `.*HTTP code = 403.*`)
41 _, err = os.Stat(s.tmpRepoRoot + "/zzzzz-s0uqq-382brsig8rp3666/.git/refs/heads/newbranchfail")
42 c.Assert(err, check.FitsTypeOf, &os.PathError{})
45 func (s *IntegrationSuite) TestReadwrite(c *check.C) {
47 os.Setenv("ARVADOS_API_TOKEN", "3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi")
48 err := s.runGit(c, "fetch", "active/foo.git")
49 c.Assert(err, check.Equals, nil)
50 err = s.runGit(c, "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) {
58 os.Setenv("ARVADOS_API_TOKEN", "zw2f4gwx8hw8cjre7yp6v1zylhrhn3m5gvjq73rtpwhmknrybu")
59 err := s.runGit(c, "fetch", "thisrepodoesnotexist.git")
60 c.Assert(err, check.ErrorMatches, `.* not found.*`)
63 func (s *IntegrationSuite) TestMissingGitdirReadableRepository(c *check.C) {
65 os.Setenv("ARVADOS_API_TOKEN", "3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi")
66 err := s.runGit(c, "fetch", "active/foo2.git")
67 c.Assert(err, check.ErrorMatches, `.* not found.*`)
70 func (s *IntegrationSuite) TestNoPermission(c *check.C) {
72 os.Setenv("ARVADOS_API_TOKEN", "4kg6k6lzmp9kj4cpkcoxie964cmvjahbt4fod9zru44k4jqdmi")
73 for _, repo := range []string{"active/foo.git", "active/foo/.git"} {
74 err := s.runGit(c, "fetch", repo)
75 c.Assert(err, check.ErrorMatches, `.* not found.*`)
79 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
80 arvadostest.StartAPI()
83 func (s *IntegrationSuite) SetUpTest(c *check.C) {
84 arvadostest.ResetEnv()
85 s.testServer = &server{}
87 s.tmpRepoRoot, err = ioutil.TempDir("", "arv-git-httpd")
88 c.Assert(err, check.Equals, nil)
89 s.tmpWorkdir, err = ioutil.TempDir("", "arv-git-httpd")
90 c.Assert(err, check.Equals, nil)
91 _, err = exec.Command("git", "init", s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666").Output()
92 c.Assert(err, check.Equals, nil)
93 _, 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()
94 c.Assert(err, check.Equals, nil)
95 _, err = exec.Command("git", "init", s.tmpWorkdir).Output()
96 c.Assert(err, check.Equals, nil)
97 _, 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()
98 c.Assert(err, check.Equals, nil)
102 GitCommand: "/usr/bin/git",
105 err = s.testServer.Start()
106 c.Assert(err, check.Equals, nil)
108 // Clear ARVADOS_API_TOKEN after starting up the server, to
109 // make sure arv-git-httpd doesn't use it.
110 os.Setenv("ARVADOS_API_TOKEN", "")
112 _, err = exec.Command("git", "config",
113 "--file", s.tmpWorkdir+"/.git/config",
114 "credential.http://"+s.testServer.Addr+"/.helper",
115 "!cred(){ echo password=$ARVADOS_API_TOKEN; };cred").Output()
116 c.Assert(err, check.Equals, nil)
117 _, err = exec.Command("git", "config",
118 "--file", s.tmpWorkdir+"/.git/config",
119 "credential.http://"+s.testServer.Addr+"/.username",
121 c.Assert(err, check.Equals, nil)
124 func (s *IntegrationSuite) TearDownTest(c *check.C) {
126 if s.testServer != nil {
127 err = s.testServer.Close()
129 c.Check(err, check.Equals, nil)
130 if s.tmpRepoRoot != "" {
131 err = os.RemoveAll(s.tmpRepoRoot)
132 c.Check(err, check.Equals, nil)
134 if s.tmpWorkdir != "" {
135 err = os.RemoveAll(s.tmpWorkdir)
136 c.Check(err, check.Equals, nil)
140 func (s *IntegrationSuite) runGit(c *check.C, gitCmd, repo string, args ...string) error {
141 cwd, err := os.Getwd()
142 c.Assert(err, check.Equals, nil)
144 os.Chdir(s.tmpWorkdir)
146 gitargs := append([]string{
147 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
149 cmd := exec.Command("git", gitargs...)
150 w, err := cmd.StdinPipe()
151 c.Assert(err, check.Equals, nil)
153 output, err := cmd.CombinedOutput()
154 c.Log("git ", gitargs, " => ", err)
155 c.Log(string(output))
156 if err != nil && len(output) > 0 {
157 // If messages appeared on stderr, they are more
158 // helpful than the err returned by CombinedOutput().
160 // Easier to match error strings without newlines:
161 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))
166 // Make a bare arvados repo at {tmpRepoRoot}/arvados.git
167 func (s *IntegrationSuite) makeArvadosRepo(c *check.C) {
168 _, err := exec.Command("git", "init", "--bare", s.tmpRepoRoot+"/zzzzz-s0uqq-arvadosrepo0123.git").Output()
169 c.Assert(err, check.Equals, nil)
170 _, err = exec.Command("git", "--git-dir", s.tmpRepoRoot+"/zzzzz-s0uqq-arvadosrepo0123.git", "fetch", "../../.git", "master:master").Output()
171 c.Assert(err, check.Equals, nil)
174 // Gocheck boilerplate
175 func Test(t *testing.T) {