diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index ae1ccb3..dc79b90 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -32,7 +32,8 @@ "evendead.help-me-add", "charliermarsh.ruff", "streetsidesoftware.code-spell-checker", - "njqdev.vscode-python-typehint" + "njqdev.vscode-python-typehint", + "Cameron.vscode-pytest" ] } } diff --git a/.github/workflows/action_integration_test.yml b/.github/workflows/action_integration_test.yml index 8228a1e..08563dc 100644 --- a/.github/workflows/action_integration_test.yml +++ b/.github/workflows/action_integration_test.yml @@ -1,62 +1,55 @@ -name: "GitHub Action integration test" - -permissions: - contents: write - pull-requests: write - -on: - pull_request: - branches: - - main - workflow_dispatch: - -jobs: - integration-test: - env: - report_json_file: InfraPatch_Statistics.json - - name: "Run GitHub Action integration test" - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v3 - - - name: Run in report only mode - uses: ./ - with: - report_only: true - - - name: Run in update mode - id: update - uses: ./ - with: - report_only: false - target_branch_name: "feat/infrapatch_test_${{ github.run_number }}" - - - name: Check update result - shell: pwsh - run: | - $report = Get-Content $env:report_json_file -Raw | ConvertFrom-Json - if ( -not $report.total_resources -gt 0 ) { - throw "Failed to get resources" - } - if ( -not ( $report.resources_patched -gt 3 ) ) { - throw "No resources should be patched" - } - if ( $report.errors -gt 0 ) { - throw "Errors have been detected" - } - - - name: Delete created branch$ - if: always() - uses: dawidd6/action-delete-branch@v3 - with: - github_token: ${{github.token}} - branches: ${{ steps.update.outputs.target_branch }} - soft_fail: true - - - - - - +name: "GitHub Action integration test" + +on: + workflow_call: + +jobs: + integration-test: + env: + report_json_file: InfraPatch_Statistics.json + + name: "Run GitHub Action integration test" + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Run in report only mode + uses: ./ + with: + report_only: true + + - name: Run in update mode + id: update + uses: ./ + with: + report_only: false + target_branch_name: "feat/infrapatch_test_${{ github.run_number }}" + + - name: Check update result + shell: pwsh + run: | + $report = Get-Content $env:report_json_file -Raw | ConvertFrom-Json + if ( -not $report.total_resources -gt 0 ) { + throw "Failed to get resources" + } + if ( -not ( $report.resources_patched -gt 3 ) ) { + throw "No resources should be patched" + } + if ( $report.errors -gt 0 ) { + throw "Errors have been detected" + } + + - name: Delete created branch$ + if: always() + uses: dawidd6/action-delete-branch@v3 + with: + github_token: ${{github.token}} + branches: ${{ steps.update.outputs.target_branch }} + soft_fail: true + + + + + + diff --git a/.github/workflows/check_format_and_lint.yml b/.github/workflows/check_format_and_lint.yml index 10ae4a8..6753ea9 100644 --- a/.github/workflows/check_format_and_lint.yml +++ b/.github/workflows/check_format_and_lint.yml @@ -1,24 +1,27 @@ +name: "Check Format and Lint Code" + on: - pull_request: - types: - - opened - - synchronize - - reopened - - closed - branches: - - main + workflow_call: jobs: check_code: + name: "Check Format and Lint Code" runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 + + - name: Get Python Version + id: get_python_verion + run: | + python_version=$(cat python_version.txt) + echo "Using Python version $python_version" + echo "::set-output name=python_version::$(echo $python_version)" - name: Install Python uses: actions/setup-python@v2 with: - python-version: 3.11 + python-version: ${{ steps.get_python_verion.outputs.python_version }} - name: Install Dependencies run: | @@ -31,4 +34,9 @@ jobs: - name: Check code with ruff run: ruff check . - \ No newline at end of file + + + + + + diff --git a/.github/workflows/cli-integration-test.yml b/.github/workflows/cli_integration_test.yml similarity index 80% rename from .github/workflows/cli-integration-test.yml rename to .github/workflows/cli_integration_test.yml index 610f9a7..46ef594 100644 --- a/.github/workflows/cli-integration-test.yml +++ b/.github/workflows/cli_integration_test.yml @@ -1,78 +1,79 @@ -name: CLI Integration test - -on: - pull_request: - types: - - opened - - synchronize - - reopened - - closed - branches: - - main - -jobs: - test: - # only run if not closed or closed with merge - if: ${{ github.event.pull_request.merged == true || github.event.pull_request.state != 'closed' }} - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - report_json_file: InfraPatch_Statistics.json - - strategy: - matrix: - os: - - macos-latest - - ubuntu-latest - # - windows-latest Windows does currently not work because of pygohcl - runs-on: ${{ matrix.os }} - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Install Python - uses: actions/setup-python@v4 - with: - python-version: "3.11" - - - name: Install InfraPatch CLI - run: | - python -m pip install . - shell: bash - - - name: Run InfraPatch report - shell: bash - run: infrapatch --debug report --dump-json-statistics - - - name: Check report result - shell: pwsh - run: | - $report = Get-Content $env:report_json_file -Raw | ConvertFrom-Json - if ( -not $report.total_resources -gt 0 ) { - throw "Failed to get resources" - } - if ( $report.resources_patched -ne 0 ) { - throw "No resources should be patched" - } - if ( $report.errors -gt 0 ) { - throw "Errors have been detected" - } - - - name: Run InfraPatch update - shell: bash - run: infrapatch --debug update --dump-json-statistics --confirm - - - name: Check update result - shell: pwsh - run: | - $report = Get-Content $env:report_json_file -Raw | ConvertFrom-Json - if ( -not $report.total_resources -gt 0 ) { - throw "Failed to get resources" - } - if ( -not ( $report.resources_patched -gt 3 ) ) { - throw "No resources should be patched" - } - if ( $report.errors -gt 0 ) { - throw "Errors have been detected" - } - - +name: CLI Integration test + +on: + workflow_call: + +jobs: + cli_integration_test: + name: CLI Integration test + # only run if not closed or closed with merge + if: ${{ github.event.pull_request.merged == true || github.event.pull_request.state != 'closed' }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + report_json_file: InfraPatch_Statistics.json + + strategy: + matrix: + os: + - macos-latest + - ubuntu-latest + # - windows-latest Windows does currently not work because of pygohcl + runs-on: ${{ matrix.os }} + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Get Python Version + id: get_python_verion + run: | + python_version=$(cat python_version.txt) + echo "Using Python version $python_version" + echo "::set-output name=python_version::$(echo $python_version)" + + - name: Install Python + uses: actions/setup-python@v4 + with: + python-version: ${{ steps.get_python_verion.outputs.python_version }} + + - name: Install InfraPatch CLI + run: | + python -m pip install . + shell: bash + + - name: Run InfraPatch report + shell: bash + run: infrapatch --debug report --dump-json-statistics + + - name: Check report result + shell: pwsh + run: | + $report = Get-Content $env:report_json_file -Raw | ConvertFrom-Json + if ( -not $report.total_resources -gt 0 ) { + throw "Failed to get resources" + } + if ( $report.resources_patched -ne 0 ) { + throw "No resources should be patched" + } + if ( $report.errors -gt 0 ) { + throw "Errors have been detected" + } + + - name: Run InfraPatch update + shell: bash + run: infrapatch --debug update --dump-json-statistics --confirm + + - name: Check update result + shell: pwsh + run: | + $report = Get-Content $env:report_json_file -Raw | ConvertFrom-Json + if ( -not $report.total_resources -gt 0 ) { + throw "Failed to get resources" + } + if ( -not ( $report.resources_patched -gt 3 ) ) { + throw "No resources should be patched" + } + if ( $report.errors -gt 0 ) { + throw "Errors have been detected" + } + + diff --git a/.github/workflows/composition.yml b/.github/workflows/composition.yml new file mode 100644 index 0000000..a75d329 --- /dev/null +++ b/.github/workflows/composition.yml @@ -0,0 +1,36 @@ +name: InfraPatch Checks + +permissions: + contents: write + pull-requests: write + +on: + pull_request: + types: + - opened + - synchronize + - reopened + - closed + branches: + - main + +env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + +jobs: + + check_code: + uses: ./.github/workflows/check_format_and_lint.yml + + unit_tests: + needs: check_code + uses: ./.github/workflows/unit_tests.yml + + cli_integration_test: + needs: unit_tests + uses: ./.github/workflows/cli_integration_test.yml + + github_action_integration_test: + needs: unit_tests + uses: ./.github/workflows/action_integration_test.yml + diff --git a/.github/workflows/unit_tests.yml b/.github/workflows/unit_tests.yml new file mode 100644 index 0000000..3807e5b --- /dev/null +++ b/.github/workflows/unit_tests.yml @@ -0,0 +1,52 @@ +name: "Run Python Unit Tests" + + +on: + workflow_call: + +jobs: + integration-test: + name: "Run Unit Tests" + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Get Python Version + id: get_python_verion + run: | + python_version=$(cat python_version.txt) + echo "Using Python version $python_version" + echo "::set-output name=python_version::$(echo $python_version)" + + - name: Install Python + uses: actions/setup-python@v2 + with: + python-version: ${{ steps.get_python_verion.outputs.python_version }} + + - name: Install Dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + pip install -r requirements-dev.txt + + - name: Set timezone + uses: szenius/set-timezone@v1.2 + with: + timezoneLinux: 'Europe/Berlin' + + - name: Run pytest + uses: pavelzw/pytest-action@v2 + with: + verbose: true + emoji: true + job-summary: true + click-to-expand: true + report-title: 'InfraPatch Unit-Tests Report' + + + + + + + diff --git a/.vscode/settings.json b/.vscode/settings.json index be312f9..14ad595 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,43 +1,49 @@ -{ - "terminal.integrated.env.osx": { - "PYTHONPATH": "${workspaceFolder}", - }, - "terminal.integrated.env.linux": { - "PYTHONPATH": "${workspaceFolder}", - }, - "terminal.integrated.env.windows": { - "PYTHONPATH": "${workspaceFolder}", - }, - "[python]": { - "editor.formatOnSave": true, - "editor.codeActionsOnSave": { - "source.fixAll": true, - "source.organizeImports": true - }, - "editor.defaultFormatter": "charliermarsh.ruff" - }, - "files.autoSave": "afterDelay", - "python.analysis.inlayHints.functionReturnTypes": true, - "python.analysis.inlayHints.pytestParameters": true, - "python.analysis.inlayHints.variableTypes": true, - "python.languageServer": "Default", - "editor.defaultFormatter": "charliermarsh.ruff", - "python.missingPackage.severity": "Error", - "python.terminal.activateEnvInCurrentTerminal": true, - "cSpell.words": [ - "infrapatch" - ], - "editor.formatOnPaste": true, - "editor.formatOnSave": true, - "python.analysis.autoImportCompletions": true, - "python.analysis.fixAll": [ - "source.unusedImports", - "source.convertImportFormat" - ], - "python.analysis.typeCheckingMode": "basic", - "python.analysis.diagnosticMode": "workspace", - "editor.guides.indentation": false, - "editor.guides.bracketPairs": true, - "editor.guides.highlightActiveBracketPair": true, - "editor.guides.bracketPairsHorizontal": false, +{ + "terminal.integrated.env.osx": { + "PYTHONPATH": "${workspaceFolder}", + }, + "terminal.integrated.env.linux": { + "PYTHONPATH": "${workspaceFolder}", + }, + "terminal.integrated.env.windows": { + "PYTHONPATH": "${workspaceFolder}", + }, + "[python]": { + "editor.formatOnSave": true, + "editor.codeActionsOnSave": { + "source.fixAll": true, + "source.organizeImports": true + }, + "editor.defaultFormatter": "charliermarsh.ruff" + }, + "files.autoSave": "afterDelay", + "python.analysis.inlayHints.functionReturnTypes": true, + "python.analysis.inlayHints.pytestParameters": true, + "python.analysis.inlayHints.variableTypes": true, + "python.languageServer": "Default", + "editor.defaultFormatter": "charliermarsh.ruff", + "python.missingPackage.severity": "Error", + "python.terminal.activateEnvInCurrentTerminal": true, + "cSpell.words": [ + "infrapatch" + ], + "editor.formatOnPaste": true, + "editor.formatOnSave": true, + "python.analysis.autoImportCompletions": true, + "python.analysis.fixAll": [ + "source.unusedImports", + "source.convertImportFormat" + ], + "python.analysis.typeCheckingMode": "basic", + "python.analysis.diagnosticMode": "workspace", + "editor.guides.indentation": false, + "editor.guides.bracketPairs": true, + "editor.guides.highlightActiveBracketPair": true, + "editor.guides.bracketPairsHorizontal": false, + "python.defaultInterpreterPath": "/usr/local/bin/python", + "python.testing.pytestArgs": [ + "." + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true, } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index a0992ef..7bf227b 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -26,6 +26,15 @@ "reveal": "always" }, "problemMatcher": [] + }, + { + "label": "pip install all dependencies", + "type": "shell", + "command": "pip3 install -r requirements.txt && pip3 install -r requirements-dev.txt", + "presentation": { + "reveal": "always" + }, + "problemMatcher": [] } ] } \ No newline at end of file diff --git a/infrapatch/action/tests/test___main__.py b/infrapatch/action/tests/test___main__.py new file mode 100644 index 0000000..7a0c0ff --- /dev/null +++ b/infrapatch/action/tests/test___main__.py @@ -0,0 +1,25 @@ +from infrapatch.action.__main__ import get_credentials_from_string + + +def test_get_credentials_from_string(): + # Test case 1: Empty credentials string + credentials_string = "" + expected_result = {} + assert get_credentials_from_string(credentials_string) == expected_result + + # Test case 2: Single line credentials string + credentials_string = "username=abc123" + expected_result = {"username": "abc123"} + assert get_credentials_from_string(credentials_string) == expected_result + + # Test case 3: Multiple line credentials string + credentials_string = "username=abc123\npassword=xyz789\ntoken=123456" + expected_result = {"username": "abc123", "password": "xyz789", "token": "123456"} + assert get_credentials_from_string(credentials_string) == expected_result + + # Test case 4: Invalid credentials string + credentials_string = "username=abc123\npassword" + try: + get_credentials_from_string(credentials_string) + except Exception as e: + assert str(e) == "Error processing secrets: 'not enough values to unpack (expected 2, got 1)'" diff --git a/pyproject.toml b/pyproject.toml index 68c10f4..5b7e0c2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,4 +4,9 @@ line-length = 180 [tool.ruff.lint] select = ["E4", "E7", "E9", "F"] -ignore = [] \ No newline at end of file +ignore = [] + +[tool.pytest.ini_options] +pythonpath = [ + "." +] \ No newline at end of file diff --git a/python_version.txt b/python_version.txt new file mode 100644 index 0000000..902b2c9 --- /dev/null +++ b/python_version.txt @@ -0,0 +1 @@ +3.11 \ No newline at end of file diff --git a/requirements-dev.txt b/requirements-dev.txt index ede3eb4..e2d8d82 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1 +1,4 @@ -ruff \ No newline at end of file +ruff +pytest +pytest-md +pytest-emoji \ No newline at end of file