Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] add testserver #19153

Merged
merged 31 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6dff5d5
tests passing locally
iscai-msft Jun 8, 2021
58c184a
move pytests.init to root
iscai-msft Jun 8, 2021
c7663fe
remove set formatting from windows run
iscai-msft Jun 8, 2021
bec31ed
try to fix windows testserver start
iscai-msft Jun 8, 2021
405d5e8
uncomment testserver termination
iscai-msft Jun 8, 2021
fb692df
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-pytho…
iscai-msft Jun 17, 2021
f826377
add devops scripts to start testserver
iscai-msft Jun 17, 2021
6ceb795
scriptSource -> scriptPath
iscai-msft Jun 17, 2021
0f10e5e
set env vars
iscai-msft Jun 18, 2021
c35fdc2
add pwsh testserver
iscai-msft Jun 18, 2021
f8fd247
return result
iscai-msft Jun 21, 2021
c4fd6d2
try retuning exit code
iscai-msft Jun 21, 2021
b322e28
remove ci work, make pytest fixture module level
iscai-msft Jun 21, 2021
449f42a
tests working without pytest.ini
iscai-msft Jun 21, 2021
a021de2
only have testserver fixture in conftest
iscai-msft Jun 21, 2021
7a68dd3
switch to package scope
iscai-msft Jun 21, 2021
df42f15
unite testserver setting
iscai-msft Jun 21, 2021
da62f4b
switch to environment variables
iscai-msft Jun 22, 2021
e06ebb4
see what happens if we don't kill testserver
iscai-msft Jun 22, 2021
04ddbd0
cycle through ports
iscai-msft Jun 22, 2021
e860e8a
remove scripts
iscai-msft Jun 22, 2021
2f39ad2
allow 2.7 compatibility
iscai-msft Jun 22, 2021
f503b2c
wait longer for pypy
iscai-msft Jun 23, 2021
8ccce34
increase sleep to 2 for pypy
iscai-msft Jun 23, 2021
30c6e39
move core testserver into tests
iscai-msft Jun 24, 2021
20e593a
switch to urllib requesting to see if port open
iscai-msft Jun 24, 2021
1483f8c
ignore coretestserver readme
iscai-msft Jun 24, 2021
4cbf6a8
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-python …
iscai-msft Jun 24, 2021
0f2ae93
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-python …
iscai-msft Jun 24, 2021
3d12735
add readme rst
iscai-msft Jun 25, 2021
c4ac95b
ignore readme.rst
iscai-msft Jun 25, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions scripts/devops_tasks/end_coretestserver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
import signal
import argparse

def end_testserver(pid):

if os.name == 'nt':
os.kill(pid, signal.CTRL_C_EVENT)
else:
os.killpg(os.getpgid(pid), signal.SIGTERM) # Send the signal to all the process groups

if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="Stop the testserver"
)
parser.add_argument(
"-p",
"--pid",
dest="pid",
help="The pid of the subprocess the testserver is running on",
required=True,
)

args = parser.parse_args()
end_testserver(int(args.pid))
14 changes: 14 additions & 0 deletions scripts/devops_tasks/start_coretestserver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os
import subprocess

def start_testserver():
if os.name == 'nt': #On windows, subprocess creation works without being in the shell
os.environ["FLASK_APP"] = "coretestserver"
result = subprocess.Popen("flask run", env=dict(os.environ))
else:
result = subprocess.Popen("FLASK_APP=coretestserver flask run", shell=True, preexec_fn=os.setsid) #On linux, have to set shell=True
print('##vso[task.setvariable variable=FLASK_PID]{}'.format(result.pid))
print("This is used in the pipelines to set the FLASK_PID env var. If you want to stop this testserver, kill this PID.")

if __name__ == "__main__":
start_testserver()
1 change: 1 addition & 0 deletions sdk/core/azure-core/dev_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ opencensus-ext-threading
mock; python_version < '3.3'
-e ../../../tools/azure-sdk-tools
-e ../../../tools/azure-devtools
git+https://github.com/iscai-msft/core.testserver#subdirectory=coretestserver
3 changes: 3 additions & 0 deletions sdk/core/azure-core/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
usefixtures=testserver
xfail_strict=true
9 changes: 9 additions & 0 deletions sdk/core/azure-core/samples/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@
# IN THE SOFTWARE.
#
# --------------------------------------------------------------------------
import pytest
import sys

@pytest.fixture(scope="session")
def testserver():
# dummy testserver for now
# pytest.ini needs to be in root bc we run pytest on the pipelines with just "pytest"
# because of this, samples conftest needs its own def of testserver.
# plan to change the samples tests to use testserver as well, so it's not always going to be a gross dummy fixture
yield

# Ignore collection of async tests for Python 2
collect_ignore = []
if sys.version_info < (3, 5):
Expand Down
37 changes: 37 additions & 0 deletions sdk/core/azure-core/tests/async_tests/test_testserver_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# --------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the ""Software""), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# --------------------------------------------------------------------------
import pytest
from azure.core.pipeline.transport import HttpRequest, AioHttpTransport
"""This file does a simple call to the testserver to make sure we can use the testserver"""

@pytest.mark.asyncio
async def test_smoke():
request = HttpRequest(method="GET", url="http://localhost:5000/basic/string")
async with AioHttpTransport() as sender:
response = await sender.send(request)
response.raise_for_status()
await response.load_body()
assert response.text() == "Hello, world!"
28 changes: 27 additions & 1 deletion sdk/core/azure-core/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,40 @@
# IN THE SOFTWARE.
#
# --------------------------------------------------------------------------
import pytest
import signal
import os
import subprocess
import sys

def start_testserver():
if os.name == 'nt': #On windows, subprocess creation works without being in the shell
os.environ["FLASK_APP"] = "coretestserver"
return subprocess.Popen("flask run", env=dict(os.environ))

return subprocess.Popen("FLASK_APP=coretestserver flask run", shell=True, preexec_fn=os.setsid) #On linux, have to set shell=True

def terminate_testserver(process):
if os.name == 'nt':
process.kill()
else:
os.killpg(os.getpgid(process.pid), signal.SIGTERM) # Send the signal to all the process groups

@pytest.fixture(scope="session")
def testserver():
"""Start the Autorest testserver."""
if not os.environ.get("FLASK_PID"):
server = start_testserver()
yield
terminate_testserver(server)
else:
yield

# Ignore collection of async tests for Python 2
collect_ignore = []
if sys.version_info < (3, 5):
collect_ignore.append("async_tests")


# If opencensus is loadable while doing these tests, register an empty tracer to avoid this:
# https://github.com/census-instrumentation/opencensus-python/issues/442
try:
Expand Down
34 changes: 34 additions & 0 deletions sdk/core/azure-core/tests/test_testserver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# --------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the ""Software""), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# --------------------------------------------------------------------------
from azure.core.pipeline.transport import HttpRequest, RequestsTransport
"""This file does a simple call to the testserver to make sure we can use the testserver"""

def test_smoke():
request = HttpRequest(method="GET", url="http://localhost:5000/basic/string")
with RequestsTransport() as sender:
response = sender.send(request)
response.raise_for_status()
assert response.text() == "Hello, world!"
12 changes: 12 additions & 0 deletions sdk/core/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ extends:
safeName: azurecorecoretracingtelemetry
- name: azure-common
safeName: azurecommon
BeforeTestSteps:
- task: PythonScript@0
inputs:
scriptSource: 'scripts/devops_tasks/start_coretestserver.py'
lmazuel marked this conversation as resolved.
Show resolved Hide resolved
displayName: "Start CoreTestServer"
AfterTestSteps:
- task: PythonScript@0
inputs:
scriptSource: 'scripts/devops_tasks/end_coretestserver.py'
arguments: >-
-p $(FLASK_PID)
displayName: "Shut down CoreTestServer"
CondaArtifacts:
- name: azure-core
meta_source: conda-recipe/meta.yaml
Expand Down