1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
13 "git.curoverse.com/arvados.git/lib/config"
14 "git.curoverse.com/arvados.git/sdk/go/arvados"
15 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
16 "git.curoverse.com/arvados.git/sdk/go/ctxlog"
17 check "gopkg.in/check.v1"
18 git_client "gopkg.in/src-d/go-git.v4/plumbing/transport/client"
19 git_http "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
22 type GitMountSuite struct {
26 var _ = check.Suite(&GitMountSuite{})
28 func (s *GitMountSuite) SetUpTest(c *check.C) {
32 s.tmpdir, err = ioutil.TempDir("", "")
33 c.Assert(err, check.IsNil)
36 func (s *GitMountSuite) TearDownTest(c *check.C) {
37 err := os.RemoveAll(s.tmpdir)
38 c.Check(err, check.IsNil)
41 // Commit fd3531f is crunch-run-tree-test
42 func (s *GitMountSuite) TestextractTree(c *check.C) {
45 UUID: arvadostest.Repository2UUID,
46 Commit: "fd3531f42995344f36c30b79f55f27b502f3d344",
48 err := gm.extractTree(&ArvTestClient{}, s.tmpdir, arvadostest.ActiveToken)
49 c.Check(err, check.IsNil)
51 fnm := filepath.Join(s.tmpdir, "dir1/dir2/file with mode 0644")
52 data, err := ioutil.ReadFile(fnm)
53 c.Check(err, check.IsNil)
54 c.Check(data, check.DeepEquals, []byte{0, 1, 2, 3})
55 fi, err := os.Stat(fnm)
56 c.Check(err, check.IsNil)
58 c.Check(fi.Mode(), check.Equals, os.FileMode(0644))
61 fnm = filepath.Join(s.tmpdir, "dir1/dir2/file with mode 0755")
62 data, err = ioutil.ReadFile(fnm)
63 c.Check(err, check.IsNil)
64 c.Check(string(data), check.DeepEquals, "#!/bin/sh\nexec echo OK\n")
65 fi, err = os.Stat(fnm)
66 c.Check(err, check.IsNil)
68 c.Check(fi.Mode(), check.Equals, os.FileMode(0755))
71 // Ensure there's no extra stuff like a ".git" dir
72 s.checkTmpdirContents(c, []string{"dir1"})
74 // Ensure tmpdir is world-readable and world-executable so the
75 // UID inside the container can use it.
76 fi, err = os.Stat(s.tmpdir)
77 c.Check(err, check.IsNil)
78 c.Check(fi.Mode()&os.ModePerm, check.Equals, os.FileMode(0755))
81 // Commit 5ebfab0 is not the tip of any branch or tag, but is
82 // reachable in branch "crunch-run-non-tip-test".
83 func (s *GitMountSuite) TestExtractNonTipCommit(c *check.C) {
85 UUID: arvadostest.Repository2UUID,
86 Commit: "5ebfab0522851df01fec11ec55a6d0f4877b542e",
88 err := gm.extractTree(&ArvTestClient{}, s.tmpdir, arvadostest.ActiveToken)
89 c.Check(err, check.IsNil)
91 fnm := filepath.Join(s.tmpdir, "file only on testbranch")
92 data, err := ioutil.ReadFile(fnm)
93 c.Check(err, check.IsNil)
94 c.Check(string(data), check.DeepEquals, "testfile\n")
97 func (s *GitMountSuite) TestNonexistentRepository(c *check.C) {
100 UUID: "zzzzz-s0uqq-nonexistentrepo",
101 Commit: "5ebfab0522851df01fec11ec55a6d0f4877b542e",
103 err := gm.extractTree(&ArvTestClient{}, s.tmpdir, arvadostest.ActiveToken)
104 c.Check(err, check.NotNil)
105 c.Check(err, check.ErrorMatches, ".*repository not found.*")
107 s.checkTmpdirContents(c, []string{})
110 func (s *GitMountSuite) TestNonexistentCommit(c *check.C) {
113 UUID: arvadostest.Repository2UUID,
114 Commit: "bb66b6bb6b6bbb6b6b6b66b6b6b6b6b6b6b6b66b",
116 err := gm.extractTree(&ArvTestClient{}, s.tmpdir, arvadostest.ActiveToken)
117 c.Check(err, check.NotNil)
118 c.Check(err, check.ErrorMatches, ".*object not found.*")
120 s.checkTmpdirContents(c, []string{})
123 func (s *GitMountSuite) TestGitUrlDiscoveryFails(c *check.C) {
124 delete(discoveryMap, "gitUrl")
127 UUID: arvadostest.Repository2UUID,
128 Commit: "5ebfab0522851df01fec11ec55a6d0f4877b542e",
130 err := gm.extractTree(&ArvTestClient{}, s.tmpdir, arvadostest.ActiveToken)
131 c.Check(err, check.ErrorMatches, ".*gitUrl.*")
134 func (s *GitMountSuite) TestInvalid(c *check.C) {
135 for _, trial := range []struct {
142 UUID: arvadostest.Repository2UUID,
150 UUID: arvadostest.Repository2UUID,
151 RepositoryName: arvadostest.Repository2Name,
152 Commit: "5ebfab0522851df01fec11ec55a6d0f4877b542e",
154 matcher: ".*repository_name.*",
159 GitURL: "https://localhost:0/" + arvadostest.Repository2Name + ".git",
160 Commit: "5ebfab0522851df01fec11ec55a6d0f4877b542e",
162 matcher: ".*git_url.*",
167 UUID: arvadostest.Repository2UUID,
168 Commit: "5ebfab0522851df01fec11ec55a6d0f4877b542e",
175 Commit: "5ebfab0522851df01fec11ec55a6d0f4877b542e",
182 UUID: arvadostest.Repository2UUID,
183 Commit: "5ebfab0522851df01fec11ec55a6d0f4877b542e",
186 matcher: ".*writable.*",
189 err := trial.gm.extractTree(&ArvTestClient{}, s.tmpdir, arvadostest.ActiveToken)
190 c.Check(err, check.NotNil)
191 s.checkTmpdirContents(c, []string{})
193 err = trial.gm.validate()
194 c.Check(err, check.ErrorMatches, trial.matcher)
198 func (s *GitMountSuite) checkTmpdirContents(c *check.C, expect []string) {
199 f, err := os.Open(s.tmpdir)
200 c.Check(err, check.IsNil)
201 names, err := f.Readdirnames(-1)
202 c.Check(err, check.IsNil)
203 c.Check(names, check.DeepEquals, expect)
206 func (*GitMountSuite) useTestGitServer(c *check.C) {
207 git_client.InstallProtocol("https", git_http.NewClient(arvados.InsecureHTTPClient))
209 loader := config.NewLoader(nil, ctxlog.TestLogger(c))
210 cfg, err := loader.Load()
211 c.Assert(err, check.IsNil)
212 cluster, err := cfg.GetCluster("")
213 c.Assert(err, check.IsNil)
215 discoveryMap["gitUrl"] = (*url.URL)(&cluster.Services.GitHTTP.ExternalURL).String()