17296: Merge branch 'master'
[arvados.git] / lib / mount / command_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package mount
6
7 import (
8         "bytes"
9         "encoding/json"
10         "io/ioutil"
11         "os"
12         "time"
13
14         "git.arvados.org/arvados.git/sdk/go/arvadostest"
15         check "gopkg.in/check.v1"
16 )
17
18 var _ = check.Suite(&CmdSuite{})
19
20 type CmdSuite struct {
21         mnt string
22 }
23
24 func (s *CmdSuite) SetUpTest(c *check.C) {
25         tmpdir, err := ioutil.TempDir("", "")
26         c.Assert(err, check.IsNil)
27         s.mnt = tmpdir
28 }
29
30 func (s *CmdSuite) TearDownTest(c *check.C) {
31         c.Check(os.RemoveAll(s.mnt), check.IsNil)
32 }
33
34 func (s *CmdSuite) TestMount(c *check.C) {
35         exited := make(chan int)
36         stdin := bytes.NewBufferString("stdin")
37         stdout := bytes.NewBuffer(nil)
38         stderr := bytes.NewBuffer(nil)
39         mountCmd := cmd{ready: make(chan struct{})}
40         ready := false
41         go func() {
42                 exited <- mountCmd.RunCommand("test mount", []string{"--experimental", s.mnt}, stdin, stdout, stderr)
43         }()
44         go func() {
45                 <-mountCmd.ready
46                 ready = true
47
48                 f, err := os.Open(s.mnt + "/by_id/" + arvadostest.FooCollection)
49                 if c.Check(err, check.IsNil) {
50                         dirnames, err := f.Readdirnames(-1)
51                         c.Check(err, check.IsNil)
52                         c.Check(dirnames, check.DeepEquals, []string{"foo"})
53                         f.Close()
54                 }
55
56                 buf, err := ioutil.ReadFile(s.mnt + "/by_id/" + arvadostest.FooCollection + "/.arvados#collection")
57                 if c.Check(err, check.IsNil) {
58                         var m map[string]interface{}
59                         err = json.Unmarshal(buf, &m)
60                         c.Check(err, check.IsNil)
61                         c.Check(m["manifest_text"], check.Matches, `\. acbd.* 0:3:foo\n`)
62                 }
63
64                 _, err = os.Open(s.mnt + "/by_id/zzzzz-4zz18-does-not-exist")
65                 c.Check(os.IsNotExist(err), check.Equals, true)
66
67                 ok := mountCmd.Unmount()
68                 c.Check(ok, check.Equals, true)
69         }()
70         select {
71         case <-time.After(5 * time.Second):
72                 c.Fatal("timed out")
73         case errCode, ok := <-exited:
74                 c.Check(ok, check.Equals, true)
75                 c.Check(errCode, check.Equals, 0)
76         }
77         c.Check(ready, check.Equals, true)
78         c.Check(stdout.String(), check.Equals, "")
79         // stdin should not have been read
80         c.Check(stdin.String(), check.Equals, "stdin")
81 }