[openstack-dev] [nova] how to unit test scripts outside of nova/nova?

Matt Riedemann mriedem at linux.vnet.ibm.com
Wed Jul 2 00:29:02 UTC 2014



On 7/1/2014 4:03 PM, Matthew Treinish wrote:
> On Tue, Jul 01, 2014 at 03:21:06PM -0500, Matt Riedemann wrote:
>> As part of the enforce-unique-instance-uuid-in-db blueprint [1] I'm writing
>> a script to scan the database and find any NULL instance_uuid records that
>> will cause the new database migration to fail so that operators can run this
>> before they run the migration, otherwise the migration blocks if these types
>> of records are found.
>>
>> I have the script written [2], but wanted to also write unit tests for it. I
>> guess I assumed the script would go under nova/tools/db like the
>> schema_diff.py script, but I'm not sure how to unit test anything outside of
>> the nova/nova tree.
>>
>> Nova's testr configuration is only discovering tests within nova/tests [3].
>> But I don't think I can put the unit tests under nova/tests and then import
>> the module from nova/tools.
>
> So we hit a similar issue in tempest when we wanted to unit test some utility
> scripts in tempest/tools. Changing the discovery path to find tests outside of
> nova/tests is actually a pretty easy change[4], but I don't think that will solve
> the use case with tox. What happened when we tried to do this in tempest use
> case was that when the project was getting installed the tools dir wasn't
> included so when we ran with tox it couldn't find the files we were trying to
> test. The solution we came up there was to put the script under the tempest
> namespace and add unit tests in tempest/tests. (we also added an entry point for
> the script to expose it as a command when tempest was installed)
>
>>
>> So I'm a bit stuck.  I could take the easy way out and just throw the script
>> under nova/db/sqlalchemy/migrate_repo and put my unit tests under
>> nova/tests/db/, and I'd also get pep8 checking with that, but that doesn't
>> seem right - but I'm also possibly over-thinking this.
>>
>> Anyone else have any ideas?
>
> I think it really comes down to how you want to present the utility to the end
> users. To enable unit testing it, it's just easier to put it in the nova
> namespace. I couldn't come up with a good way to get around the
> install/namespace issue. (maybe someone else who is more knowledgeable here has
> a good way to get around this) So then you can symlink it to the tools dir or
> add an entry point (or bake it into nova-manage) to make it easy to find. I
> think the issue with putting it in nova/db/sqlalchemy/migrate_repo is that it's
> hard to find.
>
>>
>> [1] https://blueprints.launchpad.net/nova/+spec/enforce-unique-instance-uuid-in-db
>> [2] https://review.openstack.org/#/c/97946/
>> [3] http://git.openstack.org/cgit/openstack/nova/tree/.testr.conf#n5
> [4] http://git.openstack.org/cgit/openstack/tempest/tree/tempest/test_discover/test_discover.py
>
> -Matt Treinish
>
>
>
> _______________________________________________
> OpenStack-dev mailing list
> OpenStack-dev at lists.openstack.org
> http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
>

Matt,

Thanks for the help, I completely forgot about making the new script an 
entry point in setup.cfg, that's a good idea.

Before I saw this I did move the script under 
nova/db/sqlalchemy/migrate_repo and moved the tests under nova/tests/db 
and have that all working now, so will probably just move forward with 
that rather than try to do some black magic with test discovery and 
getting the module imported.

-- 

Thanks,

Matt Riedemann




More information about the OpenStack-dev mailing list