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) TestNoPermission(c *check.C) {
65 os.Setenv("ARVADOS_API_TOKEN", "4kg6k6lzmp9kj4cpkcoxie964cmvjahbt4fod9zru44k4jqdmi")
66 for _, repo := range []string{"active/foo.git", "active/foo/.git"} {
67 err := s.runGit(c, "fetch", repo)
68 c.Assert(err, check.ErrorMatches, `.* not found.*`)
72 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
73 arvadostest.StartAPI()
76 func (s *IntegrationSuite) SetUpTest(c *check.C) {
77 arvadostest.ResetEnv()
78 s.testServer = &server{}
80 s.tmpRepoRoot, err = ioutil.TempDir("", "arv-git-httpd")
81 c.Assert(err, check.Equals, nil)
82 s.tmpWorkdir, err = ioutil.TempDir("", "arv-git-httpd")
83 c.Assert(err, check.Equals, nil)
84 _, err = exec.Command("git", "init", s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666").Output()
85 c.Assert(err, check.Equals, nil)
86 _, err = exec.Command("sh", "-c", "cd "+s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666 && echo test >test && git add test && git commit -am 'foo: test'").CombinedOutput()
87 c.Assert(err, check.Equals, nil)
88 _, err = exec.Command("git", "init", s.tmpWorkdir).Output()
89 c.Assert(err, check.Equals, nil)
90 _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && echo work >work && git add work && git commit -am 'workdir: test'").CombinedOutput()
91 c.Assert(err, check.Equals, nil)
95 GitCommand: "/usr/bin/git",
98 err = s.testServer.Start()
99 c.Assert(err, check.Equals, nil)
101 // Clear ARVADOS_API_TOKEN after starting up the server, to
102 // make sure arv-git-httpd doesn't use it.
103 os.Setenv("ARVADOS_API_TOKEN", "")
105 _, err = exec.Command("git", "config",
106 "--file", s.tmpWorkdir+"/.git/config",
107 "credential.http://"+s.testServer.Addr+"/.helper",
108 "!cred(){ echo password=$ARVADOS_API_TOKEN; };cred").Output()
109 c.Assert(err, check.Equals, nil)
110 _, err = exec.Command("git", "config",
111 "--file", s.tmpWorkdir+"/.git/config",
112 "credential.http://"+s.testServer.Addr+"/.username",
114 c.Assert(err, check.Equals, nil)
117 func (s *IntegrationSuite) TearDownTest(c *check.C) {
119 if s.testServer != nil {
120 err = s.testServer.Close()
122 c.Check(err, check.Equals, nil)
123 if s.tmpRepoRoot != "" {
124 err = os.RemoveAll(s.tmpRepoRoot)
125 c.Check(err, check.Equals, nil)
127 if s.tmpWorkdir != "" {
128 err = os.RemoveAll(s.tmpWorkdir)
129 c.Check(err, check.Equals, nil)
133 func (s *IntegrationSuite) runGit(c *check.C, gitCmd, repo string, args ...string) error {
134 cwd, err := os.Getwd()
135 c.Assert(err, check.Equals, nil)
137 os.Chdir(s.tmpWorkdir)
139 gitargs := append([]string{
140 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
142 cmd := exec.Command("git", gitargs...)
143 w, err := cmd.StdinPipe()
144 c.Assert(err, check.Equals, nil)
146 output, err := cmd.CombinedOutput()
147 c.Log("git ", gitargs, " => ", err)
148 if err != nil && len(output) > 0 {
149 // If messages appeared on stderr, they are more
150 // helpful than the err returned by CombinedOutput().
152 // Easier to match error strings without newlines:
153 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))
158 // Make a bare arvados repo at {tmpRepoRoot}/arvados.git
159 func (s *IntegrationSuite) makeArvadosRepo(c *check.C) {
160 _, err := exec.Command("git", "init", "--bare", s.tmpRepoRoot+"/zzzzz-s0uqq-arvadosrepo0123.git").Output()
161 c.Assert(err, check.Equals, nil)
162 _, err = exec.Command("git", "--git-dir", s.tmpRepoRoot+"/zzzzz-s0uqq-arvadosrepo0123.git", "fetch", "../../.git", "master:master").Output()
163 c.Assert(err, check.Equals, nil)
166 // Gocheck boilerplate
167 func Test(t *testing.T) {