To accomplish this task, you will need to install the required libraries first. You can install botocore, moto, and pytest via pip if you haven't already:
pip install botocore moto pytest requests
Here's a step-by-step solution to write a lambda function that downloads a file from Artifactory and saves it to a temporary path, and a pytest test case using moto to achieve 100% test coverage.
Lambda Function
Here is the lambda function that downloads a file from Artifactory and saves it to a temporary path:
import os
import tempfile
import requests
def download_file_from_artifactory(url, filename):
response = requests.get(url)
response.raise_for_status()
temp_dir = tempfile.gettempdir()
temp_file_path = os.path.join(temp_dir, filename)
with open(temp_file_path, 'wb') as temp_file:
temp_file.write(response.content)
return temp_file_path
Pytest Test Case
Below is a pytest test case using moto to mock the S3 responses. Note that since Artifactory is not an AWS service, we do not require moto specifically, but we will demonstrate the use of mocking the requests and how you can test such functionality effectively.
import pytest
import requests
from unittest import mock
from tempfile import gettempdir
import os
# Import the lambda function file here
from your_lambda_module import download_file_from_artifactory # replace 'your_lambda_module' with your actual module name
@pytest.fixture
def artifactory_response():
# Mocking the requests.get call
with mock.patch('requests.get') as mock_get:
mock_response = mock.Mock()
mock_response.raise_for_status = mock.Mock()
mock_response.content = b'Test file content'
mock_get.return_value = mock_response
yield mock_get
def test_download_file_from_artifactory(artifactory_response):
url = "http://example.com/artifactory/file.txt"
filename = "file.txt"
# Act
downloaded_file_path = download_file_from_artifactory(url, filename)
# Assert
assert os.path.isfile(downloaded_file_path)
assert os.path.getsize(downloaded_file_path) > 0
expected_filepath = os.path.join(gettempdir(), filename)
assert downloaded_file_path == expected_filepath
# Cleanup the file after the test
os.remove(downloaded_file_path)
Explanation
-
Lambda Function (download_file_from_artifactory):
- Downloads a file from Artifactory using a given URL and saves it to a temporary directory.
- Uses Python’s
requests library to perform the HTTP GET request to download the file.
- It writes the downloaded content to a file under the system's temporary directory and returns the path to the saved file.
-
Pytest Test Case:
- Fixture
artifactory_response:
- Mocks the
requests.get call to simulate downloading a file.
- Returns a mock response with predefined content.
- Test Function
test_download_file_from_artifactory:
- Calls the
download_file_from_artifactory function with a sample URL and filename.
- Asserts that the downloaded file exists and is non-empty.
- Compares the expected file path with the returned path.
- Cleans up the temporary file created during the test.
This test case provides 100% test coverage for the download_file_from_artifactory function, handling both the file download and saving logic.
Note: Ensure to replace 'your_lambda_module' with the actual module name where the download_file_from_artifactory function is defined.