summaryrefslogtreecommitdiff
path: root/nova/tests/functional/test_aggregates.py
blob: 508c435f547d40f9d93c324e13ac12357349d3ba (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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
#    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.

from oslo_utils.fixture import uuidsentinel as uuids

import nova.conf
from nova import test
from nova.tests import fixtures as nova_fixtures
from nova.tests.functional.api import client
from nova.tests.functional import fixtures as func_fixtures
from nova.tests.functional import integrated_helpers
from nova.tests.unit import utils as test_utils
from nova import utils

CONF = nova.conf.CONF


class AggregatesTest(integrated_helpers._IntegratedTestBase):
    api_major_version = 'v2'
    ADMIN_API = True

    def _add_hosts_to_aggregate(self):
        """List all compute services and add them all to an aggregate."""

        compute_services = [s for s in self.api.get_services()
                            if s['binary'] == 'nova-compute']
        agg = {'aggregate': {'name': 'test-aggregate'}}
        agg = self.api.post_aggregate(agg)
        for service in compute_services:
            self.api.add_host_to_aggregate(agg['id'], service['host'])
        self._test_aggregate = agg
        return len(compute_services)

    def test_add_hosts(self):
        # Default case with one compute, mapped for us
        self.assertEqual(1, self._add_hosts_to_aggregate())

    def test_add_unmapped_host(self):
        """Ensure that hosts without mappings are still found and added"""

        # Add another compute, but nuke its HostMapping
        self.start_service('compute', host='compute2')
        self.host_mappings['compute2'].destroy()
        self.assertEqual(2, self._add_hosts_to_aggregate())


class AggregatesV281Test(AggregatesTest):
    api_major_version = 'v2.1'
    microversion = '2.81'

    def setUp(self):
        self.flags(compute_driver='fake.FakeDriverWithCaching')
        super(AggregatesV281Test, self).setUp()

    def test_cache_images_on_aggregate(self):
        self._add_hosts_to_aggregate()
        agg = self._test_aggregate
        img = '155d900f-4e14-4e4c-a73d-069cbf4541e6'

        self.assertEqual(set(), self.compute.driver.cached_images)

        body = {'cache': [
            {'id': img},
        ]}
        self.api.api_post('/os-aggregates/%s/images' % agg['id'], body,
                          check_response_status=[202])

        self.assertEqual(set([img]), self.compute.driver.cached_images)

    def test_cache_images_on_aggregate_missing_image(self):
        agg = {'aggregate': {'name': 'test-aggregate'}}
        agg = self.api.post_aggregate(agg)
        # NOTE(danms): This image-id does not exist
        img = '155d900f-4e14-4e4c-a73d-069cbf4541e0'
        body = {'cache': [
            {'id': img},
        ]}
        self.api.api_post('/os-aggregates/%s/images' % agg['id'], body,
                          check_response_status=[400])

    def test_cache_images_on_missing_aggregate(self):
        img = '155d900f-4e14-4e4c-a73d-069cbf4541e6'
        body = {'cache': [
            {'id': img},
        ]}
        self.api.api_post('/os-aggregates/123/images', body,
                          check_response_status=[404])

    def test_cache_images_with_duplicates(self):
        agg = {'aggregate': {'name': 'test-aggregate'}}
        agg = self.api.post_aggregate(agg)
        img = '155d900f-4e14-4e4c-a73d-069cbf4541e6'
        body = {'cache': [
            {'id': img},
            {'id': img},
        ]}
        self.api.api_post('/os-aggregates/%i/images' % agg['id'], body,
                          check_response_status=[400])

    def test_cache_images_with_no_images(self):
        agg = {'aggregate': {'name': 'test-aggregate'}}
        agg = self.api.post_aggregate(agg)
        body = {'cache': []}
        self.api.api_post('/os-aggregates/%i/images' % agg['id'], body,
                          check_response_status=[400])

    def test_cache_images_with_additional_in_image(self):
        agg = {'aggregate': {'name': 'test-aggregate'}}
        agg = self.api.post_aggregate(agg)
        img = '155d900f-4e14-4e4c-a73d-069cbf4541e6'
        body = {'cache': [
            {'id': img, 'power': '1.21 gigawatts'},
        ]}
        self.api.api_post('/os-aggregates/%i/images' % agg['id'], body,
                          check_response_status=[400])

    def test_cache_images_with_missing_image_id(self):
        agg = {'aggregate': {'name': 'test-aggregate'}}
        agg = self.api.post_aggregate(agg)
        body = {'cache': [
            {'power': '1.21 gigawatts'},
        ]}
        self.api.api_post('/os-aggregates/%i/images' % agg['id'], body,
                          check_response_status=[400])

    def test_cache_images_with_missing_cache(self):
        agg = {'aggregate': {'name': 'test-aggregate'}}
        agg = self.api.post_aggregate(agg)
        body = {}
        self.api.api_post('/os-aggregates/%i/images' % agg['id'], body,
                          check_response_status=[400])

    def test_cache_images_with_additional_in_cache(self):
        agg = {'aggregate': {'name': 'test-aggregate'}}
        agg = self.api.post_aggregate(agg)
        img = '155d900f-4e14-4e4c-a73d-069cbf4541e6'
        body = {'cache': [{'id': img}],
                'power': '1.21 gigawatts',
        }
        self.api.api_post('/os-aggregates/%i/images' % agg['id'], body,
                          check_response_status=[400])


class AggregateRequestFiltersTest(
        integrated_helpers.ProviderUsageBaseTestCase):
    microversion = 'latest'
    compute_driver = 'fake.MediumFakeDriver'

    def setUp(self):
        super(AggregateRequestFiltersTest, self).setUp()

        self.aggregates = {}

        self._start_compute('host1')
        self._start_compute('host2')

        self.flavors = self.api.get_flavors()

        # Aggregate with only host1
        self._create_aggregate('only-host1')
        self._add_host_to_aggregate('only-host1', 'host1')

        # Aggregate with only host2
        self._create_aggregate('only-host2')
        self._add_host_to_aggregate('only-host2', 'host2')

        # Aggregate with neither host
        self._create_aggregate('no-hosts')

    def _create_aggregate(self, name):
        agg = self.admin_api.post_aggregate({'aggregate': {'name': name}})
        self.aggregates[name] = agg

    def _get_provider_uuid_by_host(self, host):
        """Return the compute node uuid for a named compute host."""
        # NOTE(gibi): the compute node id is the same as the compute node
        # provider uuid on that compute
        resp = self.admin_api.api_get(
            'os-hypervisors?hypervisor_hostname_pattern=%s' % host).body
        return resp['hypervisors'][0]['id']

    def _add_host_to_aggregate(self, agg, host):
        """Add a compute host to both nova and placement aggregates.

        :param agg: Name of the nova aggregate
        :param host: Name of the compute host
        """
        agg = self.aggregates[agg]
        self.admin_api.add_host_to_aggregate(agg['id'], host)

    def _boot_server(self, az=None, flavor_id=None, image_id=None,
                     end_status='ACTIVE'):
        flavor_id = flavor_id or self.flavors[0]['id']
        image_uuid = image_id or '155d900f-4e14-4e4c-a73d-069cbf4541e6'
        server_req = self._build_server(
            image_uuid=image_uuid,
            flavor_id=flavor_id,
            networks='none', az=az)

        created_server = self.api.post_server({'server': server_req})
        server = self._wait_for_state_change(created_server, end_status)

        return server

    def _get_instance_host(self, server):
        srv = self.admin_api.get_server(server['id'])
        return srv['OS-EXT-SRV-ATTR:host']

    def _set_az_aggregate(self, agg, az):
        """Set the availability_zone of an aggregate

        :param agg: Name of the nova aggregate
        :param az: Availability zone name
        """
        agg = self.aggregates[agg]
        action = {
            'set_metadata': {
                'metadata': {
                    'availability_zone': az,
                }
            },
        }
        self.admin_api.post_aggregate_action(agg['id'], action)

    def _set_metadata(self, agg, metadata):
        """POST /os-aggregates/{aggregate_id}/action (set_metadata)

        :param agg: Name of the nova aggregate
        :param metadata: dict of aggregate metadata key/value pairs to add,
            update, or remove if value=None (note "availability_zone" cannot be
            nulled out once set).
        """
        agg = self.aggregates[agg]
        action = {
            'set_metadata': {
                'metadata': metadata
            },
        }
        self.admin_api.post_aggregate_action(agg['id'], action)

    def _grant_tenant_aggregate(self, agg, tenants):
        """Grant a set of tenants access to use an aggregate.

        :param agg: Name of the nova aggregate
        :param tenants: A list of all tenant ids that will be allowed access
        """
        agg = self.aggregates[agg]
        action = {
            'set_metadata': {
                'metadata': {
                    'filter_tenant_id%i' % i: tenant
                    for i, tenant in enumerate(tenants)
                }
            },
        }
        self.admin_api.post_aggregate_action(agg['id'], action)

    def _set_traits_on_aggregate(self, agg, traits):
        """Set traits to aggregate.

        :param agg: Name of the nova aggregate
        :param traits: List of traits to be assigned to the aggregate
        """
        action = {
            'set_metadata': {
                'metadata': {
                    'trait:' + trait: 'required'
                    for trait in traits
                }
            }
        }
        self.admin_api.post_aggregate_action(
            self.aggregates[agg]['id'], action)


class AggregatePostTest(AggregateRequestFiltersTest):

    def test_set_az_for_aggreate_no_instances(self):
        """Should be possible to update AZ for an empty aggregate.

        Check you can change the AZ name of an aggregate when it does
        not contain any servers.
        """
        self._set_az_aggregate('only-host1', 'fake-az')

    def test_rename_to_same_az(self):
        """AZ rename should pass successfully if AZ name is not changed"""
        az = 'fake-az'
        self._set_az_aggregate('only-host1', az)
        self._boot_server(az=az)
        self._set_az_aggregate('only-host1', az)

    def test_fail_set_az(self):
        """Check it is not possible to update a non-empty aggregate.

        Check you cannot change the AZ name of an aggregate when it
        contains any servers.
        """
        az = 'fake-az'
        self._set_az_aggregate('only-host1', az)
        server = self._boot_server(az=az)
        self.assertRaisesRegex(
            client.OpenStackApiException,
            'One or more hosts contain instances in this zone.',
            self._set_az_aggregate, 'only-host1', 'new' + az)

        # Configure for the SOFT_DELETED scenario.
        self.flags(reclaim_instance_interval=300)
        self.api.delete_server(server['id'])
        server = self._wait_for_state_change(server, 'SOFT_DELETED')
        self.assertRaisesRegex(
            client.OpenStackApiException,
            'One or more hosts contain instances in this zone.',
            self._set_az_aggregate, 'only-host1', 'new' + az)
        # Force delete the SOFT_DELETED server.
        self.api.api_post(
            '/servers/%s/action' % server['id'], {'forceDelete': None})
        # Wait for it to be deleted since forceDelete is asynchronous.
        self._wait_until_deleted(server)
        # Now we can rename the AZ since the server is gone.
        self._set_az_aggregate('only-host1', 'new' + az)

    def test_cannot_delete_az(self):
        az = 'fake-az'
        # Assign the AZ to the aggregate.
        self._set_az_aggregate('only-host1', az)
        # Set some metadata on the aggregate; note the "availability_zone"
        # metadata key is not specified.
        self._set_metadata('only-host1', {'foo': 'bar'})
        # Verify the AZ was retained.
        agg = self.admin_api.api_get(
            '/os-aggregates/%s' %
            self.aggregates['only-host1']['id']).body['aggregate']
        self.assertEqual(az, agg['availability_zone'])


# NOTE: this test case has the same test methods as AggregatePostTest
# but for the AZ update it uses PUT /os-aggregates/{aggregate_id} method
class AggregatePutTest(AggregatePostTest):

    def _set_az_aggregate(self, agg, az):
        """Set the availability_zone of an aggregate via PUT

        :param agg: Name of the nova aggregate
        :param az: Availability zone name
        """
        agg = self.aggregates[agg]
        body = {
            'aggregate': {
                'availability_zone': az,
            },
        }
        self.admin_api.put_aggregate(agg['id'], body)


class TenantAggregateFilterTest(AggregateRequestFiltersTest):
    def setUp(self):
        super(TenantAggregateFilterTest, self).setUp()

        # Default to enabling the filter and making it mandatory
        self.flags(limit_tenants_to_placement_aggregate=True,
                   group='scheduler')
        self.flags(placement_aggregate_required_for_tenants=True,
                   group='scheduler')

    def test_tenant_id_required_fails_if_no_aggregate(self):
        # Without granting our tenant permission to an aggregate, instance
        # creates should fail since aggregates are required
        self._boot_server(end_status='ERROR')

    def test_tenant_id_not_required_succeeds_if_no_aggregate(self):
        self.flags(placement_aggregate_required_for_tenants=False,
                   group='scheduler')
        # Without granting our tenant permission to an aggregate, instance
        # creates should still succeed since aggregates are not required
        self._boot_server(end_status='ACTIVE')

    def test_filter_honors_tenant_id(self):
        tenant = self.api.project_id

        # Grant our tenant access to the aggregate with only host1 in it
        # and boot some servers. They should all stack up on host1.
        self._grant_tenant_aggregate('only-host1',
                                     ['foo', tenant, 'bar'])
        server1 = self._boot_server(end_status='ACTIVE')
        server2 = self._boot_server(end_status='ACTIVE')

        # Grant our tenant access to the aggregate with only host2 in it
        # and boot some servers. They should all stack up on host2.
        self._grant_tenant_aggregate('only-host1',
                                     ['foo', 'bar'])
        self._grant_tenant_aggregate('only-host2',
                                     ['foo', tenant, 'bar'])
        server3 = self._boot_server(end_status='ACTIVE')
        server4 = self._boot_server(end_status='ACTIVE')

        # Make sure the servers landed on the hosts we had access to at
        # the time we booted them.
        hosts = [self._get_instance_host(s)
                 for s in (server1, server2, server3, server4)]
        expected_hosts = ['host1', 'host1', 'host2', 'host2']
        self.assertEqual(expected_hosts, hosts)

    def test_filter_with_empty_aggregate(self):
        tenant = self.api.project_id

        # Grant our tenant access to the aggregate with no hosts in it
        self._grant_tenant_aggregate('no-hosts',
                                     ['foo', tenant, 'bar'])
        self._boot_server(end_status='ERROR')

    def test_filter_with_multiple_aggregates_for_tenant(self):
        tenant = self.api.project_id

        # Grant our tenant access to the aggregate with no hosts in it,
        # and one with a host.
        self._grant_tenant_aggregate('no-hosts',
                                     ['foo', tenant, 'bar'])
        self._grant_tenant_aggregate('only-host2',
                                     ['foo', tenant, 'bar'])

        # Boot several servers and make sure they all land on the
        # only host we have access to.
        for i in range(0, 4):
            server = self._boot_server(end_status='ACTIVE')
            self.assertEqual('host2', self._get_instance_host(server))


class AvailabilityZoneFilterTest(AggregateRequestFiltersTest):
    def setUp(self):
        # Default to enabling the filter
        self.flags(query_placement_for_availability_zone=True,
                   group='scheduler')

        # Use custom weigher to make sure that we have a predictable
        # scheduling sort order.
        self.useFixture(nova_fixtures.HostNameWeigherFixture())
        super(AvailabilityZoneFilterTest, self).setUp()

    def test_filter_with_az(self):
        self._set_az_aggregate('only-host2', 'myaz')
        server1 = self._boot_server(az='myaz')
        server2 = self._boot_server(az='myaz')
        hosts = [self._get_instance_host(s) for s in (server1, server2)]
        self.assertEqual(['host2', 'host2'], hosts)


class IsolateAggregateFilterTest(AggregateRequestFiltersTest):
    def setUp(self):
        # Default to enabling the filter
        self.flags(enable_isolated_aggregate_filtering=True,
                   group='scheduler')

        # Use a custom weigher that would prefer host1 if the isolate
        # aggregate filter were not in place otherwise it's not deterministic
        # whether we're landing on host2 because of the filter or just by
        # chance.
        self.useFixture(nova_fixtures.HostNameWeigherFixture())

        super(IsolateAggregateFilterTest, self).setUp()

        self.glance = self.useFixture(nova_fixtures.GlanceFixture(self))
        # setting traits to flavors
        flavor_body = {'flavor': {'name': 'test_flavor',
                                  'ram': 512,
                                  'vcpus': 1,
                                  'disk': 1
                                  }}
        self.flavor_with_trait_dxva = self.api.post_flavor(flavor_body)
        self.admin_api.post_extra_spec(
            self.flavor_with_trait_dxva['id'],
            {'extra_specs': {'trait:HW_GPU_API_DXVA': 'required'}})
        flavor_body['flavor']['name'] = 'test_flavor1'
        self.flavor_with_trait_sgx = self.api.post_flavor(flavor_body)
        self.admin_api.post_extra_spec(
            self.flavor_with_trait_sgx['id'],
            {'extra_specs': {'trait:HW_CPU_X86_SGX': 'required'}})
        self.flavor_without_trait = self.flavors[0]

        with nova.utils.temporary_mutation(self.api, microversion='2.35'):
            images = self.api.get_images()
            self.image_id_without_trait = images[0]['id']

    def test_filter_with_no_valid_host(self):
        """Test 'isolate_aggregates' filter with no valid hosts.

        No required traits set in image/flavor, so all aggregates with
        required traits set should be ignored.
        """

        rp_uuid1 = self._get_provider_uuid_by_host('host1')
        self._set_provider_traits(
            rp_uuid1, ['HW_CPU_X86_VMX', 'HW_CPU_X86_SGX'])
        self._set_traits_on_aggregate(
            'only-host1', ['HW_CPU_X86_VMX', 'HW_CPU_X86_SGX'])

        rp_uuid2 = self._get_provider_uuid_by_host('host2')
        self._set_provider_traits(
            rp_uuid2, ['HW_CPU_X86_VMX', 'HW_CPU_X86_SGX'])
        self._set_traits_on_aggregate(
            'only-host2', ['HW_CPU_X86_VMX', 'HW_CPU_X86_SGX'])

        server = self._boot_server(
            flavor_id=self.flavor_without_trait['id'],
            image_id=self.image_id_without_trait,
            end_status='ERROR')
        self.assertIsNone(self._get_instance_host(server))
        self.assertIn('No valid host', server['fault']['message'])

    def test_filter_without_trait(self):
        """Test 'isolate_aggregates' filter with valid hosts.

        No required traits set in image/flavor so instance should be booted on
        host from an aggregate with no required traits set.
        """

        rp_uuid1 = self._get_provider_uuid_by_host('host1')
        self._set_provider_traits(
            rp_uuid1, ['HW_CPU_X86_VMX', 'HW_CPU_X86_SGX'])
        self._set_traits_on_aggregate(
            'only-host1', ['HW_CPU_X86_VMX', 'HW_CPU_X86_SGX'])

        server = self._boot_server(
            flavor_id=self.flavor_without_trait['id'],
            image_id=self.image_id_without_trait)
        self.assertEqual('host2', self._get_instance_host(server))

    def test_filter_with_trait_on_flavor(self):
        """Test filter with matching required traits set only in one aggregate.

        Required trait (HW_GPU_API_DXVA) set in flavor so instance should be
        booted on host with matching required traits set on aggregates.
        """

        rp_uuid2 = self._get_provider_uuid_by_host('host2')
        self._set_provider_traits(rp_uuid2, ['HW_GPU_API_DXVA'])

        rp_uuid1 = self._get_provider_uuid_by_host('host1')
        self._set_provider_traits(
            rp_uuid1, ['HW_CPU_X86_VMX', 'HW_CPU_X86_SGX'])

        self._set_traits_on_aggregate('only-host2', ['HW_GPU_API_DXVA'])
        self._set_traits_on_aggregate(
            'only-host1', ['HW_CPU_X86_VMX', 'HW_CPU_X86_SGX'])
        server = self._boot_server(
            flavor_id=self.flavor_with_trait_dxva['id'],
            image_id=self.image_id_without_trait)

        self.assertEqual('host2', self._get_instance_host(server))

    def test_filter_with_common_trait_on_aggregates(self):
        """Test filter with common required traits set to aggregates.

        Required trait (HW_CPU_X86_SGX) set in flavor so instance should be
        booted on host with exact matching required traits set on aggregates.
        """

        rp_uuid2 = self._get_provider_uuid_by_host('host2')
        self._set_provider_traits(rp_uuid2, ['HW_CPU_X86_SGX'])

        rp_uuid1 = self._get_provider_uuid_by_host('host1')
        self._set_provider_traits(
            rp_uuid1, ['HW_CPU_X86_VMX', 'HW_CPU_X86_SGX'])

        self._set_traits_on_aggregate('only-host2', ['HW_CPU_X86_SGX'])
        self._set_traits_on_aggregate(
            'only-host1', ['HW_CPU_X86_VMX', 'HW_CPU_X86_SGX'])
        server = self._boot_server(
            flavor_id=self.flavor_with_trait_sgx['id'],
            image_id=self.image_id_without_trait)

        self.assertEqual('host2', self._get_instance_host(server))

    def test_filter_with_traits_on_image_and_flavor(self):
        """Test filter with common traits set to image/flavor and aggregates.

        Required trait (HW_CPU_X86_SGX) set in flavor and
        required trait (HW_CPU_X86_VMX) set in image, so instance should be
        booted on host with exact matching required traits set on aggregates.
        """

        rp_uuid2 = self._get_provider_uuid_by_host('host2')
        self._set_provider_traits(
            rp_uuid2, ['HW_CPU_X86_VMX', 'HW_CPU_X86_SGX'])

        rp_uuid1 = self._get_provider_uuid_by_host('host1')
        self._set_provider_traits(rp_uuid1, ['HW_GPU_API_DXVA'])

        self._set_traits_on_aggregate('only-host1', ['HW_GPU_API_DXVA'])
        self._set_traits_on_aggregate(
            'only-host2', ['HW_CPU_X86_VMX', 'HW_CPU_X86_SGX'])

        # Creating a new image and setting traits on it.
        with nova.utils.temporary_mutation(self.api, microversion='2.35'):
            self.ctxt = test_utils.get_test_admin_context()
            img_ref = self.glance.create(self.ctxt, {'name': 'image10'})
            image_id_with_trait = img_ref['id']
            self.addCleanup(self.glance.delete, self.ctxt, image_id_with_trait)
            self.api.api_put('/images/%s/metadata' % image_id_with_trait,
                             {'metadata': {
                                 'trait:HW_CPU_X86_VMX': 'required'}})
        server = self._boot_server(
            flavor_id=self.flavor_with_trait_sgx['id'],
            image_id=image_id_with_trait)
        self.assertEqual('host2', self._get_instance_host(server))

    def test_filter_with_traits_image_flavor_subset_of_aggregates(self):
        """Test filter with image/flavor required traits subset of aggregates.

        Image and flavor has a nonempty set of required traits that's subset
        set of the traits on the aggregates.
        """

        rp_uuid2 = self._get_provider_uuid_by_host('host2')
        self._set_provider_traits(
            rp_uuid2, ['HW_CPU_X86_VMX', 'HW_GPU_API_DXVA', 'HW_CPU_X86_SGX'])

        self._set_traits_on_aggregate(
            'only-host2',
            ['HW_CPU_X86_VMX', 'HW_GPU_API_DXVA', 'HW_CPU_X86_SGX'])

        # Creating a new image and setting traits on it.
        with nova.utils.temporary_mutation(self.api, microversion='2.35'):
            self.ctxt = test_utils.get_test_admin_context()
            img_ref = self.glance.create(self.ctxt, {'name': 'image10'})
            image_id_with_trait = img_ref['id']
            self.addCleanup(self.glance.delete, self.ctxt, image_id_with_trait)
            self.api.api_put('/images/%s/metadata' % image_id_with_trait,
                             {'metadata': {
                                 'trait:HW_CPU_X86_VMX': 'required'}})
        server = self._boot_server(
            flavor_id=self.flavor_with_trait_sgx['id'],
            image_id=image_id_with_trait,
            end_status='ERROR')

        self.assertIsNone(self._get_instance_host(server))
        self.assertIn('No valid host', server['fault']['message'])

    def test_filter_with_traits_image_flavor_disjoint_of_aggregates(self):
        """Test filter with image/flav required traits disjoint of aggregates.

        Image and flavor has a nonempty set of required traits that's disjoint
        set of the traits on the aggregates.
        """

        rp_uuid2 = self._get_provider_uuid_by_host('host2')
        self._set_provider_traits(rp_uuid2, ['HW_CPU_X86_VMX'])

        rp_uuid1 = self._get_provider_uuid_by_host('host1')
        self._set_provider_traits(rp_uuid1, ['HW_GPU_API_DXVA'])

        self._set_traits_on_aggregate('only-host1', ['HW_GPU_API_DXVA'])
        self._set_traits_on_aggregate('only-host2', ['HW_CPU_X86_VMX'])

        # Creating a new image and setting traits on it.
        with nova.utils.temporary_mutation(self.api, microversion='2.35'):
            self.ctxt = test_utils.get_test_admin_context()
            img_ref = self.glance.create(self.ctxt, {'name': 'image10'})
            image_id_with_trait = img_ref['id']
            self.addCleanup(self.glance.delete, self.ctxt, image_id_with_trait)
            self.api.api_put('/images/%s/metadata' % image_id_with_trait,
                             {'metadata': {
                                 'trait:HW_CPU_X86_VMX': 'required'}})
        server = self._boot_server(
            flavor_id=self.flavor_with_trait_sgx['id'],
            image_id=image_id_with_trait,
            end_status='ERROR')

        self.assertIsNone(self._get_instance_host(server))
        self.assertIn('No valid host', server['fault']['message'])


class IsolateAggregateFilterTestWithConcernFilters(IsolateAggregateFilterTest):
    def setUp(self):
        filters = CONF.filter_scheduler.enabled_filters

        # NOTE(shilpasd): To test `isolate_aggregates` request filter, along
        # with following filters which also filters hosts based on aggregate
        # metadata.
        if 'AggregateImagePropertiesIsolation' not in filters:
            filters.append('AggregateImagePropertiesIsolation')
        if 'AggregateInstanceExtraSpecsFilter' not in filters:
            filters.append('AggregateInstanceExtraSpecsFilter')
        self.flags(enabled_filters=filters, group='filter_scheduler')

        super(IsolateAggregateFilterTestWithConcernFilters, self).setUp()


class IsolateAggregateFilterTestWOConcernFilters(IsolateAggregateFilterTest):
    def setUp(self):
        filters = CONF.filter_scheduler.enabled_filters

        # NOTE(shilpasd): To test `isolate_aggregates` request filter, removed
        # following filters which also filters hosts based on aggregate
        # metadata.
        if 'AggregateImagePropertiesIsolation' in filters:
            filters.remove('AggregateImagePropertiesIsolation')
        if 'AggregateInstanceExtraSpecsFilter' in filters:
            filters.remove('AggregateInstanceExtraSpecsFilter')
        self.flags(enabled_filters=filters, group='filter_scheduler')

        super(IsolateAggregateFilterTestWOConcernFilters, self).setUp()


class TestAggregateFiltersTogether(AggregateRequestFiltersTest):
    def setUp(self):
        # Use a custom weigher that would prefer host1 if the forbidden
        # aggregate filter were not in place otherwise it's not deterministic
        # whether we're landing on host2 because of the filter or just by
        # chance.
        self.useFixture(nova_fixtures.HostNameWeigherFixture())

        # NOTE(danms): Do this before calling setUp() so that
        # the scheduler service that is started sees the new value
        filters = CONF.filter_scheduler.enabled_filters

        # NOTE(shilpasd): To test `isolate_aggregates` request filter, removed
        # following filters which also filters hosts based on aggregate
        # metadata.
        if 'AggregateImagePropertiesIsolation' in filters:
            filters.remove('AggregateImagePropertiesIsolation')
        if 'AggregateInstanceExtraSpecsFilter' in filters:
            filters.remove('AggregateInstanceExtraSpecsFilter')
        self.flags(enabled_filters=filters, group='filter_scheduler')

        super(TestAggregateFiltersTogether, self).setUp()

        # Default to enabling all filters
        self.flags(limit_tenants_to_placement_aggregate=True,
                   group='scheduler')
        self.flags(placement_aggregate_required_for_tenants=True,
                   group='scheduler')
        self.flags(query_placement_for_availability_zone=True,
                   group='scheduler')
        self.flags(enable_isolated_aggregate_filtering=True,
                   group='scheduler')
        # setting traits to flavors
        flavor_body = {'flavor': {'name': 'test_flavor',
                                  'ram': 512,
                                  'vcpus': 1,
                                  'disk': 1
                                  }}
        self.flavor_with_trait_dxva = self.api.post_flavor(flavor_body)
        self.admin_api.post_extra_spec(
            self.flavor_with_trait_dxva['id'],
            {'extra_specs': {'trait:HW_GPU_API_DXVA': 'required'}})

    def test_tenant_with_az_match(self):
        # Grant our tenant access to the aggregate with
        # host1
        self._grant_tenant_aggregate('only-host1',
                                     [self.api.project_id])
        # Set an az on only-host1
        self._set_az_aggregate('only-host1', 'myaz')

        # Boot the server into that az and make sure we land
        server = self._boot_server(az='myaz')
        self.assertEqual('host1', self._get_instance_host(server))

    def test_tenant_with_az_mismatch(self):
        # Grant our tenant access to the aggregate with
        # host1
        self._grant_tenant_aggregate('only-host1',
                                     [self.api.project_id])
        # Set an az on only-host2
        self._set_az_aggregate('only-host2', 'myaz')

        # Boot the server into that az and make sure we fail
        server = self._boot_server(az='myaz', end_status='ERROR')
        self.assertIsNone(self._get_instance_host(server))

    def test_tenant_with_az_and_traits_match(self):
        # Grant our tenant access to the aggregate with host2
        self._grant_tenant_aggregate('only-host2',
                                     [self.api.project_id])
        # Set an az on only-host2
        self._set_az_aggregate('only-host2', 'myaz')
        # Set trait on host2
        rp_uuid2 = self._get_provider_uuid_by_host('host2')
        self._set_provider_traits(rp_uuid2, ['HW_GPU_API_DXVA'])
        # Set trait on aggregate only-host2
        self._set_traits_on_aggregate('only-host2', ['HW_GPU_API_DXVA'])
        # Boot the server into that az and make sure we land
        server = self._boot_server(
            flavor_id=self.flavor_with_trait_dxva['id'], az='myaz')
        self.assertEqual('host2', self._get_instance_host(server))

    def test_tenant_with_az_and_traits_mismatch(self):
        # Grant our tenant access to the aggregate with host2
        self._grant_tenant_aggregate('only-host2',
                                     [self.api.project_id])
        # Set an az on only-host1
        self._set_az_aggregate('only-host2', 'myaz')
        # Set trait on host2
        rp_uuid2 = self._get_provider_uuid_by_host('host2')
        self._set_provider_traits(rp_uuid2, ['HW_CPU_X86_VMX'])
        # Set trait on aggregate only-host2
        self._set_traits_on_aggregate('only-host2', ['HW_CPU_X86_VMX'])
        # Boot the server into that az and make sure we fail
        server = self._boot_server(
            flavor_id=self.flavor_with_trait_dxva['id'],
            az='myaz',
            end_status='ERROR')
        self.assertIsNone(self._get_instance_host(server))
        self.assertIn('No valid host', server['fault']['message'])


class TestAggregateMultiTenancyIsolationFilter(
        test.TestCase, integrated_helpers.InstanceHelperMixin):

    def setUp(self):
        super(TestAggregateMultiTenancyIsolationFilter, self).setUp()
        # Stub out glance, placement and neutron.
        self.useFixture(nova_fixtures.GlanceFixture(self))
        self.useFixture(func_fixtures.PlacementFixture())
        self.useFixture(nova_fixtures.NeutronFixture(self))

        # Start nova services.
        self.start_service('conductor')
        self.admin_api = self.useFixture(
            nova_fixtures.OSAPIFixture(api_version='v2.1')).admin_api
        self.api = self.useFixture(
            nova_fixtures.OSAPIFixture(api_version='v2.1',
                                       project_id=uuids.non_admin)).api
        # Add the AggregateMultiTenancyIsolation to the list of enabled
        # filters since it is not enabled by default.
        enabled_filters = CONF.filter_scheduler.enabled_filters
        enabled_filters.append('AggregateMultiTenancyIsolation')
        self.flags(enabled_filters=enabled_filters, group='filter_scheduler')
        self.start_service('scheduler')
        for host in ('host1', 'host2'):
            self.start_service('compute', host=host)

    def test_aggregate_multitenancy_isolation_filter(self):
        """Tests common scenarios with the AggregateMultiTenancyIsolation
        filter:

        * hosts in a tenant-isolated aggregate are only accepted for that
          tenant
        * hosts not in a tenant-isolated aggregate are acceptable for all
          tenants, including tenants with access to the isolated-tenant
          aggregate
        """
        # Create a tenant-isolated aggregate for the non-admin user.
        agg_id = self.admin_api.post_aggregate(
            {'aggregate': {'name': 'non_admin_agg'}})['id']
        meta_req = {'set_metadata': {
            'metadata': {'filter_tenant_id': uuids.non_admin}}}
        self.admin_api.api_post('/os-aggregates/%s/action' % agg_id, meta_req)
        # Add host2 to the aggregate; we'll restrict host2 to the non-admin
        # tenant.
        host_req = {'add_host': {'host': 'host2'}}
        self.admin_api.api_post('/os-aggregates/%s/action' % agg_id, host_req)
        # Stub out select_destinations to assert how many host candidates were
        # available per tenant-specific request.
        original_filtered_hosts = (
            nova.scheduler.host_manager.HostManager.get_filtered_hosts)

        def spy_get_filtered_hosts(*args, **kwargs):
            self.filtered_hosts = original_filtered_hosts(*args, **kwargs)
            return self.filtered_hosts
        self.stub_out(
            'nova.scheduler.host_manager.HostManager.get_filtered_hosts',
            spy_get_filtered_hosts)

        # Create a server for the admin - should only have one host candidate.
        server_req = {'server': self._build_server(networks='none')}
        with utils.temporary_mutation(self.admin_api, microversion='2.37'):
            server = self.admin_api.post_server(server_req)
        server = self._wait_for_state_change(server, 'ACTIVE')
        # Assert it's not on host2 which is isolated to the non-admin tenant.
        self.assertNotEqual('host2', server['OS-EXT-SRV-ATTR:host'])
        self.assertEqual(1, len(self.filtered_hosts))
        # Now create a server for the non-admin tenant to which host2 is
        # isolated via the aggregate, but the other compute host is a
        # candidate. We don't assert that the non-admin tenant server shows
        # up on host2 because the other host, which is not isolated to the
        # aggregate, is still a candidate.
        server_req = {'server': self._build_server(networks='none')}
        with utils.temporary_mutation(self.api, microversion='2.37'):
            server = self.api.post_server(server_req)
        self._wait_for_state_change(server, 'ACTIVE')
        self.assertEqual(2, len(self.filtered_hosts))


class AggregateMultiTenancyIsolationColdMigrateTest(
        test.TestCase, integrated_helpers.InstanceHelperMixin):

    @staticmethod
    def _create_aggregate(admin_api, name):
        return admin_api.api_post(
            '/os-aggregates', {'aggregate': {'name': name}}).body['aggregate']

    @staticmethod
    def _add_host_to_aggregate(admin_api, aggregate, host):
        add_host_req_body = {
            "add_host": {
                "host": host
            }
        }
        admin_api.api_post(
            '/os-aggregates/%s/action' % aggregate['id'], add_host_req_body)

    @staticmethod
    def _isolate_aggregate(admin_api, aggregate, tenant_id):
        set_meta_req_body = {
            "set_metadata": {
                "metadata": {
                    "filter_tenant_id": tenant_id
                }
            }
        }
        admin_api.api_post(
            '/os-aggregates/%s/action' % aggregate['id'], set_meta_req_body)

    def setUp(self):
        super(AggregateMultiTenancyIsolationColdMigrateTest, self).setUp()
        self.useFixture(nova_fixtures.RealPolicyFixture())
        self.glance = self.useFixture(nova_fixtures.GlanceFixture(self))
        self.useFixture(nova_fixtures.NeutronFixture(self))
        self.useFixture(func_fixtures.PlacementFixture())
        # Intentionally keep these separate since we want to create the
        # server with the non-admin user in a different project.
        admin_api_fixture = self.useFixture(nova_fixtures.OSAPIFixture(
            api_version='v2.1', project_id=uuids.admin_project))
        self.admin_api = admin_api_fixture.admin_api
        self.admin_api.microversion = 'latest'
        user_api_fixture = self.useFixture(nova_fixtures.OSAPIFixture(
            api_version='v2.1', project_id=uuids.user_project))
        self.api = user_api_fixture.api
        self.api.microversion = 'latest'

        self.start_service('conductor')
        # Enable the AggregateMultiTenancyIsolation filter before starting the
        # scheduler service.
        enabled_filters = CONF.filter_scheduler.enabled_filters
        if 'AggregateMultiTenancyIsolation' not in enabled_filters:
            enabled_filters.append('AggregateMultiTenancyIsolation')
            self.flags(
                enabled_filters=enabled_filters, group='filter_scheduler')
        # Add a custom weigher which will weigh host1, which will be in the
        # admin project aggregate, higher than the other hosts which are in
        # the non-admin project aggregate.
        self.useFixture(nova_fixtures.HostNameWeigherFixture())
        self.start_service('scheduler')

        for host in ('host1', 'host2', 'host3'):
            self.start_service('compute', host=host)

        # Create an admin-only aggregate for the admin project. This is needed
        # because if host1 is not in an aggregate with the filter_tenant_id
        # metadata key, the filter will accept that host even for the non-admin
        # project.
        admin_aggregate = self._create_aggregate(
            self.admin_api, 'admin-aggregate')
        self._add_host_to_aggregate(self.admin_api, admin_aggregate, 'host1')

        # Restrict the admin project to the admin aggregate.
        self._isolate_aggregate(
            self.admin_api, admin_aggregate, uuids.admin_project)

        # Create the tenant aggregate for the non-admin project.
        tenant_aggregate = self._create_aggregate(
            self.admin_api, 'tenant-aggregate')

        # Add two compute hosts to the tenant aggregate. We exclude host1
        # since that is weighed higher due to HostNameWeigherFixture and we
        # want to ensure the scheduler properly filters out host1 before we
        # even get to weighing the selected hosts.
        for host in ('host2', 'host3'):
            self._add_host_to_aggregate(self.admin_api, tenant_aggregate, host)

        # Restrict the non-admin project to the tenant aggregate.
        self._isolate_aggregate(
            self.admin_api, tenant_aggregate, uuids.user_project)

    def test_cold_migrate_server(self):
        """Creates a server using the non-admin project, then cold migrates
        the server and asserts the server goes to the other host in the
        isolated host aggregate via the AggregateMultiTenancyIsolation filter.
        """
        server_req_body = self._build_server(networks='none')
        server = self.api.post_server({'server': server_req_body})
        server = self._wait_for_state_change(server, 'ACTIVE')
        # Ensure the server ended up in host2 or host3
        original_host = server['OS-EXT-SRV-ATTR:host']
        self.assertNotEqual('host1', original_host)
        # Now cold migrate the server and it should end up in the other host
        # in the same tenant-isolated aggregate.
        self.admin_api.api_post(
            '/servers/%s/action' % server['id'], {'migrate': None})
        server = self._wait_for_state_change(server, 'VERIFY_RESIZE')
        # Ensure the server is on the other host in the same aggregate.
        expected_host = 'host3' if original_host == 'host2' else 'host2'
        self.assertEqual(expected_host, server['OS-EXT-SRV-ATTR:host'])