Skip to content

Commit

Permalink
fixup: addjust according to flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
tonifinger committed Oct 17, 2023
1 parent 7628927 commit d08c940
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 37 deletions.
39 changes: 16 additions & 23 deletions Tests/kaas/k8s-default-storage-class/helper.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
import getopt
import sys
import time
import json
import logging
import yaml

manual_result_file_template = {
'name': None,
'status': None,
'details':{
#'stdout': "stdout from the test"
'messages': None
# - message from the test
# - another message
}
}
"name": None,
"status": None,
"details": {
"messages": None
},
}


def gen_sonobuoy_result_file(error_n: int, error_msg: str, test_file_name: str):

test_name = test_file_name.replace(".py", "")

test_status="passed"

if error_n != 0 :
test_status = test_name + "_" + str(error_n)
test_status = "passed"

if error_n != 0:
test_status = test_name + "_" + str(error_n)

result_file = manual_result_file_template

result_file['name'] = test_name
result_file['status'] = test_status
result_file['details']['messages'] = error_msg
result_file["name"] = test_name
result_file["status"] = test_status
result_file["details"]["messages"] = error_msg

with open(f'./{test_name}.result.yaml', 'w') as file:
documents = yaml.dump(result_file, file)
with open(f"./{test_name}.result.yaml", "w") as file:
yaml.dump(result_file, file)
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import logging

from kubernetes import client, config
from kubernetes.client.rest import ApiException
from helper import gen_sonobuoy_result_file

import logging.config
Expand Down Expand Up @@ -55,12 +54,16 @@ def setup_k8s_client(kubeconfigfile=None):

def print_usage(file=sys.stderr):
"""Help output"""
print("""Usage: k8s_storageclass_check.py [options]
print(
"""Usage: k8s_storageclass_check.py [options]
This tool checks the requested k8s default storage class according to the SCS Standard 0211 "kaas-default-storage-class".
Options:
[-k/--kubeconfig PATH_TO_KUBECONFIG] sets kubeconfig file to access kubernetes api
[-d/--debug] enables DEBUG logging channel
""", end='', file=file)
""",
end="",
file=file,
)


def initialize_logging():
Expand All @@ -80,7 +83,6 @@ def check_default_storageclass(k8s_client_storage):
storageclasses = api_response.read().decode("utf-8")
storageclasses_dict = json.loads(storageclasses)

default_class = {}
ndefault_class = 0

for item in storageclasses_dict["items"]:
Expand Down Expand Up @@ -109,6 +111,7 @@ def check_default_storageclass(k8s_client_storage):
logger.info(f"One default Storage Class found:'{default_storage_class}'")
return default_storage_class


def check_default_persistentvolumeclaim_readwriteonce(k8s_api_instance, storage_class):
"""
1. Create PersistantVolumeClaim
Expand Down Expand Up @@ -184,8 +187,7 @@ def check_default_persistentvolumeclaim_readwriteonce(k8s_api_instance, storage_
time.sleep(1)
retries += 1


#assert pod_status == "Running"
# assert pod_status == "Running"
if pod_status != "Running":
raise SCSTestException(
f"pod is not Running not able to setup test Enviornment",
Expand Down Expand Up @@ -218,22 +220,20 @@ def check_default_persistentvolumeclaim_readwriteonce(k8s_api_instance, storage_
return_code=42,
)


# 4. Delete resources used for testing
logger.debug(f"delete pod:{pod_name}")
api_response = k8s_api_instance.delete_namespaced_pod(pod_name, namespace)
logger.debug(f"delete pvc:{pvc_name}")
api_response = k8s_api_instance.delete_namespaced_persistent_volume_claim(
pvc_name, namespace
)

return 0


def main(argv):

initialize_logging()
error_count = 0
return_code = 0
return_message = "return_message: FAILED"

Expand All @@ -255,14 +255,14 @@ def main(argv):
else:
print_usage(kubeconfig)
return 2

print(return_code, return_message, __file__)

# Setup kubernetes client
try:
logger.debug("setup_k8s_client(kubeconfig)")
k8s_core_api, k8s_storage_api = setup_k8s_client(kubeconfig)
except Exception:
except Exception as exception_message:
logger.info(f"{exception_message}")
return_message = f"{exception_message}"
return_code = 1
Expand All @@ -279,13 +279,15 @@ def main(argv):
return_code = test_exception.return_code
except Exception as exception_message:
logger.info(f"{exception_message}")
return_message = f"{exception_messagev}"
return_message = f"{exception_message}"
return_code = 1

# Check if default_persistent volume has ReadWriteOnce defined (MENTETORY)
try:
logger.info(f"check_default_persistentvolume_readwriteonce()")
return_code = check_default_persistentvolumeclaim_readwriteonce(k8s_core_api, default_class_name)
return_code = check_default_persistentvolumeclaim_readwriteonce(
k8s_core_api, default_class_name
)
except SCSTestException as test_exception:
logger.info(f"{test_exception}")
return_message = f"{test_exception}"
Expand All @@ -297,7 +299,7 @@ def main(argv):

logger.debug(f"return_code:{return_code}")

if return_code == 0:
if return_code == 0:
return_message = f"the tests passed"

gen_sonobuoy_result_file(return_code, return_message, __file__)
Expand Down

0 comments on commit d08c940

Please sign in to comment.