[ovs-dev] [PATCH] tests: Fix the two Python XFAIL tests.

Ethan Jackson ethan at nicira.com
Tue May 24 01:30:20 UTC 2011


Seems Fine.

Ethan

On Fri, May 13, 2011 at 14:41, Ben Pfaff <blp at nicira.com> wrote:
> OVS has two Python tests that have always failed, for reasons not
> understood, since they were added to the tree.  This commit fixes them.
>
> One problem was that Python was assuming that stdout was encoded in ASCII.
> Apparently the only way to "fix" this at runtime is to set PYTHONIOENCODING
> to utf_8 in the environment, so this change does that.
>
> Second, it appears that Python really doesn't like to print invalid UTF-8,
> so this avoids doing that in python/ovs/json.py, instead just printing
> the hexadecimal values of the invalid bytes.  For consistency, it makes
> the same change to the C version.
>
> Third, the C version of test-ovsdb doesn't check UTF-8 for consistency, it
> just sends it blindly to the OVSDB server, but Python does check it and so
> it bails out earlier.  This commit changes the Python version of the
> "no invalid UTF-8 sequences in strings" to allow for the slight difference
> in output that occurs for that reason.
>
> Finally, test-ovsdb.py needs to convert error messages to Unicode
> explicitly before printing them in the "parse-atoms" function.  I don't
> really understand why, but now it works.
> ---
>  lib/ovsdb-data.c    |    3 +--
>  python/ovs/json.py  |    6 ++----
>  tests/atlocal.in    |    3 +++
>  tests/ovsdb-data.at |   25 +++++++++++++++++++------
>  tests/test-ovsdb.py |    6 +-----
>  5 files changed, 26 insertions(+), 17 deletions(-)
>
> diff --git a/lib/ovsdb-data.c b/lib/ovsdb-data.c
> index 150ae61..54ce72c 100644
> --- a/lib/ovsdb-data.c
> +++ b/lib/ovsdb-data.c
> @@ -690,8 +690,7 @@ check_string_constraints(const char *s,
>         struct ovsdb_error *error;
>
>         error = ovsdb_error("constraint violation",
> -                            "\"%s\" is not a valid UTF-8 string: %s",
> -                            s, msg);
> +                            "not a valid UTF-8 string: %s", msg);
>         free(msg);
>         return error;
>     }
> diff --git a/python/ovs/json.py b/python/ovs/json.py
> index 1e26a62..f8b02d1 100644
> --- a/python/ovs/json.py
> +++ b/python/ovs/json.py
> @@ -1,4 +1,4 @@
> -# Copyright (c) 2010 Nicira Networks
> +# Copyright (c) 2010, 2011 Nicira Networks
>  #
>  # Licensed under the Apache License, Version 2.0 (the "License");
>  # you may not use this file except in compliance with the License.
> @@ -114,9 +114,7 @@ def from_string(s):
>         s = unicode(s, 'utf-8')
>     except UnicodeDecodeError, e:
>         seq = ' '.join(["0x%2x" % ord(c) for c in e.object[e.start:e.end]])
> -        raise Error("\"%s\" is not a valid UTF-8 string: "
> -                    "invalid UTF-8 sequence %s" % (s, seq),
> -                    tag="constraint violation")
> +        return ("not a valid UTF-8 string: invalid UTF-8 sequence %s" % seq)
>     p = Parser(check_trailer=True)
>     p.feed(s)
>     return p.finish()
> diff --git a/tests/atlocal.in b/tests/atlocal.in
> index f1c0457..9c9f654 100644
> --- a/tests/atlocal.in
> +++ b/tests/atlocal.in
> @@ -6,3 +6,6 @@ PYTHON='@PYTHON@'
>
>  PYTHONPATH=$PYTHONPATH:$abs_top_srcdir/python
>  export PYTHONPATH
> +
> +PYTHONIOENCODING=utf_8
> +export PYTHONIOENCODING
> diff --git a/tests/ovsdb-data.at b/tests/ovsdb-data.at
> index d45e07b..b74e682 100644
> --- a/tests/ovsdb-data.at
> +++ b/tests/ovsdb-data.at
> @@ -231,14 +231,28 @@ OVSDB_CHECK_POSITIVE_CPY([real not acceptable integer JSON atom],
>
>  dnl <C0> is not allowed anywhere in a UTF-8 string.
>  dnl <ED A0 80> is a surrogate and not allowed in UTF-8.
> -OVSDB_CHECK_POSITIVE_CPY([no invalid UTF-8 sequences in strings],
> +OVSDB_CHECK_POSITIVE([no invalid UTF-8 sequences in strings],
>   [parse-atoms '[["string"]]' \
>      '@<:@"m4_esyscmd([printf "\300"])"@:>@' \
>      '@<:@"m4_esyscmd([printf "\355\240\200"])"@:>@' \
>  ],
> -  [constraint violation: "m4_esyscmd([printf "\300"])" is not a valid UTF-8 string: invalid UTF-8 sequence 0xc0
> -constraint violation: "m4_esyscmd([printf "\355\240\200"])" is not a valid UTF-8 string: invalid UTF-8 sequence 0xed 0xa0],
> -  [], [], [xfail])
> +  [constraint violation: not a valid UTF-8 string: invalid UTF-8 sequence 0xc0
> +constraint violation: not a valid UTF-8 string: invalid UTF-8 sequence 0xed 0xa0])
> +
> +dnl Python won't let invalid UTF-8 (its idea of invalid UTF-8, anyway) into it
> +dnl at all, so this test never gets as far as a constraint violation.  It's
> +dnl just a JSON parse error.
> +dnl
> +dnl <C0> is not allowed anywhere in a UTF-8 string.
> +dnl (<ED A0 80> is not allowed in UTF-8 but Python doesn't care.)
> +dnl <ED 80 7F> is not allowed in UTF-8.
> +OVSDB_CHECK_POSITIVE_PY([no invalid UTF-8 sequences in strings - Python],
> +  [parse-atoms '[["string"]]' \
> +     '@<:@"m4_esyscmd([printf "\300"])"@:>@' \
> +     '@<:@"m4_esyscmd([printf "\355\200\177"])"@:>@' \
> +],
> +  ["not a valid UTF-8 string: invalid UTF-8 sequence 0xc0"
> +"not a valid UTF-8 string: invalid UTF-8 sequence 0xed 0x80"])
>
>  OVSDB_CHECK_NEGATIVE([real not acceptable integer string atom],
>   [[parse-atom-strings '["integer"]' '0.5' ]],
> @@ -452,8 +466,7 @@ OVSDB_CHECK_POSITIVE_CPY([strings at least 2 characters long],
>  constraint violation: "a" length 1 is less than minimum allowed length 2
>  "ab"
>  "abc"
> -constraint violation: "𝄞" length 1 is less than minimum allowed length 2]],
> -  [], [], [xfail])
> +constraint violation: "𝄞" length 1 is less than minimum allowed length 2]])
>
>  OVSDB_CHECK_POSITIVE_CPY([strings no more than 2 characters long],
>   [[parse-atoms '{"type": "string", "maxLength": 2}' \
> diff --git a/tests/test-ovsdb.py b/tests/test-ovsdb.py
> index 2eafe99..ea45f9a 100644
> --- a/tests/test-ovsdb.py
> +++ b/tests/test-ovsdb.py
> @@ -98,7 +98,7 @@ def do_parse_atoms(type_string, *atom_strings):
>             atom = data.Atom.from_json(base, atom_json)
>             print ovs.json.to_string(atom.to_json())
>         except error.Error, e:
> -            print e
> +            print unicode(e)
>
>  def do_parse_data(type_string, *data_strings):
>     type_json = unbox_json(ovs.json.from_string(type_string))
> @@ -290,10 +290,6 @@ The following options are also available:
>     sys.exit(0)
>
>  def main(argv):
> -    # Make stdout and stderr UTF-8, even if they are redirected to a file.
> -    sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
> -    sys.stderr = codecs.getwriter("utf-8")(sys.stderr)
> -
>     try:
>         options, args = getopt.gnu_getopt(argv[1:], 't:h',
>                                           ['timeout',
> --
> 1.7.4.4
>
> _______________________________________________
> dev mailing list
> dev at openvswitch.org
> http://openvswitch.org/mailman/listinfo/dev
>



More information about the dev mailing list