While running tests on a MAAS juju cloud where the model is named admin/mymodel according to the <user>/<model> convention, the ops_test fixture does not work.
Steps to reproduce error
Models are named admin/mymodel and admin/controller.
Have a simple test that calls the ops_test fixture.
def test_something(ops_test):
assert True
And run pytest path/to/test.py --model admin/mymodel to execute the tests.
Analysis
I was trying to reuse my existing model by providing --model admin/mymodel but this still tried to create a new model (and promptly failed since I wasn't the admin user on the cloud). Upon tracing the execution flow from the plugin.py -> ops_test -> _setup_model -> track_model -> _model_exists where the last method is used to set the value of the use_existing variable which was getting the wrong value (should be True, getting False).
This seemed to be due to the fact that the self._controller.list_models() returns the models but without prefixing the user name (without "admin" in this instance).
async def _model_exists(self, model_name: str) -> bool:
"""
returns True when the model_name exists in the model.
"""
all_models = await self._controller.list_models()
return model_name in all_models
# all_models had the value ['controller', 'mymodel'] but model_name is "admin/mymodel"
As seen above, the _model_exists method returns False when the self._controller.list_models() returns the wrong name for the current model.
Temporary workaround
Made changes here.
return model_name.split("/")[-1] in all_models
While running tests on a MAAS juju cloud where the model is named
admin/mymodelaccording to the<user>/<model>convention, theops_testfixture does not work.Steps to reproduce error
Models are named
admin/mymodelandadmin/controller.Have a simple test that calls the
ops_testfixture.And run
pytest path/to/test.py --model admin/mymodelto execute the tests.Analysis
I was trying to reuse my existing model by providing
--model admin/mymodelbut this still tried to create a new model (and promptly failed since I wasn't the admin user on the cloud). Upon tracing the execution flow from theplugin.py->ops_test->_setup_model->track_model->_model_existswhere the last method is used to set the value of theuse_existingvariable which was getting the wrong value (should be True, getting False).This seemed to be due to the fact that the
self._controller.list_models()returns the models but without prefixing the user name (without "admin" in this instance).As seen above, the
_model_existsmethod returns False when theself._controller.list_models()returns the wrong name for the current model.Temporary workaround
Made changes here.