Merge remote-tracking branch 'origin/master' into 14965-arv-mount-py-three
[arvados.git] / services / fuse / tests / test_exec.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 from __future__ import absolute_import
6 import arvados_fuse.command
7 import json
8 import multiprocessing
9 import os
10 from . import run_test_server
11 import tempfile
12 import unittest
13
14 from .integration_test import workerPool
15
16 try:
17     from shlex import quote
18 except:
19     from pipes import quote
20
21 def try_exec(mnt, cmd):
22     try:
23         arvados_fuse.command.Mount(
24             arvados_fuse.command.ArgumentParser().parse_args([
25                 '--read-write',
26                 '--mount-tmp=zzz',
27                 '--unmount-timeout=0.1',
28                 mnt,
29                 '--exec'] + cmd)).run()
30     except SystemExit:
31         pass
32     else:
33         raise AssertionError('should have exited')
34
35
36 class ExecMode(unittest.TestCase):
37     @classmethod
38     def setUpClass(cls):
39         run_test_server.run()
40         run_test_server.run_keep(enforce_permissions=True, num_servers=2)
41         run_test_server.authorize_with('active')
42
43     @classmethod
44     def tearDownClass(cls):
45         run_test_server.stop_keep(num_servers=2)
46
47     def setUp(self):
48         self.mnt = tempfile.mkdtemp()
49         _, self.okfile = tempfile.mkstemp()
50
51     def tearDown(self):
52         os.rmdir(self.mnt)
53         os.unlink(self.okfile)
54
55     def test_exec(self):
56         workerPool().apply(try_exec, (self.mnt, [
57             'sh', '-c',
58             'echo -n foo >{}; cp {} {}'.format(
59                 quote(os.path.join(self.mnt, 'zzz', 'foo.txt')),
60                 quote(os.path.join(self.mnt, 'zzz', '.arvados#collection')),
61                 quote(os.path.join(self.okfile)))]))
62         self.assertRegexpMatches(
63             json.load(open(self.okfile))['manifest_text'],
64             r' 0:3:foo.txt\n')