[Kimchi-devel] [PATCH 2/2] Unit tests for the new disk image types
Daniel H Barboza
danielhb at linux.vnet.ibm.com
Thu Oct 30 19:48:19 UTC 2014
On 10/30/2014 05:15 PM, Aline Manera wrote:
>
> On 10/30/2014 10:05 AM, Daniel Henrique Barboza wrote:
>> The tests covered the case where the template is created and the
>> type of the disk is changed.
>>
>> Signed-off-by: Daniel Henrique Barboza <danielhb at linux.vnet.ibm.com>
>> ---
>> tests/test_mockmodel.py | 62
>> ++++++++++++++++++++++++++++++++++++++++---------
>> tests/test_model.py | 14 +++++++++++
>> 2 files changed, 65 insertions(+), 11 deletions(-)
>>
>> diff --git a/tests/test_mockmodel.py b/tests/test_mockmodel.py
>> index 7319531..5d9fdab 100644
>> --- a/tests/test_mockmodel.py
>> +++ b/tests/test_mockmodel.py
>> @@ -90,12 +90,15 @@ class MockModelTests(unittest.TestCase):
>> else:
>> self.fail("Expected exception not raised")
>>
>> - def test_template_cpu_info(self):
>> + def _create_default_template(self):
>> # Create default template
>> req = json.dumps({'name': 'test', 'cdrom': fake_iso})
>> resp = request(host, ssl_port, '/templates', req, 'POST')
>> rsp_body = resp.read()
>> - template_data = json.loads(rsp_body)
>> + return json.loads(rsp_body)
>> +
>> + def test_template_cpu_info(self):
>> + template_data = self._get_default_template()
>> # GET of cpu_info will be {}
>> cpu_info = template_data['cpu_info']
>> self.assertEquals(cpu_info, {})
>> @@ -103,16 +106,53 @@ class MockModelTests(unittest.TestCase):
>>
>> # Update topology
>> # GET of cpu_info will contain 'topology'
>> - req = json.dumps({'cpu_info': {'topology': {'sockets': 1,
>> - 'cores': 1,
>> - 'threads': 1}}})
>> - resp = request(host, ssl_port, '/templates/test', req, 'PUT')
>> + cpu_info_data = {'cpu_info': {'topology': {'sockets': 1,
>> + 'cores': 1,
>> + 'threads': 1}}}
>> + updated_template, _ = self._get_PUT_response('/templates/test',
>> + cpu_info_data)
>> + self.assertEquals(updated_template['cpu_info'],
>> + cpu_info_data['cpu_info'])
>> +
>> + def test_template_update_disk_type(self):
>> + def _get_default_disk_data(disk_type):
>> + return {'disks': [{'index': 0, 'type': disk_type,
>> 'size': 10}]}
>> +
>> + template = self._create_default_template()
>> + # Default template is created with 1 disk without any declared
>> + # type.
>> + disk_data = template['disks']
>> + self.assertEquals(disk_data, [{'index': 0, 'size': 10}])
>> +
>> + # For all supported types, edit the template and check if
>> + # the change was made.
>> + disk_types = ['bochs', 'cloop', 'cow', 'dmg', 'qcow', 'qcow2',
>> + 'qed', 'raw', 'vmdk', 'vpc']
>> + for disk_type in disk_types:
>> + disk_data = _get_default_disk_data(disk_type)
>> + updated_template, resp_code = \
>> + self._get_PUT_response('/templates/test',
>> + disk_data)
>> + self.assertEquals(200, resp_code)
>
> Use GET request to confirm the changes were really done.
Ok. Just want to point it out that I've used the existing test
"test_template_cpu_info" as an
template for the new test, therefore the existing method need fixing as
well. I can update
both.
>
>> + self.assertEquals(updated_template['disks'], disk_data['disks'])
>> +
>> + # Check Bad Request when type is invalid
>> + bad_disk_data = _get_default_disk_data('invalid_disk_type')
>> + _, resp_code = self._get_PUT_response('/templates/test',
>> bad_disk_data)
>> + self.assertEquals(400, resp_code)
>> +
>> + def _get_default_template(self):
>> + # Create default template
>> + req = json.dumps({'name': 'test', 'cdrom': fake_iso})
>> + resp = request(host, ssl_port, '/templates', req, 'POST')
>> rsp_body = resp.read()
>> - template_data = json.loads(rsp_body)
>> - cpu_info = template_data['cpu_info']
>> - self.assertEquals(cpu_info, {'topology': {'sockets': 1,
>> - 'cores': 1,
>> - 'threads': 1}})
>> + return json.loads(rsp_body)
>> +
>> + def _get_PUT_response(self, url, data):
>> + req = json.dumps(data)
>> + resp = request(host, ssl_port, url, req, 'PUT')
>> + rsp_body = resp.read()
>> + return json.loads(rsp_body), resp.status
>>
>> def test_screenshot_refresh(self):
>> # Create a VM
>> diff --git a/tests/test_model.py b/tests/test_model.py
>> index d9bbe9e..0ff1ea3 100644
>> --- a/tests/test_model.py
>> +++ b/tests/test_model.py
>> @@ -628,6 +628,20 @@ class ModelTests(unittest.TestCase):
>> self.assertRaises(InvalidParameter, inst.template_update,
>> 'test', params)
>>
>> + # For all supported types, edit the template and check if
>> + # the change was made.
>> + disk_types = ['bochs', 'cloop', 'cow', 'dmg', 'qcow',
>> 'qcow2',
>> + 'qed', 'raw', 'vmdk', 'vpc']
>> + for disk_type in disk_types:
>> + disk_data = {'disks': [{'index': 0, 'type': disk_type,
>> + 'size': 1}]}
>> + inst.template_update('test', disk_data)
>> + updated_template = inst.template_lookup('test')
>> + self.assertEquals(updated_template['disks'],
>> + disk_data['disks'])
>> + # Restore disk data to default value
>> + inst.template_update('test', {'disks': [{'index': 0,
>> 'size': 1}]})
>> +
>> args = {'name': pool,
>> 'path': path,
>> 'type': 'dir'}
>
More information about the Kimchi-devel
mailing list