| Index: chrome/test/functional/ispy/ispy_core/Tests/BucketManagerTests/DependancyInjection/boto_testing_injector.py
|
| diff --git a/chrome/test/functional/ispy/ispy_core/Tests/BucketManagerTests/DependancyInjection/boto_testing_injector.py b/chrome/test/functional/ispy/ispy_core/Tests/BucketManagerTests/DependancyInjection/boto_testing_injector.py
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..4e32899bda003521dcbbbb2f665457ce6cb992f8
|
| --- /dev/null
|
| +++ b/chrome/test/functional/ispy/ispy_core/Tests/BucketManagerTests/DependancyInjection/boto_testing_injector.py
|
| @@ -0,0 +1,274 @@
|
| +# Copyright (c) 2013 The Chromium Authors. All rights reserved.
|
| +# Use of this source code is governed by a BSD-style license that can be
|
| +# found in the LICENSE file.
|
| +
|
| +"""A subclass of the BotoInjector used for testing."""
|
| +
|
| +from Tests.BucketManagerTests.DependancyInjection import boto_injector
|
| +
|
| +
|
| +class TestingURI(object):
|
| + """A token class representing the uri returned by the injector."""
|
| +
|
| + def __init__(self, uri_str, default_scheme):
|
| + """Instantiates a TestingURI object.
|
| +
|
| + Args:
|
| + uri_str: the uri naming string to connect to.
|
| + default_scheme: the type of database to access, in our case: 'gs'.
|
| +
|
| + Returns:
|
| + an instance of TestingURI.
|
| + """
|
| + self.uri_str = uri_str
|
| + self.default_scheme = default_scheme
|
| +
|
| +
|
| +class TestingConnection(object):
|
| + """A token class representing a connection returned by the injector."""
|
| +
|
| + def __init__(self, uri, key, secret):
|
| + """Instantiates a TestingConnection object.
|
| +
|
| + Args:
|
| + uri: a TestingURI object to connect to.
|
| + key: the key used to connect to the uri.
|
| + secret: the secret used to connect to the uri.
|
| +
|
| + Returns:
|
| + an instance of TestingConnection.
|
| + """
|
| + self.uri = uri
|
| + self.key = key
|
| + self.secret = secret
|
| + self.uri = uri
|
| +
|
| +
|
| +class TestingBucket(object):
|
| + """A token class representing a Bucket returned by the injector."""
|
| +
|
| + def __init__(self, connection, bucket_name):
|
| + """Instantiates a TestingBucket object.
|
| +
|
| + Args:
|
| + connection: a TestingConnection object to get the bucket from.
|
| + bucket_name: the name of the bucket to connect to.
|
| +
|
| + Returns:
|
| + an instance of TestingBucket.
|
| + """
|
| + self.connection = connection
|
| + self.bucket_name = bucket_name
|
| + self.uri = connection.uri
|
| + self.key = connection.key
|
| + self.secret = connection.secret
|
| +
|
| +
|
| +class TestingKey(boto_injector.Key):
|
| + """A class representing a key to an item in a bucket."""
|
| +
|
| + def __init__(self, bucket, injector):
|
| + """Initializes the Key from a bucket and injector.
|
| +
|
| + Args:
|
| + bucket: the bucket to initialize the key from.
|
| + injector: the TestingInjector for the key which will serve
|
| + as its database during testing.
|
| +
|
| + Returns:
|
| + a TestingKey object.
|
| + """
|
| + self.bucket = bucket
|
| + self.injector = injector
|
| + self.meta_data = {}
|
| + self.path = ''
|
| + self.contents = ''
|
| +
|
| + def _LoadFromDatastore(self, entry):
|
| + """Loads this key from a key already in the injector's datastore.
|
| +
|
| + Args:
|
| + entry: the already existing key to load meta_data,
|
| + path, and contents from.
|
| +
|
| + Returns:
|
| + None.
|
| + """
|
| + self.meta_data = entry.meta_data
|
| + self.path = entry.path
|
| + self.contents = entry.contents
|
| +
|
| + def exists(self):
|
| + """Returns whether or not the key exists in the injector's datastore.
|
| +
|
| + Args:
|
| + None.
|
| +
|
| + Returns:
|
| + A boolean representing whether or not the key exists in the datastore.
|
| + """
|
| + return self.path in self.injector.datastore.keys()
|
| +
|
| + def set_metadata(self, name, value):
|
| + """Sets a field in the key's metadata.
|
| +
|
| + Args:
|
| + name: the name of the field in the key's metadata to set.
|
| + value: the value to set the given field to.
|
| +
|
| + Returns:
|
| + None.
|
| + """
|
| + self.meta_data[name] = value
|
| +
|
| + def set_path(self, path):
|
| + """Sets the path that the key points to.
|
| +
|
| + This function sets the path of the key, then checks to see
|
| + if the Key's injector has a reference to a key pointing
|
| + to the same path in the datastore. If it does, it will load
|
| + the relevant information into this key.
|
| +
|
| + Args:
|
| + path: the path to set the key to.
|
| +
|
| + Returns:
|
| + None.
|
| + """
|
| + self.path = path
|
| + if path in self.injector.datastore.keys():
|
| + self._LoadFromDatastore(self.injector.datastore[path])
|
| +
|
| + def set_contents_from_string(self, contents):
|
| + """Sets the contents of the key and sends it to the datastore.
|
| +
|
| + Args:
|
| + contents: the value to set the key's contents to.
|
| +
|
| + Returns:
|
| + None.
|
| + """
|
| + self.contents = contents
|
| + self.injector.datastore[self.path] = self
|
| +
|
| + def get_contents_as_string(self):
|
| + """Gets the contents of a given key.
|
| +
|
| + Args:
|
| + None.
|
| +
|
| + Returns:
|
| + a string representing the contents of key.
|
| + """
|
| + return self.contents
|
| +
|
| + def generate_url(self, timeout):
|
| + """Gets a url to the object pointed to by this key in the datastore.
|
| +
|
| + For the purposes of testing, the 'url' returned by this function is
|
| + simply the filepath of the object in the datastore.
|
| +
|
| + Args:
|
| + timeout: a token timeout variable representing how long the
|
| + url is valid for.
|
| +
|
| + Returns:
|
| + a string representing the url to the object in the datastore.
|
| + """
|
| + return self.path
|
| +
|
| +
|
| +class TestingInjector(boto_injector.BotoInjector):
|
| + """A subclass of the BotoInjector to be used for testing."""
|
| +
|
| + def __init__(self):
|
| + """Initializes a TestingInjector object.
|
| +
|
| + Args:
|
| + None.
|
| +
|
| + Returns:
|
| + A TestingInjector object.
|
| + """
|
| + self.datastore = {}
|
| +
|
| + def reset(self):
|
| + """Resets the datastore of the injector.
|
| +
|
| + This function sets the datastore map of the object to an empty
|
| + dictionary. This is used to clear the state of the injector
|
| + between tests.
|
| +
|
| + Args:
|
| + None.
|
| +
|
| + Returns:
|
| + None.
|
| + """
|
| + self.datastore = {}
|
| +
|
| + def storage_uri(self, uri_str, default_scheme):
|
| + """Returns a TestingURI object.
|
| +
|
| + Args:
|
| + uri_str: the uri naming string to connect to.
|
| + default_scheme: the type of database to access, in our case: 'gs'.
|
| +
|
| + Returns:
|
| + an instance of a TestingURI object.
|
| + """
|
| + self.uri = TestingURI(uri_str, default_scheme)
|
| + return self.uri
|
| +
|
| + def connect(self, uri, key, secret):
|
| + """Returns a TestingConnection object to a key and secret.
|
| +
|
| + Args:
|
| + uri: the TestingURI to connect to.
|
| + key: the key used to connect to the uri.
|
| + secret: the secret used to connect to the uri.
|
| +
|
| + Returns:
|
| + an instance of a TestingConnection object.
|
| + """
|
| + return TestingConnection(uri, key, secret)
|
| +
|
| + def get_bucket(self, connection, bucket_name):
|
| + """Returns a TestingBucket of a given name.
|
| +
|
| + Args:
|
| + connection: the TestingConnection object to get the TestingBucket
|
| + from.
|
| + bucket_name: the name of the bucket to access.
|
| +
|
| + Returns:
|
| + an instance of a TestingBucket object.
|
| + """
|
| + return TestingBucket(connection, bucket_name)
|
| +
|
| + def get_key(self, bucket):
|
| + """Produces a TestingKey from a TestingBucket.
|
| +
|
| + Args:
|
| + bucket: the TestingBucket object to get the key from.
|
| +
|
| + Returns:
|
| + an instance of a testing key from a given bucket.
|
| + """
|
| + return TestingKey(bucket, self)
|
| +
|
| + def get_all_keys(self, bucket, prefix):
|
| + """A function that gets all the keys from a given bucket.
|
| +
|
| + Args:
|
| + bucket: the TestingBucket object to get keys from.
|
| + prefix: a prefix to limit results to keys starting with.
|
| +
|
| + Returns:
|
| + a list of keys from the datastore that start with the given prefix.
|
| + """
|
| + return [
|
| + self.datastore[path]
|
| + for path in self.datastore.keys()
|
| + if path.startswith(prefix)
|
| + ]
|
|
|