Skip to content

Commit

Permalink
add unit tests to check-sum
Browse files Browse the repository at this point in the history
  • Loading branch information
gopidesupavan committed Dec 7, 2024
1 parent 0779890 commit 94b2215
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 52 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test Gh publish
name: Test actions scripts
on:
workflow_dispatch:
pull_request:
Expand All @@ -9,13 +9,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: "Checkout ${{ github.ref }} ( ${{ github.sha }} )"
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
python-version: '3.11'
- name: "Run tests"
run: |
python3 -m pip install uv
python3 -m pip install uv pytest rich
uv run pytest
2 changes: 1 addition & 1 deletion .github/workflows/test-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
environment: test
steps:
- name: Checkout Code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v4
Expand Down
16 changes: 8 additions & 8 deletions checksum/action.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
name: 'Checksum Validator'
description: 'Validate checksum of packages'
name: "Checksum Validator"
description: "Validate checksum of packages"
inputs:
checksum-config:
description: 'Json config for checksum'
description: "Json config for checksum"
required: true
temp-dir:
description: 'Checkout directory of svn repo'
description: "Checkout directory of svn repo"
required: false
default: 'temp-svn-repo'
default: "temp-svn-repo"
repo-path:
description: 'Path to the svn repo ex: airflow/providers/'
description: "Path to the svn repo ex: airflow/providers/"
required: true
repo-url:
description: 'URL of the svn repo ex: https://dist.apache.org/repos/dist/release/airflow/'
description: "URL of the svn repo ex: https://dist.apache.org/repos/dist/release/airflow/"
required: true


Expand All @@ -22,7 +22,7 @@ runs:
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
python-version: "3.11"

- name: "Check sum validation"
shell: bash
Expand Down
12 changes: 8 additions & 4 deletions checksum/checksum_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import hashlib
import json
import os
from typing import Any

from rich.console import Console

console = Console(width=400, color_system="standard")
Expand Down Expand Up @@ -39,7 +41,7 @@ def validate_checksum(check_sum_files: list[dict[str, str]], algorithm: str):
)


def get_valid_files(algorithm, files) -> list[dict[str, str]]:
def get_valid_files(algorithm: str, files: list[str]) -> list[dict[str, str]]:
eligible_files = []
for file in files:
if file.endswith(algorithm):
Expand All @@ -53,10 +55,12 @@ def get_valid_files(algorithm, files) -> list[dict[str, str]]:


if __name__ == "__main__":
check_sum_config = json.loads(os.environ.get("CHECK_SUM_CONFIG"))
check_sum_config: list[dict[str, Any]] = json.loads(
os.environ.get("CHECK_SUM_CONFIG")
)

for check in check_sum_config:
console.print(f"[blue]Checking {check.get('description')} checksum[/]")
console.print(f"[blue]{check.get('description')}[/]")
valid_files = get_valid_files(check.get("algorithm"), svn_files)
validate_checksum(valid_files, check.get("algorithm"))

Expand All @@ -68,4 +72,4 @@ def get_valid_files(algorithm, files) -> list[dict[str, str]]:
console.print(f"[red]Actual SHA: {invalid.get('actual_sha')}[/]")
exit(1)

console.print("[blue]Checksum validation passed[/]")
console.print("[blue]Checksum validation passed[/]")
128 changes: 116 additions & 12 deletions checksum/test_checksum_check.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,121 @@
from unittest.mock import patch, mock_open
from checksum.checksum_check import get_valid_files, validate_checksum, invalid_checksums
import os
from unittest.mock import patch
from checksum.checksum_check import (
get_valid_files,
validate_checksum,
invalid_checksums,
)
import tempfile
import tarfile

class TestChecksumCheck:

def test_get_valid_files(self):
files = [
'apache-airflow-2.10.3-source.tar.gz.sha512',
'apache_airflow-2.10.3-py3-none-any.whl.asc',
'apache_airflow-2.10.3-py3-none-any.whl.sha512',
'apache_airflow-2.10.3.tar.gz'
]
valida_files = get_valid_files('sha512', files)
assert valida_files == [{'sha_file': 'apache-airflow-2.10.3-source.tar.gz.sha512', 'check_file': 'apache-airflow-2.10.3-source.tar.gz'}, {'sha_file': 'apache_airflow-2.10.3-py3-none-any.whl.sha512', 'check_file': 'apache_airflow-2.10.3-py3-none-any.whl'}]


def test_get_valid_files_sha512():
files = [
"apache-airflow-2.10.3-source.tar.gz.sha512",
"apache_airflow-2.10.3-py3-none-any.whl.asc",
"apache_airflow-2.10.3-py3-none-any.whl.sha512",
"apache_airflow-2.10.3.tar.gz",
]
valida_files = get_valid_files("sha512", files)
assert valida_files == [
{
"sha_file": "apache-airflow-2.10.3-source.tar.gz.sha512",
"check_file": "apache-airflow-2.10.3-source.tar.gz",
},
{
"sha_file": "apache_airflow-2.10.3-py3-none-any.whl.sha512",
"check_file": "apache_airflow-2.10.3-py3-none-any.whl",
},
]

def test_get_valid_files_with_sha256():
files = [
"apache-airflow-2.10.3-source.tar.gz.sha256",
"apache_airflow-2.10.3-py3-none-any.whl.asc",
"apache_airflow-2.10.3-py3-none-any.whl.sha256",
"apache_airflow-2.10.3.tar.gz",
]
valida_files = get_valid_files("sha256", files)
assert valida_files == [
{
"sha_file": "apache-airflow-2.10.3-source.tar.gz.sha256",
"check_file": "apache-airflow-2.10.3-source.tar.gz",
},
{
"sha_file": "apache_airflow-2.10.3-py3-none-any.whl.sha256",
"check_file": "apache_airflow-2.10.3-py3-none-any.whl",
},
]

@patch("hashlib.file_digest")
def test_validate_checksum(mock_file_digest):
mock_file_digest.return_value.hexdigest.return_value = "bbc759357eb1980e7f80ba0b016e9ed02120e26fcd008129b5777baf8086208c45e170e3c98cf35bd96a246d59484bde3220a897e5e6a7f688a69a40bcd451bd"

invalid_checksums.clear()
temp_dir = tempfile.TemporaryDirectory()
temp_file = tempfile.NamedTemporaryFile()
os.chdir(temp_dir.name)

with open(temp_file.name, "wb") as temp_data:
temp_data.write(b"some random data")

with open(
temp_dir.name + "/apache-airflow-2.10.3-source.tar.gz.sha512", "wb"
) as temp_file:
temp_file.write(
b"bbc759357eb1980e7f80ba0b016e9ed02120e26fcd008129b5777baf8086208c45e170e3c98cf35bd96a246d59484bde3220a897e5e6a7f688a69a40bcd451bd apache-airflow-2.10.3-source.tar.gz"
)
tar = tarfile.open(
temp_dir.name + "/apache-airflow-2.10.3-source.tar.gz", "w:gz"
)
tar.add(temp_file.name)
tar.close()

check_sum_files = [
{
"sha_file": "apache-airflow-2.10.3-source.tar.gz.sha512",
"check_file": "apache-airflow-2.10.3-source.tar.gz",
}
]
validate_checksum(check_sum_files, "sha512")
assert invalid_checksums == []

@patch("hashlib.file_digest")
def test_validate_checksum_invalid(mock_file_digest):
mock_file_digest.return_value.hexdigest.return_value = "bbc759357eb1980e7f80ba0b016e9ed02120e26fcd008129b5777baf8086208c45e170e3c98cf35bd96a246d59484bde3220a897e5e6a7f688a69a40bcd451bd12"
invalid_checksums.clear()
temp_dir = tempfile.TemporaryDirectory()
temp_file = tempfile.NamedTemporaryFile()
os.chdir(temp_dir.name)

with open(temp_file.name, "wb") as temp_data:
temp_data.write(b"some random data")

with open(
temp_dir.name + "/apache-airflow-2.10.3-source.tar.gz.sha512", "wb"
) as temp_file:
temp_file.write(
b"bbc759357eb1980e7f80ba0b016e9ed02120e26fcd008129b5777baf8086208c45e170e3c98cf35bd96a246d59484bde3220a897e5e6a7f688a69a40bcd451bd apache-airflow-2.10.3-source.tar.gz"
)
tar = tarfile.open(
temp_dir.name + "/apache-airflow-2.10.3-source.tar.gz", "w:gz"
)
tar.add(temp_file.name)
tar.close()

check_sum_files = [
{
"sha_file": "apache-airflow-2.10.3-source.tar.gz.sha512",
"check_file": "apache-airflow-2.10.3-source.tar.gz",
}
]
validate_checksum(check_sum_files, "sha512")
assert invalid_checksums == [
{
"file": "apache-airflow-2.10.3-source.tar.gz.sha512",
"expected_sha": "bbc759357eb1980e7f80ba0b016e9ed02120e26fcd008129b5777baf8086208c45e170e3c98cf35bd96a246d59484bde3220a897e5e6a7f688a69a40bcd451bd",
"actual_sha": "bbc759357eb1980e7f80ba0b016e9ed02120e26fcd008129b5777baf8086208c45e170e3c98cf35bd96a246d59484bde3220a897e5e6a7f688a69a40bcd451bd12",
}
]
2 changes: 1 addition & 1 deletion publish-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project:
description: 'Example project for publishing to PyPI'
publisher:
name: providers
url: 'https://dist.apache.org/repos/dist/release/airflow/'
url: 'https://dist.apache.org/repos/dist/dev/airflow'
path: airflow/providers/
checks:
svn:
Expand Down
13 changes: 9 additions & 4 deletions signature/signature_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# "python-gnupg",
# ]
# ///
from typing import Any

import gnupg
import requests
from rich.console import Console
Expand All @@ -21,7 +23,7 @@
invalid_signature_files = []


def download_keys(key_url):
def download_keys(key_url: str):
response = requests.get(key_url)
if response.status_code != 200:
console.print(
Expand All @@ -33,8 +35,9 @@ def download_keys(key_url):
key_file.write(response.text)


def validate_signature_with_gpg(signature_check):
def validate_signature_with_gpg(signature_check: dict[str, Any]):
key_url = signature_check.get("keys")

download_keys(key_url)
gpg = gnupg.GPG()
with open(temp_signature_key_file_path, "rb") as key_file:
Expand All @@ -55,7 +58,9 @@ def validate_signature_with_gpg(signature_check):


if __name__ == "__main__":
signature_check_config = json.loads(os.environ.get("SIGNATURE_CHECK_CONFIG"))
signature_check_config: list[dict[str, Any]] = json.loads(
os.environ.get("SIGNATURE_CHECK_CONFIG")
)

for check in signature_check_config:
console.print(f"[blue]{check.get('description')}[/]")
Expand All @@ -69,4 +74,4 @@ def validate_signature_with_gpg(signature_check):
)
exit(1)

console.print("[blue]All signatures are valid[/]")
console.print("[blue]All signatures are valid[/]")
16 changes: 12 additions & 4 deletions svn/svn_check.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# /// script
# requires-python = ">=3.9"
# requires-python = ">=3.11"
# dependencies = [
# "rich",
# ]
# ///
from __future__ import annotations

import json
import os
import re
from typing import Any

from rich.console import Console

console = Console(width=400, color_system="standard")
Expand All @@ -17,7 +21,7 @@
unknown_file_extensions = []


def check_with_regex(file_to_check, pattern, check_type):
def check_with_regex(file_to_check: str, pattern: str, check_type: str) -> bool | None:
match = re.match(pattern, file_to_check)

if check_type == "extension":
Expand All @@ -27,7 +31,9 @@ def check_with_regex(file_to_check, pattern, check_type):
return match and match.group(1) in file_to_check


def check_files_with_identifiers(identifiers, all_files, check_type):
def check_files_with_identifiers(
identifiers: list[dict[str, Any]], all_files: list[str], check_type: str
):
all_files_copy = all_files.copy()

for identifier in identifiers:
Expand All @@ -48,7 +54,9 @@ def check_files_with_identifiers(identifiers, all_files, check_type):


if __name__ == "__main__":
svn_check_config = json.loads(os.environ.get("SVN_CHECK_CONFIG"))
svn_check_config: list[dict[str, Any]] = json.loads(
os.environ.get("SVN_CHECK_CONFIG")
)

for check in svn_check_config:
console.print(f"[blue]{check.get('description')}[/]")
Expand Down
Loading

0 comments on commit 94b2215

Please sign in to comment.