def test_this(self): t = prego.Task('cat', detach=True) c = t.command("sleep 1; echo 'ready'; sleep 10", expected=-15) o = prego.Task() o.wait_that(c.stdout.content, hamcrest.contains_string("ready")) prego.Task().delay()
def test_cmd_wrong_true_and_ls(self): prego.init() task = prego.Task() task.command('wrong') task.command('true') task2 = prego.Task() task2.command('ls') prego.commit()
def test_fail_by_timeout(self): prego.init() task = prego.Task() task.command('sleep 2', timeout=1, expected=None) task.run() self.assertEquals(Status.FAIL, task.status)
def test_just_1_assertion_fail(self): task = prego.Task() task.assert_that('hello world', hamcrest.contains_string('missing')) task.run() self.assertEquals(Status.FAIL, task.status) self.assertEquals(Status.FAIL, task.assertions[0].status)
def test_ls_ok(self): prego.init() task = prego.Task() task.command('ls') task.run() self.assertEquals(Status.OK, task.status)
def test_just_1_assertion_ok(self): task = prego.Task() task.assert_that('hello world', hamcrest.contains_string('llo')) task.run() self.assertEquals(1, len(task.assertions)) self.assertEquals(Status.OK, task.status)
def test_fail(self): prego.init() task = prego.Task() task.command("false") task.run() self.assertEquals(Status.FAIL, task.status)
def test_cmd_fail_with_outs(self): prego.init() task = prego.Task() task.command('echo STDOUT') task.command('echo STDERR >&2') task.command('false') prego.commit()
def test_non_exising_file_contains(self): prego.init() t = prego.Task() t.assert_that( prego.File('/tmp/kk').content, hamcrest.is_not(hamcrest.contains_string('SOMETHING'))) prego.commit()
def test_ok(self): prego.init() task = prego.Task() cmd = task.command('echo hi') task.assert_that(cmd, prego.exits_with(0)) prego.commit() self.assertEquals(Status.OK, task.status)
def test_with_postassertion_fail_by_cmd__assertion_not_executed(self): task = prego.Task() task.command('false') task.assert_that('hello world', hamcrest.contains_string('worl')) task.run() self.assertEquals(Status.FAIL, task.status) self.assertEquals(Status.NOEXEC, task.assertions[3].status)
def test_with_preassertion_fail_by_cmd(self): task = prego.Task() task.assert_that('hello world', hamcrest.contains_string('worl')) task.command('false') task.run() self.assertEquals(Status.FAIL, task.status) self.assertEquals(Status.OK, task.assertions[0].status)
def test_file_contains(self): open('/tmp/prego-content', 'wt').write('this a sample file for prego') prego.init() t = prego.Task() t.assert_that( prego.File('/tmp/prego-content').content, hamcrest.contains_string('sample file')) prego.commit()
def test_file_compare(self): prego.init() a = prego.File('/etc/passwd') b = prego.File('/etc/fstab') t = prego.Task() t.assert_that(a, hamcrest.is_not(b)) prego.commit()
def test_wait_3_tries_ok(self): with Spy() as other_task: other_task.is_running().delegates([False, False, True]) task = prego.Task() c = task.wait_that(other_task, prego.running(), delta=0.1, timeout=1) c.eval() assert_that(other_task.is_running, called().times(3))
def test_with_postassertion_fail_by_assertion(self): task = prego.Task() task.command('true') task.assert_that('hello world', hamcrest.contains_string('missing')) task.run() self.assertEquals(4, len(task.assertions)) self.assertEquals(Status.FAIL, task.status) self.assertEquals(Status.FAIL, task.assertions[3].status)
def test_logging_output(self): prego.init() task = prego.Task() cmd = task.command('python test/utils/log.py hi') task.assert_that(cmd.stderr.content, hamcrest.contains_string('hi')) task.run() self.assertIn('hi', cmd.stderr.read()) self.assertEquals(Status.OK, task.status)
def test_file_permissions(self): prego.init() t = prego.Task() t.assert_that(prego.File('/etc/fstab'), prego.has_permissions(0o644)) t.assert_that(prego.File('/etc/fstab'), prego.has_permissions(0o200)) t.assert_that(prego.File('/etc/fstab'), prego.has_permissions(0o400)) t.assert_that(prego.File('/etc/fstab'), prego.has_permissions(0o440)) t.assert_that(prego.File('/etc/fstab'), prego.has_permissions(0o004)) prego.commit()
def test_stdout_was_closed(self): prego.init() out = BytesIO() out.name = "bytes-io" task = prego.Task() task.command(u'echo hi', stdout=out) task.run() self.assert_(out.closed)
def test_cmd_pre_and_postassertion(self): task = prego.Task() task.assert_that('hello world', hamcrest.contains_string('worl')) task.command('true') task.assert_that(2, hamcrest.greater_than(1)) task.run() self.assertEquals(Status.OK, task.status) self.assertEquals(Status.OK, task.assertions[0].status) self.assertEquals(Status.OK, task.assertions[2].status)
def test_task_is_killed_by_specified_signal(self): prego.init() task = prego.Task() cmd = task.command('sleep 5', signal=SIGINT, timeout=1) try: prego.commit() self.fail() except prego.TestFailed: self.assertEquals(cmd.returncode, -SIGINT)
def test_wait_fail(self): with Spy() as cmd: cmd.is_running().delegates([False, False, False]) task = prego.Task() c = task.wait_that(cmd, prego.running(), delta=0.1, timeout=0.3) with self.assertRaises(prego.PregoAssertionFailed): c.eval() assert_that(cmd.is_running, called().times(3))
def test_file_compare2(self): with open('/tmp/a', 'w') as fd: fd.write("foobar") with open('/tmp/b', 'w') as fd: fd.write("foobar") prego.init() t = prego.Task() t.assert_that(prego.File('/tmp/a'), hamcrest.is_(prego.File('/tmp/b'))) prego.commit()
def test_stdout_with_bytesio(self): class FakeFile: closed = False def __init__(self): self.buff = bytes() def write(self, data): self.buff += data prego.init() out = FakeFile() out.name = "bytes-io" task = prego.Task() task.command('echo hi', stdout=out) task.run() self.assertIn(b'hi', out.buff)
def test_stdout_with_filename(self): prego.init() fname = '/tmp/task-out' try: os.remove(fname) except OSError: pass task = prego.Task() task.command("echo hi", stdout=fname) task.assert_that( prego.File('/tmp/task-out').content, hamcrest.contains_string('hi')) task.run() content = open(fname).read() self.assert_(os.path.exists(fname)) self.assertIn('hi', content)
def test_file_compare_fail(self): prego.init() t = prego.Task() t.assert_that(prego.File('/etc/fstab'), hamcrest.is_not(prego.File('/etc/passwd'))) prego.commit()
def test_non_exising_lastcmd(self): task = prego.Task() with self.assertRaises(IndexError): task.assert_that(task.lastcmd, prego.running())
def assert_that(self, actual, matcher): task = prego.Task() return task.assert_that(actual, matcher)
def test_file_exists_in_cwd(self): prego.init() t = prego.Task() t.assert_that(prego.File('test/$testfilename'), prego.exists()) prego.commit()
def test_fail_cmd(self): prego.init() test = prego.Task() test.command('false') prego.commit()