def test_success(tmpdir): """Test most hooks in one test during a successful rip. :param py.path.local tmpdir: pytest fixture. """ hooks = ('post-env', 'pre-prepare', 'post-prepare', 'pre-rip', 'post-title', 'post-rip', 'end') with build_image(tmpdir.join('root')) as (root, _, image_ids): for hook in hooks: root.ensure('hook-{}.sh'.format(hook)).write('env |sort') # Docker run. output = tmpdir.ensure_dir('output') stdout, stderr = pytest.run(output=output, image_id=image_ids[0]) # Verify. for hook in hooks: assert b'FIRING HOOK: /hook-%s.sh' % hook.encode('utf8') in stderr assert b'_HOOK_SCRIPT=/hook-%s.sh' % hook.encode('utf8') in stdout assert b'END OF HOOK: /hook-%s.sh' % hook.encode('utf8') in stderr if hook == 'post-title': assert re.compile( br'^TITLE_PATH=/output/Sample[a-zA-Z0-9_/.-]+/title00\.mkv$', re.MULTILINE).search(stdout) assert stderr.count(b'\nEND OF HOOK: ') == len( hooks) # Verify no other hooks fired. pytest.verify(output)
def test_manual(tmpdir): """Test example commands in "Run Manually" README section. :param py.path.local tmpdir: pytest fixture. """ # Grab commands from README. with pytest.ROOT.join('README.md').open() as handle: for line in handle: if line.strip() == 'Now go ahead and run the image:': break partial = handle.read(1024) contents = re.compile(r'\n```bash\n(.+?)\n```\n', re.DOTALL).findall(partial)[0] script = tmpdir.join('script.sh') script.write(contents) # Run. if OUTPUT.check(): OUTPUT.remove() pytest.run(['bash', script]) assert OUTPUT.check() # Verify. pytest.verify(OUTPUT, gid=os.getgid(), uid=os.getuid(), modes=('drwxr-xr-x', '-rw-r--r--'))
def test_devname(tmpdir, devname): """Test DEVNAME environment variable. :param py.path.local tmpdir: pytest fixture. :param str devname: Set environment variable to this. """ output = tmpdir.ensure_dir('output') command = [ 'docker', 'run', '-v', '{}:/output'.format(output), '-e', 'DEBUG=true', 'mboman/makemkv' ] # Docker acts weird with /dev/cdrom and /dev/sr0 specified at the same time. Workaround: if devname == '/dev/sr0': command = command[:-1] + ['--device=/dev/sr0'] + command[-1:] else: command = command[:-1] + ['--device=/dev/cdrom', '--device=/dev/sr0' ] + command[-1:] # Add env variable. if devname: command = command[:-1] + ['-e', 'DEVNAME={}'.format(devname) ] + command[-1:] # Docker run. stdout, stderr = pytest.run(command) # Verify. assert b'--directio true dev:%s all' % ( devname or '/dev/cdrom').encode('utf8') in stderr assert b'+ eject --verbose %s' % (devname or '/dev/cdrom').encode('utf8') in stderr assert b'\nDone after 00:00:' in stdout pytest.verify(output, gid=1000, uid=1000)
def setup(): y1 = "this is the SETUP function" y2 = "another message" log.high_level_step("class_scoped_fix-setup") # PASS CONDITION verify(True, "class_scoped_fix-setup-1:pass") verify(True, "class_scoped_fix-setup-2:pass")
def setup(): log.high_level_step("function_phase_fail-setup") # PASS CONDITION verify(True, "function_phase_fail-setup:pass", raise_immediately=False) # FAIL CONDITION verify(False, "function_phase_fail-setup:fail", raise_immediately=True) log.detail_step("function_phase_fail-setup: This should not print")
def setup(): y1 = "this is the SETUP function" y2 = "another message" log.high_level_step("module_phase_saved_pass-setup") # PASS CONDITION verify(True, "module_phase_saved_pass-setup-1:pass") verify(True, "module_phase_saved_pass-setup-2:pass")
def setup(): log.high_level_step("f_st1-setup") # PASS CONDITION verify(True, "function_phase_warning-setup:pass", raise_immediately=False) # WARNING CONDITION verify(False, "function_phase_warning-setup:warning", warning=True)
def setup(): log.high_level_step("function_phase_assert-setup") # PASS CONDITION verify(True, "function_phase_assert-setup:pass", raise_immediately=False) # STANDARD ASSERT FAILURE assert False, "function_phase_assert-setup:standard assert"
def test_class_1(self, c_st0, f_st1): log.high_level_step("Test the pytest-verify plugin: test class 1") log.high_level_step("Saving results with test phase and phase scope") log.detail_step("Test a verification that passes") x = True verify(x is True, "test_class_1:call:test x is True (pass)") log.detail_step("End of test_class_1")
def setup(): log.high_level_step("function_phase_saved_pass-setup") # PASS CONDITION verify(True, "function_phase_saved_pass-setup:pass", raise_immediately=False) # verify(False, "function_phase_warning-setup:warning", warning=True) # assert False setup_device_1() setup_device_2()
def setup(): log.high_level_step("function_phase_saved_failure-setup") # PASS CONDITION verify(True, "function_phase_saved_failure-setup:pass", raise_immediately=False) # FAIL CONDITION verify(False, "function_phase_saved_failure-setup:fail", raise_immediately=False)
def test_class_0(self, c_st0, f_st1): log.high_level_step("Test the pytest-verify plugin: test class 0") log.high_level_step("Saving results with test phase and phase scope") log.detail_step("Test a verification that passes") x = True verify(x is True, "test_class_0:call:test x is True (pass)") # log.detail_step("Test a verification that fails") # x = False # verify(x is True, "test1:call:test x is True (fail)", # raise_immediately=False, stop_at_test=True) log.detail_step("End of test_class_0")
def test_phases_0(f_st0): log.high_level_step("Test the pytest-verify plugin 0") log.high_level_step("Saving results with test phase and phase scope") log.detail_step("Test a verification that passes") x = True verify(x is True, "test0:call:test x is True (pass)") log.detail_step("Test a verification that fails") x = False verify(x is True, "test0:call:test x is True (fail)", raise_immediately=False, stop_at_test=True) log.detail_step("End of test_phases_0")
def test_phases_2(f_st0): log.high_level_step("Test the pytest-verify plugin") log.high_level_step("Basic use of verify conditions") log.detail_step("Test a verification that passes") x = True verify(x is True, "call:test x is True (pass)") log.detail_step("Test a verification that fails") x = False verify(x is True, "call:test x is True (fail)", raise_immediately=False, stop_at_test=True) # assert (x is # True), "call:FOO with a ver long " \ # "message over 2 lines" log.detail_step("End of test_phases_2")
def test_volume(tmpdir, volume): """Test Docker volume configurations. :param py.path.local tmpdir: pytest fixture. :param str volume: Configuration to test. """ output = tmpdir.ensure_dir('output') command = [ 'docker', 'run', '--device=/dev/cdrom', '-e', 'DEBUG=true', 'mboman/makemkv' ] if volume == 'bind': command = command[:-1] + ['-v', '{}:/output'.format(output) ] + command[-1:] elif volume == 'data': command = command[:-1] + ['-v', '/output'] + command[-1:] # Docker run. with pytest.container_ids_diff() as container_ids: pytest.run(command) assert len(container_ids) == 1 cid = container_ids[0] # Verify. if volume == 'bind': pytest.verify(output, gid=1000, uid=1000, modes=('drwxr-xr-x', '-rw-r--r--')) elif volume == 'data': stdout = pytest.run(['docker', 'inspect', cid])[0] parsed = json.loads(stdout) mount = parsed[0]['Mounts'][0] assert mount['Destination'] == '/output' output = py.path.local(mount['Source']) pytest.verify(output, gid=1000, uid=1000, modes=('drwxr-xr-x', '-rw-r--r--')) elif volume == 'unspecified': pass else: raise NotImplementedError
def test_ownership(tmpdir, gid, uid): """Test user and group IDs of directories and MKV files. 1000 is the default ID in the container. :param py.path.local tmpdir: pytest fixture. :param int gid: Set MKV_GID to this if not -1, otherwise don't set. :param int uid: Set MKV_UID to this if not -1, otherwise don't set. """ output = tmpdir.ensure_dir('output') args = list() if gid >= 0: args += ['-e', 'MKV_GID={}'.format(gid)] if uid >= 0: args += ['-e', 'MKV_UID={}'.format(uid)] # Docker run. pytest.run(args=args, output=output) # Verify. pytest.verify(output, gid=1234 if gid == 1234 else 1000, uid=1234 if uid == 1234 else 1000)
def setup(): log.high_level_step("function_phase_saved_pass-setup") if request.param["w"]: verify(False, "setup warning", warning=True) if request.param["s"]: verify(False, "setup failed verification (saved)", raise_immediately=False) if request.param["i"]: verify(False, "setup failed verification (raise immediately)") if request.param["a"]: assert False, "setup regular assertion" # else setup has passed verify(True, "function_phase_saved_pass-setup passed")
def teardown(): log.high_level_step("function_phase_saved_pass-teardown") if request.param["w"]: verify(False, "teardown warning", warning=True) if request.param["s"]: verify(False, "teardown failed verification (saved)", raise_immediately=False) if request.param["i"]: verify(False, "teardown failed verification (raise immediately)") if request.param["a"]: assert False, "teardown regular assertion" # else setup has passed verify(True, "function_phase_saved_pass-teardown:pass", raise_immediately=False)
def test_udev(request): """Test example udev rule in "Automated Run" README section. :param request: pytest fixture. """ # Grab udev rule from README. with pytest.ROOT.join('README.md').open() as handle: for line in handle: if line.strip() == '## Automated Run': break partial = handle.read(1024) contents = re.compile(r'\n```\n(.+?)\n```\n', re.DOTALL).findall(partial)[0] write_udev(contents) request.addfinalizer(write_udev) # Truncate. # Run. with pytest.container_ids_diff() as container_ids: if OUTPUT.check() and OUTPUT.listdir(): OUTPUT.remove() OUTPUT.ensure_dir() pytest.cdload() assert len(container_ids) == 1 cid = container_ids[0] # Wait up to 15 seconds for container to exit. for _ in range(30): if not pytest.run(['docker', 'ps', '-qf', 'id={}'.format(cid)])[0]: break time.sleep(0.5) # Verify. pytest.verify(OUTPUT, gid=os.getgid(), uid=os.getuid(), modes=('drwxr-xr-x', '-rw-r--r--'))
def test_basic_traceback(): log.high_level_step("Test the pytest-verify plugin") log.high_level_step("Basic traceback test") x = True verify(x is True, "Check x is true (passes)") y = False verify(y is True, "Check y is true (fails)", raise_immediately=False) log.detail_step("Test a function call as fail condition (fails)") def is_it_true(it): return it is True y = False verify(is_it_true(y), "test y is True (fail condition in function)", raise_immediately=False) log.detail_step("Check traceback to call to verify over multiple stack " "levels") def stack_l3(k): print "Finally verifying x, j, k" verify(k is True, "test k is True (fail)", raise_immediately=False) def stack_l2(j): print "In stack_l2 function" print "about to call stack_l3..." stack_l3(j) def stack_l1(i): stack_l2(i) x = False stack_l1(x) log.detail_step("Test traceback depth past test function (into pytest " "source)") verify(x is True, "Check x is true (fail)", raise_immediately=False, stop_at_test=False) log.detail_step("End of test_basic_traceback")
def test_umask(tmpdir, umask): """Test UMASK environment variable. :param py.path.local tmpdir: pytest fixture. :param str umask: Set UMASK to this if truthy. """ output = tmpdir.ensure_dir('output') args = list() if umask: args += ['-e', 'UMASK={}'.format(umask)] # Docker run. pytest.run(args=args, output=output) # Verify. if umask == '0002': pytest.verify(output, modes=('drwxrwxr-x', '-rw-rw-r--')) elif umask == '0000': pytest.verify(output, modes=('drwxrwxrwx', '-rw-rw-rw-')) else: pytest.verify(output, modes=('drwxr-xr-x', '-rw-r--r--'))
def test_2_module_scope(function_scoped_fix): """Test uses module and function scoped fixtures.""" log.high_level_step("test_module_scope_2") verify(True, "test_module_scope_2:call-1:pass") verify(True, "test_module_scope_2:call-2:pass")
def test_1_module_scope(): """Test uses module scoped fixture only.""" log.high_level_step("test_module_scope_1") verify(True, "test_module_scope_1:call-1:pass") verify(True, "test_module_scope_1:call-2:pass")
def teardown(): log.high_level_step("function_phase_saved_pass-teardown") # PASS CONDITION verify(True, "function_phase_saved_pass-teardown:pass", raise_immediately=False)
def teardown(): z = "this is the TEARDOWN function" log.high_level_step("module_phase_saved_pass-teardown") # PASS CONDITION verify(True, "module_phase_saved_pass-teardown-1:pass") verify(True, "module_phase_saved_pass-teardown-2:pass")
def stack_l3(k): print "Finally verifying x, j, k" verify(k is True, "test k is True (fail)", raise_immediately=False, full_method_trace=True)
def test_full_traceback(): log.high_level_step("Test the pytest-verify plugin") log.high_level_step("Full traceback test") log.detail_step("Ensure there is no traceback for passed verifications") x = True verify(x is True, "Check x is true (passes)") log.detail_step("Ensure default behaviour is short trace back") x = False verify(x is True, "Check x is true (fails - call line tb)", raise_immediately=False) log.detail_step("Test no change when full_method_trace is set to False") x = False verify(x is True, "Check x is true (fails - call line tb)", raise_immediately=False, full_method_trace=False) log.detail_step("Test full function traceback is printed when " "full_method_trace is set to True") x = False verify(x is True, "Check x is true (fails - full func tb)", raise_immediately=False, full_method_trace=True) log.detail_step("Test a function call as fail condition (fails)") def is_it_true(it): return it is True y = False verify(is_it_true(y), "test y is True (fail condition in function)", raise_immediately=False, full_method_trace=True) log.detail_step("Check traceback to call to verify over multiple stack " "levels") def stack_l3(k): print "Finally verifying x, j, k" verify(k is True, "test k is True (fail)", raise_immediately=False, full_method_trace=True) def stack_l2(j): print "In stack_l2 function" print "about to call stack_l3..." stack_l3(j) def stack_l1(i): stack_l2(i) x = False stack_l1(x) log.detail_step("Test traceback depth past test function (into pytest " "source)") verify(x is True, "Check x is true (fail)", raise_immediately=False, full_method_trace=True, stop_at_test=False) log.detail_step("End of test_full_traceback")
def test_param(a): log.high_level_step("Starting test with param a = {}".format(a)) verify(a > 0, "a fails", warn_condition=a > 1, warn_message="a warns")
def test_verify_basic_usage(): log.high_level_step("Test the pytest-verify plugin") log.high_level_step("Basic use of verify conditions") log.detail_step("Test a verification that passes") x = True verify(x is True, "test x is True (pass)") log.detail_step("Test a verification that fails") x = False verify(x is True, "test x is True (fail)", raise_immediately=False) log.detail_step("Test a function call as fail condition") def is_it_true(it): return it is True y = True verify(is_it_true(y), "test y is True (fail condition in function)", raise_immediately=False) log.detail_step("Test a verification that warns") x = False verify(x is True, "test x is True (warning)", warning=True) log.detail_step("Test a verification that passes initial failure check " "then warns on a second condition") x = True y = False verify(x is True, "test x is True (initial pass)", warn_condition=y is True, warn_message="test y is True (initial pass->warning)") log.detail_step("Test a variable that falls within initial range but " "fails a stricter warning range") x = 2 verify(0 < x < 4, "test x in range", warn_condition=1 < x < 3, warn_message="test x is in a narrower range (pass)") x = 3 verify(0 < x < 4, "test x in range", warn_condition=1 < x < 3, warn_message="test x is in a narrower range (warning)") x = 4 verify(0 < x < 4, "test x in range (fail)", warn_condition=1 < x < 3, warn_message="test x is in a narrower range") log.detail_step("End of test_verify_basic_usage")
def setup(): log.high_level_step("function_phase_saved_pass-setup") verify(True, "function_phase_saved_pass-setup passed")