blob: 850620689874aa0fbf839a56e83ae9e006837e2a [file] [log] [blame]
Lingxian Kongbf2d5172013-10-01 22:00:24 +08001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2013 Huawei Technologies Co.,LTD.
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
18from tempest.api.compute import base
19from tempest.common import tempest_fixtures as fixtures
20from tempest.common.utils import data_utils
21from tempest import exceptions
22from tempest.test import attr
23
24
25class AggregatesAdminNegativeTestJSON(base.BaseV2ComputeAdminTest):
26
27 """
28 Tests Aggregates API that require admin privileges
29 """
30
31 _interface = 'json'
32
33 @classmethod
34 def setUpClass(cls):
35 super(AggregatesAdminNegativeTestJSON, cls).setUpClass()
36 cls.client = cls.os_adm.aggregates_client
37 cls.user_client = cls.aggregates_client
38 cls.aggregate_name_prefix = 'test_aggregate_'
39 cls.az_name_prefix = 'test_az_'
40
41 resp, hosts_all = cls.os_adm.hosts_client.list_hosts()
42 hosts = map(lambda x: x['host_name'],
43 filter(lambda y: y['service'] == 'compute', hosts_all))
44 cls.host = hosts[0]
45
46 @attr(type=['negative', 'gate'])
47 def test_aggregate_create_as_user(self):
48 # Regular user is not allowed to create an aggregate.
49 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
50 self.assertRaises(exceptions.Unauthorized,
51 self.user_client.create_aggregate,
52 aggregate_name)
53
54 @attr(type=['negative', 'gate'])
55 def test_aggregate_create_aggregate_name_length_less_than_1(self):
56 # the length of aggregate name should >= 1 and <=255
57 self.assertRaises(exceptions.BadRequest,
58 self.client.create_aggregate,
59 '')
60
61 @attr(type=['negative', 'gate'])
62 def test_aggregate_create_aggregate_name_length_exceeds_255(self):
63 # the length of aggregate name should >= 1 and <=255
64 aggregate_name = 'a' * 256
65 self.assertRaises(exceptions.BadRequest,
66 self.client.create_aggregate,
67 aggregate_name)
68
69 @attr(type=['negative', 'gate'])
70 def test_aggregate_create_with_existent_aggregate_name(self):
71 # creating an aggregate with existent aggregate name is forbidden
72 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
73 resp, aggregate = self.client.create_aggregate(aggregate_name)
74 self.assertEqual(200, resp.status)
75 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
76
77 self.assertRaises(exceptions.Conflict,
78 self.client.create_aggregate,
79 aggregate_name)
80
81 @attr(type=['negative', 'gate'])
82 def test_aggregate_delete_as_user(self):
83 # Regular user is not allowed to delete an aggregate.
84 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
85 resp, aggregate = self.client.create_aggregate(aggregate_name)
86 self.assertEqual(200, resp.status)
87 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
88
89 self.assertRaises(exceptions.Unauthorized,
90 self.user_client.delete_aggregate,
91 aggregate['id'])
92
93 @attr(type=['negative', 'gate'])
94 def test_aggregate_list_as_user(self):
95 # Regular user is not allowed to list aggregates.
96 self.assertRaises(exceptions.Unauthorized,
97 self.user_client.list_aggregates)
98
99 @attr(type=['negative', 'gate'])
100 def test_aggregate_get_details_as_user(self):
101 # Regular user is not allowed to get aggregate details.
102 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
103 resp, aggregate = self.client.create_aggregate(aggregate_name)
104 self.assertEqual(200, resp.status)
105 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
106
107 self.assertRaises(exceptions.Unauthorized,
108 self.user_client.get_aggregate,
109 aggregate['id'])
110
111 @attr(type=['negative', 'gate'])
112 def test_aggregate_delete_with_invalid_id(self):
113 # Delete an aggregate with invalid id should raise exceptions.
114 self.assertRaises(exceptions.NotFound,
115 self.client.delete_aggregate, -1)
116
117 @attr(type=['negative', 'gate'])
118 def test_aggregate_get_details_with_invalid_id(self):
119 # Get aggregate details with invalid id should raise exceptions.
120 self.assertRaises(exceptions.NotFound,
121 self.client.get_aggregate, -1)
122
123 @attr(type=['negative', 'gate'])
124 def test_aggregate_add_non_exist_host(self):
125 # Adding a non-exist host to an aggregate should raise exceptions.
126 resp, hosts_all = self.os_adm.hosts_client.list_hosts()
127 hosts = map(lambda x: x['host_name'], hosts_all)
128 while True:
129 non_exist_host = data_utils.rand_name('nonexist_host_')
130 if non_exist_host not in hosts:
131 break
132
133 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
134 resp, aggregate = self.client.create_aggregate(aggregate_name)
135 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
136
137 self.assertRaises(exceptions.NotFound, self.client.add_host,
138 aggregate['id'], non_exist_host)
139
140 @attr(type=['negative', 'gate'])
141 def test_aggregate_add_host_as_user(self):
142 # Regular user is not allowed to add a host to an aggregate.
143 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
144 resp, aggregate = self.client.create_aggregate(aggregate_name)
145 self.assertEqual(200, resp.status)
146 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
147
148 self.assertRaises(exceptions.Unauthorized,
149 self.user_client.add_host,
150 aggregate['id'], self.host)
151
152 @attr(type=['negative', 'gate'])
153 def test_aggregate_add_existent_host(self):
154 self.useFixture(fixtures.LockFixture('availability_zone'))
155 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
156 resp, aggregate = self.client.create_aggregate(aggregate_name)
157 self.assertEqual(200, resp.status)
158 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
159
160 resp, body = self.client.add_host(aggregate['id'], self.host)
161 self.assertEqual(200, resp.status)
162 self.addCleanup(self.client.remove_host, aggregate['id'], self.host)
163
164 self.assertRaises(exceptions.Conflict, self.client.add_host,
165 aggregate['id'], self.host)
166
167 @attr(type=['negative', 'gate'])
168 def test_aggregate_remove_host_as_user(self):
169 # Regular user is not allowed to remove a host from an aggregate.
170 self.useFixture(fixtures.LockFixture('availability_zone'))
171 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
172 resp, aggregate = self.client.create_aggregate(aggregate_name)
173 self.assertEqual(200, resp.status)
174 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
175 resp, body = self.client.add_host(aggregate['id'], self.host)
176 self.assertEqual(200, resp.status)
177 self.addCleanup(self.client.remove_host, aggregate['id'], self.host)
178
179 self.assertRaises(exceptions.Unauthorized,
180 self.user_client.remove_host,
181 aggregate['id'], self.host)
182
183 @attr(type=['negative', 'gate'])
184 def test_aggregate_remove_nonexistent_host(self):
185 non_exist_host = data_utils.rand_name('nonexist_host_')
186 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
187 resp, aggregate = self.client.create_aggregate(aggregate_name)
188 self.assertEqual(200, resp.status)
189 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
190
191 self.assertRaises(exceptions.NotFound, self.client.remove_host,
192 aggregate['id'], non_exist_host)
193
194
195class AggregatesAdminNegativeTestXML(AggregatesAdminNegativeTestJSON):
196 _interface = 'xml'