Debian Codegraph @

glanceclient/tests/unit/test_progressbar.py

raw symbols SCIP
1 # Copyright 2013 OpenStack Foundation
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import io
17 import sys
18
19 import requests
20 import testtools
21
22 from glanceclient.common import progressbar
23 from glanceclient.common import utils
24 from glanceclient.tests import utils as test_utils
25
26
27 class TestProgressBarWrapper(testtools.TestCase):
28
29 def test_iter_iterator_display_progress_bar(self):
30 size = 100
31 # create fake response object to return request-id with iterator
32 resp = requests.Response()
33 resp.headers['x-openstack-request-id'] = 'req-1234'
34 iterator_with_len = utils.IterableWithLength(iter('X' * 100), size)
35 requestid_proxy = utils.RequestIdProxy((iterator_with_len, resp))
36 saved_stdout = sys.stdout
37 try:
38 sys.stdout = output = test_utils.FakeTTYStdout()
39 # Consume iterator.
40 data = list(progressbar.VerboseIteratorWrapper(requestid_proxy,
41 size))
42 self.assertEqual(['X'] * 100, data)
43 self.assertEqual(
44 '[%s>] 100%%\n' % ('=' * 29),
45 output.getvalue()
46 )
47 finally:
48 sys.stdout = saved_stdout
49
50 def test_iter_file_display_progress_bar(self):
51 size = 98304
52 file_obj = io.StringIO('X' * size)
53 saved_stdout = sys.stdout
54 try:
55 sys.stdout = output = test_utils.FakeTTYStdout()
56 file_obj = progressbar.VerboseFileWrapper(file_obj, size)
57 chunksize = 1024
58 chunk = file_obj.read(chunksize)
59 while chunk:
60 chunk = file_obj.read(chunksize)
61 self.assertEqual(
62 '[%s>] 100%%\n' % ('=' * 29),
63 output.getvalue()
64 )
65 finally:
66 sys.stdout = saved_stdout
67
68 def test_iter_file_no_tty(self):
69 size = 98304
70 file_obj = io.StringIO('X' * size)
71 saved_stdout = sys.stdout
72 try:
73 sys.stdout = output = test_utils.FakeNoTTYStdout()
74 file_obj = progressbar.VerboseFileWrapper(file_obj, size)
75 chunksize = 1024
76 chunk = file_obj.read(chunksize)
77 while chunk:
78 chunk = file_obj.read(chunksize)
79 # If stdout is not a tty progress bar should do nothing.
80 self.assertEqual('', output.getvalue())
81 finally:
82 sys.stdout = saved_stdout