Re: [U-Boot] [PATCH 3/3] test/py: Create tests for ext4 and fat testing on sandbox

2016-11-11 Thread Simon Glass
Hi,

On 5 November 2016 at 10:45, Stefan Brüns  wrote:
>
> The following checks are currently implemented:
> 1. listing a directory
> 2. verifying size of a file
> 3. veryfying md5sum for a file region
> 4. reading the beginning of a file
>
> Signed-off-by: Stefan Brüns 
> ---
>  test/py/tests/test_fs.py | 298 
> +++
>  1 file changed, 298 insertions(+)
>  create mode 100644 test/py/tests/test_fs.py
>
> diff --git a/test/py/tests/test_fs.py b/test/py/tests/test_fs.py
> new file mode 100644
> index 000..5ac91e4
> --- /dev/null
> +++ b/test/py/tests/test_fs.py
> @@ -0,0 +1,298 @@
> +# Copyright (c) 2016, Stefan Bruens 
> +#
> +# SPDX-License-Identifier: GPL-2.0
> +
> +# Test U-Boot's filesystem implementations

Can you add a few details here about what this tests?

> +
> +import hashlib
> +import pytest
> +import os
> +import random
> +import re
> +import u_boot_utils as util
> +
> +
> +mkfs_opts = {
> +   "fat" :'-t vfat',
> +   "ext4" : '-t ext4 -F',

Can you please use a single quote unless you can't? That is the style
used in U-Boot.

> +}
> +
> +fs_commands = {
> +   "fat" : {
> +   'listcmd'   : 'ls',
> +   'readcmd'   : 'load',
> +   'sizecmd'   : 'size',
> +   'writecmd'  : 'size',
> +   },
> +   "ext4" : {
> +   'listcmd'   : 'ls',
> +   'readcmd'   : 'load',
> +   'sizecmd'   : 'size',
> +   'writecmd'  : 'size',
> +   },
> +}
> +
> +cmd_parameters = {
> +   "hostfs" : {
> +   'prefix': 'host ',
> +   'interface' : 'hostfs -',
> +   },
> +   "generic" : {
> +   'prefix': '',
> +   'interface' : 'host 0:0',
> +   },
> +}
> +
> +files = {
> +"empty.file" : [(0, 0)],
> +"1MB.file"   : [(0, 1e6)],
> +"1MB.sparse.file"   : [(1e6-1, 1e6)],
> +}
> +# "2_5GB.sparse.file"   : [(0, 1e6), (1e9, 1e9+1e6), (2.5e9-1e6, 2.5e9)],

What is that line for?

> +
> +@pytest.fixture(scope="session")
> +def prereq_commands():
> +from distutils.spawn import find_executable

Why not import this at the top of the file?

> +for command in ["mkfs", "mount", "umount"]:
> +if find_executable(command) is None:
> +pytest.skip('Filesystem tests, "{0}" not in 
> PATH'.format(command))
> +
> +class FsImage:
> +def __init__(self, fstype, imagename, mountpath):

Please add comments as to what these params are.

> +self.fstype = fstype
> +self.imagename = imagename
> +self.mountpath = mountpath
> +self.md5s = {}
> +with open(self.imagename, 'w') as fd:
> +fd.truncate(0)
> +fd.seek(3e9)
> +fd.write(bytes([0]))
> +
> +def mkfs(self, log):
> +mkfsopts = mkfs_opts.get(self.fstype)
> +util.run_and_log(log,
> +'mkfs {0} {1}'.format(mkfsopts, self.imagename))
> +
> +def create_file(self, log, filename):

Please add a short comment on each non-function describing what it
does and what the args are (and return value if any).

> +md5sums = []
> +with open(self.mountpath + "/" + filename, 'w') as fd:

I think this is better as os.path.join(self.mountpath, filename)

> +for stride in files[filename]:
> +length = int(stride[1] - stride[0])
> +data = bytearray(random.getrandbits(8) for _ in 
> xrange(length))
> +md5 = hashlib.md5(data).hexdigest()
> +md5sums.append(md5)
> +log.info("{0}: write {1} bytes @ {2} : {3}".format(
> +filename, int(stride[1] - stride[0]),
> +int(stride[0]), md5))
> +fd.seek(stride[0])
> +fd.write(data);
> +self.md5s[filename] = md5sums
> +
> +def create_files(self, log):
> +with log.section("Create initial files"):
> +for filename in files:
> +self.create_file(log, filename)
> +log.info("Created test files in {0}".format(self.mountpath))
> +util.run_and_log(log, 'ls -la {0}'.format(self.mountpath))
> +util.run_and_log(log, 'sync {0}'.format(self.mountpath))
> +
> +def mount(self, log):
> +if not os.path.exists(self.mountpath):
> +os.mkdir(self.mountpath)
> +log.info("Mounting {0} at {1}".format(self.imagename, 
> self.mountpath))
> +if self.fstype == "ext4":
> +cmd = 'sudo -n mount -o loop,rw {0} {1}'.format(self.imagename, 
> self.mountpath)
> +else:
> +cmd = 'sudo -n mount -o loop,rw,umask=000 {0} 
> {1}'.format(self.imagename, self.mountpath)
> +util.run_and_log(log, cmd)
> +if self.fstype == "ext4":
> +cmd = 'sudo -n chmod og+rw {0}'.format(self.mountpath)
> +return 

[U-Boot] [PATCH 3/3] test/py: Create tests for ext4 and fat testing on sandbox

2016-11-05 Thread Stefan Brüns
The following checks are currently implemented:
1. listing a directory
2. verifying size of a file
3. veryfying md5sum for a file region
4. reading the beginning of a file

Signed-off-by: Stefan Brüns 
---
 test/py/tests/test_fs.py | 298 +++
 1 file changed, 298 insertions(+)
 create mode 100644 test/py/tests/test_fs.py

diff --git a/test/py/tests/test_fs.py b/test/py/tests/test_fs.py
new file mode 100644
index 000..5ac91e4
--- /dev/null
+++ b/test/py/tests/test_fs.py
@@ -0,0 +1,298 @@
+# Copyright (c) 2016, Stefan Bruens 
+#
+# SPDX-License-Identifier: GPL-2.0
+
+# Test U-Boot's filesystem implementations
+
+import hashlib
+import pytest
+import os
+import random
+import re
+import u_boot_utils as util
+
+
+mkfs_opts = {
+   "fat" :'-t vfat',
+   "ext4" : '-t ext4 -F',
+}
+
+fs_commands = {
+   "fat" : {
+   'listcmd'   : 'ls',
+   'readcmd'   : 'load',
+   'sizecmd'   : 'size',
+   'writecmd'  : 'size',
+   },
+   "ext4" : {
+   'listcmd'   : 'ls',
+   'readcmd'   : 'load',
+   'sizecmd'   : 'size',
+   'writecmd'  : 'size',
+   },
+}
+
+cmd_parameters = {
+   "hostfs" : {
+   'prefix': 'host ',
+   'interface' : 'hostfs -',
+   },
+   "generic" : {
+   'prefix': '',
+   'interface' : 'host 0:0',
+   },
+}
+
+files = {
+"empty.file" : [(0, 0)],
+"1MB.file"   : [(0, 1e6)],
+"1MB.sparse.file"   : [(1e6-1, 1e6)],
+}
+# "2_5GB.sparse.file"   : [(0, 1e6), (1e9, 1e9+1e6), (2.5e9-1e6, 2.5e9)],
+
+@pytest.fixture(scope="session")
+def prereq_commands():
+from distutils.spawn import find_executable
+for command in ["mkfs", "mount", "umount"]:
+if find_executable(command) is None:
+pytest.skip('Filesystem tests, "{0}" not in PATH'.format(command))
+
+class FsImage:
+def __init__(self, fstype, imagename, mountpath):
+self.fstype = fstype
+self.imagename = imagename
+self.mountpath = mountpath
+self.md5s = {}
+with open(self.imagename, 'w') as fd:
+fd.truncate(0)
+fd.seek(3e9)
+fd.write(bytes([0]))
+
+def mkfs(self, log):
+mkfsopts = mkfs_opts.get(self.fstype)
+util.run_and_log(log,
+'mkfs {0} {1}'.format(mkfsopts, self.imagename))
+
+def create_file(self, log, filename):
+md5sums = []
+with open(self.mountpath + "/" + filename, 'w') as fd:
+for stride in files[filename]:
+length = int(stride[1] - stride[0])
+data = bytearray(random.getrandbits(8) for _ in xrange(length))
+md5 = hashlib.md5(data).hexdigest()
+md5sums.append(md5)
+log.info("{0}: write {1} bytes @ {2} : {3}".format(
+filename, int(stride[1] - stride[0]),
+int(stride[0]), md5))
+fd.seek(stride[0])
+fd.write(data);
+self.md5s[filename] = md5sums
+
+def create_files(self, log):
+with log.section("Create initial files"):
+for filename in files:
+self.create_file(log, filename)
+log.info("Created test files in {0}".format(self.mountpath))
+util.run_and_log(log, 'ls -la {0}'.format(self.mountpath))
+util.run_and_log(log, 'sync {0}'.format(self.mountpath))
+
+def mount(self, log):
+if not os.path.exists(self.mountpath):
+os.mkdir(self.mountpath)
+log.info("Mounting {0} at {1}".format(self.imagename, self.mountpath))
+if self.fstype == "ext4":
+cmd = 'sudo -n mount -o loop,rw {0} {1}'.format(self.imagename, 
self.mountpath)
+else:
+cmd = 'sudo -n mount -o loop,rw,umask=000 {0} 
{1}'.format(self.imagename, self.mountpath)
+util.run_and_log(log, cmd)
+if self.fstype == "ext4":
+cmd = 'sudo -n chmod og+rw {0}'.format(self.mountpath)
+return util.run_and_log(log, cmd)
+
+def unmount(self, log):
+log.info("Unmounting {0}".format(self.imagename))
+cmd = 'sudo -n umount -l {0}'.format(self.mountpath)
+util.run_and_log(log, cmd, ignore_errors=True)
+
+
+@pytest.fixture(scope="module", params=["fat", "ext4"])
+def fsimage(prereq_commands, u_boot_config, u_boot_log, request):
+datadir = u_boot_config.result_dir + '/'
+fstype = request.param
+imagename = datadir + "3GB." + fstype + ".img"
+mountpath = datadir + "mnt_" + fstype
+
+with u_boot_log.section('Create image {0}'.format(imagename)):
+fsimage = FsImage(fstype, imagename, mountpath)
+fsimage.mkfs(u_boot_log)
+
+yield fsimage
+fsimage.unmount(u_boot_log)
+
+@pytest.fixture(scope="module")
+def populated_image(fsimage,