Skip to content

Stabilized db create for tests#441

Merged
ellbosch merged 16 commits intomasterfrom
ellbosch/stabilize-create-db
Apr 14, 2020
Merged

Stabilized db create for tests#441
ellbosch merged 16 commits intomasterfrom
ellbosch/stabilize-create-db

Conversation

@ellbosch
Copy link
Copy Markdown
Collaborator

@ellbosch ellbosch commented Mar 24, 2020

Closes #280.

This PR introduces retry logic to check the status of a db creation using sys.dm_operation_status.

Comment thread tests/mssqltestutils.py Outdated
Comment thread tests/mssqltestutils.py Outdated
@pensivebrian pensivebrian self-requested a review March 31, 2020 22:51
Copy link
Copy Markdown
Member

@pensivebrian pensivebrian left a comment

Choose a reason for hiding this comment

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

Couple of changes related to retry logic.

@ellbosch ellbosch requested a review from pensivebrian April 2, 2020 18:01
Comment thread tests/mssqltestutils.py Outdated
@ellbosch ellbosch requested a review from pensivebrian April 2, 2020 22:59
Copy link
Copy Markdown
Member

@pensivebrian pensivebrian left a comment

Choose a reason for hiding this comment

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

Approved with a suggestion to move the master check to the calling function

Comment thread tests/mssqltestutils.py Outdated
@ellbosch
Copy link
Copy Markdown
Collaborator Author

ellbosch commented Apr 4, 2020

@pensivebrian I've made the suggested changes, please let me know your thoughts.

We still need to fix the known issue in the Special module but I can submit that in a separate PR.

Copy link
Copy Markdown
Member

@pensivebrian pensivebrian left a comment

Choose a reason for hiding this comment

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

We need to be able to run against on-premise databases as well as azure.

Comment thread tests/mssqltestutils.py Outdated
Copy link
Copy Markdown
Member

@pensivebrian pensivebrian left a comment

Choose a reason for hiding this comment

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

We should fail the test with a clear error that database creation failed.

Comment thread tests/mssqltestutils.py Outdated
Comment thread tests/mssqltestutils.py Outdated
shutdown(client)

# cleanup db just in case, then raise exception
clean_up_test_db(test_db_name)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

clean_up_test_db [](start = 4, length = 16)

This will most likely fail, so let's wrap in a try catch

Comment thread tests/mssqltestutils.py Outdated

# cleanup db just in case, then raise exception
clean_up_test_db(test_db_name)
raise AssertionError("DB creation failed.")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we put the failing exception in the assert message - this way we don't have look at logs

Comment thread tests/mssqltestutils.py Outdated
if is_create_error or create_db_status == 'FAILED':
# log warning to console and cleanup db
warnings.warn('Test DB create failed with error: {0}'.format(status))
clean_up_test_db(test_db_name)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

clean_up_test_db [](start = 16, length = 16)

I don't think we should we should try an cleanup here. Let's let the caller perform the cleanup.

Comment thread tests/mssqltestutils.py Outdated
Comment thread tests/mssqltestutils.py Outdated
shutdown(client)
return test_db_name

shutdown(client)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we put one shutdown call in a finally block?

Copy link
Copy Markdown
Member

@pensivebrian pensivebrian left a comment

Choose a reason for hiding this comment

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

Why are we refactoring the globalization tests?

Comment thread tests/test_globalization.py Outdated
u'ꇽꇾꇿꈀꈁꈂꈃꈄꈅꈆꈇꈈꈉ'
),
'zang': (
# zang
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We remove the language key? With the key, you could narrow down the failure by logging the language

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

That's a good point, I can use a tuple here for the test data.

Comment thread tests/test_globalization.py Outdated
self.run_charset_validation('zang')

def run_charset_validation(self, charset):
@pytest.mark.parametrize("charset", test_charset_data)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Using pytest, you no longer get integrated tests in vscode. Is there a reason why we're change this?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I don't believe that's true about Pytest anymore (source).

Pytest provides a lot of benefits over unittest, especially in readability of tests. In this particular instance we're using parametrized tests, enabling us to define one test method for multiple tests. This allowed us to remove redundant code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

pytest doesn't work well with parameterized tests. Gene, the engineer before you, ported all pytests to be unittest tests due to this issue. I'd prefer we just leave them as is for now.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

That's very interesting—truthfully I've already ported parametrized tests for 4 other test files (interactive, non_interactive, mssqlclient, multiline). Also looks like our original merge uses parametrized tests for ctes and parseutils.

I'll defer to your judgement if you still prefer to remove this, but in my experience it works great.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As long as it easy to debug a particular language, without having to others, I'm okay with it I guess. What's the command line to run pytest with a specific parameter set?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes there's still the ability to log which language fails. I'm making a new commit to accommodate this.

I'm not positive on how to call a test from the command line with just a single parameter. This StackOverflow post may show it's possible. I usually comment out the other parameters and call pytest test_mod.py::TestClass::test_method.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So that's what I want to avoid - having to comment out tests to run a test.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I've validated you can test for a single parameter using the -k flag, followed by a string which matches the parameter to test.

@ellbosch
Copy link
Copy Markdown
Collaborator Author

ellbosch commented Apr 9, 2020

I just added another commit to improve reliability of our tests:

  • Adds more cleanup that I should have added in the first place for non-interactive tests
  • Creates test db for some tests that were running off of master db

@ellbosch
Copy link
Copy Markdown
Collaborator Author

Another change today to improve DB create reliability and code quality:

  • Tests in test_interactive_mode.py no longer use master DB.
  • A new test_db fixture has been added to mssqltestutils.py to centralize redundant use of create and teardown of test DBs.

Copy link
Copy Markdown
Member

@pensivebrian pensivebrian left a comment

Choose a reason for hiding this comment

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

I think the PR is becoming too complicated. I can tell what's stabilization and what is pytest migration. Can we just fix the create database issue, an address pytest some other time?

Comment thread tests/mssqltestutils.py Outdated
"""
@staticmethod
@pytest.fixture(scope='module')
def test_db():
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

creating an iterator for a single element to injecting the client seems overkill. Can we just have a setup method with creates the client?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'm confused, I don't believe this is an iterator.

We're using Pytest fixtures for tests which were already using this behavior, I was just trying to reduce redundant code.

Comment thread tests/test_special.py
assert len(rows) >= min_rows_expected
assert len(col) == cols_expected

if __name__ == u'__main__':
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These main's were useful to run files :(

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I can put this back!

@ellbosch
Copy link
Copy Markdown
Collaborator Author

I think the PR is becoming too complicated. I can tell what's stabilization and what is pytest migration. Can we just fix the create database issue, an address pytest some other time?

I can simplify this a bit—unfortunately, most of the code changes were about ensuring tests can run off of the master DB (which because a requirement now that we call sys.dm_operation_status).

However, I did refactor things a little in test_special.py, which I can save for another day 🙂

@ellbosch ellbosch force-pushed the ellbosch/stabilize-create-db branch from bbcfc71 to e347b5b Compare April 10, 2020 17:17
Comment thread tests/mssqltestutils.py Outdated
warnings.warn('Test DB create failed with error: {0}'.format(status))
count += 1
shutdown(client)
if is_create_error:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This will prevent the retry logic from ever running.

Comment thread tests/mssqltestutils.py Outdated
@@ -122,6 +118,10 @@ def create_test_db():
raise AssertionError("Database creation failed: retry logic for SQL " \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we surface the status error message in the failure here as well?

Copy link
Copy Markdown
Member

@pensivebrian pensivebrian left a comment

Choose a reason for hiding this comment

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

One comment to update the assertion message.

@ellbosch
Copy link
Copy Markdown
Collaborator Author

@pensivebrian I made a small tweak to our helper method to return an error message with the state response in the form of a tuple.

@ellbosch ellbosch force-pushed the ellbosch/stabilize-create-db branch from 575e39b to 0ef1d8c Compare April 11, 2020 02:31
@ellbosch ellbosch force-pushed the ellbosch/stabilize-create-db branch from 0ef1d8c to 616829c Compare April 11, 2020 17:44
@ellbosch ellbosch merged commit df4246d into master Apr 14, 2020
@ellbosch ellbosch deleted the ellbosch/stabilize-create-db branch April 14, 2020 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create database against Azure needs to more resilient.

2 participants