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