Best Python code snippet using avocado_python
test_examples.py
Source:test_examples.py
1#!/usr/bin/env python32# -*- coding: utf-8 -*-3'''4test_examples.py - Unit tests for pyfuse3.5Copyright © 2015 Nikolaus Rath <Nikolaus.org>6This file is part of pyfuse3. This work may be distributed under7the terms of the GNU LGPL.8'''9if __name__ == '__main__':10 import pytest11 import sys12 sys.exit(pytest.main([__file__] + sys.argv[1:]))13import subprocess14import os15import sys16import pytest17import stat18import shutil19import filecmp20import errno21from tempfile import NamedTemporaryFile22from util import fuse_test_marker, wait_for_mount, umount, cleanup23from pyfuse3 import _NANOS_PER_SEC24basename = os.path.join(os.path.dirname(__file__), '..')25TEST_FILE = __file__26pytestmark = fuse_test_marker()27with open(TEST_FILE, 'rb') as fh:28 TEST_DATA = fh.read()29def name_generator(__ctr=[0]):30 __ctr[0] += 131 return 'testfile_%d' % __ctr[0]32@pytest.mark.parametrize('filename', ('hello.py', 'hello_asyncio.py'))33def test_hello(tmpdir, filename):34 mnt_dir = str(tmpdir)35 cmdline = [sys.executable,36 os.path.join(basename, 'examples', filename),37 mnt_dir ]38 mount_process = subprocess.Popen(cmdline, stdin=subprocess.DEVNULL,39 universal_newlines=True)40 try:41 wait_for_mount(mount_process, mnt_dir)42 assert os.listdir(mnt_dir) == [ 'message' ]43 filename = os.path.join(mnt_dir, 'message')44 with open(filename, 'r') as fh:45 assert fh.read() == 'hello world\n'46 with pytest.raises(IOError) as exc_info:47 open(filename, 'r+')48 assert exc_info.value.errno == errno.EACCES49 with pytest.raises(IOError) as exc_info:50 open(filename + 'does-not-exist', 'r+')51 assert exc_info.value.errno == errno.ENOENT52 except:53 cleanup(mount_process, mnt_dir)54 raise55 else:56 umount(mount_process, mnt_dir)57def test_tmpfs(tmpdir):58 mnt_dir = str(tmpdir)59 cmdline = [sys.executable,60 os.path.join(basename, 'examples', 'tmpfs.py'),61 mnt_dir ]62 mount_process = subprocess.Popen(cmdline, stdin=subprocess.DEVNULL,63 universal_newlines=True)64 try:65 wait_for_mount(mount_process, mnt_dir)66 tst_write(mnt_dir)67 tst_mkdir(mnt_dir)68 tst_symlink(mnt_dir)69 tst_mknod(mnt_dir)70 tst_chown(mnt_dir)71 tst_chmod(mnt_dir)72 tst_utimens(mnt_dir)73 tst_rounding(mnt_dir)74 tst_link(mnt_dir)75 tst_rename(mnt_dir)76 tst_readdir(mnt_dir)77 tst_statvfs(mnt_dir)78 tst_truncate_path(mnt_dir)79 tst_truncate_fd(mnt_dir)80 tst_unlink(mnt_dir)81 except:82 cleanup(mount_process, mnt_dir)83 raise84 else:85 umount(mount_process, mnt_dir)86def test_passthroughfs(tmpdir):87 mnt_dir = str(tmpdir.mkdir('mnt'))88 src_dir = str(tmpdir.mkdir('src'))89 cmdline = [sys.executable,90 os.path.join(basename, 'examples', 'passthroughfs.py'),91 src_dir, mnt_dir ]92 mount_process = subprocess.Popen(cmdline, stdin=subprocess.DEVNULL,93 universal_newlines=True)94 try:95 wait_for_mount(mount_process, mnt_dir)96 tst_write(mnt_dir)97 tst_mkdir(mnt_dir)98 tst_symlink(mnt_dir)99 tst_mknod(mnt_dir)100 if os.getuid() == 0:101 tst_chown(mnt_dir)102 tst_chmod(mnt_dir)103 # Underlying fs may not have full nanosecond resolution104 tst_utimens(mnt_dir, ns_tol=1000)105 tst_rounding(mnt_dir)106 tst_link(mnt_dir)107 tst_rename(mnt_dir)108 tst_readdir(mnt_dir)109 tst_statvfs(mnt_dir)110 tst_truncate_path(mnt_dir)111 tst_truncate_fd(mnt_dir)112 tst_unlink(mnt_dir)113 tst_passthrough(src_dir, mnt_dir)114 except:115 cleanup(mount_process, mnt_dir)116 raise117 else:118 umount(mount_process, mnt_dir)119def checked_unlink(filename, path, isdir=False):120 fullname = os.path.join(path, filename)121 if isdir:122 os.rmdir(fullname)123 else:124 os.unlink(fullname)125 with pytest.raises(OSError) as exc_info:126 os.stat(fullname)127 assert exc_info.value.errno == errno.ENOENT128 assert filename not in os.listdir(path)129def tst_mkdir(mnt_dir):130 dirname = name_generator()131 fullname = mnt_dir + "/" + dirname132 os.mkdir(fullname)133 fstat = os.stat(fullname)134 assert stat.S_ISDIR(fstat.st_mode)135 assert os.listdir(fullname) == []136 assert fstat.st_nlink in (1,2)137 assert dirname in os.listdir(mnt_dir)138 checked_unlink(dirname, mnt_dir, isdir=True)139def tst_symlink(mnt_dir):140 linkname = name_generator()141 fullname = mnt_dir + "/" + linkname142 os.symlink("/imaginary/dest", fullname)143 fstat = os.lstat(fullname)144 assert stat.S_ISLNK(fstat.st_mode)145 assert os.readlink(fullname) == "/imaginary/dest"146 assert fstat.st_nlink == 1147 assert linkname in os.listdir(mnt_dir)148 checked_unlink(linkname, mnt_dir)149def tst_mknod(mnt_dir):150 filename = os.path.join(mnt_dir, name_generator())151 shutil.copyfile(TEST_FILE, filename)152 fstat = os.lstat(filename)153 assert stat.S_ISREG(fstat.st_mode)154 assert fstat.st_nlink == 1155 assert os.path.basename(filename) in os.listdir(mnt_dir)156 assert filecmp.cmp(TEST_FILE, filename, False)157 checked_unlink(filename, mnt_dir)158def tst_chown(mnt_dir):159 filename = os.path.join(mnt_dir, name_generator())160 os.mkdir(filename)161 fstat = os.lstat(filename)162 uid = fstat.st_uid163 gid = fstat.st_gid164 uid_new = uid + 1165 os.chown(filename, uid_new, -1)166 fstat = os.lstat(filename)167 assert fstat.st_uid == uid_new168 assert fstat.st_gid == gid169 gid_new = gid + 1170 os.chown(filename, -1, gid_new)171 fstat = os.lstat(filename)172 assert fstat.st_uid == uid_new173 assert fstat.st_gid == gid_new174 checked_unlink(filename, mnt_dir, isdir=True)175def tst_chmod(mnt_dir):176 filename = os.path.join(mnt_dir, name_generator())177 os.mkdir(filename)178 fstat = os.lstat(filename)179 mode = stat.S_IMODE(fstat.st_mode)180 mode_new = 0o640181 assert mode != mode_new182 os.chmod(filename, mode_new)183 fstat = os.lstat(filename)184 assert stat.S_IMODE(fstat.st_mode) == mode_new185 checked_unlink(filename, mnt_dir, isdir=True)186def tst_write(mnt_dir):187 name = os.path.join(mnt_dir, name_generator())188 shutil.copyfile(TEST_FILE, name)189 assert filecmp.cmp(name, TEST_FILE, False)190 checked_unlink(name, mnt_dir)191def tst_unlink(mnt_dir):192 name = os.path.join(mnt_dir, name_generator())193 data1 = b'foo'194 data2 = b'bar'195 with open(os.path.join(mnt_dir, name), 'wb+', buffering=0) as fh:196 fh.write(data1)197 checked_unlink(name, mnt_dir)198 fh.write(data2)199 fh.seek(0)200 assert fh.read() == data1+data2201def tst_statvfs(mnt_dir):202 os.statvfs(mnt_dir)203def tst_link(mnt_dir):204 name1 = os.path.join(mnt_dir, name_generator())205 name2 = os.path.join(mnt_dir, name_generator())206 shutil.copyfile(TEST_FILE, name1)207 assert filecmp.cmp(name1, TEST_FILE, False)208 os.link(name1, name2)209 fstat1 = os.lstat(name1)210 fstat2 = os.lstat(name2)211 assert fstat1 == fstat2212 assert fstat1.st_nlink == 2213 assert os.path.basename(name2) in os.listdir(mnt_dir)214 assert filecmp.cmp(name1, name2, False)215 os.unlink(name2)216 fstat1 = os.lstat(name1)217 assert fstat1.st_nlink == 1218 os.unlink(name1)219def tst_rename(mnt_dir):220 name1 = os.path.join(mnt_dir, name_generator())221 name2 = os.path.join(mnt_dir, name_generator())222 shutil.copyfile(TEST_FILE, name1)223 assert os.path.basename(name1) in os.listdir(mnt_dir)224 assert os.path.basename(name2) not in os.listdir(mnt_dir)225 assert filecmp.cmp(name1, TEST_FILE, False)226 fstat1 = os.lstat(name1)227 os.rename(name1, name2)228 fstat2 = os.lstat(name2)229 assert fstat1 == fstat2230 assert filecmp.cmp(name2, TEST_FILE, False)231 assert os.path.basename(name1) not in os.listdir(mnt_dir)232 assert os.path.basename(name2) in os.listdir(mnt_dir)233 os.unlink(name2)234def tst_readdir(mnt_dir):235 dir_ = os.path.join(mnt_dir, name_generator())236 file_ = dir_ + "/" + name_generator()237 subdir = dir_ + "/" + name_generator()238 subfile = subdir + "/" + name_generator()239 os.mkdir(dir_)240 shutil.copyfile(TEST_FILE, file_)241 os.mkdir(subdir)242 shutil.copyfile(TEST_FILE, subfile)243 listdir_is = os.listdir(dir_)244 listdir_is.sort()245 listdir_should = [ os.path.basename(file_), os.path.basename(subdir) ]246 listdir_should.sort()247 assert listdir_is == listdir_should248 os.unlink(file_)249 os.unlink(subfile)250 os.rmdir(subdir)251 os.rmdir(dir_)252def tst_truncate_path(mnt_dir):253 assert len(TEST_DATA) > 1024254 filename = os.path.join(mnt_dir, name_generator())255 with open(filename, 'wb') as fh:256 fh.write(TEST_DATA)257 fstat = os.stat(filename)258 size = fstat.st_size259 assert size == len(TEST_DATA)260 # Add zeros at the end261 os.truncate(filename, size + 1024)262 assert os.stat(filename).st_size == size + 1024263 with open(filename, 'rb') as fh:264 assert fh.read(size) == TEST_DATA265 assert fh.read(1025) == b'\0' * 1024266 # Truncate data267 os.truncate(filename, size - 1024)268 assert os.stat(filename).st_size == size - 1024269 with open(filename, 'rb') as fh:270 assert fh.read(size) == TEST_DATA[:size-1024]271 os.unlink(filename)272def tst_truncate_fd(mnt_dir):273 assert len(TEST_DATA) > 1024274 with NamedTemporaryFile('w+b', 0, dir=mnt_dir) as fh:275 fd = fh.fileno()276 fh.write(TEST_DATA)277 fstat = os.fstat(fd)278 size = fstat.st_size279 assert size == len(TEST_DATA)280 # Add zeros at the end281 os.ftruncate(fd, size + 1024)282 assert os.fstat(fd).st_size == size + 1024283 fh.seek(0)284 assert fh.read(size) == TEST_DATA285 assert fh.read(1025) == b'\0' * 1024286 # Truncate data287 os.ftruncate(fd, size - 1024)288 assert os.fstat(fd).st_size == size - 1024289 fh.seek(0)290 assert fh.read(size) == TEST_DATA[:size-1024]291def tst_utimens(mnt_dir, ns_tol=0):292 filename = os.path.join(mnt_dir, name_generator())293 os.mkdir(filename)294 fstat = os.lstat(filename)295 atime = fstat.st_atime + 42.28296 mtime = fstat.st_mtime - 42.23297 atime_ns = fstat.st_atime_ns + int(42.28*1e9)298 mtime_ns = fstat.st_mtime_ns - int(42.23*1e9)299 os.utime(filename, None, ns=(atime_ns, mtime_ns))300 fstat = os.lstat(filename)301 assert abs(fstat.st_atime - atime) < 1e-3302 assert abs(fstat.st_mtime - mtime) < 1e-3303 assert abs(fstat.st_atime_ns - atime_ns) <= ns_tol304 assert abs(fstat.st_mtime_ns - mtime_ns) <= ns_tol305 checked_unlink(filename, mnt_dir, isdir=True)306def tst_rounding(mnt_dir, ns_tol=0):307 filename = os.path.join(mnt_dir, name_generator())308 os.mkdir(filename)309 fstat = os.lstat(filename)310 # Approximately 100 years311 secs = 100 * 365 * 24 * 3600 + 999312 # Max nanos313 nanos = _NANOS_PER_SEC - 1314 # seconds+ns and ns_tol as a float in seconds 315 secs_f = secs + nanos / _NANOS_PER_SEC316 secs_tol = ns_tol / _NANOS_PER_SEC317 318 atime_ns = secs * _NANOS_PER_SEC + nanos319 mtime_ns = atime_ns320 os.utime(filename, None, ns=(atime_ns, mtime_ns))321 fstat = os.lstat(filename)322 assert abs(fstat.st_atime - secs_f) <= secs_tol323 assert abs(fstat.st_mtime - secs_f) <= secs_tol324 assert abs(fstat.st_atime_ns - atime_ns) <= ns_tol325 assert abs(fstat.st_mtime_ns - mtime_ns) <= ns_tol326 checked_unlink(filename, mnt_dir, isdir=True)327def tst_passthrough(src_dir, mnt_dir):328 # Test propagation from source to mirror329 name = name_generator()330 src_name = os.path.join(src_dir, name)331 mnt_name = os.path.join(mnt_dir, name)332 assert name not in os.listdir(src_dir)333 assert name not in os.listdir(mnt_dir)334 with open(src_name, 'w') as fh:335 fh.write('Hello, world')336 assert name in os.listdir(src_dir)337 assert name in os.listdir(mnt_dir)338 assert_same_stats(src_name, mnt_name)339 # Test propagation from mirror to source340 name = name_generator()341 src_name = os.path.join(src_dir, name)342 mnt_name = os.path.join(mnt_dir, name)343 assert name not in os.listdir(src_dir)344 assert name not in os.listdir(mnt_dir)345 with open(mnt_name, 'w') as fh:346 fh.write('Hello, world')347 assert name in os.listdir(src_dir)348 assert name in os.listdir(mnt_dir)349 assert_same_stats(src_name, mnt_name)350 # Test propagation inside subdirectory351 name = name_generator()352 src_dir = os.path.join(src_dir, 'subdir')353 mnt_dir = os.path.join(mnt_dir, 'subdir')354 os.mkdir(src_dir)355 src_name = os.path.join(src_dir, name)356 mnt_name = os.path.join(mnt_dir, name)357 assert name not in os.listdir(src_dir)358 assert name not in os.listdir(mnt_dir)359 with open(mnt_name, 'w') as fh:360 fh.write('Hello, world')361 assert name in os.listdir(src_dir)362 assert name in os.listdir(mnt_dir)363 assert_same_stats(src_name, mnt_name)364def assert_same_stats(name1, name2):365 stat1 = os.stat(name1)366 stat2 = os.stat(name2)367 for name in ('st_atime_ns', 'st_mtime_ns', 'st_ctime_ns',368 'st_mode', 'st_ino', 'st_nlink', 'st_uid',369 'st_gid', 'st_size'):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!