github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/gubernator/gcs_async_test.py (about) 1 # Copyright 2016 The Kubernetes Authors. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 15 import json 16 import unittest 17 import urlparse 18 19 import cloudstorage as gcs 20 import webtest 21 22 import gcs_async 23 24 app = webtest.TestApp(None) 25 26 27 def write(path, data): 28 if not isinstance(data, basestring): 29 data = json.dumps(data) 30 with gcs.open(path, 'w') as f: 31 f.write(data) 32 33 34 def install_handler_dispatcher(stub, matches, dispatch): 35 def fetch_stub(url, payload, method, headers, request, response, 36 follow_redirects=False, deadline=None, 37 validate_certificate=None): 38 # pylint: disable=too-many-arguments,unused-argument 39 result, code = dispatch(method, url, payload, headers) 40 response.set_statuscode(code) 41 response.set_content(result) 42 header = response.add_header() 43 header.set_key('content-length') 44 header.set_value(str(len(result))) 45 46 # this is gross, but there doesn't appear to be a better way 47 # pylint: disable=protected-access 48 stub._urlmatchers_to_fetch_functions.append((matches, fetch_stub)) 49 50 51 def install_handler(stub, structure, base='pr-logs/pull/'): 52 ''' 53 Add a stub to mock out GCS JSON API ListObject requests-- with 54 just enough detail for our code. 55 56 This is based on google.appengine.ext.cloudstorage.stub_dispatcher. 57 58 Args: 59 stub: a URLFetch stub, to register our new handler against. 60 structure: a dictionary of {paths: subdirectory names}. 61 This will be transformed into the (more verbose) form 62 that the ListObject API returns. 63 ''' 64 prefixes_for_paths = {} 65 66 for path, subdirs in structure.iteritems(): 67 path = base + path 68 prefixes_for_paths[path] = ['%s%s/' % (path, d) for d in subdirs] 69 70 def matches(url): 71 return url.startswith(gcs_async.STORAGE_API_URL) 72 73 def dispatch(method, url, _payload, _headers): 74 if method != 'GET': 75 raise ValueError('unhandled method %s' % method) 76 parsed = urlparse.urlparse(url) 77 param_dict = urlparse.parse_qs(parsed.query, True) 78 prefix = param_dict['prefix'][0] 79 return json.dumps({'prefixes': prefixes_for_paths[prefix]}), 200 80 81 install_handler_dispatcher(stub, matches, dispatch) 82 83 84 class GCSAsyncTest(unittest.TestCase): 85 def setUp(self): 86 self.testbed.init_memcache_stub() 87 self.testbed.init_app_identity_stub() 88 self.testbed.init_urlfetch_stub() 89 self.testbed.init_blobstore_stub() 90 self.testbed.init_datastore_v3_stub() 91 # redirect GCS calls to the local proxy 92 gcs_async.GCS_API_URL = gcs.common.local_api_url() 93 94 def test_read(self): 95 write('/foo/bar', 'test data') 96 self.assertEqual(gcs_async.read('/foo/bar').get_result(), 'test data') 97 self.assertEqual(gcs_async.read('/foo/quux').get_result(), None) 98 99 def test_listdirs(self): 100 install_handler(self.testbed.get_stub('urlfetch'), 101 {'foo/': ['bar', 'baz']}, base='base/') 102 self.assertEqual(gcs_async.listdirs('buck/base/foo/').get_result(), 103 ['buck/base/foo/bar/', 'buck/base/foo/baz/'])