Skip to content

Commit

Permalink
Merge pull request #167 from larsks/fix/tests
Browse files Browse the repository at this point in the history
  • Loading branch information
larsks authored Jul 19, 2024
2 parents 7a33db3 + 8db5b55 commit 92922ca
Show file tree
Hide file tree
Showing 14 changed files with 161 additions and 154 deletions.
2 changes: 1 addition & 1 deletion esi_leap/db/sqlalchemy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from oslo_db.sqlalchemy import types as db_types

from sqlalchemy.ext.compiler import compiles
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base
from sqlalchemy import orm
from sqlalchemy import Column, DateTime, ForeignKey
from sqlalchemy import Index, Integer, String
Expand Down
2 changes: 1 addition & 1 deletion esi_leap/resource_objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# types derived from base won't show as subclasses unless imported somewhere
from esi_leap.resource_objects import dummy_node # noqa: F401
from esi_leap.resource_objects import ironic_node # noqa: F401
from esi_leap.resource_objects import test_node # noqa: F401
from esi_leap.resource_objects import fake_node # noqa: F401


_RESOURCE_TYPE_MAP = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from esi_leap.resource_objects import base


class TestNode(base.ResourceObjectInterface):
class FakeNode(base.ResourceObjectInterface):
resource_type = "test_node"

def __init__(self, uuid, project_id="12345"):
Expand Down
4 changes: 2 additions & 2 deletions esi_leap/tests/api/controllers/v1/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import mock

from esi_leap.common import exception
from esi_leap.resource_objects.test_node import TestNode
from esi_leap.resource_objects.fake_node import FakeNode
from esi_leap.tests.api import base as test_api_base


Expand Down Expand Up @@ -82,7 +82,7 @@ def test_get_all_resource_filter(self, mock_ega, mock_gro, mock_gpufi, mock_pa):
fake_event = FakeEvent()
expected_filters = {"resource_type": "test_node", "resource_uuid": "1111"}
mock_pa.side_effect = None
mock_gro.return_value = TestNode("1111")
mock_gro.return_value = FakeNode("1111")
mock_ega.return_value = [fake_event]

data = self.get_json("/events?resource_uuid=1111&resource_type=test_node")
Expand Down
8 changes: 4 additions & 4 deletions esi_leap/tests/api/controllers/v1/test_lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from esi_leap.common import statuses
from esi_leap.objects import lease as lease_obj
from esi_leap.resource_objects.ironic_node import IronicNode
from esi_leap.resource_objects.test_node import TestNode
from esi_leap.resource_objects.fake_node import FakeNode
from esi_leap.tests.api import base as test_api_base


Expand Down Expand Up @@ -59,7 +59,7 @@ def setUp(self):
resource_uuid="111",
project_id="lesseeid",
owner_id="ownerid",
parent_lease_uuid="parent-lease-uuid",
parent_lease_uuid=uuidutils.generate_uuid(),
)

def test_empty(self):
Expand Down Expand Up @@ -98,7 +98,7 @@ def test_post(
mock_gro,
mock_lgdwai,
):
resource = TestNode("1234567890")
resource = FakeNode("1234567890")
data = {
"project_id": "lesseeid",
"resource_type": "test_node",
Expand Down Expand Up @@ -453,7 +453,7 @@ def test_get_owner_filter(
def test_get_resource_filter(
self, mock_get_all, mock_lgaaf, mock_gro, mock_lgdwai, mock_gpl, mock_gnl
):
mock_gro.return_value = TestNode("54321")
mock_gro.return_value = FakeNode("54321")
mock_get_all.return_value = [self.test_lease, self.test_lease]
mock_gpl.return_value = []
mock_gnl.return_value = []
Expand Down
121 changes: 60 additions & 61 deletions esi_leap/tests/api/controllers/v1/test_offer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from esi_leap.common import statuses
from esi_leap.objects import offer
from esi_leap.resource_objects.ironic_node import IronicNode
from esi_leap.resource_objects.test_node import TestNode
from esi_leap.resource_objects.fake_node import FakeNode
from esi_leap.tests.api import base as test_api_base


Expand Down Expand Up @@ -109,7 +109,7 @@ def setUp(self):
start_time=start,
end_time=start + datetime.timedelta(days=100),
project_id=self.context.project_id,
parent_lease_uuid="parent-lease-uuid",
parent_lease_uuid=uuidutils.generate_uuid(),
)

def test_empty(self):
Expand All @@ -124,7 +124,7 @@ def test_empty(self):
def test_post(
self, mock_ogdwai, mock_create, mock_cra, mock_generate_uuid, mock_gro
):
resource = TestNode(self.test_offer.resource_uuid)
resource = FakeNode(self.test_offer.resource_uuid)
mock_gro.return_value = resource
mock_generate_uuid.return_value = self.test_offer.uuid
mock_create.return_value = self.test_offer
Expand Down Expand Up @@ -210,7 +210,7 @@ def test_post_lessee(
mock_gpufi,
mock_gro,
):
resource = TestNode(self.test_offer_lessee.resource_uuid)
resource = FakeNode(self.test_offer_lessee.resource_uuid)
mock_gro.return_value = resource
mock_gpufi.return_value = "lessee_uuid"
mock_generate_uuid.return_value = self.test_offer_lessee.uuid
Expand Down Expand Up @@ -260,7 +260,7 @@ def test_post_non_admin_parent_lease(
mock_gro,
mock_crla,
):
resource = TestNode(self.test_offer_with_parent.resource_uuid)
resource = FakeNode(self.test_offer_with_parent.resource_uuid)
mock_gro.return_value = resource
mock_generate_uuid.return_value = self.test_offer_with_parent.uuid
mock_create.return_value = self.test_offer_with_parent
Expand Down Expand Up @@ -320,7 +320,7 @@ def test_post_non_admin_no_parent_lease(
mock_gro,
mock_crla,
):
resource = TestNode(self.test_offer_with_parent.resource_uuid)
resource = FakeNode(self.test_offer_with_parent.resource_uuid)
mock_gro.return_value = resource
mock_generate_uuid.return_value = self.test_offer_with_parent.uuid
mock_create.return_value = self.test_offer_with_parent
Expand Down Expand Up @@ -496,7 +496,7 @@ def test_get_resource_filter(
_get_offer_response(self.test_offer, use_datetime=True),
_get_offer_response(self.test_offer_2, use_datetime=True),
]
mock_gro.return_value = TestNode("54321")
mock_gro.return_value = FakeNode("54321")
mock_gpl.return_value = []
mock_gnl.return_value = []

Expand Down Expand Up @@ -651,71 +651,70 @@ def test_get_one(self, mock_ogdwai, mock_copar, mock_col):
)
mock_ogdwai.assert_called_once_with(self.test_offer)

@mock.patch("oslo_utils.uuidutils.generate_uuid")
@mock.patch("esi_leap.objects.lease.Lease.create")
@mock.patch("esi_leap.api.controllers.v1.utils.check_offer_lessee")
@mock.patch("esi_leap.api.controllers.v1.utils." "check_offer_policy_and_retrieve")
@mock.patch("esi_leap.api.controllers.v1.utils." "lease_get_dict_with_added_info")
def test_claim(
self, mock_lgdwai, mock_copar, mock_col, mock_lease_create, mock_generate_uuid
):
lease_uuid = "12345"
mock_generate_uuid.return_value = lease_uuid
mock_copar.return_value = self.test_offer
data = {
"name": "lease_claim",
"start_time": "2016-07-16T19:20:30",
"end_time": "2016-08-16T19:20:30",
}

request = self.post_json("/offers/" + self.test_offer.uuid + "/claim", data)
def test_claim(self, mock_lgdwai, mock_copar, mock_col, mock_lease_create):
lease_uuid = uuidutils.generate_uuid()

with mock.patch("oslo_utils.uuidutils.generate_uuid") as mock_generate_uuid:
mock_generate_uuid.return_value = lease_uuid
mock_copar.return_value = self.test_offer
data = {
"name": "lease_claim",
"start_time": "2016-07-16T19:20:30",
"end_time": "2016-08-16T19:20:30",
}

request = self.post_json("/offers/" + self.test_offer.uuid + "/claim", data)

mock_copar.assert_called_once_with(
self.context,
"esi_leap:offer:claim",
self.test_offer.uuid,
[statuses.AVAILABLE],
)
mock_col.assert_called_once_with(
self.context.to_policy_values(), self.test_offer
)
mock_lease_create.assert_called_once()
mock_lgdwai.assert_called_once()
self.assertEqual(http_client.CREATED, request.status_int)

mock_copar.assert_called_once_with(
self.context,
"esi_leap:offer:claim",
self.test_offer.uuid,
[statuses.AVAILABLE],
)
mock_col.assert_called_once_with(
self.context.to_policy_values(), self.test_offer
)
mock_lease_create.assert_called_once()
mock_lgdwai.assert_called_once()
self.assertEqual(http_client.CREATED, request.status_int)

@mock.patch("oslo_utils.uuidutils.generate_uuid")
@mock.patch("esi_leap.objects.lease.Lease.create")
@mock.patch("esi_leap.api.controllers.v1.utils.check_offer_lessee")
@mock.patch("esi_leap.api.controllers.v1.utils." "check_offer_policy_and_retrieve")
@mock.patch("esi_leap.api.controllers.v1.utils." "lease_get_dict_with_added_info")
def test_claim_parent_lease(
self, mock_lgdwai, mock_copar, mock_col, mock_lease_create, mock_generate_uuid
self, mock_lgdwai, mock_copar, mock_col, mock_lease_create
):
lease_uuid = "12345"
mock_generate_uuid.return_value = lease_uuid
mock_copar.return_value = self.test_offer_with_parent
data = {
"name": "lease_claim",
"start_time": "2016-07-16T19:20:30",
"end_time": "2016-08-16T19:20:30",
}

request = self.post_json(
"/offers/" + self.test_offer_with_parent.uuid + "/claim", data
)

mock_copar.assert_called_once_with(
self.context,
"esi_leap:offer:claim",
self.test_offer_with_parent.uuid,
[statuses.AVAILABLE],
)
mock_col.assert_called_once_with(
self.context.to_policy_values(), self.test_offer_with_parent
)
mock_lease_create.assert_called_once()
mock_lgdwai.assert_called_once()
self.assertEqual(http_client.CREATED, request.status_int)
lease_uuid = uuidutils.generate_uuid()
with mock.patch("oslo_utils.uuidutils.generate_uuid") as mock_generate_uuid:
mock_generate_uuid.return_value = lease_uuid
mock_copar.return_value = self.test_offer_with_parent
data = {
"name": "lease_claim",
"start_time": "2016-07-16T19:20:30",
"end_time": "2016-08-16T19:20:30",
}

request = self.post_json(
"/offers/" + self.test_offer_with_parent.uuid + "/claim", data
)

mock_copar.assert_called_once_with(
self.context,
"esi_leap:offer:claim",
self.test_offer_with_parent.uuid,
[statuses.AVAILABLE],
)
mock_col.assert_called_once_with(
self.context.to_policy_values(), self.test_offer_with_parent
)
mock_lease_create.assert_called_once()
mock_lgdwai.assert_called_once()
self.assertEqual(http_client.CREATED, request.status_int)

@mock.patch("esi_leap.api.controllers.v1.utils." "check_offer_policy_and_retrieve")
@mock.patch("esi_leap.objects.offer.Offer.cancel")
Expand Down
12 changes: 6 additions & 6 deletions esi_leap/tests/api/controllers/v1/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from esi_leap.common import statuses
from esi_leap.objects import lease
from esi_leap.objects import offer
from esi_leap.resource_objects.test_node import TestNode
from esi_leap.resource_objects.fake_node import FakeNode


admin_ctx = ctx.RequestContext(project_id="adminid", roles=["admin"])
Expand All @@ -45,8 +45,8 @@
end = start + datetime.timedelta(days=100)
end_iso = "2016-10-24T00:00:00"

test_node_1 = TestNode("111", owner_ctx.project_id)
test_node_2 = TestNode("bbb", owner_ctx_2.project_id)
test_node_1 = FakeNode("111", owner_ctx.project_id)
test_node_2 = FakeNode("bbb", owner_ctx_2.project_id)

o_uuid = uuidutils.generate_uuid()

Expand Down Expand Up @@ -328,7 +328,7 @@ def setUp(self):
uuid=uuidutils.generate_uuid(),
start_time=datetime.datetime(2016, 7, 16, 19, 20, 30),
end_time=datetime.datetime(2016, 8, 16, 19, 20, 30),
parent_lease_uuid="parent-lease-uuid",
parent_lease_uuid=uuidutils.generate_uuid(),
)

@mock.patch("esi_leap.objects.lease.Lease.get")
Expand Down Expand Up @@ -684,11 +684,11 @@ def setUp(self):
parent_lease_uuid=None,
)

@mock.patch("esi_leap.resource_objects.test_node.TestNode.get_name")
@mock.patch("esi_leap.resource_objects.fake_node.FakeNode.get_name")
@mock.patch("esi_leap.common.keystone.get_project_name")
@mock.patch("esi_leap.objects.lease.get_resource_object")
def test_lease_get_dict_with_added_info(self, mock_gro, mock_gpn, mock_gn):
mock_gro.return_value = TestNode("111")
mock_gro.return_value = FakeNode("111")
mock_gpn.return_value = "project-name"
mock_gn.return_value = "resource-name"

Expand Down
9 changes: 9 additions & 0 deletions esi_leap/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ def setUp(self):
auth_token=None, project_id="12345", is_admin=True, overwrite=False
)

def catch_exception(self, func, exc=Exception):
def wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
except exc:
pass

return wrapper


class DBTestCase(TestCase):
def setUp(self):
Expand Down
5 changes: 3 additions & 2 deletions esi_leap/tests/common/test_notification_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
import datetime

import mock
from oslo_utils import uuidutils

from esi_leap.common import notification_utils as notif_utils
from esi_leap.objects import fields
from esi_leap.objects import lease as lease_obj
from esi_leap.resource_objects.test_node import TestNode
from esi_leap.resource_objects.fake_node import FakeNode
from esi_leap.tests import base as tests_base


Expand Down Expand Up @@ -47,7 +48,7 @@ def setUp(self):
status="created",
purpose=None,
)
self.node = TestNode("test-node", "12345")
self.node = FakeNode(uuidutils.generate_uuid(), "12345")

def test_emit_notification(self):
self.config(host="fake-host")
Expand Down
Loading

0 comments on commit 92922ca

Please sign in to comment.