partition_table: Add Python 2 & 3 support

This commit is contained in:
Roland Dobai 2018-09-17 14:27:36 +02:00
parent 91f7a9a9e7
commit 9dc024bc4c
3 changed files with 39 additions and 28 deletions

View file

@ -305,7 +305,7 @@ test_partition_table_on_host:
- build
script:
- cd components/partition_table/test_gen_esp32part_host
- ./gen_esp32part_tests.py
- ${IDF_PATH}/tools/ci/multirun_with_pyenv.sh ./gen_esp32part_tests.py
test_wl_on_host:
<<: *host_test_template

View file

@ -21,6 +21,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function, division
from __future__ import unicode_literals
import argparse
import os
import re

View file

@ -7,7 +7,7 @@ import sys
import subprocess
import tempfile
import os
import StringIO
import io
sys.path.append("..")
from gen_esp32part import *
@ -53,8 +53,18 @@ def _strip_trailing_ffs(binary_table):
binary_table = binary_table[0:len(binary_table)-32]
return binary_table
class Py23TestCase(unittest.TestCase):
class CSVParserTests(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(Py23TestCase, self).__init__(*args, **kwargs)
try:
self.assertRaisesRegex
except AttributeError:
# assertRaisesRegexp is deprecated in Python3 but assertRaisesRegex doesn't exist in Python2
# This fix is used in order to avoid using the alias from the six library
self.assertRaisesRegex = self.assertRaisesRegexp
class CSVParserTests(Py23TestCase):
def test_simple_partition(self):
table = PartitionTable.from_csv(SIMPLE_CSV)
@ -71,7 +81,7 @@ class CSVParserTests(unittest.TestCase):
# Name,Type, SubType,Offset,Size
ihavenotype,
"""
with self.assertRaisesRegexp(InputError, "type"):
with self.assertRaisesRegex(InputError, "type"):
PartitionTable.from_csv(csv)
@ -162,7 +172,7 @@ second, data, 0x15, , 1M
first, app, factory, 0x100000, 2M
second, app, ota_0, 0x200000, 1M
"""
with self.assertRaisesRegexp(InputError, "overlap"):
with self.assertRaisesRegex(InputError, "overlap"):
t = PartitionTable.from_csv(csv)
t.verify()
@ -171,11 +181,11 @@ second, app, ota_0, 0x200000, 1M
first, app, factory, 0x100000, 1M
first, app, ota_0, 0x200000, 1M
"""
with self.assertRaisesRegexp(InputError, "Partition names must be unique"):
with self.assertRaisesRegex(InputError, "Partition names must be unique"):
t = PartitionTable.from_csv(csv)
t.verify()
class BinaryOutputTests(unittest.TestCase):
class BinaryOutputTests(Py23TestCase):
def test_binary_entry(self):
csv = """
first, 0x30, 0xEE, 0x100400, 0x300000
@ -215,7 +225,7 @@ first, app, factory,, 1M, encrypted
self.assertTrue(tr[0].encrypted)
class BinaryParserTests(unittest.TestCase):
class BinaryParserTests(Py23TestCase):
def test_parse_one_entry(self):
# type 0x30, subtype 0xee,
# offset 1MB, size 2MB
@ -264,7 +274,7 @@ class BinaryParserTests(unittest.TestCase):
b"\x00\x00\x20\x00" + \
b"0123456789abc\0\0\0" + \
b"\x00\x00\x00\x00"
with self.assertRaisesRegexp(InputError, "Invalid magic bytes"):
with self.assertRaisesRegex(InputError, "Invalid magic bytes"):
PartitionTable.from_binary(bad_magic)
def test_bad_length(self):
@ -272,11 +282,11 @@ class BinaryParserTests(unittest.TestCase):
b"\x00\x00\x10\x00" + \
b"\x00\x00\x20\x00" + \
b"0123456789"
with self.assertRaisesRegexp(InputError, "32 bytes"):
with self.assertRaisesRegex(InputError, "32 bytes"):
PartitionTable.from_binary(bad_length)
class CSVOutputTests(unittest.TestCase):
class CSVOutputTests(Py23TestCase):
def _readcsv(self, source_str):
return list(csv.reader(source_str.split("\n")))
@ -317,7 +327,7 @@ class CSVOutputTests(unittest.TestCase):
roundtrip = PartitionTable.from_csv(as_csv)
self.assertEqual(roundtrip, table)
class CommandLineTests(unittest.TestCase):
class CommandLineTests(Py23TestCase):
def test_basic_cmdline(self):
try:
@ -332,7 +342,7 @@ class CommandLineTests(unittest.TestCase):
output = subprocess.check_output([sys.executable, "../gen_esp32part.py",
binpath, csvpath], stderr=subprocess.STDOUT)
# reopen the CSV and check the generated binary is identical
self.assertNotIn("WARNING", output)
self.assertNotIn(b"WARNING", output)
with open(csvpath, 'r') as f:
from_csv = PartitionTable.from_csv(f.read())
self.assertEqual(_strip_trailing_ffs(from_csv.to_binary()), LONGER_BINARY_TABLE)
@ -340,7 +350,7 @@ class CommandLineTests(unittest.TestCase):
# run gen_esp32part.py to conver the CSV to binary again
output = subprocess.check_output([sys.executable, "../gen_esp32part.py",
csvpath, binpath], stderr=subprocess.STDOUT)
self.assertNotIn("WARNING", output)
self.assertNotIn(b"WARNING", output)
# assert that file reads back as identical
with open(binpath, 'rb') as f:
binary_readback = f.read()
@ -355,14 +365,14 @@ class CommandLineTests(unittest.TestCase):
pass
class VerificationTests(unittest.TestCase):
class VerificationTests(Py23TestCase):
def test_bad_alignment(self):
csv = """
# Name,Type, SubType,Offset,Size
app,app, factory, 32K, 1M
"""
with self.assertRaisesRegexp(ValidationError,
with self.assertRaisesRegex(ValidationError,
r"Offset.+not aligned"):
t = PartitionTable.from_csv(csv)
t.verify()
@ -370,14 +380,14 @@ app,app, factory, 32K, 1M
def test_warnings(self):
try:
sys.stderr = StringIO.StringIO() # capture stderr
sys.stderr = io.StringIO() # capture stderr
csv_1 = "app, 1, 2, 32K, 1M\n"
PartitionTable.from_csv(csv_1).verify()
self.assertIn("WARNING", sys.stderr.getvalue())
self.assertIn("partition type", sys.stderr.getvalue())
sys.stderr = StringIO.StringIO()
sys.stderr = io.StringIO()
csv_2 = "ota_0, app, ota_1, , 1M\n"
PartitionTable.from_csv(csv_2).verify()
self.assertIn("WARNING", sys.stderr.getvalue())
@ -386,7 +396,7 @@ app,app, factory, 32K, 1M
finally:
sys.stderr = sys.__stderr__
class PartToolTests(unittest.TestCase):
class PartToolTests(Py23TestCase):
def _run_parttool(self, csvcontents, args):
csvpath = tempfile.mktemp()
@ -395,8 +405,8 @@ class PartToolTests(unittest.TestCase):
try:
output = subprocess.check_output([sys.executable, "../parttool.py"] + args.split(" ") + [ csvpath ],
stderr=subprocess.STDOUT)
self.assertNotIn("WARNING", output)
m = re.search("0x[0-9a-fA-F]+", output)
self.assertNotIn(b"WARNING", output)
m = re.search(b"0x[0-9a-fA-F]+", output)
return m.group(0) if m else ""
finally:
os.remove(csvpath)
@ -411,13 +421,13 @@ factory, app, factory, 0x10000, 1M
rpt = lambda args: self._run_parttool(csv, args)
self.assertEqual(
rpt("--type data --subtype nvs --offset"), "0x9000")
rpt("--type data --subtype nvs --offset"), b"0x9000")
self.assertEqual(
rpt("--type data --subtype nvs --size"), "0x4000")
rpt("--type data --subtype nvs --size"), b"0x4000")
self.assertEqual(
rpt("--partition-name otadata --offset"), "0xd000")
rpt("--partition-name otadata --offset"), b"0xd000")
self.assertEqual(
rpt("--default-boot-partition --offset"), "0x10000")
rpt("--default-boot-partition --offset"), b"0x10000")
def test_fallback(self):
csv = """
@ -430,13 +440,13 @@ ota_1, app, ota_1, , 1M
rpt = lambda args: self._run_parttool(csv, args)
self.assertEqual(
rpt("--type app --subtype ota_1 --offset"), "0x130000")
rpt("--type app --subtype ota_1 --offset"), b"0x130000")
self.assertEqual(
rpt("--default-boot-partition --offset"), "0x30000") # ota_0
rpt("--default-boot-partition --offset"), b"0x30000") # ota_0
csv_mod = csv.replace("ota_0", "ota_2")
self.assertEqual(
self._run_parttool(csv_mod, "--default-boot-partition --offset"),
"0x130000") # now default is ota_1
b"0x130000") # now default is ota_1
if __name__ =="__main__":
unittest.main()