def async_runner(timeout): """ Make a ``RunTest`` instance for asynchronous tests. :param timedelta timeout: The maximum length of time that a test is allowed to take. """ # XXX: Looks like the acceptance tests (which were the first tests that we # tried to migrate) aren't cleaning up after themselves even in the # successful case. Use the RunTest that loops the reactor a couple of # times after the test is done. return AsynchronousDeferredRunTestForBrokenTwisted.make_factory( timeout=timeout.total_seconds())
def async_runner(timeout): """ Make a ``RunTest`` instance for asynchronous tests. :param timedelta timeout: The maximum length of time that a test is allowed to take. """ # XXX: The acceptance tests (which were the first tests that we tried to # migrate) aren't cleaning up after themselves even in the successful # case. Use AsynchronousDeferredRunTestForBrokenTwisted, which loops the # reactor a couple of times after the test is done. async_factory = AsynchronousDeferredRunTestForBrokenTwisted.make_factory( timeout=timeout.total_seconds(), suppress_twisted_logging=False, store_twisted_logs=False, ) return retry_flaky(async_factory)
def async_runner(timeout, flaky_output=None): """ Make a ``RunTest`` instance for asynchronous tests. :param timedelta timeout: The maximum length of time that a test is allowed to take. :param file flaky_output: A file-like object to which we'll send output about flaky tests. This is a temporary measure until we fix FLOC-3469, at which point we will just use standard logging. """ if flaky_output is None: flaky_output = sys.stdout # XXX: Looks like the acceptance tests (which were the first tests that we # tried to migrate) aren't cleaning up after themselves even in the # successful case. Use AsynchronousDeferredRunTestForBrokenTwisted, which # loops the reactor a couple of times after the test is done. return retry_flaky( AsynchronousDeferredRunTestForBrokenTwisted.make_factory( timeout=timeout.total_seconds()), output=flaky_output, )
class IntegrationTest(UnitTest): run_tests_with = AsynchronousDeferredRunTestForBrokenTwisted.make_factory( timeout=10) resources = [('rabbit', FixtureResource(RabbitServerWithoutReset()))]
class AMQTest(TestCase): run_tests_with = AsynchronousDeferredRunTestForBrokenTwisted.make_factory( timeout=5) VHOST = "/" USER = "******" PASSWORD = "******" @skip( "RabbitMQ is not yet a required component " "of a running MAAS pserv instance.") def setUp(self): """ At each run, we delete the test vhost and recreate it, to be sure to be in a clean environment. """ super(AMQTest, self).setUp() self.rabbit = RabbitServer() self.useFixture(self.rabbit) self.queues = set() self.exchanges = set() self.connected_deferred = Deferred() self.factory = AMQFactory( self.USER, self.PASSWORD, self.VHOST, self.amq_connected, self.amq_disconnected, self.amq_failed) self.factory.initialDelay = 2.0 self.connector = reactor.connectTCP( self.rabbit.config.hostname, self.rabbit.config.port, self.factory) return self.connected_deferred @inlineCallbacks def tearDown(self): # XXX: Moving this up here to silence a nigh-on inexplicable error # that occurs when it's at the bottom of the function. self.factory.stopTrying() self.connector.disconnect() super(AMQTest, self).tearDown() # XXX: This is only safe because we tear down the whole server. # We can't run this after the tearDown above, because the # fixture is gone. return self.connected_deferred = Deferred() factory = AMQFactory( self.USER, self.PASSWORD, self.VHOST, self.amq_connected, self.amq_disconnected, self.amq_failed) connector = reactor.connectTCP( self.rabbit.config.hostname, self.rabbit.config.port, factory) yield self.connected_deferred channel_id = 1 for queue in self.queues: try: yield self.channel.queue_delete(queue=queue) except Closed: channel_id += 1 self.channel = yield self.client.channel(channel_id) yield self.channel.channel_open() for exchange in self.exchanges: try: yield self.channel.exchange_delete(exchange=exchange) except Closed: channel_id += 1 self.channel = yield self.client.channel(channel_id) yield self.channel.channel_open() factory.stopTrying() connector.disconnect() def amq_connected(self, client_and_channel): """ Save the channel and client, and fire C{self.connected_deferred}. This is the connected_callback that's pased to the L{AMQFactory}. """ client, channel = client_and_channel self.real_queue_declare = channel.queue_declare channel.queue_declare = self.queue_declare self.real_exchange_declare = channel.exchange_declare channel.exchange_declare = self.exchange_declare self.channel = channel self.client = client self.connected_deferred.callback(None) def amq_disconnected(self): """ This is the disconnected_callback that's passed to the L{AMQFactory}. """ def amq_failed(self, connector_and_reason): """ This is the failed_callback that's passed to the L{AMQFactory}. """ connector, reason = connector_and_reason self.connected_deferred.errback(reason) def queue_declare(self, queue, **kwargs): """ Keep track of the queue declaration, and forward to the real queue_declare function. """ self.queues.add(queue) return self.real_queue_declare(queue=queue, **kwargs) def exchange_declare(self, exchange, **kwargs): """ Keep track of the exchange declaration, and forward to the real exchange_declare function. """ self.exchanges.add(exchange) return self.real_exchange_declare(exchange=exchange, **kwargs)
class TestSlaveWithLibrarian(TestCaseWithFactory): """Tests that need more of Launchpad to run.""" layer = LaunchpadZopelessLayer run_tests_with = AsynchronousDeferredRunTestForBrokenTwisted.make_factory( timeout=20) def setUp(self): super(TestSlaveWithLibrarian, self).setUp() self.slave_helper = self.useFixture(SlaveTestHelpers()) def test_ensurepresent_librarian(self): # ensurepresent, when given an http URL for a file will download the # file from that URL and report that the file is present, and it was # downloaded. # Use the Librarian because it's a "convenient" web server. lf = self.factory.makeLibraryFileAlias( 'HelloWorld.txt', content="Hello World") self.layer.txn.commit() self.slave_helper.getServerSlave() slave = self.slave_helper.getClientSlave() d = slave.ensurepresent( lf.content.sha1, lf.http_url, "", "") d.addCallback(self.assertEqual, [True, 'Download']) return d def test_retrieve_files_from_filecache(self): # Files that are present on the slave can be downloaded with a # filename made from the sha1 of the content underneath the # 'filecache' directory. content = "Hello World" lf = self.factory.makeLibraryFileAlias( 'HelloWorld.txt', content=content) self.layer.txn.commit() expected_url = '%s/filecache/%s' % ( self.slave_helper.BASE_URL, lf.content.sha1) self.slave_helper.getServerSlave() slave = self.slave_helper.getClientSlave() d = slave.ensurepresent( lf.content.sha1, lf.http_url, "", "") def check_file(ignored): d = getPage(expected_url.encode('utf8')) return d.addCallback(self.assertEqual, content) return d.addCallback(check_file) def test_getFiles(self): # Test BuilderSlave.getFiles(). # It also implicitly tests getFile() - I don't want to test that # separately because it increases test run time and it's going # away at some point anyway, in favour of getFiles(). contents = ["content1", "content2", "content3"] self.slave_helper.getServerSlave() slave = self.slave_helper.getClientSlave() filemap = {} content_map = {} def got_files(ignored): # Called back when getFiles finishes. Make sure all the # content is as expected. for sha1 in filemap: local_file = filemap[sha1] file = open(local_file) self.assertEqual(content_map[sha1], file.read()) file.close() def finished_uploading(ignored): d = slave.getFiles(filemap) return d.addCallback(got_files) # Set up some files on the builder and store details in # content_map so we can compare downloads later. dl = [] for content in contents: filename = content + '.txt' lf = self.factory.makeLibraryFileAlias(filename, content=content) content_map[lf.content.sha1] = content fd, filemap[lf.content.sha1] = tempfile.mkstemp() self.addCleanup(os.remove, filemap[lf.content.sha1]) self.layer.txn.commit() d = slave.ensurepresent(lf.content.sha1, lf.http_url, "", "") dl.append(d) return defer.DeferredList(dl).addCallback(finished_uploading)