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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
# Copyright (c) 2010-2013 OpenStack, LLC.
#
# 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 mock
from six import StringIO
import unittest
from swiftclient import command_helpers as h
from swiftclient.multithreading import OutputManager
class TestStatHelpers(unittest.TestCase):
def setUp(self):
super(TestStatHelpers, self).setUp()
conn_attrs = {
'url': 'http://storage/v1/a',
'token': 'tk12345',
}
self.conn = mock.MagicMock(**conn_attrs)
self.options = {'human': False, 'verbose': 1}
self.stdout = StringIO()
self.stderr = StringIO()
self.output_manager = OutputManager(self.stdout, self.stderr)
def assertOut(self, expected):
real = self.stdout.getvalue()
# commonly if we strip of blank lines we have a match
try:
self.assertEqual(expected.strip('\n'),
real.strip('\n'))
except AssertionError:
# could be anything, try to find typos line by line
expected_lines = [line.lstrip() for line in
expected.splitlines() if line.strip()]
real_lines = [line.lstrip() for line in
real.splitlines() if line.strip()]
for expected, real in zip(expected_lines, real_lines):
self.assertEqual(expected, real)
# not a typo, might be an indent thing, hopefully you can spot it
raise
def test_stat_account_human(self):
self.options['human'] = True
# stub head_account
stub_headers = {
'x-account-container-count': 42,
'x-account-object-count': 1000000,
'x-account-bytes-used': 2 ** 30,
}
self.conn.head_account.return_value = stub_headers
with self.output_manager as output_manager:
items, headers = h.stat_account(self.conn, self.options)
h.print_account_stats(items, headers, output_manager)
expected = """
Account: a
Containers: 42
Objects: 976K
Bytes: 1.0G
"""
self.assertOut(expected)
def test_stat_account_verbose(self):
self.options['verbose'] += 1
# stub head_account
stub_headers = {
'x-account-container-count': 42,
'x-account-object-count': 1000000,
'x-account-bytes-used': 2 ** 30,
}
self.conn.head_account.return_value = stub_headers
with self.output_manager as output_manager:
items, headers = h.stat_account(self.conn, self.options)
h.print_account_stats(items, headers, output_manager)
expected = """
StorageURL: http://storage/v1/a
Auth Token: tk12345
Account: a
Containers: 42
Objects: 1000000
Bytes: 1073741824
"""
self.assertOut(expected)
def test_stat_account_policy_stat(self):
# stub head_account
stub_headers = {
'x-account-container-count': 42,
'x-account-object-count': 1000000,
'x-account-bytes-used': 2 ** 30,
'x-account-storage-policy-nada-object-count': 1000000,
'x-account-storage-policy-nada-bytes-used': 2 ** 30,
}
self.conn.head_account.return_value = stub_headers
with self.output_manager as output_manager:
items, headers = h.stat_account(self.conn, self.options)
h.print_account_stats(items, headers, output_manager)
expected = """
Account: a
Containers: 42
Objects: 1000000
Bytes: 1073741824
Objects in policy "nada": 1000000
Bytes in policy "nada": 1073741824
"""
self.assertOut(expected)
def test_stat_account_policy_stat_with_container_counts(self):
# stub head_account
stub_headers = {
'x-account-container-count': 42,
'x-account-object-count': 1000000,
'x-account-bytes-used': 2 ** 30,
'x-account-storage-policy-nada-container-count': 10,
'x-account-storage-policy-nada-object-count': 1000000,
'x-account-storage-policy-nada-bytes-used': 2 ** 30,
}
self.conn.head_account.return_value = stub_headers
with self.output_manager as output_manager:
items, headers = h.stat_account(self.conn, self.options)
h.print_account_stats(items, headers, output_manager)
expected = """
Account: a
Containers: 42
Objects: 1000000
Bytes: 1073741824
Containers in policy "nada": 10
Objects in policy "nada": 1000000
Bytes in policy "nada": 1073741824
"""
self.assertOut(expected)
def test_stat_container_human(self):
self.options['human'] = True
# stub head container request
stub_headers = {
'x-container-object-count': 10 ** 6,
'x-container-bytes-used': 2 ** 30,
}
self.conn.head_container.return_value = stub_headers
args = ('c',)
with self.output_manager as output_manager:
items, headers = h.stat_container(self.conn, self.options, *args)
h.print_container_stats(items, headers, output_manager)
expected = """
Account: a
Container: c
Objects: 976K
Bytes: 1.0G
Read ACL:
Write ACL:
Sync To:
Sync Key:
"""
self.assertOut(expected)
def test_stat_container_verbose(self):
self.options['verbose'] += 1
# stub head container request
stub_headers = {
'x-container-object-count': 10 ** 6,
'x-container-bytes-used': 2 ** 30,
}
self.conn.head_container.return_value = stub_headers
args = ('c',)
with self.output_manager as output_manager:
items, headers = h.stat_container(self.conn, self.options, *args)
h.print_container_stats(items, headers, output_manager)
expected = """
URL: http://storage/v1/a/c
Auth Token: tk12345
Account: a
Container: c
Objects: 1000000
Bytes: 1073741824
Read ACL:
Write ACL:
Sync To:
Sync Key:
"""
self.assertOut(expected)
def test_stat_object_human(self):
self.options['human'] = True
# stub head object request
stub_headers = {
'content-length': 2 ** 20,
'x-object-meta-color': 'blue',
'etag': '68b329da9893e34099c7d8ad5cb9c940',
'content-encoding': 'gzip',
}
self.conn.head_object.return_value = stub_headers
args = ('c', 'o')
with self.output_manager as output_manager:
items, headers = h.stat_object(self.conn, self.options, *args)
h.print_object_stats(items, headers, output_manager)
expected = """
Account: a
Container: c
Object: o
Content Length: 1.0M
ETag: 68b329da9893e34099c7d8ad5cb9c940
Meta Color: blue
Content-Encoding: gzip
"""
self.assertOut(expected)
def test_stat_object_verbose(self):
self.options['verbose'] += 1
# stub head object request
stub_headers = {
'content-length': 2 ** 20,
'x-object-meta-color': 'blue',
'etag': '68b329da9893e34099c7d8ad5cb9c940',
'content-encoding': 'gzip',
}
self.conn.head_object.return_value = stub_headers
args = ('c', 'o')
with self.output_manager as output_manager:
items, headers = h.stat_object(self.conn, self.options, *args)
h.print_object_stats(items, headers, output_manager)
expected = """
URL: http://storage/v1/a/c/o
Auth Token: tk12345
Account: a
Container: c
Object: o
Content Length: 1048576
ETag: 68b329da9893e34099c7d8ad5cb9c940
Meta Color: blue
Content-Encoding: gzip
"""
self.assertOut(expected)
|