1- from kubernetes import client , config
1+ import json
2+ import logging
23import os
34import subprocess
4- import logging
5+
6+ from kubernetes import client , config
57from junitparser import JUnitXml
6- import json
78
89logger = logging .getLogger (__name__ )
910
1011
1112def setup_k8s_client (kubeconfigfile = None ):
12-
13- if kubeconfigfile :
14- logger .debug (f"loading kubeconfig file '{ kubeconfigfile } '" )
15- config .load_kube_config (kubeconfigfile )
16- logger .info ("kubeconfigfile loaded successfully" )
17- else :
13+ if not kubeconfigfile :
1814 logger .error ("no kubeconfig file provided" )
19- return None
20- k8s_api_client = client .CoreV1Api ()
21- return k8s_api_client
15+ return # XXX smelly: why not raise an exception here?
16+ logger .debug (f"loading kubeconfig file '{ kubeconfigfile } '" )
17+ config .load_kube_config (kubeconfigfile )
18+ logger .info ("kubeconfigfile loaded successfully" )
19+ return client .CoreV1Api ()
2220
2321
2422class SonobuoyHandler :
@@ -51,43 +49,35 @@ def __init__(
5149 )
5250 self .args = args
5351
54- def _build_command (self , process , args ):
55- command = (
56- [
57- "sonobuoy" ,
58- "--kubeconfig" ,
59- self .kubeconfig_path ,
60- ] + [process ] + args
52+ def _build_command (self , process , * args ):
53+ return " " .join (
54+ ("sonobuoy" , "--kubeconfig" , self .kubeconfig_path , process ) + args
6155 )
62- command_string = ""
63- for entry in command :
64- command_string += entry + " "
65- return command_string
6656
6757 def _sonobuoy_run (self ):
6858 logger .debug ("sonobuoy run" )
6959 check_args = ["--wait" ]
7060 check_args += [str (arg ) for arg in self .args ]
7161 subprocess .run (
72- self ._build_command ("run" , check_args ),
62+ self ._build_command ("run" , * check_args ),
7363 shell = True ,
7464 capture_output = True ,
7565 check = True ,
7666 )
7767
78- def _sonobouy_delete (self ):
68+ def _sonobuoy_delete (self ):
7969 logger .info ("removing sonobuoy resources from cluster" )
8070 subprocess .run (
81- self ._build_command ("delete" , [ "--wait" ] ),
71+ self ._build_command ("delete" , "--wait" ),
8272 shell = True ,
8373 capture_output = True ,
8474 check = True ,
8575 )
8676
87- def _sonobouy_status_result (self ):
77+ def _sonobuoy_status_result (self ):
8878 logger .debug ("sonobuoy status" )
8979 process = subprocess .run (
90- self ._build_command ("status" , [ "--json" ] ),
80+ self ._build_command ("status" , "--json" ),
9181 shell = True ,
9282 capture_output = True ,
9383 check = True ,
@@ -117,12 +107,9 @@ def _sonobouy_status_result(self):
117107
118108 def _preflight_check (self ):
119109 """
120- Prefligth test to ensure that everything is set up correctly for execution
121- :param: None
122- :return: None
110+ Preflight test to ensure that everything is set up correctly for execution
123111 """
124112 logger .info ("check kubeconfig" )
125- print ("check kubeconfig" )
126113 self .k8s_api_client = setup_k8s_client (self .kubeconfig_path )
127114
128115 for api in client .ApisApi ().get_api_versions ().groups :
@@ -135,7 +122,7 @@ def _preflight_check(self):
135122 versions .append (name )
136123 logger .info (f"[supported api]: { api .name :<40} { ',' .join (versions )} " )
137124
138- logger .debug ("checks if sonobuoy is availabe " )
125+ logger .debug ("checks if sonobuoy is available " )
139126 return_value = os .system (
140127 f"sonobuoy version --kubeconfig='{ self .kubeconfig_path } '"
141128 )
@@ -144,19 +131,15 @@ def _preflight_check(self):
144131
145132 def _sonobuoy_retrieve_result (self ):
146133 """
147- This method invokes sonobouy to store the results in a subdirectory of
134+ This method invokes sonobuoy to store the results in a subdirectory of
148135 the working directory. The Junit results file contained in it is then
149136 analyzed in order to interpret the relevant information it containes
150- :param: result_file_name:
151- :return: None
152137 """
153138 logger .debug (f"retrieving results to { self .result_dir_name } " )
154- print (f"retrieving results to { self .result_dir_name } " )
155- result_dir = self .working_directory + "/" + self .result_dir_name
139+ result_dir = os .path .join (self .working_directory , self .result_dir_name )
156140 if os .path .exists (result_dir ):
157- raise Exception ("result directory allready excisting" )
158- else :
159- os .mkdir (result_dir )
141+ raise Exception ("result directory already existing" )
142+ os .mkdir (result_dir )
160143
161144 os .system (
162145 # ~ f"sonobuoy retrieve {result_dir} -x --filename='{result_dir}' --kubeconfig='{self.kubeconfig_path}'"
@@ -175,19 +158,17 @@ def _sonobuoy_retrieve_result(self):
175158 passed_test_cases += 1
176159 elif case .is_skipped is True :
177160 skipped_test_cases += 1
178- # ~ logger.warning(f"SKIPPED:{case.name}") # TODO:!!! decide if skipped is error or warning only ?
179161 else :
180162 failed_test_cases += 1
181- logger .error (f"ERROR: { case .name } " )
182- print (f"ERROR: { case .name } " )
163+ logger .error (f"{ case .name } " )
183164
184165 result_message = f" { passed_test_cases } passed, { failed_test_cases } failed, { skipped_test_cases } skipped"
185- if failed_test_cases == 0 and skipped_test_cases == 0 :
166+ if failed_test_cases : # TODO: add `or skipped_test_cases`?
167+ logger .error (result_message )
168+ self .return_code = 3
169+ else :
186170 logger .info (result_message )
187171 self .return_code = 0
188- else :
189- logger .error ("ERROR:" + result_message )
190- self .return_code = 3
191172
192173 def run (self ):
193174 """
@@ -196,12 +177,12 @@ def run(self):
196177 self .return_code = 11
197178 self ._preflight_check ()
198179 self ._sonobuoy_run ()
199- self ._sonobouy_status_result ()
180+ self ._sonobuoy_status_result ()
200181
201182 # ERROR: currently disabled do to: "error retrieving results: unexpected EOF"
202- # migth be related to following bug: https://github.com/vmware-tanzu/sonobuoy/issues/1633
183+ # might be related to following bug: https://github.com/vmware-tanzu/sonobuoy/issues/1633
203184 # self._sonobuoy_retrieve_result(self)
204185
205- self ._sonobouy_delete ()
186+ self ._sonobuoy_delete ()
206187 print (self .check_name + ": " + ("PASS" , "FAIL" )[min (1 , self .return_code )])
207188 return self .return_code
0 commit comments