Files
python-cratonclient/cratonclient/tests/integration/v1/test_hosts.py
Ian Cordasco a510d64cec Add Betamax for testing
Use keystoneauth1's Betamax test fixture to start recording
request-response interaction pairs in files. I'm starting this bit of
work with our hosts API module.

As a result of this testing, I discovered that we were not handling
keystoneauth1 exceptions inside of our session. This adds handling for
that with a new function for finding the right cratonclient exception to
raise in cratonclient.exceptions. Thus, this also includes testing for
the convenience functions in that module.

Users need only setup 4 environment variables for testing:

* CRATON_DEMO_USERNAME
* CRATON_DEMO_PROJECT
* CRATON_DEMO_TOKEN
* CRATON_DEMO_URL

Depends-On: I45de545b040d2b99ac8f3f4d3c81359615d328e8
Change-Id: Ib69d825a50a7e4179aefd11bcbfbed39c27c7fbe
Partially-implements: bp testing-plan
2017-03-21 10:27:33 -05:00

117 lines
4.0 KiB
Python

# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Module containing the cratonclient.v1.hosts integration tests."""
from cratonclient import exceptions
from cratonclient.tests.integration import base
class TestHosts(base.BetamaxTestCase):
"""HostsManager integration tests."""
def setUp(self):
"""Prepare our hosts test case."""
super(TestHosts, self).setUp()
self.create_demo_client()
self.cloud = self.client.clouds.create(
name='cloud-{}'.format(self.cassette_name),
)
self.addCleanup(self.client.clouds.delete, self.cloud.id)
self.region = self.client.regions.create(
name='region-{}'.format(self.cassette_name),
cloud_id=self.cloud.id,
)
self.addCleanup(self.client.regions.delete, self.region.id)
def cleanupHost(self, host):
"""Add a cleanup task for this host."""
self.addCleanup(self.client.hosts.delete, host.id)
return host
def test_create(self):
"""Test creation of hosts via the Python API."""
host = self.cleanupHost(self.client.hosts.create(
name='host-0',
ip_address='127.0.1.0',
device_type='server',
region_id=self.region.id,
cloud_id=self.cloud.id,
))
self.assertEqual('host-0', host.name)
def test_delete(self):
"""Test deletion of a host via the Python API."""
host = self.client.hosts.create(
name='host-to-delete',
ip_address='127.0.1.0',
device_type='server',
region_id=self.region.id,
cloud_id=self.cloud.id,
)
self.assertTrue(self.client.hosts.delete(host.id))
self.assertRaises(exceptions.NotFound, self.client.hosts.get,
host.id)
def test_list_autopaginates(self):
"""Verify listing of hosts via the Python API."""
for i in range(0, 62):
self.cleanupHost(self.client.hosts.create(
name='host-{}'.format(i),
ip_address='127.0.1.{}'.format(i),
device_type='server',
region_id=self.region.id,
cloud_id=self.cloud.id,
))
hosts = list(self.client.hosts.list())
self.assertEqual(62, len(hosts))
def test_list_only_retrieves_first_page(self):
"""Verify the behaviour of not auto-paginating listing."""
for i in range(0, 32):
self.cleanupHost(self.client.hosts.create(
name='host-{}'.format(i),
ip_address='127.0.1.{}'.format(i),
device_type='server',
region_id=self.region.id,
cloud_id=self.cloud.id,
))
hosts = list(self.client.hosts.list(autopaginate=False))
self.assertEqual(30, len(hosts))
def test_update(self):
"""Verify the ability to update a host."""
host = self.cleanupHost(self.client.hosts.create(
name='host-0',
ip_address='127.0.1.0',
device_type='server',
region_id=self.region.id,
cloud_id=self.cloud.id,
))
self.assertTrue(host.active)
updated_host = self.client.hosts.update(
item_id=host.id,
note='This is an updated note',
ip_address='127.0.1.1',
)
self.assertEqual('host-0', updated_host.name)
self.assertEqual(host.id, updated_host.id)
self.assertEqual('This is an updated note', updated_host.note)