def test_hash(self): with mock.open() as a: a.write('the quick brown fox\n') a.seek(0) A = file_hash('foo') with mock.open() as b: b.write('the quick brown bar\n') b.seek(0) B = file_hash('bar') self.assertNotEqual(A, B)
def test_read_pid_bogus_pidfile(self): with mock.open() as s: s.write('eighteensixteen\n') s.seek(0) p = Pidfile('/var/pid') with self.assertRaises(ValueError): p.read_pid()
def test_read_pid_raises_IOError(self): exc = IOError() exc.errno = errno.EAGAIN with mock.open(side_effect=exc): p = Pidfile('/var/pid') with self.assertRaises(IOError): p.read_pid()
def test_read_pid_partially_written(self): with mock.open() as s: s.write('1816') s.seek(0) p = Pidfile('/var/pid') with self.assertRaises(ValueError): p.read_pid()
def test_setup_registry_complete(self, dis, reg, key='KEY', cert='CERT'): calls = [0] def effect(*args): try: m = Mock() m.read.return_value = 'B' if calls[0] else 'A' return m finally: calls[0] += 1 self.app.conf.task_serializer = 'auth' with mock.open(side_effect=effect): with patch('celery.security.registry') as registry: store = Mock() self.app.setup_security(['json'], key, cert, store) dis.assert_called_with(['json']) reg.assert_called_with('A', 'B', store, 'sha1', 'json') registry._set_default_serializer.assert_called_with('auth')
def test_init(self, Certificate, glob, isdir): cert = Certificate.return_value = Mock() cert.has_expired.return_value = False isdir.return_value = True glob.return_value = ['foo.cert'] with mock.open(): cert.get_id.return_value = 1 x = FSCertStore('/var/certs') self.assertIn(1, x._certs) glob.assert_called_with('/var/certs/*') # they both end up with the same id glob.return_value = ['foo.cert', 'bar.cert'] with self.assertRaises(SecurityError): x = FSCertStore('/var/certs') glob.return_value = ['foo.cert'] cert.has_expired.return_value = True with self.assertRaises(SecurityError): x = FSCertStore('/var/certs') isdir.return_value = False with self.assertRaises(SecurityError): x = FSCertStore('/var/certs')
def test_read_pid_raises_ENOENT(self): exc = IOError() exc.errno = errno.ENOENT with mock.open(side_effect=exc): p = Pidfile('/var/pid') self.assertIsNone(p.read_pid())
def test_read_pid(self): with mock.open() as s: s.write('1816\n') s.seek(0) p = Pidfile('/var/pid') self.assertEqual(p.read_pid(), 1816)