Using regular expression fails unit test

Asked by Vahid Hashemian on 2014-03-06

Sorry if this is not the best forum to ask this question, but I have run into an issue with using regular expressions on a Horizon patch I'm working on:

When I try to search for instances whose names follow a regular expression the unit test for its module returns an error saying:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 9-11: ordinal not in range(128)

Here's some more information:

The code:
                instances, self._more = api.nova.server_list(
                    search_opts={'name': "^" + ins_name + "$"})

The error returned:
ERROR: test_launch_form_instance_duplicate_case_insensitive_name_error (openstack_dashboard.dashboards.project.instances.tests.InstanceTests)
Traceback (most recent call last):
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/test/", line 224, in inner
    return test_func(*args, **kwargs)
  File "/opt/stack/horizon/openstack_dashboard/test/", line 80, in instance_stub_out
    return fn(self, *args, **kwargs)
  File "/opt/stack/horizon/openstack_dashboard/dashboards/project/instances/", line 2249, in test_launch_form_instance_duplicate_case_insensitive_name_error
    res =, form_data)
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/test/", line 463, in post
    response = super(Client, self).post(path, data=data, content_type=content_type, **extra)
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/test/", line 297, in post
    return self.request(**r)
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/test/", line 424, in request
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/core/handlers/", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/opt/stack/horizon/horizon/", line 38, in dec
    return view_func(request, *args, **kwargs)
  File "/opt/stack/horizon/horizon/", line 54, in dec
    return view_func(request, *args, **kwargs)
  File "/opt/stack/horizon/horizon/", line 38, in dec
    return view_func(request, *args, **kwargs)
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/views/generic/", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/views/generic/", line 86, in dispatch
    return handler(request, *args, **kwargs)
  File "/opt/stack/horizon/horizon/workflows/", line 165, in post
    context = self.get_context_data(**kwargs)
  File "/opt/stack/horizon/horizon/workflows/", line 89, in get_context_data
    workflow = self.get_workflow()
  File "/opt/stack/horizon/horizon/workflows/", line 79, in get_workflow
  File "/opt/stack/horizon/horizon/workflows/", line 650, in __init__
    valid = step.action.is_valid()
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/forms/", line 126, in is_valid
    return self.is_bound and not bool(self.errors)
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/forms/", line 117, in _get_errors
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/forms/", line 273, in full_clean
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/forms/", line 299, in _clean_form
    self.cleaned_data = self.clean()
  File "/opt/stack/horizon/openstack_dashboard/dashboards/project/instances/workflows/", line 199, in clean
  File "/opt/stack/horizon/openstack_dashboard/dashboards/project/instances/workflows/", line 194, in clean
    search_opts={'name': "*" + ins_name + "*"})
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/", line 765, in __call__
    return mock_method(*params, **named_params)
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/", line 1002, in __call__
    expected_method = self._VerifyMethodCall()
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/", line 1060, in _VerifyMethodCall
    raise UnexpectedMethodCallError(self, expected)
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/", line 131, in __init__
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/", line 1068, in __str__
    full_desc = "%s(%s) -> %r" % (self._name, params, self._return_value)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 9-11: ordinal not in range(128)

Note that if I change the code to:
                instances, self._more = api.nova.server_list(
                    search_opts={'name': ins_name})
the test runs fine and no error is returned.

Does anyone know what's causing this and how to get around it?


Question information

English Edit question
OpenStack Dashboard (Horizon) Edit question
No assignee Edit question
Solved by:
Vahid Hashemian
Last query:
Last reply:
Julie Pichon (jpichon) said : #1

Vahid - the 'Answers' section of Launchpad is deprecated, should be used from now on. Though for such development questions, asking on IRC (#openstack-horizon) or on the dev list is probably better.

Did you try making the string you're concatenating with into unicode to see if that makes a difference? i.e.: u"^" + ins_name + u"$"

Thank you Julie for your response. I have tried a few tricks like that but none of them worked (I get the same error).
I'll try to ask this on IRC as well as you suggested (thanks for the heads-up on the 'Answers' section).

I realized the mistake I made.
After changing the code to use regular expression I had forgot to update the test case to do the same.
Silly me!