Skip to content

Commit 91f18ff

Browse files
authored
Update linux consumption tests to test with dev library (#990)
* Update host version for linux consumption tests * Revert host version to v3 * Adding isolate dependencies flag in docker image * Fixed lint tests * Skipping common libraries test for python3.7
1 parent 300cf00 commit 91f18ff

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

azure_functions_worker/testutils_lc.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
3-
4-
from typing import Dict
5-
63
import base64
74
import json
85
import os
96
import re
7+
import shutil
108
import subprocess
119
import sys
10+
import tempfile
1211
import time
1312
import uuid
13+
from io import BytesIO
14+
from typing import Dict
15+
from urllib.request import urlopen
16+
from zipfile import ZipFile
1417

18+
import requests
1519
from Crypto.Cipher import AES
1620
from Crypto.Hash.SHA256 import SHA256Hash
1721
from Crypto.Util.Padding import pad
18-
import requests
1922

2023
# Linux Consumption Testing Constants
2124
_DOCKER_PATH = "DOCKER_PATH"
2225
_DOCKER_DEFAULT_PATH = "docker"
2326
_MESH_IMAGE_URL = "https://mcr.microsoft.com/v2/azure-functions/mesh/tags/list"
2427
_MESH_IMAGE_REPO = "mcr.microsoft.com/azure-functions/mesh"
2528
_DUMMY_CONT_KEY = "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY="
29+
_FUNC_GITHUB_ZIP = "https://github.com/Azure/azure-functions-python-library" \
30+
"/archive/refs/heads/dev.zip"
31+
_FUNC_FILE_NAME = "azure-functions-python-library-dev"
2632

2733

2834
class LinuxConsumptionWebHostController:
@@ -80,9 +86,9 @@ def assign_container(self, env: Dict[str, str] = {}):
8086
f' stdout: {stdout}')
8187

8288
def send_request(
83-
self,
84-
req: requests.Request,
85-
ses: requests.Session = None
89+
self,
90+
req: requests.Request,
91+
ses: requests.Session = None
8692
) -> requests.Response:
8793
"""Send a request with authorization token. Return a Response object"""
8894
session = ses
@@ -129,6 +135,12 @@ def _find_latest_mesh_image(cls,
129135
cls._mesh_images[host_major] = image_tag
130136
return image_tag
131137

138+
@staticmethod
139+
def _download_azure_functions() -> str:
140+
with urlopen(_FUNC_GITHUB_ZIP) as zipresp:
141+
with ZipFile(BytesIO(zipresp.read())) as zfile:
142+
zfile.extractall(tempfile.gettempdir())
143+
132144
def spawn_container(self,
133145
image: str,
134146
env: Dict[str, str] = {}) -> int:
@@ -137,10 +149,18 @@ def spawn_container(self,
137149
"""
138150
# Construct environment variables and start the docker container
139151
worker_path = os.path.dirname(__file__)
152+
library_path = os.path.join(tempfile.gettempdir(), _FUNC_FILE_NAME,
153+
'azure', 'functions')
154+
self._download_azure_functions()
155+
140156
container_worker_path = (
141157
f"/azure-functions-host/workers/python/{self._py_version}/"
142158
"LINUX/X64/azure_functions_worker"
143159
)
160+
container_library_path = (
161+
f"/azure-functions-host/workers/python/{self._py_version}/"
162+
"LINUX/X64/azure/functions"
163+
)
144164

145165
run_cmd = []
146166
run_cmd.extend([self._docker_cmd, "run", "-p", "0:80", "-d"])
@@ -150,7 +170,9 @@ def spawn_container(self,
150170
run_cmd.extend(["-e", f"CONTAINER_NAME={self._uuid}"])
151171
run_cmd.extend(["-e", f"CONTAINER_ENCRYPTION_KEY={_DUMMY_CONT_KEY}"])
152172
run_cmd.extend(["-e", "WEBSITE_PLACEHOLDER_MODE=1"])
173+
run_cmd.extend(["-e", "PYTHON_ISOLATE_WORKER_DEPENDENCIES=1"])
153174
run_cmd.extend(["-v", f'{worker_path}:{container_worker_path}'])
175+
run_cmd.extend(["-v", f'{library_path}:{container_library_path}'])
154176

155177
for key, value in env.items():
156178
run_cmd.extend(["-e", f"{key}={value}"])
@@ -181,7 +203,7 @@ def spawn_container(self,
181203
self._ports[self._uuid] = port_number
182204

183205
# Wait for three seconds for the container to be in ready state
184-
time.sleep(3)
206+
time.sleep(6)
185207
return port_number
186208

187209
def get_container_logs(self) -> str:
@@ -259,6 +281,7 @@ def __enter__(self):
259281
def __exit__(self, exc_type, exc_value, traceback):
260282
logs = self.get_container_logs()
261283
self.safe_kill_container()
284+
shutil.rmtree(os.path.join(tempfile.gettempdir(), _FUNC_FILE_NAME))
262285

263286
if traceback:
264287
print(f'Test failed with container logs: {logs}',

tests/endtoend/test_linux_consumption.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
3-
from unittest import TestCase, skipIf
4-
53
import os
64
import sys
7-
from requests import Request
5+
from unittest import TestCase, skipIf
86

97
from azure_functions_worker.testutils_lc import (
108
LinuxConsumptionWebHostController
119
)
1210
from azure_functions_worker.utils.common import is_python_version
11+
from requests import Request
12+
13+
_DEFAULT_HOST_VERSION = "3"
1314

1415

1516
@skipIf(is_python_version('3.10'),
@@ -42,7 +43,8 @@ def test_placeholder_mode_root_returns_ok(self):
4243
"""In any circumstances, a placeholder container should returns 200
4344
even when it is not specialized.
4445
"""
45-
with LinuxConsumptionWebHostController("3", self._py_version) as ctrl:
46+
with LinuxConsumptionWebHostController(_DEFAULT_HOST_VERSION,
47+
self._py_version) as ctrl:
4648
req = Request('GET', ctrl.url)
4749
resp = ctrl.send_request(req)
4850
self.assertTrue(resp.ok)
@@ -51,7 +53,8 @@ def test_http_no_auth(self):
5153
"""An HttpTrigger function app with 'azure-functions' library
5254
should return 200.
5355
"""
54-
with LinuxConsumptionWebHostController("3", self._py_version) as ctrl:
56+
with LinuxConsumptionWebHostController(_DEFAULT_HOST_VERSION,
57+
self._py_version) as ctrl:
5558
ctrl.assign_container(env={
5659
"AzureWebJobsStorage": self._storage,
5760
"SCM_RUN_FROM_PACKAGE": self._get_blob_url("HttpNoAuth")
@@ -60,6 +63,8 @@ def test_http_no_auth(self):
6063
resp = ctrl.send_request(req)
6164
self.assertEqual(resp.status_code, 200)
6265

66+
@skipIf(is_python_version('3.7'),
67+
"Skip the tests for Python 3.7.")
6368
def test_common_libraries(self):
6469
"""A function app with the following requirements.txt:
6570
@@ -73,7 +78,8 @@ def test_common_libraries(self):
7378
7479
should return 200 after importing all libraries.
7580
"""
76-
with LinuxConsumptionWebHostController("3", self._py_version) as ctrl:
81+
with LinuxConsumptionWebHostController(_DEFAULT_HOST_VERSION,
82+
self._py_version) as ctrl:
7783
ctrl.assign_container(env={
7884
"AzureWebJobsStorage": self._storage,
7985
"SCM_RUN_FROM_PACKAGE": self._get_blob_url("CommonLibraries")
@@ -98,7 +104,8 @@ def test_new_protobuf(self):
98104
99105
should return 200 after importing all libraries.
100106
"""
101-
with LinuxConsumptionWebHostController("3", self._py_version) as ctrl:
107+
with LinuxConsumptionWebHostController(_DEFAULT_HOST_VERSION,
108+
self._py_version) as ctrl:
102109
ctrl.assign_container(env={
103110
"AzureWebJobsStorage": self._storage,
104111
"SCM_RUN_FROM_PACKAGE": self._get_blob_url("NewProtobuf")
@@ -123,10 +130,11 @@ def test_old_protobuf(self):
123130
124131
should return 200 after importing all libraries.
125132
"""
126-
with LinuxConsumptionWebHostController("3", self._py_version) as ctrl:
133+
with LinuxConsumptionWebHostController(_DEFAULT_HOST_VERSION,
134+
self._py_version) as ctrl:
127135
ctrl.assign_container(env={
128136
"AzureWebJobsStorage": self._storage,
129-
"SCM_RUN_FROM_PACKAGE": self._get_blob_url("NewProtobuf")
137+
"SCM_RUN_FROM_PACKAGE": self._get_blob_url("OldProtobuf")
130138
})
131139
req = Request('GET', f'{ctrl.url}/api/HttpTrigger')
132140
resp = ctrl.send_request(req)
@@ -144,7 +152,8 @@ def test_debug_logging_disabled(self):
144152
should return 200 and by default customer debug logging should be
145153
disabled.
146154
"""
147-
with LinuxConsumptionWebHostController("3", self._py_version) as ctrl:
155+
with LinuxConsumptionWebHostController(_DEFAULT_HOST_VERSION,
156+
self._py_version) as ctrl:
148157
ctrl.assign_container(env={
149158
"AzureWebJobsStorage": self._storage,
150159
"SCM_RUN_FROM_PACKAGE": self._get_blob_url("EnableDebugLogging")
@@ -170,7 +179,8 @@ def test_debug_logging_enabled(self):
170179
should return 200 and with customer debug logging enabled, debug logs
171180
should be written to container logs.
172181
"""
173-
with LinuxConsumptionWebHostController("3", self._py_version) as ctrl:
182+
with LinuxConsumptionWebHostController(_DEFAULT_HOST_VERSION,
183+
self._py_version) as ctrl:
174184
ctrl.assign_container(env={
175185
"AzureWebJobsStorage": self._storage,
176186
"SCM_RUN_FROM_PACKAGE": self._get_blob_url(

0 commit comments

Comments
 (0)