summaryrefslogtreecommitdiff
path: root/nova/tests/unit/api/openstack/compute/test_aggregates.py
blob: 21d644f0bed338f162720bc74e28058f6228f018 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
# Copyright (c) 2012 Citrix Systems, Inc.
# All Rights Reserved.
#
#    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.

"""Tests for the aggregates admin api."""

from unittest import mock

from oslo_utils.fixture import uuidsentinel
from webob import exc

from nova.api.openstack import api_version_request
from nova.api.openstack.compute import aggregates as aggregates_v21
from nova.compute import api as compute_api
from nova import context
from nova import exception
from nova import objects
from nova.objects import base as obj_base
from nova import test
from nova.tests.unit.api.openstack import fakes


def _make_agg_obj(agg_dict):
    return objects.Aggregate(**agg_dict)


def _make_agg_list(agg_list):
    return objects.AggregateList(objects=[_make_agg_obj(a) for a in agg_list])


def _transform_aggregate_az(agg_dict):
    # the Aggregate object looks for availability_zone within metadata,
    # so if availability_zone is in the top-level dict, move it down into
    # metadata. We also have to delete the key from the top-level dict because
    # availability_zone is a read-only property on the Aggregate object
    md = agg_dict.get('metadata', {})
    if 'availability_zone' in agg_dict:
        md['availability_zone'] = agg_dict['availability_zone']
        del agg_dict['availability_zone']
        agg_dict['metadata'] = md
    return agg_dict


def _transform_aggregate_list_azs(agg_list):
    for agg_dict in agg_list:
        yield _transform_aggregate_az(agg_dict)


AGGREGATE_LIST = [
        {"name": "aggregate1", "id": "1",
            "metadata": {"availability_zone": "nova1"}},
        {"name": "aggregate2", "id": "2",
            "metadata": {"availability_zone": "nova1"}},
        {"name": "aggregate3", "id": "3",
            "metadata": {"availability_zone": "nova2"}},
        {"name": "aggregate1", "id": "4",
            "metadata": {"availability_zone": "nova1"}}]
AGGREGATE_LIST = _make_agg_list(AGGREGATE_LIST)

AGGREGATE = {"name": "aggregate1",
                  "id": "1",
                  "metadata": {"foo": "bar",
                               "availability_zone": "nova1"},
                  "hosts": ["host1", "host2"]}
AGGREGATE = _make_agg_obj(AGGREGATE)

FORMATTED_AGGREGATE = {"name": "aggregate1",
                  "id": "1",
                  "metadata": {"availability_zone": "nova1"}}
FORMATTED_AGGREGATE = _make_agg_obj(FORMATTED_AGGREGATE)


class FakeRequest(object):
    environ = {"nova.context": context.get_admin_context()}


class AggregateTestCaseV21(test.NoDBTestCase):
    """Test Case for aggregates admin api."""

    add_host = 'self.controller._add_host'
    remove_host = 'self.controller._remove_host'
    set_metadata = 'self.controller._set_metadata'
    bad_request = exception.ValidationError

    def _set_up(self):
        self.controller = aggregates_v21.AggregateController()
        self.req = fakes.HTTPRequest.blank('/v2/os-aggregates',
                                           use_admin_context=True)
        self.user_req = fakes.HTTPRequest.blank('/v2/os-aggregates')
        self.context = self.req.environ['nova.context']

    def setUp(self):
        super(AggregateTestCaseV21, self).setUp()
        self._set_up()

    def test_index(self):
        def _list_aggregates(context):
            if context is None:
                raise Exception()
            return AGGREGATE_LIST

        with mock.patch.object(self.controller.api, 'get_aggregate_list',
                               side_effect=_list_aggregates) as mock_list:
            result = self.controller.index(self.req)
            result = _transform_aggregate_list_azs(result['aggregates'])
            self._assert_agg_data(AGGREGATE_LIST, _make_agg_list(result))
            self.assertTrue(mock_list.called)

    def test_create(self):
        with mock.patch.object(self.controller.api, 'create_aggregate',
                               return_value=AGGREGATE) as mock_create:
            result = self.controller.create(self.req,
                body={"aggregate": {"name": "test",
                                    "availability_zone": "nova1"}})
            result = _transform_aggregate_az(result['aggregate'])
            self._assert_agg_data(FORMATTED_AGGREGATE, _make_agg_obj(result))
            mock_create.assert_called_once_with(self.context, 'test', 'nova1')

    def test_create_with_duplicate_aggregate_name(self):
        side_effect = exception.AggregateNameExists(aggregate_name="test")
        with mock.patch.object(self.controller.api, 'create_aggregate',
                               side_effect=side_effect) as mock_create:
            self.assertRaises(exc.HTTPConflict, self.controller.create,
                self.req, body={"aggregate": {"name": "test",
                                              "availability_zone": "nova1"}})
            mock_create.assert_called_once_with(self.context, 'test', 'nova1')

    @mock.patch.object(compute_api.AggregateAPI, 'create_aggregate')
    def test_create_with_unmigrated_aggregates(self, mock_create_aggregate):
        mock_create_aggregate.side_effect = \
                exception.ObjectActionError(action='create',
                    reason='main database still contains aggregates')

        self.assertRaises(exc.HTTPConflict, self.controller.create,
                          self.req, body={"aggregate":
                                     {"name": "test",
                                      "availability_zone": "nova1"}})

    def test_create_with_incorrect_availability_zone(self):
        side_effect = exception.InvalidAggregateAction(
                        action='create_aggregate',
                        aggregate_id="'N/A'",
                        reason='invalid zone')
        with mock.patch.object(self.controller.api, 'create_aggregate',
                               side_effect=side_effect) as mock_create:
            self.assertRaises(exc.HTTPBadRequest, self.controller.create,
                self.req,
                body={"aggregate": {"name": "test",
                                    "availability_zone": "nova_bad"}})
            mock_create.assert_called_once_with(self.context, 'test',
                                                'nova_bad')

    def test_create_with_no_aggregate(self):
        self.assertRaises(self.bad_request, self.controller.create,
                          self.req, body={"foo":
                                     {"name": "test",
                                      "availability_zone": "nova1"}})

    def test_create_with_no_name(self):
        self.assertRaises(self.bad_request, self.controller.create,
                          self.req, body={"aggregate":
                                     {"foo": "test",
                                      "availability_zone": "nova1"}})

    def test_create_name_with_leading_trailing_spaces(self):
        self.assertRaises(self.bad_request, self.controller.create,
                          self.req, body={"aggregate":
                                          {"name": "  test  ",
                                           "availability_zone": "nova1"}})

    def test_create_name_with_leading_trailing_spaces_compat_mode(self):

        def fake_mock_aggs(context, name, az):
            self.assertEqual('test', name)
            return AGGREGATE

        with mock.patch.object(compute_api.AggregateAPI,
                               'create_aggregate') as mock_aggs:
            mock_aggs.side_effect = fake_mock_aggs
            self.req.set_legacy_v2()
            self.controller.create(self.req,
                                   body={"aggregate":
                                         {"name": "  test  ",
                                          "availability_zone": "nova1"}})

    def test_create_with_no_availability_zone(self):
        with mock.patch.object(self.controller.api, 'create_aggregate',
                               return_value=AGGREGATE) as mock_create:
            result = self.controller.create(self.req,
                body={"aggregate": {"name": "test"}})

            result = _transform_aggregate_az(result['aggregate'])
            self._assert_agg_data(FORMATTED_AGGREGATE, _make_agg_obj(result))
            mock_create.assert_called_once_with(self.context, 'test', None)

    def test_create_with_null_name(self):
        self.assertRaises(self.bad_request, self.controller.create,
                          self.req, body={"aggregate":
                                     {"name": "",
                                      "availability_zone": "nova1"}})

    def test_create_with_name_too_long(self):
        self.assertRaises(self.bad_request, self.controller.create,
                          self.req, body={"aggregate":
                                     {"name": "x" * 256,
                                      "availability_zone": "nova1"}})

    def test_create_with_availability_zone_too_long(self):
        self.assertRaises(self.bad_request, self.controller.create,
                          self.req, body={"aggregate":
                                     {"name": "test",
                                      "availability_zone": "x" * 256}})

    def test_create_with_availability_zone_invalid(self):
        self.assertRaises(self.bad_request, self.controller.create,
                          self.req, body={"aggregate":
                                     {"name": "test",
                                      "availability_zone": "bad:az"}})

    def test_create_availability_zone_with_leading_trailing_spaces(self):
        self.assertRaises(self.bad_request, self.controller.create,
                          self.req, body={"aggregate":
                                          {"name": "test",
                                           "availability_zone": "  nova1  "}})

    def test_create_availability_zone_with_leading_trailing_spaces_compat_mode(
            self):

        def fake_mock_aggs(context, name, az):
            self.assertEqual('nova1', az)
            return AGGREGATE

        with mock.patch.object(compute_api.AggregateAPI,
                               'create_aggregate') as mock_aggs:
            mock_aggs.side_effect = fake_mock_aggs
            self.req.set_legacy_v2()
            self.controller.create(self.req,
                                   body={"aggregate":
                                         {"name": "test",
                                          "availability_zone": "  nova1  "}})

    def test_create_with_empty_availability_zone(self):
        self.assertRaises(self.bad_request, self.controller.create,
                          self.req, body={"aggregate":
                                     {"name": "test",
                                      "availability_zone": ""}})

    @mock.patch('nova.compute.api.AggregateAPI.create_aggregate')
    def test_create_with_none_availability_zone(self, mock_create_agg):
        mock_create_agg.return_value = objects.Aggregate(
            self.context,
            name='test',
            uuid=uuidsentinel.aggregate,
            hosts=[],
            metadata={})
        body = {"aggregate": {"name": "test",
                              "availability_zone": None}}
        result = self.controller.create(self.req, body=body)
        mock_create_agg.assert_called_once_with(self.context, 'test', None)
        self.assertEqual(result['aggregate']['name'], 'test')

    def test_create_with_extra_invalid_arg(self):
        self.assertRaises(self.bad_request, self.controller.create,
                          self.req, body={"name": "test",
                                          "availability_zone": "nova1",
                                          "foo": 'bar'})

    def test_show(self):
        with mock.patch.object(self.controller.api, 'get_aggregate',
                               return_value=AGGREGATE) as mock_get:
            aggregate = self.controller.show(self.req, "1")
            aggregate = _transform_aggregate_az(aggregate['aggregate'])
            self._assert_agg_data(AGGREGATE, _make_agg_obj(aggregate))
            mock_get.assert_called_once_with(self.context, '1')

    def test_show_with_bad_aggregate(self):
        side_effect = exception.AggregateNotFound(aggregate_id='2')
        with mock.patch.object(self.controller.api, 'get_aggregate',
                               side_effect=side_effect) as mock_get:
            self.assertRaises(exc.HTTPNotFound, self.controller.show,
                              self.req, "2")
            mock_get.assert_called_once_with(self.context, '2')

    def test_show_with_invalid_id(self):
        self.assertRaises(exc.HTTPBadRequest, self.controller.show,
                          self.req, 'foo')

    def test_update(self):
        body = {"aggregate": {"name": "new_name",
                              "availability_zone": "nova1"}}
        with mock.patch.object(self.controller.api, 'update_aggregate',
                               return_value=AGGREGATE) as mock_update:
            result = self.controller.update(self.req, "1", body=body)

            result = _transform_aggregate_az(result['aggregate'])
            self._assert_agg_data(AGGREGATE, _make_agg_obj(result))
            mock_update.assert_called_once_with(self.context, '1',
                                                body["aggregate"])

    def test_update_with_only_name(self):
        body = {"aggregate": {"name": "new_name"}}
        with mock.patch.object(self.controller.api, 'update_aggregate',
                               return_value=AGGREGATE) as mock_update:
            result = self.controller.update(self.req, "1", body=body)

            result = _transform_aggregate_az(result['aggregate'])
            self._assert_agg_data(AGGREGATE, _make_agg_obj(result))
            mock_update.assert_called_once_with(self.context, '1',
                                                body["aggregate"])

    def test_update_with_only_availability_zone(self):
        body = {"aggregate": {"availability_zone": "nova1"}}
        with mock.patch.object(self.controller.api, 'update_aggregate',
                               return_value=AGGREGATE) as mock_update:
            result = self.controller.update(self.req, "1", body=body)

            result = _transform_aggregate_az(result['aggregate'])
            self._assert_agg_data(AGGREGATE, _make_agg_obj(result))
            mock_update.assert_called_once_with(self.context, '1',
                                                body["aggregate"])

    def test_update_with_no_updates(self):
        test_metadata = {"aggregate": {}}
        self.assertRaises(self.bad_request, self.controller.update,
                          self.req, "2", body=test_metadata)

    def test_update_with_no_update_key(self):
        test_metadata = {"asdf": {}}
        self.assertRaises(self.bad_request, self.controller.update,
                          self.req, "2", body=test_metadata)

    def test_update_with_wrong_updates(self):
        test_metadata = {"aggregate": {"status": "disable",
                                     "foo": "bar"}}
        self.assertRaises(self.bad_request, self.controller.update,
                          self.req, "2", body=test_metadata)

    def test_update_with_null_name(self):
        test_metadata = {"aggregate": {"name": ""}}
        self.assertRaises(self.bad_request, self.controller.update,
                          self.req, "2", body=test_metadata)

    def test_update_with_name_too_long(self):
        test_metadata = {"aggregate": {"name": "x" * 256}}
        self.assertRaises(self.bad_request, self.controller.update,
                          self.req, "2", body=test_metadata)

    def test_update_with_availability_zone_too_long(self):
        test_metadata = {"aggregate": {"availability_zone": "x" * 256}}
        self.assertRaises(self.bad_request, self.controller.update,
                          self.req, "2", body=test_metadata)

    def test_update_with_availability_zone_invalid(self):
        test_metadata = {"aggregate": {"availability_zone": "bad:az"}}
        self.assertRaises(self.bad_request, self.controller.update,
                          self.req, "2", body=test_metadata)

    def test_update_with_empty_availability_zone(self):
        test_metadata = {"aggregate": {"availability_zone": ""}}
        self.assertRaises(self.bad_request, self.controller.update,
                          self.req, "2", body=test_metadata)

    @mock.patch('nova.compute.api.AggregateAPI.update_aggregate')
    def test_update_with_none_availability_zone(self, mock_update_agg):
        agg_id = 173
        mock_update_agg.return_value = objects.Aggregate(self.context,
                                                         name='test',
                                                         uuid=uuidsentinel.agg,
                                                         id=agg_id,
                                                         hosts=[],
                                                         metadata={})
        body = {"aggregate": {"name": "test",
                              "availability_zone": None}}
        result = self.controller.update(self.req, agg_id, body=body)
        mock_update_agg.assert_called_once_with(self.context, agg_id,
                                                body['aggregate'])
        self.assertEqual(result['aggregate']['name'], 'test')

    def test_update_with_bad_aggregate(self):
        body = {"aggregate": {"name": "test_name"}}
        side_effect = exception.AggregateNotFound(aggregate_id=2)

        with mock.patch.object(self.controller.api, 'update_aggregate',
                               side_effect=side_effect) as mock_update:
            self.assertRaises(exc.HTTPNotFound, self.controller.update,
                self.req, "2", body=body)
            mock_update.assert_called_once_with(self.context, '2',
                                                body["aggregate"])

    def test_update_with_invalid_id(self):
        body = {"aggregate": {"name": "test_name"}}
        self.assertRaises(exc.HTTPBadRequest, self.controller.update,
                          self.req, 'foo', body=body)

    def test_update_with_duplicated_name(self):
        body = {"aggregate": {"name": "test_name"}}
        side_effect = exception.AggregateNameExists(aggregate_name="test_name")

        with mock.patch.object(self.controller.api, 'update_aggregate',
                               side_effect=side_effect) as mock_update:
            self.assertRaises(exc.HTTPConflict, self.controller.update,
                self.req, "2", body=body)
            mock_update.assert_called_once_with(self.context, '2',
                                                body["aggregate"])

    def test_invalid_action(self):
        body = {"append_host": {"host": "host1"}}
        self.assertRaises(self.bad_request,
                          eval(self.add_host), self.req, "1", body=body)

    def test_update_with_invalid_action(self):
        with mock.patch.object(self.controller.api, "update_aggregate",
            side_effect=exception.InvalidAggregateAction(
                action='invalid', aggregate_id='1', reason= "not empty")):
            body = {"aggregate": {"availability_zone": "nova"}}
            self.assertRaises(exc.HTTPBadRequest, self.controller.update,
                              self.req, "1", body=body)

    def test_add_host(self):
        with mock.patch.object(self.controller.api, 'add_host_to_aggregate',
                               return_value=AGGREGATE) as mock_add:
            aggregate = eval(self.add_host)(self.req, "1",
                                            body={"add_host": {"host":
                                                               "host1"}})

            aggregate = _transform_aggregate_az(aggregate['aggregate'])
            self._assert_agg_data(AGGREGATE, _make_agg_obj(aggregate))
            mock_add.assert_called_once_with(self.context, "1", "host1")

    def test_add_host_with_already_added_host(self):
        side_effect = exception.AggregateHostExists(aggregate_id="1",
                                                    host="host1")
        with mock.patch.object(self.controller.api, 'add_host_to_aggregate',
                               side_effect=side_effect) as mock_add:
            self.assertRaises(exc.HTTPConflict, eval(self.add_host),
                              self.req, "1",
                              body={"add_host": {"host": "host1"}})
            mock_add.assert_called_once_with(self.context, "1", "host1")

    def test_add_host_with_bad_aggregate(self):
        side_effect = exception.AggregateNotFound(
            aggregate_id="2")
        with mock.patch.object(self.controller.api, 'add_host_to_aggregate',
                               side_effect=side_effect) as mock_add:
            self.assertRaises(exc.HTTPNotFound, eval(self.add_host),
                              self.req, "2",
                              body={"add_host": {"host": "host1"}})
            mock_add.assert_called_once_with(self.context, "2",
                                             "host1")

    def test_add_host_with_invalid_id(self):
        body = {"add_host": {"host": "host1"}}
        self.assertRaises(exc.HTTPBadRequest, eval(self.add_host),
                          self.req, 'foo', body=body)

    def test_add_host_with_bad_host(self):
        side_effect = exception.ComputeHostNotFound(host="bogus_host")
        with mock.patch.object(self.controller.api, 'add_host_to_aggregate',
                               side_effect=side_effect) as mock_add:
            self.assertRaises(exc.HTTPNotFound, eval(self.add_host),
                              self.req, "1",
                              body={"add_host": {"host": "bogus_host"}})
            mock_add.assert_called_once_with(self.context, "1", "bogus_host")

    def test_add_host_with_missing_host(self):
        self.assertRaises(self.bad_request, eval(self.add_host),
                self.req, "1", body={"add_host": {"asdf": "asdf"}})

    def test_add_host_with_invalid_format_host(self):
        self.assertRaises(self.bad_request, eval(self.add_host),
                self.req, "1", body={"add_host": {"host": "a" * 300}})

    def test_add_host_with_invalid_name_host(self):
        self.assertRaises(self.bad_request, eval(self.add_host),
                self.req, "1", body={"add_host": {"host": "bad:host"}})

    def test_add_host_with_multiple_hosts(self):
        self.assertRaises(self.bad_request, eval(self.add_host),
                self.req, "1", body={"add_host": {"host": ["host1", "host2"]}})

    def test_add_host_raises_key_error(self):
        with mock.patch.object(self.controller.api, 'add_host_to_aggregate',
                               side_effect=KeyError) as mock_add:
            self.assertRaises(exc.HTTPInternalServerError,
                              eval(self.add_host), self.req, "1",
                              body={"add_host": {"host": "host1"}})
            mock_add.assert_called_once_with(self.context, "1", "host1")

    def test_add_host_with_invalid_request(self):
        self.assertRaises(self.bad_request, eval(self.add_host),
                self.req, "1", body={"add_host": "1"})

    def test_add_host_with_non_string(self):
        self.assertRaises(self.bad_request, eval(self.add_host),
                self.req, "1", body={"add_host": {"host": 1}})

    def test_remove_host(self):
        return_value = _make_agg_obj({'metadata': {}})
        with mock.patch.object(self.controller.api,
                               'remove_host_from_aggregate',
                               return_value=return_value) as mock_rem:
            eval(self.remove_host)(self.req, "1",
                                   body={"remove_host": {"host": "host1"}})
            mock_rem.assert_called_once_with(self.context, "1", "host1")

    def test_remove_host_with_bad_aggregate(self):
        side_effect = exception.AggregateNotFound(
            aggregate_id="2")
        with mock.patch.object(self.controller.api,
                               'remove_host_from_aggregate',
                               side_effect=side_effect) as mock_rem:
            self.assertRaises(exc.HTTPNotFound, eval(self.remove_host),
                              self.req, "2",
                              body={"remove_host": {"host": "host1"}})
            mock_rem.assert_called_once_with(self.context, "2",
                                             "host1")

    def test_remove_host_with_invalid_id(self):
        body = {"remove_host": {"host": "host1"}}
        self.assertRaises(exc.HTTPBadRequest, eval(self.remove_host),
                          self.req, 'foo', body=body)

    def test_remove_host_with_host_not_in_aggregate(self):
        side_effect = exception.AggregateHostNotFound(aggregate_id="1",
                                                      host="host1")
        with mock.patch.object(self.controller.api,
                               'remove_host_from_aggregate',
                               side_effect=side_effect) as mock_rem:
            self.assertRaises(exc.HTTPNotFound, eval(self.remove_host),
                              self.req, "1",
                              body={"remove_host": {"host": "host1"}})
            mock_rem.assert_called_once_with(self.context, "1", "host1")

    def test_remove_host_with_bad_host(self):
        side_effect = exception.ComputeHostNotFound(host="bogushost")
        with mock.patch.object(self.controller.api,
                               'remove_host_from_aggregate',
                               side_effect=side_effect) as mock_rem:
            self.assertRaises(exc.HTTPNotFound, eval(self.remove_host),
                self.req, "1", body={"remove_host": {"host": "bogushost"}})
            mock_rem.assert_called_once_with(self.context, "1", "bogushost")
        self.assertIn('Failed to remove host bogushost from aggregate 1. '
                      'Error: Compute host bogushost could not be found.',
                      self.stdlog.logger.output)

    def test_remove_host_with_missing_host(self):
        self.assertRaises(self.bad_request, eval(self.remove_host),
                self.req, "1", body={"asdf": "asdf"})

    def test_remove_host_with_multiple_hosts(self):
        self.assertRaises(self.bad_request, eval(self.remove_host),
                self.req, "1", body={"remove_host": {"host":
                                                     ["host1", "host2"]}})

    def test_remove_host_with_extra_param(self):
        self.assertRaises(self.bad_request, eval(self.remove_host),
                self.req, "1", body={"remove_host": {"asdf": "asdf",
                                                     "host": "asdf"}})

    def test_remove_host_with_invalid_request(self):
        self.assertRaises(self.bad_request,
                          eval(self.remove_host),
                self.req, "1", body={"remove_host": "1"})

    def test_remove_host_with_missing_host_empty(self):
        self.assertRaises(self.bad_request,
                          eval(self.remove_host),
                self.req, "1", body={"remove_host": {}})

    def test_remove_host_resource_provider_update_conflict(self):
        """Tests that ResourceProviderUpdateConflict is handled as a 409."""
        side_effect = exception.ResourceProviderUpdateConflict(
            uuid=uuidsentinel.provider_id, generation=1, error='try again!')
        with mock.patch.object(self.controller.api,
                               'remove_host_from_aggregate',
                               side_effect=side_effect) as mock_rem:
            self.assertRaises(exc.HTTPConflict, eval(self.remove_host),
                              self.req, "1",
                              body={"remove_host": {"host": "bogushost"}})
            mock_rem.assert_called_once_with(self.context, "1", "bogushost")
        self.assertIn('Failed to remove host bogushost from aggregate 1. '
                      'Error: A conflict was encountered attempting to update '
                      'resource provider', self.stdlog.logger.output)

    def test_set_metadata(self):
        body = {"set_metadata": {"metadata": {"foo": "bar"}}}
        with mock.patch.object(self.controller.api,
                               'update_aggregate_metadata',
                               return_value=AGGREGATE) as mock_update:
            result = eval(self.set_metadata)(self.req, "1", body=body)
            result = _transform_aggregate_az(result['aggregate'])
            self._assert_agg_data(AGGREGATE, _make_agg_obj(result))
            mock_update.assert_called_once_with(self.context, "1",
                body["set_metadata"]['metadata'])

    def test_set_metadata_delete(self):
        body = {"set_metadata": {"metadata": {"foo": None}}}

        with mock.patch.object(self.controller.api,
                               'update_aggregate_metadata') as mocked:
            mocked.return_value = AGGREGATE
            result = eval(self.set_metadata)(self.req, "1", body=body)

        result = _transform_aggregate_az(result['aggregate'])
        self._assert_agg_data(AGGREGATE, _make_agg_obj(result))
        mocked.assert_called_once_with(self.context, "1",
                                       body["set_metadata"]["metadata"])

    def test_set_metadata_with_bad_aggregate(self):
        body = {"set_metadata": {"metadata": {"foo": "bar"}}}
        side_effect = exception.AggregateNotFound(aggregate_id="2")

        with mock.patch.object(self.controller.api,
                               'update_aggregate_metadata',
                               side_effect=side_effect) as mock_update:
            self.assertRaises(exc.HTTPNotFound, eval(self.set_metadata),
                self.req, "2", body=body)
            mock_update.assert_called_once_with(self.context, "2",
                body["set_metadata"]['metadata'])

    def test_set_metadata_with_invalid_id(self):
        body = {"set_metadata": {"metadata": {"foo": "bar"}}}
        self.assertRaises(exc.HTTPBadRequest, eval(self.set_metadata),
                          self.req, 'foo', body=body)

    def test_set_metadata_with_missing_metadata(self):
        body = {"asdf": {"foo": "bar"}}
        self.assertRaises(self.bad_request, eval(self.set_metadata),
                          self.req, "1", body=body)

    def test_set_metadata_with_extra_params(self):
        body = {"metadata": {"foo": "bar"}, "asdf": {"foo": "bar"}}
        self.assertRaises(self.bad_request, eval(self.set_metadata),
                          self.req, "1", body=body)

    def test_set_metadata_without_dict(self):
        body = {"set_metadata": {'metadata': 1}}
        self.assertRaises(self.bad_request, eval(self.set_metadata),
                          self.req, "1", body=body)

    def test_set_metadata_with_empty_key(self):
        body = {"set_metadata": {"metadata": {"": "value"}}}
        self.assertRaises(self.bad_request, eval(self.set_metadata),
                          self.req, "1", body=body)

    def test_set_metadata_with_key_too_long(self):
        body = {"set_metadata": {"metadata": {"x" * 256: "value"}}}
        self.assertRaises(self.bad_request, eval(self.set_metadata),
                          self.req, "1", body=body)

    def test_set_metadata_with_value_too_long(self):
        body = {"set_metadata": {"metadata": {"key": "x" * 256}}}
        self.assertRaises(self.bad_request, eval(self.set_metadata),
                          self.req, "1", body=body)

    def test_set_metadata_with_string(self):
        body = {"set_metadata": {"metadata": "test"}}
        self.assertRaises(self.bad_request, eval(self.set_metadata),
                          self.req, "1", body=body)

    def test_delete_aggregate(self):
        with mock.patch.object(self.controller.api,
                               'delete_aggregate') as mock_del:
            self.controller.delete(self.req, "1")
            mock_del.assert_called_once_with(self.context, "1")

    def test_delete_aggregate_with_bad_aggregate(self):
        side_effect = exception.AggregateNotFound(
            aggregate_id="2")
        with mock.patch.object(self.controller.api, 'delete_aggregate',
                               side_effect=side_effect) as mock_del:
            self.assertRaises(exc.HTTPNotFound, self.controller.delete,
                self.req, "2")
            mock_del.assert_called_once_with(self.context, "2")

    def test_delete_with_invalid_id(self):
        self.assertRaises(exc.HTTPBadRequest, self.controller.delete,
                          self.req, 'foo')

    def test_delete_aggregate_with_host(self):
        with mock.patch.object(self.controller.api, "delete_aggregate",
                               side_effect=exception.InvalidAggregateAction(
                               action="delete", aggregate_id="2",
                               reason="not empty")):
            self.assertRaises(exc.HTTPBadRequest,
                              self.controller.delete,
                              self.req, "2")

    def test_marshall_aggregate(self):
        # _marshall_aggregate() just basically turns the aggregate returned
        # from the AggregateAPI into a dict, so this tests that transform.
        # We would expect the dictionary that comes out is the same one
        # that we pump into the aggregate object in the first place
        agg = {'name': 'aggregate1',
               'id': 1, 'uuid': uuidsentinel.aggregate,
               'metadata': {'foo': 'bar', 'availability_zone': 'nova'},
               'hosts': ['host1', 'host2']}
        agg_obj = _make_agg_obj(agg)

        # _marshall_aggregate() puts all fields and obj_extra_fields in the
        # top-level dict, so we need to put availability_zone at the top also
        agg['availability_zone'] = 'nova'

        avr_v240 = api_version_request.APIVersionRequest("2.40")
        avr_v241 = api_version_request.APIVersionRequest("2.41")

        req = mock.MagicMock(api_version_request=avr_v241)
        marshalled_agg = self.controller._marshall_aggregate(req, agg_obj)

        self.assertEqual(agg, marshalled_agg['aggregate'])

        req = mock.MagicMock(api_version_request=avr_v240)
        marshalled_agg = self.controller._marshall_aggregate(req, agg_obj)

        # UUID isn't in microversion 2.40 and before
        del agg['uuid']
        self.assertEqual(agg, marshalled_agg['aggregate'])

    def _assert_agg_data(self, expected, actual):
        self.assertTrue(obj_base.obj_equal_prims(expected, actual),
                        "The aggregate objects were not equal")

    def test_images_with_invalid_id(self):
        body = {'cache': [{'id': uuidsentinel.cache}]}
        req = fakes.HTTPRequest.blank('/v2/os-aggregates',
                                      use_admin_context=True,
                                      version='2.81')
        self.assertRaises(exc.HTTPBadRequest, self.controller.images,
                          req, 'foo', body=body)