summaryrefslogtreecommitdiff
path: root/tests/unit/utils.py
Commit message (Collapse)AuthorAgeFilesLines
* Rename "tests" directory to be "test" like in the swift repoTim Burke2019-11-061-582/+0
| | | | | | | | | | | | | In addition to being less confusing for devs, this lets us actually run tempauth tests in swiftclient dsvm jobs. The job definition (over in the swift repo) specifies test/sample.conf, which does not exist in this repo. As a result, those tests would skip with SKIPPING FUNCTIONAL TESTS DUE TO NO CONFIG Change-Id: I558dbf9a657d442e6e19468e543bbec855129eeb
* Merge "Support pdb in tests better"Zuul2019-07-101-2/+14
|\
| * Support pdb in tests betterClay Gerrard2017-06-131-2/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Not really "better" so much as "at all" - the thing we do with the capture stderr *everywhere* is probably brilliant - but absolutely not strictly necessary for every MockHttpTest TestCase and comes with the annoying overhead of trying to get into a debugger causes tests to hang inexplicably and you can't even do debug prints in tests!? Now if you add SWIFTCLIENT_DEBUG=1 to your nose -vsx command you can not only jump into debugger, but if you're "in the know" you could even get some stderr print debugging going on! If you're not "in the know" when you try to pdb.set_trace() the tests will blow-up for you because we monkeypatch pdb when not in SWIFTCLIENT_DEBUG mode, you're welcome. Change-Id: I21298bfd39fe386b5ea19e3a6f4408d8a0459c92
* | Stop leaking quite so many connectionsTim Burke2018-11-091-2/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | While investigating the failures when you move func tests to py3, I noticed a whole bunch of ResourceWarning: unclosed <socket.socket ...> noise. This should fix it. While we're at it, make get_capabilities less stupid. Change-Id: I3913e9334090b04a78143e0b70f621aad30fc642 Related-Change: I86d24104033b490a35178fc504d88c1e4a566628
* | Stop lazy importing keystoneclientTim Burke2018-09-071-8/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | There were two basic problems: - We'd try to import on every attempt at getting auth, even when we already know keystoneclient isn't available. - Sometimes devs would hit some crazy import race involving (some combination of?) greenthreads and OS threads. So let's just try the imports *once*, at import time, and have None sentinels if it fails. Try both versions separately to decouple failures; this should let us support a wider range of keystoneclient versions. Change-Id: I2367310aac74f1b7c5ea0cb1a822a491e4ba8e68
* | Allow for object uploads > 5GB from stdin.Timur Alperovich2018-01-181-0/+21
|/ | | | | | | | | | | | | When uploading from standard input, swiftclient should turn the upload into an SLO in the case of large objects. This patch picks the threshold as 10MB (and uses that as the default segment size). The consumers can also supply the --segment-size option to alter that threshold and the SLO segment size. The patch does buffer one segment in memory (which is why 10MB default was chosen). (test is updated) Change-Id: Ib13e0b687bc85930c29fe9f151cf96bc53b2e594
* Add __ne__ built-in functionyuyafei2016-07-051-0/+3
| | | | | | | | | | | | | In Python 3 __ne__ by default delegates to __eq__ and inverts the result, but in Python 2 they urge you to define __ne__ when you define __eq__ for it to work properly [1].There are no implied relationships among the comparison operators. The truth of x==y does not imply that x!=y is false. Accordingly, when defining __eq__(), one should also define __ne__() so that the operators will behave as expected. [1]https://docs.python.org/2/reference/datamodel.html#object.__ne__ Change-Id: I5485022f010cdcb88c0d4ebe5c44a39a1bf242b0
* Support client certificate/keyCedric Brandily2016-04-101-0/+6
| | | | | | | | | This change enables to specify a client certificate/key with: * usual CLI options (--os-cert/--os-key) * usual environment variables ($OS_CERT/$OS_KEY) Closes-Bug: #1565112 Change-Id: I12e151adcb6084d801c6dfed21d82232a3259aea
* Include response headers in ClientExceptionsTim Burke2016-03-031-8/+13
| | | | | | | | | | | Now, client applications can get to things like transaction IDs for failures without needing to turn on all of logging. While we're at it, add a from_response factory method for ClientException. Co-Authored-By: Alexander Corwin <ancorwin@gmail.com> Change-Id: Ib46d5f8fc7f36f651f5908bb9d900316fdaebce3
* Drop testtools from test-requirements.txtTim Burke2016-02-121-2/+1
| | | | | | | | | | | My understanding is that it was mainly being used so we could have sane testing on py26. With py26 support being dropped, we no longer need it. Also drop discover from test-requirements.txt, as we don't seem to actually use it. Change-Id: Iee04c42890596d3b483c1473169480a3ae19aac8 Related-Change: I37116731db11449d0c374a6a83a3a43789a19d5f
* Merge "Accept token and tenant_id for authenticating against KS"Jenkins2016-02-101-4/+4
|\
| * Accept token and tenant_id for authenticating against KSPratik Mallya2016-01-181-4/+4
| | | | | | | | | | | | | | | | | | | | | | Allow swiftclient to authenticate against keystone using tenant name/id and token only. Without this patch, the password is required, which may not always be available. Authentication against keystone is required to get the service catalog, which includes the endpoints for swift. Change-Id: I4477af445474c5fa97ff864c4942f1330b59e5d6 Closes-Bug: #1476002
* | Get rid of FakeConn cruftTim Burke2016-01-181-18/+0
| | | | | | | | | | | | | | Presumably, this was left over from before the httplib -> requests transition? Change-Id: I7f505514070bf9d8fefda77203bee78f0a5dd71d
* | Fix some typosTim Burke2015-12-301-1/+1
| | | | | | | | Change-Id: Iaf7f30a7ae0c2ac76fc5cdcee31ea74c08ce601e
* | Merge "Centralize header parsing"Jenkins2015-11-121-2/+2
|\ \
| * | Centralize header parsingTim Burke2015-09-031-2/+2
| |/ | | | | | | | | | | | | | | | | | | | | | | All response headers are now exposed as unicode objects. Any url-encoding is interpretted as UTF-8; if that causes decoding to fail, the url-encoded form is returned. As a result, deleting DLOs with unicode characters will no longer raise UnicodeEncodeErrors under Python 2. Related-Bug: #1431866 Change-Id: Idb111c5bf3ac1f5ccfa724b3f4ede8f37d5bfac4
* | Miscellaneous (mostly test) cleanupTim Burke2015-10-071-3/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Always use testtools.TestCase, since we're relying on testtools * Always use mock (as opposed to unittest.mock) since we're relying on mock * Add note about when a missing logging handler was added * Stop %-formatting the giant usage string that doesn't actually need any formatting * Prefer assertIs, assertIn, assertIsInstance over assertTrue * Use else-self.fail instead of sentinel values * Check resp.get('error') is None before checking resp['success'] is True, so test failures actually tell you something useful * Tighten some isinstance assertions * Import MockHttpTest from correct location * Only populate clean_os_environ once * Use setUp for setup, not __init__ * Replace assertIn(key, dict) and assertEqual(foo, dict[key]) with assertEqual(foo, dict.get(key)) when key is a literal and foo is not None * Use mock.patch.object instead of manually patching for tests * Use six.binary_type instead of type(''.encode('utf-8')) * Stop shadowing builtin bytes * Reclaim some margin * Stop checking the return-type of encode_utf8; we already know it's bytes Change-Id: I2138ea553378ce88810b7353147c8645a8f8c90e
* | Actually make assertions when testing get_account and get_containerTim Burke2015-09-221-1/+15
|/ | | | Change-Id: Ibb1301b00d1bc99ec089ead02f944aa94972120a
* fix old style class definition(H238)Hiroshi Miura2015-08-041-1/+1
| | | | | Change-Id: Ib5be06fa544f5eb3061c6a3077a3b9986382ecfe Signed-off-by: Hiroshi Miura <miurahr@nttdata.co.jp>
* Merge "Properly test raw writes in Python 3"Jenkins2015-07-161-13/+12
|\
| * Properly test raw writes in Python 3Tim Burke2015-07-071-13/+12
| | | | | | | | | | | | | | | | Previously we were trying to test writing bytes in Python 3 using only native (unicode) string objects. That doesn't test what we thought we were testing. Change-Id: I10a0a38143d7f7d850ab9a7005ad87f5d314c375
* | Add some bash helpers for auth stuffClay Gerrard2015-06-151-2/+4
|/ | | | Change-Id: If61ac9a050e7a115f37dbf4e74b904ac5dfd2052
* Merge "Allow reading from object body on download"Jenkins2015-06-041-1/+4
|\
| * Allow reading from object body on downloadStuart McLaren2015-03-241-1/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently, get_object returns a generator. This allows access to the object's data in multiples of 'resp_chunk_size'. This patch adds a read function to also allow accessing the object data using read(size). This allows, for example, the consumer of an object (where no byte range has been specified) to read up to certain boundaries while streaming to a new Large Object with segments of a specified size. Reading and chunking can be safely mixed. Related-Bug: 1367925 Co-Authored-By: Clay Gerrard <clay.gerrard@gmail.com> Change-Id: I1cfb67f45afc7015fd896f1a89bebae048871769
* | Merge "Add test for timeout being passed to keystone client"Jenkins2015-05-211-0/+49
|\ \
| * | Add test for timeout being passed to keystone clientAlistair Coles2015-04-281-0/+49
| | | | | | | | | | | | | | | | | | | | | | | | | | | Extends existing unit test for timeout being passed to get_auth to cover v2.0 auth when keystone client should get the timeout kwarg. Related-Bug: 1447847 Change-Id: Ie9cfc86fa2156b94b45d290ac12e3f71b20d6c4f
* | | Merge "Compare each chunk of large objects when uploading"Jenkins2015-04-301-1/+8
|\ \ \ | |/ / |/| |
| * | Compare each chunk of large objects when uploadingTim Burke2015-03-231-1/+8
| |/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Previously, we compared the ETag from Swift against the MD5 of the entire large object. However, the ETag for large objects is generally the MD5 of the concatenation of the ETags for each segment, unless the object is a DLO whose segments span more than one page of a container listing. Rather than worry about ETags, just compare each chunk of the segmented file. This allows the use of --skip-identical when uploading SLOs and DLOs. Additionally, there are several test-related improvements: * The default arguments for OutputManager are now evaluated on construction, rather than on definition, so that TestOutputManager.test_instantiation will succeed when using nosetest as a test runner. (See also: bug 1251507) * An account_username option is now available in the functional tests config file for auth systems that do not follow the account:username format. * CaptureOutput no longer writes to the captured stream, and MockHttpTest now captures output. These were polluting test output unnecessarily. (See also: bug 1201376) Change-Id: Ic484e9a0c186c9283c4012c6a2fa77b96b8edf8a Closes-Bug: #1201376 Closes-Bug: #1379252 Related-Bug: #1251507
* | Add socket-level read timeout parameterMonty Taylor2015-03-301-1/+1
| | | | | | | | | | | | | | | | | | | | | | The underlying requests library supports a timeout input parameter. The other python-*client libraries support passing it in, so while working on shade, it seemed like a great idea to be able to pass in timeout to swiftclient too. For reference, there's a change in shade: I095c1240693abf024bda2315dd77f4400b24a45b that shows interaction with the other libs, and a tentative patch that would consume this one. Change-Id: I699ebb1e092aa010af678de7ba15712da6ed5315
* | Add improvements to MD5 validation.Daniel Wakefield2015-03-041-1/+2
|/ | | | | | | | | | | | | | | With MD5Sum checking being added, a concern was brought up that It was a change with no possibility of reverting to the old behaviour. This change adds the flag '--ignore-checksum' to the upload subcommand allowing the checks to be turned off. Changed occurrences of the magic string for a null md5 to use a descriptive constant instead. Updated Error messages generated when validation fails. They should now be more descriptive and not output a literal newline sequence. Change-Id: Id1756cbb6700bb7e38f0ee0e75bc535e37f777ed
* Verify MD5 of uploaded objects.Daniel Wakefield2015-02-191-1/+1
| | | | | | | | | | | 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
* Merge "This patch fixes downloading files to stdout."Jenkins2015-02-031-0/+28
|\
| * This patch fixes downloading files to stdout.Joel Wright2015-01-231-0/+28
| | | | | | | | | | | | | | | | | | | | | | | | 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
* | Fix cross account upload using --os-storage-urlAlistair Coles2015-01-061-26/+56
|/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* Merge "Make preauth params work"Jenkins2014-12-311-24/+144
|\
| * Make preauth params workClay Gerrard2014-12-171-24/+144
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* | Change tests to use new CaptureOutput class.Daniel Wakefield2014-12-051-2/+18
|/ | | | | | | | | | 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
* Capture test output betterClay Gerrard2014-11-201-0/+72
| | | | | | | | 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
* Fix KeyError raised from client ConnectionAlistair Coles2014-10-201-0/+3
| | | | | | | | | | | | | | | | | Some client.Connection methods will raise a KeyError if a response_dict argument is passed and an error occurs during authentication or making the request. The fix is straightforward: add a test for existence of a response_dict before attempting to get it from kwargs. The bulk of this patch is adding unit tests for the response_dict feature. Closes-Bug: 1381304 Change-Id: Ic7e1b3dfae33909533931c52ac97355867a08a07
* Fix bug with some OS options not being passed to clientAlistair Coles2014-09-241-0/+54
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* Add keystone v3 auth supportanc2014-07-231-6/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* Add functional tests for python-swiftclientChristian Schwede2014-05-051-0/+154
Coverage for swiftclient.client is 71% with these tests. Unit tests have been moved into another subdirectory to separate them from functional tests. Change-Id: Ib8c4d78f7169cee893f82906f6388a5b06c45602