1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
12 "git.curoverse.com/arvados.git/sdk/go/arvados"
13 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
14 check "gopkg.in/check.v1"
15 git_client "gopkg.in/src-d/go-git.v4/plumbing/transport/client"
16 git_http "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
19 type GitMountSuite struct {
23 var _ = check.Suite(&GitMountSuite{})
25 func (s *GitMountSuite) SetUpTest(c *check.C) {
29 s.tmpdir, err = ioutil.TempDir("", "")
30 c.Assert(err, check.IsNil)
33 func (s *GitMountSuite) TearDownTest(c *check.C) {
34 err := os.RemoveAll(s.tmpdir)
35 c.Check(err, check.IsNil)
38 // Commit fd3531f is crunch-run-tree-test
39 func (s *GitMountSuite) TestextractTree(c *check.C) {
42 UUID: arvadostest.Repository2UUID,
43 Commit: "fd3531f42995344f36c30b79f55f27b502f3d344",
45 err := gm.extractTree(&ArvTestClient{}, s.tmpdir, arvadostest.ActiveToken)
46 c.Check(err, check.IsNil)
48 fnm := filepath.Join(s.tmpdir, "dir1/dir2/file with mode 0644")
49 data, err := ioutil.ReadFile(fnm)
50 c.Check(err, check.IsNil)
51 c.Check(data, check.DeepEquals, []byte{0, 1, 2, 3})
52 fi, err := os.Stat(fnm)
53 c.Check(err, check.IsNil)
55 c.Check(fi.Mode(), check.Equals, os.FileMode(0644))
58 fnm = filepath.Join(s.tmpdir, "dir1/dir2/file with mode 0755")
59 data, err = ioutil.ReadFile(fnm)
60 c.Check(err, check.IsNil)
61 c.Check(string(data), check.DeepEquals, "#!/bin/sh\nexec echo OK\n")
62 fi, err = os.Stat(fnm)
63 c.Check(err, check.IsNil)
65 c.Check(fi.Mode(), check.Equals, os.FileMode(0755))
68 // Ensure there's no extra stuff like a ".git" dir
69 s.checkTmpdirContents(c, []string{"dir1"})
71 // Ensure tmpdir is world-readable and world-executable so the
72 // UID inside the container can use it.
73 fi, err = os.Stat(s.tmpdir)
74 c.Check(err, check.IsNil)
75 c.Check(fi.Mode()&os.ModePerm, check.Equals, os.FileMode(0755))
78 // Commit 5ebfab0 is not the tip of any branch or tag, but is
79 // reachable in branch "crunch-run-non-tip-test".
80 func (s *GitMountSuite) TestExtractNonTipCommit(c *check.C) {
82 UUID: arvadostest.Repository2UUID,
83 Commit: "5ebfab0522851df01fec11ec55a6d0f4877b542e",
85 err := gm.extractTree(&ArvTestClient{}, s.tmpdir, arvadostest.ActiveToken)
86 c.Check(err, check.IsNil)
88 fnm := filepath.Join(s.tmpdir, "file only on testbranch")
89 data, err := ioutil.ReadFile(fnm)
90 c.Check(err, check.IsNil)
91 c.Check(string(data), check.DeepEquals, "testfile\n")
94 func (s *GitMountSuite) TestNonexistentRepository(c *check.C) {
97 UUID: "zzzzz-s0uqq-nonexistentrepo",
98 Commit: "5ebfab0522851df01fec11ec55a6d0f4877b542e",
100 err := gm.extractTree(&ArvTestClient{}, s.tmpdir, arvadostest.ActiveToken)
101 c.Check(err, check.NotNil)
102 c.Check(err, check.ErrorMatches, ".*repository not found.*")
104 s.checkTmpdirContents(c, []string{})
107 func (s *GitMountSuite) TestNonexistentCommit(c *check.C) {
110 UUID: arvadostest.Repository2UUID,
111 Commit: "bb66b6bb6b6bbb6b6b6b66b6b6b6b6b6b6b6b66b",
113 err := gm.extractTree(&ArvTestClient{}, s.tmpdir, arvadostest.ActiveToken)
114 c.Check(err, check.NotNil)
115 c.Check(err, check.ErrorMatches, ".*object not found.*")
117 s.checkTmpdirContents(c, []string{})
120 func (s *GitMountSuite) TestGitUrlDiscoveryFails(c *check.C) {
121 delete(discoveryMap, "gitUrl")
124 UUID: arvadostest.Repository2UUID,
125 Commit: "5ebfab0522851df01fec11ec55a6d0f4877b542e",
127 err := gm.extractTree(&ArvTestClient{}, s.tmpdir, arvadostest.ActiveToken)
128 c.Check(err, check.ErrorMatches, ".*gitUrl.*")
131 func (s *GitMountSuite) TestInvalid(c *check.C) {
132 for _, trial := range []struct {
139 UUID: arvadostest.Repository2UUID,
147 UUID: arvadostest.Repository2UUID,
148 RepositoryName: arvadostest.Repository2Name,
149 Commit: "5ebfab0522851df01fec11ec55a6d0f4877b542e",
151 matcher: ".*repository_name.*",
156 GitURL: "https://localhost:0/" + arvadostest.Repository2Name + ".git",
157 Commit: "5ebfab0522851df01fec11ec55a6d0f4877b542e",
159 matcher: ".*git_url.*",
164 UUID: arvadostest.Repository2UUID,
165 Commit: "5ebfab0522851df01fec11ec55a6d0f4877b542e",
172 Commit: "5ebfab0522851df01fec11ec55a6d0f4877b542e",
179 UUID: arvadostest.Repository2UUID,
180 Commit: "5ebfab0522851df01fec11ec55a6d0f4877b542e",
183 matcher: ".*writable.*",
186 err := trial.gm.extractTree(&ArvTestClient{}, s.tmpdir, arvadostest.ActiveToken)
187 c.Check(err, check.NotNil)
188 s.checkTmpdirContents(c, []string{})
190 err = trial.gm.validate()
191 c.Check(err, check.ErrorMatches, trial.matcher)
195 func (s *GitMountSuite) checkTmpdirContents(c *check.C, expect []string) {
196 f, err := os.Open(s.tmpdir)
197 c.Check(err, check.IsNil)
198 names, err := f.Readdirnames(-1)
199 c.Check(err, check.IsNil)
200 c.Check(names, check.DeepEquals, expect)
203 func (*GitMountSuite) useTestGitServer(c *check.C) {
204 git_client.InstallProtocol("https", git_http.NewClient(arvados.InsecureHTTPClient))
206 port, err := ioutil.ReadFile("../../tmp/arv-git-httpd-ssl.port")
207 c.Assert(err, check.IsNil)
208 discoveryMap["gitUrl"] = "https://localhost:" + string(port)