From 03de569e1b8841d16bd283b98d232361dc12295c Mon Sep 17 00:00:00 2001 From: questcollector Date: Wed, 16 Oct 2024 12:51:10 +0900 Subject: [PATCH] error handling: move import block into try block --- ...st_kubernetes_commandline_code_executor.py | 57 ++++++++++--------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/test/coding/test_kubernetes_commandline_code_executor.py b/test/coding/test_kubernetes_commandline_code_executor.py index a4483f0c67f..09e6b36aafa 100644 --- a/test/coding/test_kubernetes_commandline_code_executor.py +++ b/test/coding/test_kubernetes_commandline_code_executor.py @@ -5,15 +5,15 @@ import pytest -client = importlib.import_module("kubernetes.client") -config = importlib.import_module("kubernetes.config") - from autogen.code_utils import TIMEOUT_MSG from autogen.coding.base import CodeBlock, CodeExecutor try: from autogen.coding.kubernetes import PodCommandLineCodeExecutor + client = importlib.import_module("kubernetes.client") + config = importlib.import_module("kubernetes.config") + kubeconfig = Path(".kube/config") if os.environ.get("KUBECONFIG", None): kubeconfig = Path(os.environ["KUBECONFIG"]) @@ -29,33 +29,34 @@ skip_kubernetes_tests = False else: skip_kubernetes_tests = True -except Exception: - skip_kubernetes_tests = True - -pod_spec = client.V1Pod( - metadata=client.V1ObjectMeta(name="abcd", namespace="default", annotations={"sidecar.istio.io/inject": "false"}), - spec=client.V1PodSpec( - restart_policy="Never", - containers=[ - client.V1Container( - args=["-c", "while true;do sleep 5; done"], - command=["/bin/sh"], - name="abcd", - image="python:3.11-slim", - env=[ - client.V1EnvVar(name="TEST", value="TEST"), - client.V1EnvVar( - name="POD_NAME", - value_from=client.V1EnvVarSource( - field_ref=client.V1ObjectFieldSelector(field_path="metadata.name") + pod_spec = client.V1Pod( + metadata=client.V1ObjectMeta( + name="abcd", namespace="default", annotations={"sidecar.istio.io/inject": "false"} + ), + spec=client.V1PodSpec( + restart_policy="Never", + containers=[ + client.V1Container( + args=["-c", "while true;do sleep 5; done"], + command=["/bin/sh"], + name="abcd", + image="python:3.11-slim", + env=[ + client.V1EnvVar(name="TEST", value="TEST"), + client.V1EnvVar( + name="POD_NAME", + value_from=client.V1EnvVarSource( + field_ref=client.V1ObjectFieldSelector(field_path="metadata.name") + ), ), - ), - ], - ) - ], - ), -) + ], + ) + ], + ), + ) +except Exception: + skip_kubernetes_tests = True @pytest.mark.skipif(skip_kubernetes_tests, reason="kubernetes not accessible")