Skip to content

Commit

Permalink
fixed playwright tests
Browse files Browse the repository at this point in the history
wired back execution of tests in CI Pipeline
  • Loading branch information
DinisCruz committed Oct 3, 2024
1 parent 85bca40 commit fbb021e
Show file tree
Hide file tree
Showing 10 changed files with 339 additions and 213 deletions.
38 changes: 19 additions & 19 deletions .github/workflows/ci-pipeline__dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ env:

jobs:

# run-unit-and-integration-tests:
# if: False
# name: "Run Unit & Integration Tests"
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v4
# - name: Run Tests and Integration Tests
# uses: ./.github/actions/pytest__run-unit-and-integration
run-unit-and-integration-tests:
#if: False
name: "Run Unit & Integration Tests"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run Tests and Integration Tests
uses: ./.github/actions/pytest__run-unit-and-integration

increment-tag:
#if: False
Expand All @@ -37,8 +37,8 @@ jobs:
uses: owasp-sbot/OSBot-GitHub-Actions/.github/actions/git__increment-tag@dev
with:
release_type: ${{ env.RELEASE_TYPE }}
# needs:
# - run-unit-and-integration-tests
needs:
- run-unit-and-integration-tests

publish-to-pypi:
name: "Publish to: PYPI"
Expand Down Expand Up @@ -101,12 +101,12 @@ jobs:
needs:
- publish-to-aws-ecr

# run-qa-tests:
# name: "Run QA Tests"
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v4
# - name: Run Tests and Integration Tests
# uses: ./.github/actions/pytest__run-qa
# needs:
# - deploy-lambda-function
run-qa-tests:
name: "Run QA Tests"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run Tests and Integration Tests
uses: ./.github/actions/pytest__run-qa
needs:
- deploy-lambda-function
18 changes: 16 additions & 2 deletions deploy/docker/osbot-serverless-flows__integration-tests/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
FROM amazon/aws-lambda-python:3.12
FROM python:3.12-bookworm

RUN pip install playwright
RUN playwright install --with-deps chromium

RUN pip install osbot-aws osbot-fast-api
RUN pip install requests mangum httpx
RUN pip install osbot_playwright

COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.4 /lambda-adapter /opt/extensions/lambda-adapter

RUN python --version
ENV PORT=8080

WORKDIR /app

RUN pip install osbot-utils==v1.47.0

COPY ../../../osbot_serverless_flows /app/osbot_serverless_flows

ENV PYTHONPATH="/app"

CMD ["osbot_serverless_flows.lambdas.handler.run"]
COPY ./deploy/docker/osbot-serverless-flows__playwright/start.sh /app/start.sh

CMD ["./start.sh"]

3 changes: 3 additions & 0 deletions deploy/docker/osbot-serverless-flows__playwright/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ RUN python --version
ENV PORT=8080

WORKDIR /app

RUN pip install osbot-utils==v1.47.0

COPY ../../../osbot_serverless_flows /app/osbot_serverless_flows

ENV PYTHONPATH="/app"
Expand Down
82 changes: 42 additions & 40 deletions tests/integration/test__fastapi__via_boto3.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
from unittest import TestCase

from osbot_utils.utils.Dev import pprint
from osbot_utils.utils.Misc import list_set

from osbot_aws.aws.lambda_.Lambda import Lambda
from osbot_aws.aws.session.Session__Kwargs__Lambda import Session__Kwargs__Lambda
from osbot_aws.testing.Temp__Random__AWS_Credentials import Temp__Random__AWS_Credentials



class test__fastapi__via_boto3(TestCase):
@classmethod
def setUpClass(cls):
cls.function_name = 'function'
cls.endpoint_url = 'http://localhost:5002/'
cls.session_kwargs__lambda = Session__Kwargs__Lambda(endpoint_url=cls.endpoint_url).__locals__()

def invoke_lambda(self, path):
with Temp__Random__AWS_Credentials():
with Lambda(name=self.function_name, session_kwargs__lambda=self.session_kwargs__lambda) as _:
payload = { "resource" : "" ,
"requestContext": {} ,
"httpMethod" : "GET" ,
"path" : path }
response = _.invoke(payload)
return response

def test__root(self):
response = self.invoke_lambda('/')
assert response.get('body' ) == ""
assert response.get('statusCode') == 307
assert list_set(response.get('headers')) == ['content-length', 'fast-api-request-id', 'location']

def test__ping(self):
response = self.invoke_lambda('/info/ping')
assert response.get('body' ) == '{"pong":"42"}'
assert response.get('statusCode') == 200
assert list_set(response.get('headers')) == ['content-length', 'content-type', 'fast-api-request-id']

# from unittest import TestCase
#
# from osbot_utils.utils.Dev import pprint
# from osbot_utils.utils.Misc import list_set
#
# from osbot_aws.aws.lambda_.Lambda import Lambda
# from osbot_aws.aws.session.Session__Kwargs__Lambda import Session__Kwargs__Lambda
# from osbot_aws.testing.Temp__Random__AWS_Credentials import Temp__Random__AWS_Credentials
#
#
#
# class test__fastapi__via_boto3(TestCase):
# @classmethod
# def setUpClass(cls):
# cls.function_name = 'function'
# cls.endpoint_url = 'http://localhost:5002/'
# cls.session_kwargs__lambda = Session__Kwargs__Lambda(endpoint_url=cls.endpoint_url).__locals__()
#
# def invoke_lambda(self, path):
# with Temp__Random__AWS_Credentials():
# with Lambda(name=self.function_name, session_kwargs__lambda=self.session_kwargs__lambda) as _:
# payload = { "resource" : "" ,
# "requestContext": {} ,
# "httpMethod" : "GET" ,
# "path" : path }
# response = _.invoke(payload)
# return response
#
# def test__root(self):
# response = self.invoke_lambda('/')
# pprint(response)
# return
# assert response.get('body' ) == ""
# assert response.get('statusCode') == 307
# assert list_set(response.get('headers')) == ['content-length', 'fast-api-request-id', 'location']
#
# def test__ping(self):
# response = self.invoke_lambda('/info/ping')
# assert response.get('body' ) == '{"pong":"42"}'
# assert response.get('statusCode') == 200
# assert list_set(response.get('headers')) == ['content-length', 'content-type', 'fast-api-request-id']
#
43 changes: 21 additions & 22 deletions tests/integration/test__fastapi__via_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,43 @@
from osbot_serverless_flows.utils.Version import version__osbot_serverless_flows


def create_payload_for_path(path):
return { "resource" : "" ,
"requestContext": {} ,
"httpMethod" : "GET" ,
"path" : path }
# def create_payload_for_path(path):
# return { "resource" : "" ,
# "requestContext": {} ,
# "httpMethod" : "GET" ,
# "path" : path }

class test__fastapi__via_requests(TestCase):
@classmethod
def setUpClass(cls):
cls.function_name = 'function'
cls.function_server = 'http://localhost:5002'
cls.headers = {'Content-Type': 'application/json'}
cls.invoke_url = f"{cls.function_server}/2015-03-31/functions/{cls.function_name}/invocations"
#cls.invoke_url = f"{cls.function_server}/2015-03-31/functions/{cls.function_name}/invocations"

def test__ping(self):
payload = create_payload_for_path('/info/ping')
response = requests.post(self.invoke_url, headers=self.headers, json=payload)
fast_api_request_id = response.json().get('headers').get('fast-api-request-id')

url = f"{self.function_server}/info/ping"
response = requests.get(url)
response__fast_api_request_id = response.headers.get('fast-api-request-id')
response__date = response.headers.get('date')
assert response.status_code == 200
assert response.json() == { 'body' : '{"pong":"42"}',
'headers' : {'content-length' : '13' ,
'content-type' : 'application/json' ,
'fast-api-request-id': fast_api_request_id },
'isBase64Encoded' : False ,
'multiValueHeaders' : {} ,
'statusCode' : 200 }
assert response.json() == {"pong":"42"}
assert response.headers == {'content-length' : '13' ,
'content-type' : 'application/json' ,
'date' : response__date ,
'fast-api-request-id': response__fast_api_request_id ,
'server' : 'uvicorn' }

def test__docs(self):
payload = create_payload_for_path('/docs')
response = requests.post(self.invoke_url, headers=self.headers, json=payload)
url = f"{self.function_server}/docs"
response = requests.get(url)
assert response.status_code == 200
assert "Swagger UI" in response.text



def test__version(self):
payload = create_payload_for_path('/info/version')
response = requests.post(self.invoke_url, headers=self.headers, json=payload)
url = f"{self.function_server}/info/version"
response = requests.get(url)
assert response.status_code == 200
assert response.json().get('body') == f'{{"version":"{version__osbot_serverless_flows}"}}'
assert response.json() == {"version": version__osbot_serverless_flows}
99 changes: 99 additions & 0 deletions tests/integration/test__remote_shell__local_docker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from unittest import TestCase
from osbot_utils.utils.Files import bytes_to_file, file_exists, file_delete, file_bytes
from osbot_utils.utils.Misc import base64_to_bytes
from osbot_serverless_flows.utils._for_osbot_aws.Http__Remote_Shell import Http__Remote_Shell

class test__remote_shell_docker(TestCase):

def setUp(self):
self.port = 5002
target_url = f'http://localhost:{self.port}/debug/lambda-shell'
self.shell = Http__Remote_Shell(target_url=target_url)

def test_0_lambda_shell_setup(self):
assert self.shell.ping() == 'pong'

def test_1_check_osbot_utils_version(self):

def check_osbot_utils_version():
from osbot_utils.utils.Version import Version
return Version().value()

assert self.shell.function(check_osbot_utils_version) == 'v1.47.0'


def test_2_playwright__install_chrome(self):
def playwright__install_chrome():
from osbot_playwright.playwright.api.Playwright_CLI import Playwright_CLI
playwright_cli = Playwright_CLI()
result = playwright_cli.install__chrome()
return f'{result}'

assert self.shell.function(playwright__install_chrome) == 'True'

def test_3_playwright__new_page(self):

def playwright__new_page():
from osbot_utils.utils.Threads import async_invoke_in_new_loop
from playwright.async_api import async_playwright

async def run_in_new_loop():
context = await async_playwright().start()
browser = await context.chromium.launch(args=["--disable-gpu", "--single-process"])
page = await browser.new_page()
return f'{page}'

return async_invoke_in_new_loop(run_in_new_loop())

assert self.shell.function(playwright__new_page) == "<Page url='about:blank'>"

def test_4_playwright__open_page(self):

def playwright__open_page():
from osbot_utils.utils.Threads import async_invoke_in_new_loop
from playwright.async_api import async_playwright

async def run_in_new_loop(url):
context = await async_playwright().start()
browser = await context.chromium.launch(args=["--disable-gpu", "--single-process"])
page = await browser.new_page()
await page.goto(url)
return f'{page}'

target_url = 'https://www.google.com/404'
return async_invoke_in_new_loop(run_in_new_loop(target_url))

assert self.shell.function(playwright__open_page) == "<Page url='https://www.google.com/404'>"


def test_5_playwright__page_screenshot(self):

def playwright__page_screenshot():
from osbot_utils.utils.Threads import async_invoke_in_new_loop
from playwright.async_api import async_playwright
from osbot_utils.utils.Misc import bytes_to_base64

async def run_in_new_loop(url):
context = await async_playwright().start()
browser = await context.chromium.launch(args=["--disable-gpu", "--single-process"])
page = await browser.new_page()
await page.goto(url)
screenshot = await page.screenshot(full_page=True)

return bytes_to_base64(screenshot)

target_url = 'https://www.google.com/404'
target_url = 'https://dev.cyber-boardroom.com/docs'
return async_invoke_in_new_loop(run_in_new_loop(target_url))

screenshot_base64 = self.shell.function(playwright__page_screenshot)
screenshot_bytes = base64_to_bytes(screenshot_base64)
screenshot_file = bytes_to_file(bytes=screenshot_bytes, extension='.png')
assert len (screenshot_base64) > 50000
assert type (screenshot_bytes ) is bytes
assert file_exists(screenshot_file ) is True
assert file_bytes (screenshot_file ) == screenshot_bytes
assert file_delete(screenshot_file ) is True



11 changes: 3 additions & 8 deletions tests/qa/test__live_lambda_server.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import requests
from unittest import TestCase

from osbot_aws.apis.shell.Lambda_Shell import SHELL_VAR
from osbot_fast_api.utils.Version import version__osbot_fast_api
from osbot_utils.utils.Dev import pprint
from osbot_utils.utils.Env import get_env, load_dotenv

from unittest import TestCase
from osbot_fast_api.utils.Version import version__osbot_fast_api
from osbot_utils.utils.Env import get_env, load_dotenv
from osbot_serverless_flows.utils.Version import version__osbot_serverless_flows
from osbot_serverless_flows.utils._for_osbot_aws.Http__Remote_Shell import Http__Remote_Shell

ENDPOINT_URL__QA_LAMBDA = 'https://serverless-flows.dev.aws.cyber-boardroom.com'

Expand Down
Loading

0 comments on commit fbb021e

Please sign in to comment.