Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow MongoDB to be instantiated without mongomock library #11023

Merged
merged 1 commit into from
Mar 4, 2022

Conversation

amaltaro
Copy link
Contributor

@amaltaro amaltaro commented Mar 4, 2022

Fixes #11022

Status

ready

Description

Don't crash the service that is importing MongoDB if it does not have the mongomock library in the path, which is actually only needed for unit tests.

Is it backward compatible (if not, which system it affects?)

YES

Related PRs

Complement for: #11007

External dependencies / deployment changes

None

@cmsdmwmbot
Copy link

Jenkins results:

  • Python3 Unit tests: succeeded
    • 3 changes in unstable tests
  • Python3 Pylint check: succeeded
  • Pylint py3k check: succeeded
  • Pycodestyle check: succeeded

Details at https://cmssdt.cern.ch/dmwm-jenkins/view/All/job/DMWM-WMCore-PR-test/12841/artifact/artifacts/PullRequestReport.html

Copy link
Contributor

@todor-ivanov todor-ivanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @amaltaro for your quick fix. I have left one comment inline, which I think is worth taking a look.

import mongomock
except ImportError:
# this library should only be required by unit tests
mongomock = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a good workaround, but if we go that way we should then walk to the end.

We do know that it is supposed to be used only in the unit tests. But, since the class itself provides the option for an object to be created with mockMongoDB=True, then if someone, who is missing the mongomock library, does try to benefit from the flag, this would generate the following error on line 53 :

In [1]: mongomock = None

In [2]: client = mongomock.MongoClient()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-233abe3373bf> in <module>
----> 1 client = mongomock.MongoClient()

AttributeError: 'NoneType' object has no attribute 'MongoClient'

In this case I would suggest to add some extra protection on line in question, like:

In [7]: if mockMongoDB and mongomock is not None:
   ...:     self.client = mongomock.MongoClient()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually wanted it to crash indeed, since it would be a setup that people should never even try. Nothing as a big error in our face :)
But I think I can do something slightly better here, still along the same line of thought. New change coming up in a few minutes.

@cmsdmwmbot
Copy link

Jenkins results:

  • Python3 Unit tests: succeeded
    • 1 changes in unstable tests
  • Python3 Pylint check: succeeded
  • Pylint py3k check: succeeded
  • Pycodestyle check: succeeded

Details at https://cmssdt.cern.ch/dmwm-jenkins/view/All/job/DMWM-WMCore-PR-test/12844/artifact/artifacts/PullRequestReport.html

Copy link
Contributor

@todor-ivanov todor-ivanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @amaltaro this looks perfect now.

if mockMongoDB and mongomock is None:
msg = "You are trying to mock MongoDB, but you do not have mongomock in the python path."
self.logger.critical(msg)
raise ImportError(msg)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, this is sound.
Agreed!

raise ImportError if requested to mock mongo without mongomock in the path
@amaltaro
Copy link
Contributor Author

amaltaro commented Mar 4, 2022

Thanks Todor. Code rebased and will get merged soon.

@cmsdmwmbot
Copy link

Jenkins results:

  • Python3 Unit tests: succeeded
    • 2 changes in unstable tests
  • Python3 Pylint check: succeeded
  • Pylint py3k check: succeeded
  • Pycodestyle check: succeeded

Details at https://cmssdt.cern.ch/dmwm-jenkins/view/All/job/DMWM-WMCore-PR-test/12845/artifact/artifacts/PullRequestReport.html

@amaltaro amaltaro merged commit 96af8ec into dmwm:master Mar 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MSOutput/MSUnmerged fails to get an instance of the MongoDB object
3 participants