测试在 Github 操作上失败,但在我的本地环境上失败

问题描述 投票:0回答:1

我的 github 操作 CI/CD 有以下 yaml 文件。

name: FastAPI endpoint  
on:
  push:
    branches: [ "main" ]

  pull_request:
    branches: [ "main" ]

permissions:
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Set up Python 3.10
      uses: actions/setup-python@v3
      with:
        python-version: "3.10"
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install pytest pylint 
        make install 
    - name: Lint with Pylint
      run: |
        pylint --disable=R,C *.py || true 
    - name: Create .env file 
      run: |
        touch .env 
        echo OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }} >> .env
        echo GEMINI_API_KEY=${{ secrets.GEMINI_API_KEY }} >> .env 
        echo HF_TOKEN=${{ secrets.HF_TOKEN }} >> .env 
        echo FILE_ID=${{ secrets.FILE_ID }} >> .env 
        cat .env
    - name: Intialize the Server 
      run: |
        fastapi run main.py & sleep 5
    - name: Checking the status of the output
      run: |
        python test.py 
    - name: Test with pytest
      run: |
        pytest test.py 

pytest test.py 在 github 操作上不断失败。这些是测试!

def test_data_loader() -> None: 
    if not os.listdir(PDF_BASE_PATH): return 
    for pdf_filename in os.listdir(PDF_BASE_PATH): 
        with open(os.path.join(PDF_BASE_PATH, pdf_filename), "rb") as f: 
            byte_content = f.read() # reading the content in bytes 

        # test if the files are working 
        files = {
            "pdf_file": (pdf_filename, byte_content, "application/pdf"), 
        }

        response = requests.post(API_URL + "/data_loader", files=files)
        print(response.content)
        assert isinstance(response.json(), list)
    
    files = {
        "pdf_file": (pdf_filename, b'', "image/jpeg")
    }


    # test if the http error occurs correctly 
    response = requests.post(API_URL + "/data_loader", files=files, timeout=300 )
    assert response.status_code == 400  
   
def test_data_classifer() -> None: 
    headers: Dict[str, str] = {
        "Content-Type": "application/json"
    }
    response = requests.post(
        API_URL + "/data_classifier", 
        json=data_classifier_json, 
        headers=headers, 
        timeout=300, 
    )

    print(response.content)

    output_json: List[Dict] = response.json()

    for json_result in output_json: 
        assert list(json_result.keys()) == [
            "root_concept", 
            "major_domains", 
            "sub_domains", 
            "concepts", 
            "Attributes and connections", 
            "formal_representations", 
            "heading_identifier", 
            "heading_text", 
            "sub_heading_text", 
            "text_type", 
            "paragraph_number", 
            "text", 
        ]

def test_generate() -> None: 
    json: Dict[str, str|List[str]] = {
        "context": book_text, 
        "topics": topics
    }

    headers: Dict[str, str] = {
        "Content-Type": "application/json"
    }

    response = requests.post(
        API_URL + "/generate", 
        json=json, 
        headers=headers, 
    )

    print(response.content)     
    assert isinstance(response.json()["output"], str)  

但这似乎根本没有帮助,因为它不会在任何步骤中打印 json。 我怀疑该错误是因为未找到 OpenAI 和 Gemini api 密钥。但我不能确定,因为我不知道如何调试它。

如您所见,我尝试通过添加额外的步骤检查状态来使用打印语句对其进行调试。

打印语句在 Github Actions Platform 上不显示任何输出。

测试在本地完美运行,但是在 Github 操作上运行它们会出现以下错误。

Traceback (most recent call last):
b'Traceback (most recent call last):\n  File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/starlette/middleware/errors.py", line 165, in __call__\n    await self.app(scope, receive, _send)\n  File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 62, in __call__\n    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)\n  File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/starlette/_exception_handler.py", line 62, in wrapped_app\n    raise exc\n  File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/starlette/_exception_handler.py", line 51, in wrapped_app\n    await app(scope, receive, sender)\n  File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/starlette/routing.py", line 715, in __call__\n    await self.middleware_stack(scope, receive, send)\n  File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/starlette/routing.py", line 735, in app\n    await route.handle(scope, receive, send)\n  File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/starlette/routing.py", line 288, in handle\n    await self.app(scope, receive, send)\n  File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/starlette/routing.py", line 76, in app\n    await wrap_app_handling_exceptions(app, request)(scope, receive, send)\n  File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/starlette/_exception_handler.py", line 62, in wrapped_app\n    raise exc\n  File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/starlette/_exception_handler.py", line 51, in wrapped_app\n    await app(scope, receive, sender)\n  File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/starlette/routing.py", line 73, in app\n    response = await f(request)\n  File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/fastapi/routing.py", line 301, in app\n    raw_response = await run_endpoint_function(\n  File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/fastapi/routing.py", line 212, in run_endpoint_function\n    return await dependant.call(**values)\n  File "/home/runner/work/project-astra/project-astra/main.py", line 100, in data_classifier\n    op: Dict[str, Dict|str|None|int|List[str]]|str = get_json(t_json, HF_TOKEN)\n  File "/home/runner/work/project-astra/project-astra/data_classifier/classification_pipeline.py", line 36, in get_json\n    extracted_json = response[0]["generated_text"].split("Extracted JSON:")[1].split("###")[0].strip()\nKeyError: 0\n'
  File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/requests/models.py", line 974, in json
    return complexjson.loads(self.text, **kwargs)
  File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

当我尝试解码 JSON 时出现错误。 我尝试打印语句并向 yaml 文件添加一个新的 STEP,正如您在 yaml 文件中看到的那样...

 - name: Checking the status of the output
      run: |
        python test.py 

我认为它无法加载 .env 文件,但就像我说的那样。我不能确定

python github github-actions fastapi cicd
1个回答
0
投票

尝试提供环境变量作为 python 脚本调用步骤的一部分 -

- name: Checking the status of the output
      run: |
        python test.py
      env:
        OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }}
        GEMINI_API_KEY=${{ secrets.GEMINI_API_KEY }}
        HF_TOKEN=${{ secrets.HF_TOKEN }}
        FILE_ID=${{ secrets.FILE_ID }}
© www.soinside.com 2019 - 2024. All rights reserved.