Skip to content

Commit

Permalink
Python tests for GitHub Action (#41)
Browse files Browse the repository at this point in the history
Python tests for GitHub Action

Signed-off-by: Petr "Stone" Hracek <[email protected]>
  • Loading branch information
phracek authored Mar 21, 2022
1 parent 6afae1a commit dd909a3
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.idea
tests/copr_artifacts
tests/secrets
tests/variables
25 changes: 25 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# HOWTO: https://pre-commit.com/#usage
# pip3 install pre-commit
# pre-commit install

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.2.3
hooks:
- id: check-added-large-files
- id: check-ast
- id: check-merge-conflict
- id: check-yaml
args: ['--unsafe'] #Suppress !include in config yaml files
- id: detect-private-key
- id: end-of-file-fixer
- id: trailing-whitespace
- id: flake8
args:
- --max-line-length=120
ignore: W605
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.711
hooks:
- id: mypy
args: [--no-strict-optional, --ignore-missing-imports]
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.PHONY: check test-unit

test-unit:
cd tests && PYTHONDONTWRITEBYTECODE=1 python3 -m pytest --showlocals --verbose test_*
check: test-unit
45 changes: 45 additions & 0 deletions generate_artifacts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright (c) 2022 Red Hat s.r.o.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

# File generates artifacts for TMT
# Format is in JSON file:
# { 'artifacts' : [{'type': 'fedora-copr-build', 'id': '1234123:epel8-x86_64'}] }

import json
import sys
from typing import List

json_string = ""
if len(sys.argv) > 2:
artifacts = sys.argv[1]
copr = sys.argv[2]
if not artifacts.strip():
json_string = ""
else:
list_artifacts: List = []
for copr_id in artifacts.split(";"):
list_artifacts.append({"type": "fedora-copr-build", "id": f"{copr_id}:{copr}"})
json_string = json.dumps({"artifacts": list_artifacts})

with open("copr_artifacts", "w") as file:
file.write(json_string)
50 changes: 50 additions & 0 deletions generate_tmt_vars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright (c) 2022 Red Hat s.r.o.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

import json
import sys
from typing import Dict

# First parameter has to be defined.
# File generates variables or secreted for TMT
# Format is in JSON file:
# { 'key1': 'value1', 'key2': 'value2' }

json_dict: Dict = {}
output_name = ""
if len(sys.argv) == 2:
output_name = sys.argv[1]
if output_name != "variables" and output_name != "secrets":
sys.exit(1)

if len(sys.argv) > 2:
output_name = sys.argv[1]
input_variables = sys.argv[2]
if not input_variables.strip():
json_dict = {}
else:
json_dict = {key: value for key, value in [s.split("=", 1) for s in input_variables.split(";")]}

json_string = json.dumps(json_dict)
with open(output_name, "w") as file:
file.write(json_string)
159 changes: 159 additions & 0 deletions tests/test_tmt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#!/usr/bin/python
# Copyright (c) 2022 Red Hat s.r.o.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

# This file tests generate_tmt_vars.py and generate_artifacts.py

import os
import subprocess
import json


TESTDIR = os.path.dirname(os.path.realpath(__file__))


def test_tmt_variables_empty():
ret_code = subprocess.call(f"python3 {TESTDIR}/../generate_tmt_vars.py", shell=True)
assert ret_code == 1


def test_tmt_variables_wrong_first_parameter():
ret_code = subprocess.call(f"python3 {TESTDIR}/../generate_tmt_vars.py \"data=123\"", shell=True)
assert ret_code == 1


def test_tmt_variables_missing_second_parameter():
expected = {}
output_name = "variables"
ret_val = subprocess.call(f"python3 {TESTDIR}/../generate_tmt_vars.py \"{output_name}\"", shell=True)
assert ret_val == 0
with open(output_name, "r") as f:
data = f.read()
json_string = json.loads(data)
assert json_string == expected


def test_tmt_variables_one_parameter():
expected = {"data": "123"}
output_name = "variables"
ret_val = subprocess.call(f"python3 {TESTDIR}/../generate_tmt_vars.py \"{output_name}\" \"data=123\"", shell=True)
assert ret_val == 0
with open(output_name, "r") as f:
data = f.read()
json_string = json.loads(data)
assert json_string == expected


def test_tmt_variables_two_variable():
expected = {"data": "123", "second": "987"}
output_name = "variables"
ret_code = subprocess.call(f"python3 {TESTDIR}/../generate_tmt_vars.py \"{output_name}\" \"data=123;second=987\"", shell=True)
assert ret_code == 0
with open(output_name, "r") as f:
data = f.read()
json_string = json.loads(data)
assert json_string == expected


def test_tmt_artifacts_one_variable():
expected = {"data": "123"}
output_name = "artifacts"
ret_code = subprocess.call(f"python3 {TESTDIR}/../generate_tmt_vars.py \"{output_name}\" \"data=123\"", shell=True)
assert ret_code == 0
with open(output_name, "r") as f:
data = f.read()
json_string = json.loads(data)
assert json_string == expected


def test_tmt_secrets_missing_second_parameter():
expected = "{}"
output_name = "secrets"
ret_code = subprocess.call(f"python3 {TESTDIR}/../generate_tmt_vars.py \"{output_name}\"", shell=True)
assert ret_code == 0
with open(output_name, "r") as f:
data = f.read()
assert data == expected


def test_tmt_secrets_one_variable():
expected = {"TOPSECRET": "MY_TOKEN"}
output_name = "secrets"
ret_code = subprocess.call(f"python3 {TESTDIR}/../generate_tmt_vars.py \"{output_name}\" \"TOPSECRET=MY_TOKEN\"",
shell=True)
assert ret_code == 0
with open(output_name, "r") as f:
data = f.read()
json_string = json.loads(data)
assert json_string == expected


def test_tmt_secrets_two_variable():
expected = {"TOPSECRET": "MY_TOKEN", "GITHUB_TOKEN": "FOOBAR_TOKEN"}
output_name = "secrets"
ret_code = subprocess.call(
f"python3 {TESTDIR}/../generate_tmt_vars.py \"{output_name}\" \"TOPSECRET=MY_TOKEN;GITHUB_TOKEN=FOOBAR_TOKEN\"",
shell=True
)
assert ret_code == 0
with open(output_name, "r") as f:
data = f.read()
json_string = json.loads(data)
assert json_string == expected


def test_artifacts_empty():
expected = ""
output_name = "copr_artifacts"
ret_code = subprocess.call(f"python3 {TESTDIR}/../generate_artifacts.py", shell=True)
assert ret_code == 0
with open(output_name, "r") as f:
data = f.read()
assert data == expected


def test_artifacts_empty_copr():
expected = ""
output_name = "copr_artifacts"
ret_code = subprocess.call(f"python3 {TESTDIR}/../generate_artifacts.py \"12345;98876\"", shell=True)
assert ret_code == 0
with open(output_name, "r") as f:
data = f.read()
assert data == expected


def test_artifacts():
expected = {"artifacts": [
{"type": "fedora-copr-build", "id": "12345:epel-8-x86_64"},
{"type": "fedora-copr-build", "id": "98876:epel-8-x86_64"}]
}
output_name = "copr_artifacts"
ret_code = subprocess.call(
f"python3 {TESTDIR}/../generate_artifacts.py \"12345;98876\" \"epel-8-x86_64\"",
shell=True
)
assert ret_code == 0
with open(output_name, "r") as f:
data = f.read()
json_string = json.loads(data)
assert json_string == expected

0 comments on commit dd909a3

Please sign in to comment.