141 Commits

Author SHA1 Message Date
Daniel Wakefield
f0300e3714 Verify MD5 of uploaded objects.
Changed existing code to calculate the MD5 of the object
during the upload stream. Checks this MD5 against the etag
returned in the response.
An exception is raised if they do not match.

Closes-Bug: 1379263

Change-Id: I6c8bc1366dfb591a26d934a30cd21c9e6b9a04ce
2015-02-19 11:21:32 +00:00
Joel Wright
06c73c6020 Fix deleting SLO segments on overwrite
This patch fixes a bug that results in SLO segments being left
behind regardless of the setting of --leave-segments.

Change-Id: I2b3c8ab4817df6facb4b5961dbe570645441a552
Closes-Bug: 1418007
2015-02-04 14:17:54 +00:00
Jenkins
eb28d8aff6 Merge "This patch fixes downloading files to stdout." 2015-02-03 18:55:47 +00:00
Jenkins
21473f1bc4 Merge "Fix cross account upload using --os-storage-url" 2015-02-02 08:30:40 +00:00
Joel Wright
bd42c2b00d This patch fixes downloading files to stdout.
This patch fixes downloading files to stdout and modifies
_SwiftReader to operate as an iterator that performs file
checks at the end of iteration as well as a context manager.
File verification checks have been removed from __exit__
and added to __iter__.

Change-Id: I3250bdeeef8484a9122c4b5b854756a7c8f8731e
Closes-Bug: 1395922
Closes-Bug: 1387376
2015-01-23 11:46:13 +00:00
Jenkins
7709fea51e Merge "Check that content_type header exists before using" 2015-01-10 03:42:45 +00:00
Alistair Coles
9593d4b58a Fix cross account upload using --os-storage-url
Removes an account stat from the object upload path.
This stat fails when user is not account admin even
though the user may have container ACL permission to
write objects.

Reduces the severity of the CLI output message when
upload fails to create the given container (this is
not an error since the container may exist - the user
just does not have permission to PUT or POST the
container).

Changes the 'swift upload' exit return code from 1 to
0 if container PUT fails but object PUT succeeds.

For segment uploads, makes the attempt to create the
segment container conditional on it not being the same
as the manifest container. This avoids an unnecessary
container PUT.

Fixes another bug that became apparent: with segmented
upload a container HEAD may be attempted to determine
the policy to be used for the segment container. When
this failed the result dict has headers=None which was
causing an exception in the shell result handler.

Add unit tests for object upload/download and container
list with --os-storage-url option.

Closes-Bug: #1371650
Change-Id: If1f8a02ee7459ea2158ffa6e958f67d299ec529e
2015-01-06 16:13:39 +00:00
Alistair Coles
488272ee59 Change tests to use CaptureOutput class
Modify two tests to use the CaptureOutput class. These
tests were added after the comprehensive transition to using
CaptureOutput made in change [1], so this is just bringing
them in line with that test pattern.

Also deletes an unused mock.

[1] change id Ib59bbbe88256f215eed0a8ebc8282e02181d4377

Change-Id: Ic524311ffb3b0d6566addec0676633ddb8075e25
2015-01-05 11:23:56 +00:00
Jiří Suchomel
5d57018707 Print info message about incorrect --totals usage
when neither -l nor --lh is provided.
Added test coverage for --totals.

Change-Id: I3245e715c26ec28457a21dec07311a58c475c066
Closes-Bug: 1258392
2015-01-05 11:02:00 +00:00
Jenkins
eace20c14a Merge "Make preauth params work" 2014-12-31 00:06:12 +00:00
Jenkins
dfbb1807fb Merge "Change tests to use new CaptureOutput class." 2014-12-30 21:28:43 +00:00
Clay Gerrard
fbe558885f Make preauth params work
If you specify a token and storage url when creating a Connection,
regardless of the auth api version the first request will be made
directly to swift.  You can either provide a preauthurl and preauthtoken
or fall back to os_options' object_storage_url and auth_token keys
(exposed as --os-storage-url and --os-auth-token on the command line or
OS_STORAGE_URL and OS_AUTH_TOKEN in the environment).

If a _retry wrapped request on a Connection fails because of invalid
authentication (401) the Connection's cached token and url will be
invalidated.  If the Connection's retries attribute is > 0 the
subsequent attempt will call get_auth to refresh the token, but the
pre-configured storage_url will always be re-used.  This is consistent
with current auth v2 behavior and less surprising for auth v1.

The pre-existing, but previously undocumented behavior/interface of
get_auth would override the storage_url returned by the auth service if
the 'os_storage_url' option was provided in the os_options dict.  To
ensure that this behavior is consistent across auth v1 and v2 from the
command line and when using the Connection class as a library - the
preauthurl is stashed in the os_options dict when provided.

Improved Connection.get_capabilities storage_url handling to better
support the consistent behavior of a preauthurl/object_storage_url on
the connection regardless of auth version.

Fixed up some test infrastructure to enable setting up and testing
multiple requests/responses.

Change-Id: I6950fb73f3e28fdddb62760cae9320e2f4336776
2014-12-17 05:53:56 -07:00
Daniel Wakefield
5756fcc4f2 Fix misnamed dictionary key.
The response dictionary in _delete_segment attach's any error
it encounters to the dict key 'exception', all other response
dict's use 'error' to store the exception.

Changed to make it consistent and added tests

Also added a third branch in st_delete as messages
created in _delete_container where being silently dropped.

Change-Id: Ifbc3b1fae78910fbc6acf4a86cfb0f60bb1aa336
2014-12-08 15:45:40 +00:00
Daniel Wakefield
49a80f734c Change tests to use new CaptureOutput class.
Added the ability to clear the buffers in the
CaptureOutput class so it can be easily used multiple
times in the same context manager.
Also added a option to suppress the SystemExit
associated with printing an error.

Change-Id: Ib59bbbe88256f215eed0a8ebc8282e02181d4377
2014-12-05 10:57:09 +00:00
Clay Gerrard
d89e08f722 Capture test output better
This change adds some new classes to test.unit.utils which wrap up the
collection and mocking of the std err and std out.  This will make the
testing code easier to write and maintain.

Change-Id: I50ad2a736b2bb550ab83f6a43fb5a0fb5393573e
2014-11-20 14:42:55 +00:00
Jenkins
202bbab2d0 Merge "Make swift post output an error message when failing" 2014-11-13 03:30:01 +00:00
Chris Buccella
68f271aae2 Check that content_type header exists before using
When downloading an object, the content_type header is inspected
to determine if a directory needs to be created. This header is
assumed to always be in the response; if it isn't an Exception is
raised. swiftclient should not assume content_type will always be
set.

Change-Id: I156195c02b6e0bc398fa962eb1f78c4dbddd1596
2014-11-07 01:01:53 -05:00
Alistair Coles
95056d1ef4 Make swift post output an error message when failing
swiftclient.shell.st_post was ignoring the result dict
returned from SwiftService.post and therefore giving no
indication when posts failed e.g. due to invalid auth
credentials.

This patch ensures that SwiftService always returns a
result_dict from post() and then checks the result dict.
On failure the shell now outputs the ClientException
message and exits with error code 1.

Also adds unit tests and cleans up some unnecessary mocked
return values in existing tests.

Closes-Bug: 1383243

Change-Id: I111150eb3d026c8d01c2cac13d3613ca7304e5b9
2014-10-24 18:13:37 +01:00
Daniel Wakefield
025da14326 Replaces Stacktraces with useful error messages.
Changed the message shown when a user doesn't enter project/tenant
id/name to be more informative.

When attempting to stat a container without supplying project/tenant
name or id, an empty response was being returned instead of an error
being raised.

Changed the error raised in swiftclient.client when no tenant or project
is specified to be more specific.

Add tests for basic regression checking.

Closes-Bug: #1372589

Change-Id: I4eb6964d9f1702db119cc0294edc02841b1ecd5f
2014-10-21 08:23:26 +00:00
Alistair Coles
589d34ecda Fix race between container create jobs during upload
During a segmented object upload, attempts are made
to create the container for the manifest and the
segment container. These jobs are currently placed
on separate threads and can occur out of order which
causes the unit test failure seen in the bug report.

The container jobs should be ordered because the
segment container job attempts to HEAD the manifest
container to determine correct storage policy.

This patch enforces ordering of the two jobs and
modifies the unit test to assert that both container
PUTs occur in the expected order.

Change-Id: If90bec126867a4be2df34067ccefee660202f59f
Closes-Bug: #1376878
2014-10-20 08:57:37 +01:00
Jenkins
c88cddd2d8 Merge "Allow segment size to be specified in a human readable way." 2014-10-17 19:33:06 +00:00
Daniel Wakefield
5f89dcc46e Allow segment size to be specified in a human readable way.
Instead of always specifying segment size in bytes the user
can now use B,K,M or G as suffixes for the corresponding
size.

Conversion is done with Binary units (1024) rather than SI
units (1000).

e.g

swift upload test_container -S 1073741824 large_file
can now be written
swift upload test_container -S 1G large_file

The change is backwards compatible as it ignores arguments to -S
that don't have a valid suffix.

Updated unit tests and help message.

Change-Id: I6314b4e45cf2fbffde2fe57a02df77a25e911e84
2014-10-10 12:57:35 +01:00
Christian Schwede
c37cecb616 Add tests for account listing using --lh switch
Change-Id: I2e68a24212eda463f39a5d0be13f1645ca2a23d8
2014-10-01 16:11:55 +00:00
Jenkins
08c111e771 Merge "Fix unit tests failing when OS_ env vars are set" 2014-09-26 15:48:49 +00:00
Alistair Coles
b13c84b2ef Fix unit tests failing when OS_ env vars are set
tests/unit/test_shell.py:TestParsing tests can fail if
there are OS_* variables set in the environment. There
is already code in the test setUp to remove ST_* variables,
so we should do the same for OS_* variables.

This patch also changes the mechanism used to remove and
then restore any unwanted variables found in os.environ.

The existing setUp() takes a copy of os.environ and then
deletes any ST_* variables in the original. In tearDown() it
sets os.environ as the copy. However, the environ imported
into shell.py remains pointing to the original os.environ
object. So after the first call to tearDown, subsequent
mocking of os.environ has no effect on shell.environ.
This renders some of the tests ineffective e.g.
test_insufficient_env_vars_v3 is not actually setting any
vars in shell.environ.

The issue can be provoked by repeating a test:
nosetests -w tests/unit/ test_shell.py:TestParsing.test_args_v3 \
 test_shell.py:TestParsing.test_args_v3

The test will pass first time and fail second time.

Change-Id: I5d100f81115e74878d510326acb5777e6a3626c8
2014-09-25 11:44:39 +01:00
Alistair Coles
f90d7f28e5 Fix bug with some OS options not being passed to client
Fixes a regression that is preventing swiftclient from
authenticating using keystone v3 options.

swiftclient/shell.py processes the dict of command
line options to set up a child dict ('os_options')
of keystone auth specific options (line 960). The
processing includes stripping --os- prefixes from
the command line options before adding keys to
os_options.

A recent patch https://review.openstack.org/#/c/85453/
introduced a duplication of this option processing
in service.py (line 79) which replaces the os_options
created in shell.py, but omits keystone v3 related
options. Consequently the keystone v3 options are
not being passed to the client get_auth() method.

This patch adds the keystone v3 related options to
the option processing in service.py.

For pragmatic reasons (i.e. fixing the bug quickly)
the option processing code has not been removed from
parse_args in shell.py. It is likely that the code in
parse_args is now redundant, but all code paths between
parse_args and process_options should be inspected and
test coverage added before removing that code.

Unit tests have been added in test_shell.py to verify
that command line options are correctly passed to the
client get_auth method.

The MockHttpTest class is re-used in test_shell.py,
so it is moved from test_swiftclient.py
to tests/unit/utils.py

Closes-bug: #1372465

Change-Id: I4fed013cdb8936509609d06093337cc147ade0d6
2014-09-24 18:19:12 +01:00
Joel Wright
24673f8d19 Add importable SwiftService incorporating shell.py logic
This patch adds a SwiftService class that incorporates the high
level logic from swiftclient/shell.py. It also ports shell.py to
use the new class, and updates the code in swiftclient/multithreading.py
to allow the SwiftService to be used for multiple operations whilst
using only one thread pool.

Currently, code that imports swiftclient has to have its own logic for
things like creating large objects, parallel uploads, and parallel
downloads. This patch adds a SwiftService class that makes that
functionality available in Python code as well as through the shell.

Change-Id: I08c5796b4c01001d79fd571651c3017c16462ffd
Implements: blueprint bin-swift-logic-as-importable-library
2014-08-26 14:14:21 +02:00
Jenkins
d97ec374cb Merge "fixed unit tests when env vars are set" 2014-08-21 20:34:35 +00:00
Pete Zaitcev
309437bb52 Do not create an empty directory 'pseudo/'
This is a fix-up for the commit 3d56b65c. The code change was good,
but the additional test had a side effect of creating an empty
directory "pseudo/". Fix this by mocking-out mkdirs().

Change-Id: Iead55e72d8a75d96bde0a7491aca12bbdcc269cf
2014-08-18 16:51:01 -06:00
John Dickinson
2eca5278e6 fixed unit tests when env vars are set
Change-Id: Ia73c10c7c81b9434895941ea29282dbcdc667556
2014-08-18 15:09:33 -07:00
Jenkins
eedb0d4ab5 Merge "Fix crash when downloading a pseudo-directory" 2014-08-17 19:08:15 +00:00
Jenkins
23474b7d11 Merge "Add keystone v3 auth support" 2014-08-15 23:24:00 +00:00
Pete Zaitcev
3d56b65c89 Fix crash when downloading a pseudo-directory
If a user creates an object with name ending with a slash, then
downloading such container ends in a traceback like this:

..............
test5g.file [auth 1.516s, headers 1.560s, total 244.565s, 22.089 MB/s]
Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/swiftclient/multithreading.py", lin
    result = self.func(item, *self.args, **self.kwargs)
  File "/usr/lib/python2.6/site-packages/swiftclient/shell.py", line 403, in
    fp = open(path, 'wb')
IOError: [Errno 21] Is a directory: 'first-pseudo-folder/'

The proposed fix is not to save this object. Note that the contents of
the object are available with --output option, as before. Only the
crash is fixed.

Even though we do not use the contents, we download the object and
check its Etag, in case. We also create a corresponding directory,
in case the pseudo-directory contains no objects.

The format of printout is changed, so user realizes easier when
pseudo-directory convention is in effect. Note that this is not a
compatibility issue because previously there was crash in such case.

Change-Id: I3352f7a4eaf9970961af0cc84c4706fc1eab281d
2014-08-14 13:57:53 -06:00
Yuan Zhou
776133bd29 Clean up raw policy stats in account stat
Storage policy stats was not well parsed in account stat. This
patch parses the stats and print out the stats in a format like below:

$swift -A http://swift_cluster/auth/v1.0 -U test:tester -K testing stat
                   Account: AUTH_test
                Containers: 5
                   Objects: 1
                     Bytes: 2097152
Objects in policy "golden": 1
 Bytess in policy "golden": 2097152
Objects in policy "silver": 0
  Bytes in policy "silver": 0
               X-Timestamp: 1404697760.88809
                X-Trans-Id: txec519e24b44a413abb705-0053da2dcb
              Content-Type: text/plain; charset=utf-8
             Accept-Ranges: bytes

Change-Id: I7ad0ee6d88f8393e3a93e90cd52b9b592da7072d
2014-08-06 16:07:17 +08:00
anc
cae12940b1 Add keystone v3 auth support
Enables swiftclient to authenticate using
the keystone v3 API, allowing user id's, user
domains and tenant/project domains to be
specified.

Since swiftclient imports keystoneclient, the
main changes in swiftclient/client.py are to
selectively import the correct keystoneclient
library version and pass a number of new
options to it via the get_auth() function. In
addition the get_keystoneclient_2_0 method
has been renamed get_auth_keystone to better
reflect its purpose since it now deals with
both v2 and v3 use cases.

In swiftclient/shell.py the new options are
added to the parser. To make the default help
message shorter, help for all the --os-*
options (including the existing v2 options)
is only displayed when explicitly requested
usng a new --os-help option.

A new set of unit tests is added to
test_shell.py to verify the parser. A comment
in tests/sample.conf explains how to
configure the existing functional tests to
run using keystone v3 API.

Note that to use keystone v3
with swift you will need to set
auth_version = v3.0 in the auth_token
middleware config section of
proxy-server.conf.

Change-Id: Ifda0b3263eb919a8c6a1b204ba0a1215ed6f642f
2014-07-23 16:55:38 +01:00
Alistair Coles
394cb57f63 Fix context sensitive help for info and tempurl
Make it so that swift <cmd> --help will print the info
subcommand help for info and tempurl just like all the
other subcommands.

Also add unit tests to verify subcommand help.

Change-Id: Id3666dcf72a9727fbfda2f74c23293ada1c53aa0
2014-07-21 15:27:40 -07:00
Jenkins
f9ea672322 Merge "Allow to specify storage policy when uploading objects" 2014-07-21 21:43:59 +00:00
Yuan Zhou
defdf1929c Allow to specify storage policy when uploading objects
Client already supports -H/--header option when creating container
or uploading objects. This patch extends this option to support
Storage Policy.

e.g.,
    swift post con -H 'X-Storage-Policy:p1'
This creates one container 'con' with storage policy 'p1'.

    swift upload con obj -H 'X-Storage-Policy:p2'
This creates container 'con' with storage policy 'p2' and uploads
object 'obj' into it.

Also fixes segmented uploading to non-default storage policy container

When uploading large objects with segmentation to container with
non-default storage policy, there will be another 'xxx_segments'
container created, but with the default storage policy. This
results all the segments to be stored with the wrong policy.

This patch is for the Storage Policy feature, and also
compatible with old versions w/o Storage Policy support.

Change-Id: I5c19e90604a0bcf2c85e1732b8a0b97ae6801994
2014-07-17 11:44:03 +08:00
Josh Gachnang
def0e0a643 Adding Swift Temporary URL support
Temporary URLs allow a user to sign an object URL with a shared
secret to so that the object can be downloaded without auth for
a specified amount of time.

http://docs.openstack.org/trunk/config-reference/content/object-storage-tempurl.html

Change-Id: Ife0b6c98c975e074d4dad0a31145573b784747c5
2014-07-11 13:05:22 -07:00
Christian Berendt
d08bd458be fixed several pep8 issues
* E111 indentation is not a multiple of four
  * E128 continuation line under-indented for visual indent
  * E265 block comment should start with '# '
  * E713 test for membership should be 'not in'

Tested with pep8 version 1.5.6 (2014-04-14).

Change-Id: I7f85c143d463c501a0df20724362ad5c0f2d4dde
2014-05-21 16:31:53 +00:00
anc
e7dc854257 Move test_shell.py under tests/unit/
Looks like test_shell.py got left behind when
the unit tests were relocated under tests/unit.
As a consequence they weren't included in tox
runs.

Change-Id: I93435e6141c5e3ad4a9403626f226d7c760cbd7c
2014-05-20 11:21:11 +01:00