Skip to content

Commit

Permalink
Enhance integration tests: add cleanup mode for repository management…
Browse files Browse the repository at this point in the history
… and implement test for basic repository operations
  • Loading branch information
Dave Arnold committed Apr 29, 2025
1 parent 3c3d1a3 commit 4d564ab
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ cd eks_automation
python -m pytest tests/ -v -m integration
```

Note: Integration tests will create temporary repositories in your GitHub organization. These repositories will be archived (not deleted) after the tests complete.
Note: Integration tests will create temporary repositories in your GitHub organization. These repositories will be archived (not deleted) after the tests complete. To implement full cleanup, the test code would need to be modified to delete repositories instead of archiving them.

## Resources

Expand Down
54 changes: 51 additions & 3 deletions template_automation/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
import pytest
import os
import json
import pytest
from github import Github
import time

@pytest.fixture(scope="session")
def github_client():
"""Create a GitHub client for integration tests."""
token = os.environ.get("GITHUB_TOKEN")
if not token:
pytest.skip("GITHUB_TOKEN environment variable not set")

api_url = os.environ.get("GITHUB_API", "https://api.github.com")
return Github(base_url=api_url, login_or_token=token)

@pytest.fixture(scope="session")
def cleanup_mode():
"""Determine if repositories should be deleted or just archived."""
return os.environ.get("INTEGRATION_TEST_DELETE_REPOS", "").lower() in ("true", "1", "yes")

@pytest.fixture
def test_repo(github_client, cleanup_mode, request):
"""Create a test repository and clean it up after the test."""
org_name = os.environ.get("GITHUB_ORG")
if not org_name:
pytest.skip("GITHUB_ORG environment variable not set")

# Create a unique repo name for this test
repo_name = f"test-repo-{pytest.config.getoption('--timestamp', default='')}-{id(request)}"

org = github_client.get_organization(org_name)
repo = org.create_repo(
repo_name,
description="Temporary repository for integration testing",
private=True
)

yield repo

# Clean up after the test
if cleanup_mode:
# Delete the repository
repo.delete()
else:
# Archive the repository (the original behavior)
repo.edit(archived=True)

@pytest.fixture
def github_client_params():
Expand Down Expand Up @@ -71,4 +114,9 @@ def mock_reference_response():
"sha": "test-commit-sha",
"type": "commit"
}
}
}

def pytest_addoption(parser):
"""Add custom command line options."""
timestamp = int(time.time())
parser.addoption("--timestamp", action="store", default=str(timestamp))
16 changes: 16 additions & 0 deletions template_automation/tests/integration/test_github_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest
import os

@pytest.mark.integration
def test_repository_operations(test_repo, cleanup_mode):
"""Test basic repository operations."""
# Your test code here that uses the test_repo

# This is just an example verification
assert test_repo.name.startswith("test-repo-")

# Log what will happen to this repository
if cleanup_mode:
print(f"Repository {test_repo.name} will be DELETED after this test")
else:
print(f"Repository {test_repo.name} will be ARCHIVED after this test")
5 changes: 5 additions & 0 deletions template_automation/tests/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[pytest]
markers =
integration: marks tests as integration tests (requires GitHub API access)
addopts = --timestamp=%(timestamp)s
python_functions = test_*

0 comments on commit 4d564ab

Please sign in to comment.