def test_RaiseError(self): l = log.Log(self.ctx, 'Path/To/Git/repo2', None) s = shell.LoggingShell(l, 'false') self.assertRaises(shell.ErrorExitCode, s.finish) l.close() self.assertTrue(self.logdata.getvalue().startswith('\n')) self.logdata.truncate(0)
def test_Empty(self): l = log.Log(self.ctx, 'Path/To/Git/repo2', None) l.close() thislog = '/'.join((self.logdir, 'repo2')) files = os.listdir(thislog) for filename in files: self.assertEqual(os.stat('/'.join((thislog, filename))).st_size, 0) self.assertEqual(len(files), 2)
def test_writeError(self): l = log.Log(self.ctx, 'Path/To/Git/repo2', None) l.writeError('this is a test\n') thislog = '/'.join((self.logdir, 'repo2')) files = os.listdir(thislog) err = [x for x in files if x.endswith('.err')][0] self.assertTrue('this is a test\n' in open('/'.join((self.logdir, 'repo2', err))).read()) l.close()
def test_ErrorOutput(self): l = log.Log(self.ctx, 'Path/To/Git/repo2', None) os.write(l.stderr, 'this is a test\n') l.close() thislog = '/'.join((self.logdir, 'repo2')) files = os.listdir(thislog) self.assertEqual(len([x for x in files if x.endswith('.err.gz')]), 1) self.assertEqual(len([x for x in files if x.endswith('.log')]), 1) self.assertEqual(len(files), 2) sizes = set(os.stat('/'.join((thislog, x))).st_size for x in files) self.assertEqual(len(sizes), 2)
def test_readShellOutputError(self): l = log.Log(self.ctx, 'Path/To/Git/repo2', None) retcode = shell.run(l, 'sh', '-c', 'echo bar; echo baz; echo foo >&2 ; exit 1', error=False) self.assertEqual(retcode, 1) self.assertEqual(l.lastOutput(), ('bar\nbaz\n', 'foo\n')) self.assertEqual(l.lastError(), 'foo\n') ao = mock.Mock() self.ctx.mails['Path/To/Git/repo2'].addOutput = ao l.mailLastOutput('broke') ao.assert_called_with('broke', 'bar\nbaz\n', 'foo\n')
def test_ErrorAndStandardOutputNoCompress(self): self.ctx._ac.set('global', 'compresslogs', 'false') l = log.Log(self.ctx, 'Path/To/Git/repo2', None) os.write(l.stdout, 'this is a test of standard output\n') os.write(l.stderr, 'this is a test of error output\n') l.close() thislog = '/'.join((self.logdir, 'repo2')) files = os.listdir(thislog) self.assertEqual(len([x for x in files if x.endswith('.gz')]), 0) self.assertEqual(len([x for x in files if x.endswith('.log')]), 1) self.assertEqual(len([x for x in files if x.endswith('.err')]), 1) self.assertEqual(len(files), 2) sizes = set(os.stat('/'.join((thislog, x))).st_size for x in files) self.assertTrue(0 not in sizes)
def test_run(self): l = log.Log(self.ctx, 'Path/To/Git/repo2', None) retcode = shell.run(l, 'false', error=False) self.assertEqual(retcode, 1) l.close() thislog = '/'.join((self.logdir, 'repo2')) files = os.listdir(thislog) for filename in files: fileName = '/'.join((thislog, filename)) logLines = gzip.GzipFile(fileName).readlines() nonLogLines = [x for x in logLines if not x.startswith('[')] self.assertEqual(len(nonLogLines), 0) self.assertTrue( logLines[-1].endswith(' COMPLETE with return code: 1\n')) self.assertEqual(self.logdata.getvalue(), '') self.logdata.truncate(0)
def test_ErrorAndStandardOutput(self): l = log.Log(self.ctx, 'Path/To/Git/repo2', None) os.write(l.stdout, 'this is a test of standard output\n') # warning of bad exit code will write to stderr s = shell.LoggingShell(l, 'false', error=False) retcode = s.finish() self.assertEqual(retcode, 1) self.assertEqual(l.lastOutput(), ('', '')) l.close() thislog = '/'.join((self.logdir, 'repo2')) files = os.listdir(thislog) self.assertEqual(len([x for x in files if x.endswith('.err.gz')]), 1) self.assertEqual(len([x for x in files if x.endswith('.log.gz')]), 1) self.assertEqual(len(files), 2) sizes = set(os.stat('/'.join((thislog, x))).st_size for x in files) self.assertTrue(0 not in sizes) self.assertEqual(self.logdata.getvalue(), '') self.logdata.truncate(0)
def test_readWithData(self): l = log.Log(self.ctx, 'Path/To/Git/repo2', None) retcode, output = shell.read(l, 'echo', 'foo') self.assertEqual(retcode, 0) self.assertEqual(output, 'foo\n') self.assertEqual(l.lastOutput(), ('', '')) l.close() thislog = '/'.join((self.logdir, 'repo2')) files = os.listdir(thislog) self.assertEqual(len(files), 2) for filename in files: fileName = '/'.join((thislog, filename)) logLines = gzip.GzipFile(fileName).readlines() nonLogLines = [x for x in logLines if not x.startswith('[')] self.assertEqual(len(nonLogLines), 0) self.assertTrue( logLines[-1].endswith(' COMPLETE with return code: 0\n')) self.assertEqual(self.logdata.getvalue(), '') self.logdata.truncate(0)
def test_Empty(self): l = log.Log(self.ctx, 'Path/To/Git/repo2', None) s = shell.LoggingShell(l, 'true') self.assertEqual(l.lastOutput(), (None, None)) self.assertEqual(l.lastError(), None) retcode = s.finish() self.assertEqual(l.lastOutput(), ('', '')) self.assertEqual(l.lastError(), '') self.assertEqual(retcode, 0) l.close() thislog = '/'.join((self.logdir, 'repo2')) files = os.listdir(thislog) for filename in files: filename = '/'.join((thislog, filename)) self.assertEqual([ x for x in gzip.GzipFile(filename).readlines() if not x.startswith('[') ], []) self.assertEqual(len(files), 2) self.assertEqual(self.logdata.getvalue(), '') self.logdata.truncate(0)
def test_OutputNoErrors(self): l = log.Log(self.ctx, 'Path/To/Git/repo2', None) os.write(l.stdout, 'this is a test\n') s = shell.LoggingShell(l, 'true') retcode = s.finish() self.assertEqual(retcode, 0) self.assertEqual(l.lastOutput(), ('', '')) l.close() thislog = '/'.join((self.logdir, 'repo2')) files = os.listdir(thislog) for filename in files: fileName = '/'.join((thislog, filename)) logLines = gzip.GzipFile(fileName).readlines() nonLogLines = [x for x in logLines if not x.startswith('[')] if '.log' in filename: self.assertEqual(len(nonLogLines), 1) else: self.assertEqual(len(nonLogLines), 0) self.assertTrue( logLines[-1].endswith(' COMPLETE with return code: 0\n')) self.assertEqual(self.logdata.getvalue(), '') self.logdata.truncate(0)
def test_timestamp(self, P): l = log.Log(self.ctx, 'Path/To/Git/repo2', None) s = shell.LoggingShell(l, 'ignoreme') s._tzname = lambda: 'EDT' s._now = lambda: 1454508561.227579 self.assertEqual(s.timestamp(), '[Wed Feb 03 09:09:21.2276 EDT 2016]')
def test_readShellOutputData(self): l = log.Log(self.ctx, 'Path/To/Git/repo2', None) retcode = shell.run(l, 'echo', 'foo') self.assertEqual(retcode, 0) self.assertEqual(l.lastOutput(), ('foo\n', ''))