File: test_ringcomposer.py

package info (click to toggle)
swift 2.35.1-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 22,760 kB
  • sloc: python: 281,901; javascript: 1,059; sh: 619; pascal: 295; makefile: 81; xml: 32
file content (195 lines) | stat: -rw-r--r-- 9,125 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import io
import json

import os
import shutil
import tempfile
import unittest

from unittest import mock

from swift.cli import ringcomposer
from test.unit import write_stub_builder


class TestCommands(unittest.TestCase):

    def setUp(self):
        self.tmpdir = tempfile.mkdtemp()
        self.composite_builder_file = os.path.join(self.tmpdir,
                                                   'composite.builder')
        self.composite_ring_file = os.path.join(self.tmpdir,
                                                'composite.ring')

    def tearDown(self):
        shutil.rmtree(self.tmpdir)

    def _run_composer(self, args):
        mock_stdout = io.StringIO()
        mock_stderr = io.StringIO()
        with mock.patch("sys.stdout", mock_stdout):
            with mock.patch("sys.stderr", mock_stderr):
                with self.assertRaises(SystemExit) as cm:
                    ringcomposer.main(args)
        return (cm.exception.code,
                mock_stdout.getvalue(),
                mock_stderr.getvalue())

    def test_unknown_command(self):
        args = ('', self.composite_builder_file, 'unknown')
        exit_code, stdout, stderr = self._run_composer(args)
        self.assertEqual(2, exit_code)
        self.assertIn('invalid choice', stderr)

        args = ('', 'non-existent-file', 'unknown')
        exit_code, stdout, stderr = self._run_composer(args)
        self.assertEqual(2, exit_code)
        self.assertIn('invalid choice', stderr)

    def test_bad_composite_builder_file(self):
        cmds = (('', self.composite_builder_file, 'show'),
                ('', self.composite_builder_file, 'compose',
                 'b1_file', 'b2_file', '--output', self.composite_ring_file))
        for cmd in cmds:
            try:
                with open(self.composite_builder_file, 'wb') as fd:
                    fd.write(b'not json')
                exit_code, stdout, stderr = self._run_composer(cmd)
                self.assertEqual(2, exit_code)
                self.assertIn('An error occurred while loading the composite '
                              'builder file', stderr)
                self.assertIn(
                    'File does not contain valid composite ring data', stderr)
            except AssertionError as err:
                self.fail('Failed testing command %r due to: %s' % (cmd, err))

    def test_compose(self):
        b1, b1_file = write_stub_builder(self.tmpdir, 1)
        b2, b2_file = write_stub_builder(self.tmpdir, 2)
        args = ('', self.composite_builder_file, 'compose', b1_file, b2_file,
                '--output', self.composite_ring_file)
        exit_code, stdout, stderr = self._run_composer(args)
        self.assertEqual(0, exit_code)
        self.assertTrue(os.path.exists(self.composite_builder_file))
        self.assertTrue(os.path.exists(self.composite_ring_file))

    def test_compose_existing(self):
        b1, b1_file = write_stub_builder(self.tmpdir, 1)
        b2, b2_file = write_stub_builder(self.tmpdir, 2)
        args = ('', self.composite_builder_file, 'compose', b1_file, b2_file,
                '--output', self.composite_ring_file)
        exit_code, stdout, stderr = self._run_composer(args)
        self.assertEqual(0, exit_code)
        os.unlink(self.composite_ring_file)
        # no changes - expect failure
        args = ('', self.composite_builder_file, 'compose',
                '--output', self.composite_ring_file)
        exit_code, stdout, stderr = self._run_composer(args)
        self.assertEqual(2, exit_code)
        self.assertFalse(os.path.exists(self.composite_ring_file))
        # --force should force output
        args = ('', self.composite_builder_file, 'compose',
                '--output', self.composite_ring_file, '--force')
        exit_code, stdout, stderr = self._run_composer(args)
        self.assertEqual(0, exit_code)
        self.assertTrue(os.path.exists(self.composite_ring_file))

    def test_compose_insufficient_component_builder_files(self):
        b1, b1_file = write_stub_builder(self.tmpdir, 1)
        args = ('', self.composite_builder_file, 'compose', b1_file,
                '--output', self.composite_ring_file)
        exit_code, stdout, stderr = self._run_composer(args)
        self.assertEqual(2, exit_code)
        self.assertIn('An error occurred while composing the ring', stderr)
        self.assertIn('Two or more component builders are required', stderr)
        self.assertFalse(os.path.exists(self.composite_builder_file))
        self.assertFalse(os.path.exists(self.composite_ring_file))

    def test_compose_nonexistent_component_builder_file(self):
        b1, b1_file = write_stub_builder(self.tmpdir, 1)
        bad_file = os.path.join(self.tmpdir, 'non-existent-file')
        args = ('', self.composite_builder_file, 'compose', b1_file, bad_file,
                '--output', self.composite_ring_file)
        exit_code, stdout, stderr = self._run_composer(args)
        self.assertIn('An error occurred while composing the ring', stderr)
        self.assertIn('Ring Builder file does not exist', stderr)
        self.assertEqual(2, exit_code)
        self.assertFalse(os.path.exists(self.composite_builder_file))
        self.assertFalse(os.path.exists(self.composite_ring_file))

    def test_compose_fails_to_write_composite_ring_file(self):
        b1, b1_file = write_stub_builder(self.tmpdir, 1)
        b2, b2_file = write_stub_builder(self.tmpdir, 2)
        args = ('', self.composite_builder_file, 'compose', b1_file, b2_file,
                '--output', self.composite_ring_file)
        with mock.patch('swift.common.ring.RingData.save',
                        side_effect=IOError('io error')):
            exit_code, stdout, stderr = self._run_composer(args)
        self.assertEqual(2, exit_code)
        self.assertIn(
            'An error occurred while writing the composite ring file', stderr)
        self.assertIn('io error', stderr)
        self.assertFalse(os.path.exists(self.composite_builder_file))
        self.assertFalse(os.path.exists(self.composite_ring_file))

    def test_compose_fails_to_write_composite_builder_file(self):
        b1, b1_file = write_stub_builder(self.tmpdir, 1)
        b2, b2_file = write_stub_builder(self.tmpdir, 2)
        args = ('', self.composite_builder_file, 'compose', b1_file, b2_file,
                '--output', self.composite_ring_file)
        func = 'swift.common.ring.composite_builder.CompositeRingBuilder.save'
        with mock.patch(func, side_effect=IOError('io error')):
            exit_code, stdout, stderr = self._run_composer(args)
        self.assertEqual(2, exit_code)
        self.assertIn(
            'An error occurred while writing the composite builder file',
            stderr)
        self.assertIn('io error', stderr)
        self.assertFalse(os.path.exists(self.composite_builder_file))
        self.assertTrue(os.path.exists(self.composite_ring_file))

    def test_show(self):
        b1, b1_file = write_stub_builder(self.tmpdir, 1)
        b2, b2_file = write_stub_builder(self.tmpdir, 2)
        args = ('', self.composite_builder_file, 'compose', b1_file, b2_file,
                '--output', self.composite_ring_file)
        exit_code, stdout, stderr = self._run_composer(args)
        self.assertEqual(0, exit_code)
        args = ('', self.composite_builder_file, 'show')
        exit_code, stdout, stderr = self._run_composer(args)
        self.assertEqual(0, exit_code)
        expected = {'component_builder_files': {b1.id: b1_file,
                                                b2.id: b2_file},
                    'components': [
                        {'id': b1.id,
                         'replicas': b1.replicas,
                         # added replicas devices plus rebalance
                         'version': b1.replicas + 1},
                        {'id': b2.id,
                         'replicas': b2.replicas,
                         # added replicas devices plus rebalance
                         'version': b2.replicas + 1}],
                    'version': 1
                    }
        self.assertEqual(expected, json.loads(stdout))

    def test_show_nonexistent_composite_builder_file(self):
        args = ('', 'non-existent-file', 'show')
        exit_code, stdout, stderr = self._run_composer(args)
        self.assertEqual(2, exit_code)
        self.assertIn(
            'An error occurred while loading the composite builder file',
            stderr)
        self.assertIn("No such file or directory: 'non-existent-file'", stderr)