def testWrongContentType(self): with DatabaseFixture() as dbFixture: clientFixture = ClientFixture(dbFixture.db) headers = { 'Content-Type': 'bad/header', 'Accept': 'application/json' } response = self.app.post(url_for(controller='/conversions'), '', headers, extra_environ=GetEnviron(clientFixture), status=httplib.BAD_REQUEST)
def testCreateJobFromMessage(self): """ Test that a job is created properly from a JSON message """ # Trying to test that objects are correctly constructed but # don't really care about interactions yet. message = { 'input': { 'url': 'http://filehost.yourcorp.com/txpeng540.exe' }, 'installation_command': '/S /V/quiet', } createDownloader = Mock() createConversionRunner = Mock() with DatabaseFixture() as f: factory = JobFactory( f.db, stubs.StubWorkPool(), createDownloader, createConversionRunner, createInflater=InflaterFactory(), ).Create job = factory(message) createDownloader.assert_called_with('job-%d' % job.id, message['input']['url'], SameType(CancellationFlag)) createConversionRunner.assert_called_with( message['installation_command'], SameType(CancellationFlag))
def testConverterFromCIFSInput(self): message = { 'input': { 'url': 'cifs://filehost.yourcorp.com/packages/input/TextPad', 'login': '******', 'password': '******' }, 'output': { 'url': 'cifs://filehost.yourcorp.com/packages/output/TextPad', 'login': '******', 'password': '******' }, 'installation_command': '/S /V/quiet', 'vm': { 'href': 'http://localhost:9081/pools/win-xp-sp3' } } headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } with DatabaseFixture() as dbFixture: clientFixture = ClientFixture(dbFixture.db) response = self.app.post(url_for(controller='/conversions'), json.dumps(message), headers, extra_environ=GetEnviron(clientFixture)) assert response.status_int == httplib.CREATED msg = response.json assert isinstance(msg['id'], int)
def testSuccessIsFalseWhenRunnerFails(self): """ Test that success is False when the runner fails """ with DatabaseFixture() as f: job = CreateStubbedJob(f.db, runner=stubs.StubConverterRunnerFails()) job.Run() j = f.session.query(model.Job).get(job.id) # Check by identity to ensure it's not None. assert j.success is False
def testDownloaderCalledDuringRun(self): """ Test that the downloader is proplery called when running """ downloader = Mock() with DatabaseFixture() as f: job = CreateStubbedJob(f.db, downloader=downloader) job.Run() downloader.Acquire.assert_called_with() downloader.Release.assert_called_with()
def testThatExceptionMarksJobAsFailed(self): """ Test that a job is properly marked as failed when an exception is raised in the WorkPool """ with DatabaseFixture() as f: job = CreateStubbedJob(f.db, workpool=stubs.WorkPoolFailsOnAcquire()) job.Run() j = f.session.query(model.Job).get(job.id) assert j.events[-1].name == 'finished' assert j.success is False
def testStatusGoesZeroToOneHundred(self): """ Test that job goes from 0 to 100 percent """ with DatabaseFixture() as f: job = CreateStubbedJob(f.db) j = f.session.query(model.Job).get(job.id) assert j.percent == 0 job.Run() f.session.refresh(j) assert j.percent == 100
def testInflateIsCorrectlyCalled(self): """ Test that inflate is correctly called during a job run """ with DatabaseFixture() as f: inflaterFactory = Mock() inflater = Mock() job = CreateStubbedJob(f.db, runner=stubs.StubConverterRunnerSucceeds(), downloader=stubs.ZipStubDownloader(), createInflater=inflaterFactory) inflaterFactory.Create.return_value = inflater inflater.Inflate.return_value = '/tmp/inflated/job-n' job.Run() inflater.Inflate.assert_called_with()
def testSuccessIsTrueWhenRunnerFails(self): """ Test that success is True when the runner succeeds """ with DatabaseFixture() as f: job = CreateStubbedJob(f.db, runner=stubs.StubConverterRunnerSucceeds()) j = f.session.query(model.Job).get(job.id) # Success is only set at the end of the job once a runner result # is known. assert j.success is None job.Run() f.session.refresh(j) assert j.success
def testAllEventsFire(self): """ Test that events are fired for each action """ with DatabaseFixture() as f: job = CreateStubbedJob(f.db) job._downloader = stubs.StubDownloader() receieved = [] q = job.Subscribe() job.Run() for i in JOB_STATES: n = q.get() assert n == i receieved.append(n) assert receieved == JOB_STATES
def testDispositionIsSuccessOnSuccess(self): with DatabaseFixture() as dbFixture: clientFixture = ClientFixture(dbFixture.db) jobFixture = JobFixture(dbFixture.db) job = jobFixture.CreateSucceededJob() headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } response = self.app.get(url_for(controller='/conversions', action='show', id=job.id), None, headers, extra_environ=GetEnviron(clientFixture)) assert response.json == { 'id': job.id, 'state': 'finished', 'percent': 100, 'result': { 'disposition': 'success', 'files': [{'url': 'http://localhost/packages/job-%d/TestPackage.exe' % job.id, 'size': 1050, 'fileName': 'TestPackage.exe'}] } }
def testDispositionIsFailureOnFailure(self): with DatabaseFixture() as dbFixture: clientFixture = ClientFixture(dbFixture.db) jobFixture = JobFixture(dbFixture.db) job = jobFixture.CreateFailedJob() headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } response = self.app.get(url_for(controller='/conversions', action='show', id=job.id), None, headers, extra_environ=GetEnviron(clientFixture)) assert response.json == { 'id': job.id, 'state': 'finished', 'percent': 100, 'result': { 'disposition': 'failure', 'files': [] } }
def testWrongAcceptHeaders(self): with DatabaseFixture() as dbFixture: clientFixture = ClientFixture(dbFixture.db) headers = { 'Accept': 'bad/header' } response = self.app.post(url_for(controller='/conversions'), '', headers, extra_environ=GetEnviron(clientFixture), status=httplib.NOT_ACCEPTABLE)