1 | """ |
---|
2 | Ported to Python 3. |
---|
3 | """ |
---|
4 | |
---|
5 | import json |
---|
6 | |
---|
7 | from twisted.trial import unittest |
---|
8 | from twisted.internet.defer import inlineCallbacks |
---|
9 | |
---|
10 | from allmydata.scripts.common import get_aliases |
---|
11 | from allmydata.test.no_network import GridTestMixin |
---|
12 | from .common import CLITestMixin |
---|
13 | from allmydata.util import encodingutil |
---|
14 | |
---|
15 | # see also test_create_alias |
---|
16 | |
---|
17 | class ListAlias(GridTestMixin, CLITestMixin, unittest.TestCase): |
---|
18 | |
---|
19 | @inlineCallbacks |
---|
20 | def _check_create_alias(self, alias, encoding): |
---|
21 | """ |
---|
22 | Verify that ``tahoe create-alias`` can be used to create an alias named |
---|
23 | ``alias`` when argv is encoded using ``encoding``. |
---|
24 | |
---|
25 | :param unicode alias: The alias to try to create. |
---|
26 | |
---|
27 | :param NoneType|str encoding: The name of an encoding to force the |
---|
28 | ``create-alias`` implementation to use. This simulates the |
---|
29 | effects of setting LANG and doing other locale-foolishness without |
---|
30 | actually having to mess with this process's global locale state. |
---|
31 | If this is ``None`` then the encoding used will be ascii but the |
---|
32 | stdio objects given to the code under test will not declare any |
---|
33 | encoding (this is like Python 2 when stdio is not a tty). |
---|
34 | |
---|
35 | :return Deferred: A Deferred that fires with success if the alias can |
---|
36 | be created and that creation is reported on stdout appropriately |
---|
37 | encoded or with failure if something goes wrong. |
---|
38 | """ |
---|
39 | self.basedir = self.mktemp() |
---|
40 | self.set_up_grid(oneshare=True) |
---|
41 | |
---|
42 | # We can pass an encoding into the test utilities to invoke the code |
---|
43 | # under test but we can't pass such a parameter directly to the code |
---|
44 | # under test. Instead, that code looks at io_encoding. So, |
---|
45 | # monkey-patch that value to our desired value here. This is the code |
---|
46 | # that most directly takes the place of messing with LANG or the |
---|
47 | # locale module. |
---|
48 | self.patch(encodingutil, "io_encoding", encoding or "ascii") |
---|
49 | |
---|
50 | rc, stdout, stderr = yield self.do_cli_unicode( |
---|
51 | u"create-alias", |
---|
52 | [alias], |
---|
53 | encoding=encoding, |
---|
54 | ) |
---|
55 | |
---|
56 | # Make sure the result of the create-alias command is as we want it to |
---|
57 | # be. |
---|
58 | self.assertEqual(u"Alias '{}' created\n".format(alias), stdout) |
---|
59 | self.assertEqual("", stderr) |
---|
60 | self.assertEqual(0, rc) |
---|
61 | |
---|
62 | # Make sure it had the intended side-effect, too - an alias created in |
---|
63 | # the node filesystem state. |
---|
64 | aliases = get_aliases(self.get_clientdir()) |
---|
65 | self.assertIn(alias, aliases) |
---|
66 | self.assertTrue(aliases[alias].startswith(b"URI:DIR2:")) |
---|
67 | |
---|
68 | # And inspect the state via the user interface list-aliases command |
---|
69 | # too. |
---|
70 | rc, stdout, stderr = yield self.do_cli_unicode( |
---|
71 | u"list-aliases", |
---|
72 | [u"--json"], |
---|
73 | encoding=encoding, |
---|
74 | ) |
---|
75 | |
---|
76 | self.assertEqual(0, rc) |
---|
77 | data = json.loads(stdout) |
---|
78 | self.assertIn(alias, data) |
---|
79 | data = data[alias] |
---|
80 | self.assertIn(u"readwrite", data) |
---|
81 | self.assertIn(u"readonly", data) |
---|
82 | |
---|
83 | |
---|
84 | def test_list_none(self): |
---|
85 | """ |
---|
86 | An alias composed of all ASCII-encodeable code points can be created when |
---|
87 | stdio aren't clearly marked with an encoding. |
---|
88 | """ |
---|
89 | return self._check_create_alias( |
---|
90 | u"tahoe", |
---|
91 | encoding=None, |
---|
92 | ) |
---|
93 | |
---|
94 | |
---|
95 | def test_list_ascii(self): |
---|
96 | """ |
---|
97 | An alias composed of all ASCII-encodeable code points can be created when |
---|
98 | the active encoding is ASCII. |
---|
99 | """ |
---|
100 | return self._check_create_alias( |
---|
101 | u"tahoe", |
---|
102 | encoding="ascii", |
---|
103 | ) |
---|
104 | |
---|
105 | |
---|
106 | def test_list_utf_8(self): |
---|
107 | """ |
---|
108 | An alias composed of all UTF-8-encodeable code points can be created when |
---|
109 | the active encoding is UTF-8. |
---|
110 | """ |
---|
111 | return self._check_create_alias( |
---|
112 | u"tahoe\N{SNOWMAN}", |
---|
113 | encoding="utf-8", |
---|
114 | ) |
---|