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

test: cleanup resources at startup time in system and sample tests #590

Closed
tswast opened this issue Apr 6, 2021 · 2 comments · Fixed by #1741
Closed

test: cleanup resources at startup time in system and sample tests #590

tswast opened this issue Apr 6, 2021 · 2 comments · Fixed by #1741
Assignees
Labels
api: bigquery Issues related to the googleapis/python-bigquery API. priority: p3 Desirable enhancement or fix. May not be included in next release. samples Issues that are directly related to samples. type: process A process-related concern. May include testing, release, or the like.

Comments

@tswast
Copy link
Contributor

tswast commented Apr 6, 2021

  1. Update fixtures to use a consistent prefix, so we can tell which resources are created by python-bigquery (and possibly disambiguate between samples and system tests). Also include a timestamp in the prefix.
  2. List all resources in the project, filter by our prefix, delete those resources that were created >= 24 hours ago.

This will prevent resource leaks from getting too bad. Teardown doesn't always get a chance to run because of intermittent Kokoro errors and timeouts.

@product-auto-label product-auto-label bot added api: bigquery Issues related to the googleapis/python-bigquery API. samples Issues that are directly related to samples. labels Apr 6, 2021
@tswast tswast added the type: process A process-related concern. May include testing, release, or the like. label Apr 6, 2021
@tswast
Copy link
Contributor Author

tswast commented Apr 6, 2021

I experiment with this in #591 (but we'll still need to cleanup the other tests in the samples/snippets directory, even after that is merged.

Datasets are the primary resource that we're concerned about, as they contain tables, models, routines. We should be able to cleanup those before the tests run with a fixture like the following:

RESOURCE_PREFIX = "python_bigquery_samples_snippets"


def resource_prefix() -> str:
    timestamp = datetime.datetime.utcnow().strftime("%Y%m%d_%H%M%S")
    random_string = hex(random.randrange(1000000))[2:]
    return f"{RESOURCE_PREFIX}_{timestamp}_{random_string}"


@pytest.fixture(scope="session", autouse=True)
def cleanup_datasets(bigquery_client: bigquery.Client):
    yesterday = datetime.datetime.utcnow() - datetime.timedelta(days=1)
    for dataset in bigquery_client.list_datasets():
        if (
            dataset.dataset_id.startswith(RESOURCE_PREFIX)
            and dataset.created < yesterday
        ):
            bigquery_client.delete_dataset(
                dataset, delete_contents=True, not_found_ok=True

@tswast
Copy link
Contributor Author

tswast commented Apr 6, 2021

Also, we could standardize on the prefix format and put the logic here: https://github.com/googleapis/python-test-utils

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the googleapis/python-bigquery API. priority: p3 Desirable enhancement or fix. May not be included in next release. samples Issues that are directly related to samples. type: process A process-related concern. May include testing, release, or the like.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants