Skip to content

Commit

Permalink
Verify the resources API extension is available.
Browse files Browse the repository at this point in the history
Signed-off-by: Lee Trager <lee.trager@canonical.com>
  • Loading branch information
ltrager committed Feb 28, 2020
1 parent e993878 commit 2f5f1fd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions pylxd/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ def trusted(self):
@property
def resources(self):
if self._resource_cache is None:
self.assert_has_api_extension('resources')
response = self.api.resources.get()
if response.status_code != 200:
raise exceptions.ClientConnectionFailed()
Expand Down
10 changes: 9 additions & 1 deletion pylxd/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,20 +282,28 @@ def powerset(types):

def test_resources(self):
a_client = client.Client()
a_client.host_info['api_extensions'] = ['resources']
response = mock.MagicMock(status_code=200)
response.json.return_value = {'metadata': {
'cpu': {},
}}
self.get.return_value = response
self.assertIn('cpu', a_client.resources)

def test_resources_raises_exception(self):
def test_resources_raises_conn_failed_exception(self):
a_client = client.Client()
a_client.host_info['api_extensions'] = ['resources']
response = mock.MagicMock(status_code=400)
self.get.return_value = response
with self.assertRaises(exceptions.ClientConnectionFailed):
a_client.resources

def test_resources_raises_api_extension_not_avail_exception(self):
a_client = client.Client()
a_client.host_info['api_extensions'] = []
with self.assertRaises(exceptions.LXDAPIExtensionNotAvailable):
a_client.resources

def test_resources_uses_cache(self):
a_client = client.Client()
a_client._resource_cache = {'cpu': {}}
Expand Down

0 comments on commit 2f5f1fd

Please sign in to comment.