From 757edf629c23304d05656793792a09618f2236aa Mon Sep 17 00:00:00 2001 From: Feng Shengqin Date: Tue, 18 Oct 2016 20:08:45 +0800 Subject: [PATCH] add some tests for cluster and clustertemplate api Change-Id: I9d08912aeb023dfc9cfeb7a18f9d8176712a6ead --- .../unit/api/controllers/v1/test_cluster.py | 55 +++++++++++++++++++ .../controllers/v1/test_cluster_template.py | 28 ++++++++++ 2 files changed, 83 insertions(+) diff --git a/magnum/tests/unit/api/controllers/v1/test_cluster.py b/magnum/tests/unit/api/controllers/v1/test_cluster.py index 9eb84188c1..c5e343e7e5 100644 --- a/magnum/tests/unit/api/controllers/v1/test_cluster.py +++ b/magnum/tests/unit/api/controllers/v1/test_cluster.py @@ -16,6 +16,7 @@ import mock from oslo_config import cfg from oslo_utils import timeutils from oslo_utils import uuidutils +from wsme import types as wtypes from magnum.api import attr_validator from magnum.api.controllers.v1 import cluster as api_cluster @@ -39,6 +40,15 @@ class TestClusterObject(base.TestCase): self.assertEqual(1, cluster.master_count) self.assertEqual(60, cluster.create_timeout) + # test unset value for cluster_template_id + cluster.cluster_template_id = wtypes.Unset + self.assertEqual(wtypes.Unset, cluster.cluster_template_id) + + # test backwards compatibility of bay fields with new objects + cluster_dict['create_timeout'] = 15 + cluster = api_cluster.Cluster(**cluster_dict) + self.assertEqual(15, cluster.create_timeout) + class TestListCluster(api_base.FunctionalTest): _cluster_attrs = ("name", "cluster_template_id", "node_count", "status", @@ -115,6 +125,22 @@ class TestListCluster(api_base.FunctionalTest): self.assertEqual('application/json', response.content_type) self.assertTrue(response.json['errors']) + def test_get_one_by_uuid(self): + temp_uuid = uuidutils.generate_uuid() + obj_utils.create_test_cluster(self.context, uuid=temp_uuid) + response = self.get_json( + '/clusters/%s' % temp_uuid) + self.assertEqual(temp_uuid, response['uuid']) + + def test_get_one_by_uuid_not_found(self): + temp_uuid = uuidutils.generate_uuid() + response = self.get_json( + '/clusters/%s' % temp_uuid, + expect_errors=True) + self.assertEqual(404, response.status_int) + self.assertEqual('application/json', response.content_type) + self.assertTrue(response.json['errors']) + def test_get_one_by_name_multiple_cluster(self): obj_utils.create_test_cluster(self.context, name='test_cluster', uuid=uuidutils.generate_uuid()) @@ -290,6 +316,19 @@ class TestPatch(api_base.FunctionalTest): self.assertEqual('application/json', response.content_type) self.assertEqual(404, response.status_code) + @mock.patch('oslo_utils.timeutils.utcnow') + def test_replace_ok_by_uuid_not_found(self, mock_utcnow): + uuid = uuidutils.generate_uuid() + test_time = datetime.datetime(2000, 1, 1, 0, 0) + mock_utcnow.return_value = test_time + + response = self.patch_json('/clusters/%s' % uuid, + [{'path': '/cluster_id', 'value': uuid, + 'op': 'replace'}], + expect_errors=True) + self.assertEqual('application/json', response.content_type) + self.assertEqual(404, response.status_code) + def test_replace_cluster_template_id_failed(self): cluster_template = obj_utils.create_test_cluster_template( self.context, @@ -486,6 +525,14 @@ class TestPost(api_base.FunctionalTest): self.assertEqual(400, response.status_int) self.assertTrue(response.json['errors']) + def test_create_cluster_with_non_existent_cluster_template_name(self): + modelname = 'notfound' + bdict = apiutils.cluster_post_data(cluster_template_id=modelname) + response = self.post_json('/clusters', bdict, expect_errors=True) + self.assertEqual('application/json', response.content_type) + self.assertEqual(400, response.status_int) + self.assertTrue(response.json['errors']) + def test_create_cluster_with_cluster_template_name(self): modelname = self.cluster_template.name bdict = apiutils.cluster_post_data(cluster_template_id=modelname) @@ -759,6 +806,14 @@ class TestPost(api_base.FunctionalTest): # Verify keypair from ClusterTemplate is used self.assertEqual('keypair1', cluster[0].keypair) + def test_create_cluster_with_multi_keypair_same_name(self): + bdict = apiutils.cluster_post_data() + self.mock_valid_os_res.side_effect = exception.Conflict('keypair2') + response = self.post_json('/clusters', bdict, expect_errors=True) + self.assertEqual('application/json', response.content_type) + self.assertTrue(self.mock_valid_os_res.called) + self.assertEqual(409, response.status_int) + class TestDelete(api_base.FunctionalTest): def setUp(self): diff --git a/magnum/tests/unit/api/controllers/v1/test_cluster_template.py b/magnum/tests/unit/api/controllers/v1/test_cluster_template.py index 277e640578..194fddeb7f 100644 --- a/magnum/tests/unit/api/controllers/v1/test_cluster_template.py +++ b/magnum/tests/unit/api/controllers/v1/test_cluster_template.py @@ -35,9 +35,21 @@ class TestClusterTemplateObject(base.TestCase): def test_cluster_template_init(self): cluster_template_dict = apiutils.cluster_template_post_data() del cluster_template_dict['image_id'] + del cluster_template_dict['registry_enabled'] + del cluster_template_dict['tls_disabled'] + del cluster_template_dict['public'] + del cluster_template_dict['server_type'] + del cluster_template_dict['master_lb_enabled'] + del cluster_template_dict['floating_ip_enabled'] cluster_template = api_cluster_template.ClusterTemplate( **cluster_template_dict) self.assertEqual(wtypes.Unset, cluster_template.image_id) + self.assertFalse(cluster_template.registry_enabled) + self.assertFalse(cluster_template.tls_disabled) + self.assertFalse(cluster_template.public) + self.assertEqual('vm', cluster_template.server_type) + self.assertFalse(cluster_template.master_lb_enabled) + self.assertTrue(cluster_template.floating_ip_enabled) class TestListClusterTemplate(api_base.FunctionalTest): @@ -86,6 +98,22 @@ class TestListClusterTemplate(api_base.FunctionalTest): self.assertEqual('application/json', response.content_type) self.assertTrue(response.json['errors']) + def test_get_one_by_uuid(self): + temp_uuid = uuidutils.generate_uuid() + obj_utils.create_test_cluster_template(self.context, uuid=temp_uuid) + response = self.get_json( + '/clustertemplates/%s' % temp_uuid) + self.assertEqual(temp_uuid, response['uuid']) + + def test_get_one_by_uuid_not_found(self): + temp_uuid = uuidutils.generate_uuid() + response = self.get_json( + '/clustertemplates/%s' % temp_uuid, + expect_errors=True) + self.assertEqual(404, response.status_int) + self.assertEqual('application/json', response.content_type) + self.assertTrue(response.json['errors']) + def test_get_one_by_name_multiple_cluster_template(self): obj_utils.create_test_cluster_template( self.context, name='test_clustertemplate',