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

Fixes #302

Merged
merged 5 commits into from
Aug 30, 2023
Merged

Fixes #302

Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 7 additions & 2 deletions ocw/lib/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,13 @@
try:
interface.delete()
except ClientError as exc:
self.log_err("delete_vpc_subnets: %s", exc)
continue
# From https://docs.aws.amazon.com/AWSEC2/latest/APIReference/errors-overview.html
# If a network interface is in use, you may also receive the InvalidParameterValue error.
if exc.response['Error']['Code'] == 'InvalidParameterValue':
self.log_info(exc.response['Error'])
continue

Check warning on line 183 in ocw/lib/ec2.py

View check run for this annotation

Codecov / codecov/patch

ocw/lib/ec2.py#L181-L183

Added lines #L181 - L183 were not covered by tests
else:
raise

Check warning on line 185 in ocw/lib/ec2.py

View check run for this annotation

Codecov / codecov/patch

ocw/lib/ec2.py#L185

Added line #L185 was not covered by tests
if self.dry_run:
self.log_info(f'Deletion of {subnet} skipped due to dry_run mode')
else:
Expand Down
9 changes: 6 additions & 3 deletions ocw/lib/gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,12 @@
.execute()
)
return [basename(z) for z in region["zones"]]
except (KeyError, HttpError) as exc:
self.log_err("list_zones: %s", exc)
return []
except HttpError as exc:
if GCE.get_error_reason(exc) == 'notFound':
self.log_dbg("list_zones: region {} not found", region)
return []

Check warning on line 105 in ocw/lib/gce.py

View check run for this annotation

Codecov / codecov/patch

ocw/lib/gce.py#L102-L105

Added lines #L102 - L105 were not covered by tests
else:
raise exc

Check warning on line 107 in ocw/lib/gce.py

View check run for this annotation

Codecov / codecov/patch

ocw/lib/gce.py#L107

Added line #L107 was not covered by tests

def delete_instance(self, instance_id, zone) -> None:
self._delete_resource(
Expand Down
13 changes: 8 additions & 5 deletions ocw/lib/openqa.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from urllib.parse import urlparse
from cachetools import cached
import requests
Expand All @@ -11,11 +12,13 @@


def verify_tls(url: str) -> bool:
try:
requests.head(url, timeout=DEFAULT_TIMEOUT, verify=True).raise_for_status()
except (RequestException, SSLError):
return False
return True
verify = os.environ.get("REQUESTS_CA_BUNDLE", False)
if verify:
try:
requests.head(url, timeout=DEFAULT_TIMEOUT, verify=verify).raise_for_status()
except (RequestException, SSLError):
return False
return verify


@cached(cache={})
Expand Down
5 changes: 5 additions & 0 deletions tests/test_openqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ def raise_for_status(self):
pass


@pytest.fixture(autouse=True)
def mock_env_missing(monkeypatch):
monkeypatch.setenv("REQUESTS_CA_BUNDLE", "/path/to/cert")


def test_verify_tls_valid_url():
with patch("requests.head", return_value=MockResponse()):
assert verify_tls("https://www.example.com")
Expand Down
57 changes: 34 additions & 23 deletions tests/test_webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import os
import shutil
import sys
import pytest

from subprocess import DEVNULL

import pytest
from podman import PodmanClient
from podman.errors import APIError, PodmanError
from selenium.webdriver import firefox
Expand All @@ -24,11 +24,7 @@


@pytest.fixture(scope="session")
def podman_container():
import warnings
# Ignore ResourceWarning messages that can happen at random when closing resources
warnings.filterwarnings(action="ignore", message="unclosed", category=ResourceWarning)

def client():
if os.getenv("SKIP_SELENIUM"):
pytest.skip("Skipping because SKIP_SELENIUM is set")

Expand All @@ -39,13 +35,24 @@ def podman_container():
client = PodmanClient()
except (APIError, PodmanError) as exc:
pytest.skip(f"Broken Podman environment: {exc}")

if not client.info()['host']['remoteSocket']["exists"]:
if not client.info()["host"]["remoteSocket"]["exists"]:
pytest.skip("Please run systemctl --user enable --now podman.socket")

yield client

client.close()


@pytest.fixture(scope="session")
def random_port():
# Get random number for ephemeral port, container and image name
port = random.randint(32768, 60999) # Typical values from /proc/sys/net/ipv4/ip_local_port_range
image_name = f"pcw-test{port}"
# Typical values from /proc/sys/net/ipv4/ip_local_port_range
return random.randint(32768, 60999)


@pytest.fixture(scope="session")
def image(random_port, client):
image_name = f"pcw-test{random_port}"

# Build image
try:
Expand All @@ -60,17 +67,26 @@ def podman_container():
for log in exc.build_log:
line = json.loads(log.decode("utf-8"))
if line:
print(line.get("stream"), file=sys.stderr, end='')
print(line.get("stream"), file=sys.stderr, end="")
pytest.fail(f"{exc}")

yield image_name

# Cleanup
with contextlib.suppress(APIError, PodmanError):
client.images.remove(image_name)


@pytest.fixture(scope="session")
def container(random_port, image, client):
try:
# Run container
container = client.containers.run(
image=image_name,
name=image_name,
image=image,
name=image,
detach=True,
remove=True,
ports={f"{PORT}/tcp": port}
ports={f"{PORT}/tcp": random_port}
)
# Create user in database
container.exec_run(f"/pcw/container-startup createuser {USERNAME} {PASSWORD}")
Expand All @@ -84,13 +100,10 @@ def podman_container():
container.stop()
with contextlib.suppress(APIError, PodmanError):
container.remove()
with contextlib.suppress(APIError, PodmanError):
client.images.remove(image_name)
client.close()


@pytest.fixture
def browser():
def browser(container):
service = firefox.service.Service(log_output=DEVNULL)
options = firefox.options.Options()
options.add_argument('-headless')
Expand All @@ -99,10 +112,8 @@ def browser():
driver.quit()


def test_login_logout(podman_container, browser): # pylint: disable=redefined-outer-name
# Get randomly assigned port
port = podman_container.ports[f'{PORT}/tcp'][0]['HostPort']
browser.get(f"http://127.0.0.1:{port}")
def test_login_logout(random_port, browser): # pylint: disable=redefined-outer-name
browser.get(f"http://127.0.0.1:{random_port}")
browser.find_element(By.XPATH, XPATH["login"]).click()
browser.find_element(By.NAME, value="username").send_keys(USERNAME)
browser.find_element(By.NAME, value="password").send_keys(PASSWORD)
Expand Down
Loading