Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Aflynn50 committed Apr 3, 2024
1 parent da81293 commit 01982c9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
12 changes: 8 additions & 4 deletions juju/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2539,10 +2539,7 @@ async def consume(self, endpoint, application_alias="", controller_name=None, co
controller name must be included in the endpoint. The controller_name
argument is being deprecated.
"""
if controller_name:
log.warning(
'controller_name argument will soon be deprecated, controller '
'should be specified in endpoint url')

if controller and controller_name:
raise JujuError("cannot set both controller_name and controller")
try:
Expand All @@ -2556,6 +2553,13 @@ async def consume(self, endpoint, application_alias="", controller_name=None, co
offer.user = self.info.username
endpoint = offer.string()

if controller_name:
log.warning(
'controller_name argument will soon be deprecated, controller '
'should be specified in endpoint url')
if offer.source == "":
offer.source = controller_name

if offer.source:
source = await self._get_source_api(offer)
else:
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_crossmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,4 @@ async def test_add_bundle():
await model_2.deploy('local:{}'.format(tmp_path))
await model_2.wait_for_idle(status="active")

await model_1.remove_offer("admin/{}.influxdb".format(model_1.name), force=True)
await model_1.remove_offer("admin/{}.influxdb".format(model_1.name), force=True)
59 changes: 59 additions & 0 deletions tests/unit/test_offerendpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#

import unittest
import mock
from juju.model import Model
from juju.controller import Controller

from juju.offerendpoints import (LocalEndpoint, OfferEndpoints, OfferURL,
ParseError, maybe_parse_offer_url_source,
Expand Down Expand Up @@ -90,3 +93,59 @@ def test_parse_local_endpoint_failures(self):
self.assertEqual(e.message, case)
except Exception:
raise


class TestConsume(unittest.IsolatedAsyncioTestCase):
@mock.patch.object(Model, 'connection')
@mock.patch.object(Controller, 'disconnect')
@mock.patch('juju.model._create_consume_args')
@mock.patch('juju.client.client.ApplicationFacade.from_connection')
async def test_external_controller_consume(self, mock_from_connection,
mock_controller, mock_connection, mock_create_consume_args):
""" Test consuming an offer from an external controller. This would be
better suited as an integration test however pylibjuju does not allow
for bootstrapping of extra controllers.
"""

mock_create_consume_args.return_value = None
mock_connection.return_value = None

mock_consume_details = mock.MagicMock()
mock_consume_details.offer.offer_url = "user/offerer.app"
mock_controller.get_consume_details = mock.AsyncMock(return_value=mock_consume_details)
mock_controller.disconnect = mock.AsyncMock()

mock_facade = mock.MagicMock()
mock_from_connection.return_value = mock_facade

mock_results = mock.MagicMock()
mock_results.results = [mock.MagicMock()]
mock_results.results[0].error = None
mock_facade.Consume = mock.AsyncMock(return_value=mock_results)

m = Model()
m._get_source_api = mock.AsyncMock(return_value=mock_controller)

# Test with an external controller.
offer_url = "externalcontroller:user/offerer.app"
await m.consume(offer_url)
m._get_source_api.assert_called_once_with(parse_offer_url(offer_url))
mock_controller.get_consume_details.assert_called_with("user/offerer.app")

# Test with an external controller and controller_name arg.
offer_url = "externalcontroller:user/offerer.app"
await m.consume(offer_url, controller_name="externalcontroller")
m._get_source_api.assert_called_with(parse_offer_url(offer_url))
mock_controller.get_consume_details.assert_called_with("user/offerer.app")

# Test with a local (mocked) controller.
offer_url = "user/offerer.app"
await m.consume(offer_url, controller=mock_controller)
mock_controller.get_consume_details.assert_called_with("user/offerer.app")

# Test with an external controller with just controller_name. This will
# soon be deprecated.
offer_url = "user/offerer.app"
await m.consume(offer_url, controller_name="externalcontroller")
m._get_source_api.assert_called_with(parse_offer_url("externalcontroller:user/offerer.app"))
mock_controller.get_consume_details.assert_called_with("user/offerer.app")

0 comments on commit 01982c9

Please sign in to comment.