Skip to content

Commit

Permalink
warmer
Browse files Browse the repository at this point in the history
  • Loading branch information
Test User authored and morga471 committed Sep 30, 2025
1 parent c231f15 commit 2bb101e
Show file tree
Hide file tree
Showing 20 changed files with 850 additions and 122 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ help: ## Show this help message
@echo 'Targets:'
@egrep '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}'

all: setup-env clean develop lint format test verify-all ## Run the complete build, test, and validation workflow
all: clean setup-env develop lint format test verify-all ## Run the complete build, test, and validation workflow

install: ## Install the package
pip install .
Expand All @@ -25,7 +25,7 @@ clean: ## Clean build artifacts
@rm -rf dist/
@rm -rf *.egg-info
@find . -name "__pycache__" -exec rm -rf {} +
@find . -name "*.pyc" -delete
@find . -name "*.log" -delete
@find . -name ".pytest_cache" -exec rm -rf {} +
@rm -f test-results.log
@rm -f $(VERIFICATION_LOG)
Expand Down Expand Up @@ -97,6 +97,12 @@ verbose: ## Run with verbose output
debug: ## Run with debug output
@python -m tf_upgrade.cli --debug upgrade $(DIR)

debug-scan: ## Run diagnostic scan for troubleshooting
@python tf_upgrade_diagnostics.py $(DIR) --verbose

recursive-scan: ## Run explicitly recursive scan
@python -m tf_upgrade.cli scan --recursive $(DIR)

analyze: ## Analyze complexity of Terraform configurations
@python -m tf_upgrade.cli analyze-complexity $(DIR)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
provider "aws" {
version = "~> 2.0"
region = "us-west-2"
}

resource "aws_s3_bucket" "simple" {
bucket = "my-simple-bucket"
acl = "private"
}

output "bucket_id" {
value = "${aws_s3_bucket.simple.id}"
}
21 changes: 21 additions & 0 deletions tests/fixtures/0.12/backup-test/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}

provider "aws" {
version = "~> 2.0"
region = "us-west-2"
}

resource "aws_s3_bucket" "simple" {
bucket = "my-simple-bucket"
acl = "private"
}

output "bucket_id" {
value = "${aws_s3_bucket.simple.id}"
}
12 changes: 12 additions & 0 deletions tests/fixtures/0.12/backup-test/terraform-upgrade-dryrun-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

## Summary

- Current Version: 0.12
- Upgrade Path: 0.13 → 0.14 → 0.15 → 1.0
- Complexity Level: Low
- Complexity Score: 8.5

### Deprecated Syntax

- interpolation_only: `"${aws_s3_bucket.simple.id}"` in main.tf
- quoted_interpolation: `"${aws_s3_bucket.simple.id}"` in main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Terraform Upgrade Report

Report generated on 2025-03-27 19:59:34

## Progress

| Step | Status | Duration | Details |
|------|--------|----------|--------|
| 1: Upgrading to Terraform 0.13 | ❌ Failed | 2s | **Error:** Failed to upgrade to Terraform 0.13 |

## Summary

- **Status:** ❌ Failed
- **Total Duration:** 2s
- **Steps Completed:** 1/1
- **Successful Steps:** 0
- **Failed Steps:** 1

## Details

- **Started:** 2025-03-27 19:59:34
- **Completed:** 2025-03-27 19:59:36

### Failed Steps

#### Step 1: Upgrading to Terraform 0.13

- **Details:** Failed to upgrade to Terraform 0.13

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"started_at": "2025-03-27T19:59:24.695824",
"steps": [],
"completed": 0,
"total": 0,
"status": "in_progress"
}
12 changes: 12 additions & 0 deletions tests/fixtures/0.12/simple/terraform-upgrade-dryrun-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

## Summary

- Current Version: 0.12
- Upgrade Path: 0.13 → 0.14 → 0.15 → 1.0
- Complexity Level: Low
- Complexity Score: 8.5

### Deprecated Syntax

- interpolation_only: `"${aws_s3_bucket.simple.id}"` in main.tf
- quoted_interpolation: `"${aws_s3_bucket.simple.id}"` in main.tf
7 changes: 7 additions & 0 deletions tests/fixtures/0.12/simple/upgrade-logs/upgrade-progress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"started_at": "2025-03-27T19:59:24.695824",
"steps": [],
"completed": 0,
"total": 0,
"status": "in_progress"
}
19 changes: 17 additions & 2 deletions tests/fixtures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,24 @@ def transform_provider_version(directory):

# Replace provider block with required_providers syntax
if 'provider "aws"' in content and "version =" in content:
# Fix the string replacement - the previous version had mismatched quotes
content = content.replace(
'provider "aws" {\n version = "~> 2.0"\n region = "us-west-2"\n}',
'terraform {\n required_providers {\n aws = {\n source = "hashicorp/aws"\n version = "~> 2.0"\n }\n }\n}\n\nprovider "aws" {\n region = "us-west-2"\n}',
"""provider "aws" {
version = "~> 2.0"
region = "us-west-2"
}""",
"""terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 2.0"
}
}
}
provider "aws" {
region = "us-west-2"
}""",
)

with open(main_tf, "w") as f:
Expand Down
176 changes: 80 additions & 96 deletions tests/integration/test_upgrade_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,71 +27,89 @@
"""


# The key to fixing the git errors is to apply the patches at the module level
# before any of the test functions run
@patch("subprocess.run")
@patch("subprocess.check_output")
@patch("tf_upgrade.utils.terraform.subprocess.check_output")
@patch("tf_upgrade.utils.git.subprocess.check_output")
@patch("tf_upgrade.utils.git.os.path.exists")
@patch("shutil.which")
class TestUpgradeWorkflow(unittest.TestCase):
def setUp(
self,
mock_run=None,
mock_check_output=None,
mock_tf_check_output=None,
mock_git_check_output=None,
mock_git_exists=None,
mock_which=None,
):
def setUp(self):
"""Set up test environment with pre-configured mocks."""
# Create patchers
self.patchers = [
patch("shutil.which"),
patch("tf_upgrade.utils.git.os.path.exists"),
patch("tf_upgrade.utils.terraform.subprocess.check_output"),
patch("subprocess.check_output"),
patch("subprocess.run"),
patch("tf_upgrade.utils.git.subprocess.check_output"),
patch("tf_upgrade.utils.git.subprocess.run"),
# Add patches for config loading and environment checks
patch("tf_upgrade.config.os.path.exists", return_value=False),
patch(
"tf_upgrade.utils.terraform.get_terraform_binary",
return_value="/usr/bin/terraform",
),
]

# Start all the patchers and get the mocks
self.mock_which = self.patchers[0].start()
self.mock_git_exists = self.patchers[1].start()
self.mock_tf_check_output = self.patchers[2].start()
self.mock_check_output = self.patchers[3].start()
self.mock_run = self.patchers[4].start()
self.mock_git_check_output = self.patchers[5].start()
self.mock_git_run = self.patchers[6].start()

# Create a temporary directory for the test
self.test_dir = tempfile.mkdtemp()

# Configure mocks to handle git commands
self.mock_which.return_value = "/usr/bin/git"
self.mock_git_exists.return_value = True # Simulate that .git directory exists
self.mock_git_check_output.return_value = b"main"
self.mock_check_output.return_value = b"dummy/git/path"
self.mock_tf_check_output.return_value = b"terraform version"
self.mock_run.return_value = MagicMock(returncode=0, stdout=b"Success")
self.mock_git_run.return_value = MagicMock(returncode=0, stdout=b"Success")

# Create a test Terraform configuration file
self.config_file = os.path.join(self.test_dir, "main.tf")
with open(self.config_file, "w") as f:
f.write(TERRAFORM_0_12_FIXTURE)

# Initialize the components we need
self.file_manager = FileManager(self.test_dir)
self.terraform_runner = TerraformRunner(self.test_dir)
self.controller = UpgradeController(self.test_dir)
with patch(
"tf_upgrade.config.load_config", return_value={}
): # Mock config loading
self.file_manager = FileManager(self.test_dir)
self.terraform_runner = TerraformRunner(self.test_dir)
self.controller = UpgradeController(self.test_dir)

def tearDown(self):
# Stop all the patchers
for patcher in self.patchers:
patcher.stop()

# Clean up the temporary directory
shutil.rmtree(self.test_dir)

@patch("tf_upgrade.version_detector.VersionDetector.analyze_directory")
def test_complete_upgrade_workflow(
self,
mock_analyze,
mock_which,
mock_git_exists,
mock_git_check_output,
mock_tf_check_output,
mock_check_output,
mock_run,
):
@patch("tf_upgrade.utils.terraform.get_available_terraform_versions")
def test_complete_upgrade_workflow(self, mock_get_versions, mock_analyze):
"""Test complete upgrade workflow with mocked git commands."""
# Configure mocks for this test
self._configure_mocks(
mock_which,
mock_git_exists,
mock_git_check_output,
mock_tf_check_output,
mock_check_output,
mock_run,
)

# Mock version detection
mock_analyze.return_value = {
"final_version": "0.12",
"upgrade_path": ["0.13", "0.14", "0.15", "1.10"],
"upgrade_path": ["0.13", "0.14", "0.15", "1.0"],
"requires_upgrade": True,
}

# Mock available Terraform versions
mock_get_versions.return_value = [
"0.12.31",
"0.13.7",
"0.14.11",
"0.15.5",
"1.0.11",
"1.10.5",
]

# Replace the upgrade method with a mock that always succeeds
with patch.object(
self.controller,
Expand All @@ -105,27 +123,9 @@ def test_complete_upgrade_workflow(
self.assertTrue(result["success"])

@patch("tf_upgrade.version_detector.VersionDetector.analyze_directory")
def test_step_by_step_upgrade(
self,
mock_analyze,
mock_which,
mock_git_exists,
mock_git_check_output,
mock_tf_check_output,
mock_check_output,
mock_run,
):
@patch("tf_upgrade.utils.terraform.get_available_terraform_versions")
def test_step_by_step_upgrade(self, mock_get_versions, mock_analyze):
"""Test step-by-step upgrade with mocked git commands."""
# Configure mocks for this test
self._configure_mocks(
mock_which,
mock_git_exists,
mock_git_check_output,
mock_tf_check_output,
mock_check_output,
mock_run,
)

# Mock version detection for each version
mock_analyze.side_effect = [
{
Expand All @@ -150,6 +150,16 @@ def test_step_by_step_upgrade(
},
]

# Mock available Terraform versions
mock_get_versions.return_value = [
"0.12.31",
"0.13.7",
"0.14.11",
"0.15.5",
"1.0.11",
"1.10.5",
]

# Replace the upgrade_to_version method to always succeed
with patch.object(
self.controller,
Expand All @@ -169,42 +179,16 @@ def test_step_by_step_upgrade(
result_1_10 = self.controller.upgrade(target_version="1.10")
self.assertTrue(result_1_10["success"], "Upgrade to 1.10 failed")

def _configure_mocks(
self,
mock_which,
mock_git_exists,
mock_git_check_output,
mock_tf_check_output,
mock_check_output,
mock_run,
):
def _configure_mocks(self):
"""Configure the mocks with common behavior."""
# Configure all mock outputs for git commands
mock_which.return_value = "/usr/bin/git" # Make git appear to be installed
mock_git_exists.return_value = (
False # Make it appear as if .git directory doesn't exist
)
mock_git_check_output.return_value = b"dummy/git/path"
mock_tf_check_output.return_value = b"dummy/git/path"
mock_check_output.return_value = b"dummy/git/path"

# Configure subprocess.run to handle git commands specially
def mock_subprocess_run(*args, **kwargs):
cmd = args[0] if args else kwargs.get("args", [])
if isinstance(cmd, list) and cmd and cmd[0] == "git":
# Return failed for git commands with appropriate error
result = MagicMock()
result.returncode = 128
result.stdout = b""
return result

# For non-git commands, return success
result = MagicMock()
result.returncode = 0
result.stdout = b"Success"
return result

mock_run.side_effect = mock_subprocess_run
# Set up mock return values (in case they need to be reset)
self.mock_which.return_value = "/usr/bin/git"
self.mock_git_exists.return_value = True
self.mock_git_check_output.return_value = b"main"
self.mock_tf_check_output.return_value = b"terraform version"
self.mock_check_output.return_value = b"dummy/git/path"
self.mock_run.return_value = MagicMock(returncode=0, stdout=b"Success")
self.mock_git_run.return_value = MagicMock(returncode=0, stdout=b"Success")


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 2bb101e

Please sign in to comment.