Skip to content

Commit

Permalink
Merge pull request #7 from stfc/add-openstack-conn-tests
Browse files Browse the repository at this point in the history
Add openstack connection tests
  • Loading branch information
khalford authored Dec 12, 2024
2 parents b8fa2d3 + 17d65db commit 33278fc
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions tests/test_openstack_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from unittest.mock import patch

import pytest
from openstackquery.openstack_connection import OpenstackConnection


@patch("openstackquery.openstack_connection.connect")
def test_openstack_connection_connects_first_time(patched_connect):
"""
Tests that connect it correctly called on entry.
Does not check the args passed to connect
"""
with OpenstackConnection("a") as instance:
patched_connect.assert_called_once()
assert instance == patched_connect.return_value


@patch("openstackquery.openstack_connection.connect")
def test_openstack_connection_uses_cloud_name(patched_connect):
"""
Tests that the cloud name gets used in the call to connect correctly
"""
expected_cloud = "foo"

with OpenstackConnection(expected_cloud):
patched_connect.assert_called_once_with(cloud=expected_cloud)


def test_connection_throws_for_no_cloud_name():
"""
Tests a None type will throw if used as the account name
"""
with pytest.raises(RuntimeError):
with OpenstackConnection(None):
pass


@patch("openstackquery.openstack_connection.connect")
def test_connection_throws_for_empty_cloud_name(_):
"""
Tests an empty string will throw for the cloud name
"""
with pytest.raises(RuntimeError):
with OpenstackConnection(""):
pass


@patch("openstackquery.openstack_connection.connect")
def test_connection_throws_for_whitespace_cloud_name(_):
"""
Tests a whitespace string will throw for the cloud name
"""
with pytest.raises(RuntimeError):
with OpenstackConnection(" \t"):
pass


@patch("openstackquery.openstack_connection.connect")
def test_openstack_connection_disconnects(patched_connect):
"""
Checks the session is correctly closed (to not leak handles)
when the context manager exits
"""
with OpenstackConnection("a") as instance:
connection_handle = patched_connect.return_value
assert instance == connection_handle
connection_handle.close.assert_called_once()


@patch("openstackquery.openstack_connection.connect")
def test_openstack_connection_connects_second_time(patched_connect):
"""
Tests that creating two connections calls connect twice.
Why, because Singletons are evil and cause nothing but problems
"""
with OpenstackConnection("a"):
pass
with OpenstackConnection("a"):
pass
assert patched_connect.call_count == 2

0 comments on commit 33278fc

Please sign in to comment.