summaryrefslogtreecommitdiffstats
path: root/nova/tests/scheduler/test_vsa_scheduler.py
blob: a7cd2358a3479af2370cc3cad1a1099050566705 (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
# Copyright 2011 OpenStack LLC.
# 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.

import nova

from nova import context
from nova import exception
from nova import flags
from nova import log as logging
from nova import rpc
from nova import test
from nova import utils
from nova.volume import volume_types

from nova.scheduler import vsa as vsa_sched

FLAGS = flags.FLAGS
LOG = logging.getLogger('nova.tests.scheduler.vsa')

scheduled_volumes = []
scheduled_volume = {}
global_volume = {}


def fake_rpc_cast(*args, **kwargs):
    pass


class FakeVsaLeastUsedScheduler(
                vsa_sched.VsaSchedulerLeastUsedHost):
    # No need to stub anything at the moment
    pass


class FakeVsaMostAvailCapacityScheduler(
                vsa_sched.VsaSchedulerMostAvailCapacity):
    # No need to stub anything at the moment
    pass


class VsaSchedulerTestCase(test.TestCase):

    def _get_vol_creation_request(self, num_vols, drive_ix, size=0):
        volume_params = []
        for i in range(num_vols):

            name = 'name_' + str(i)
            try:
                volume_types.create(self.context, name,
                            extra_specs={'type': 'vsa_drive',
                                         'drive_name': name,
                                         'drive_type': 'type_' + str(drive_ix),
                                         'drive_size': 1 + 100 * (drive_ix)})
                self.created_types_lst.append(name)
            except exception.ApiError:
                # type is already created
                pass

            volume_type = volume_types.get_volume_type_by_name(self.context,
                                                                name)
            volume = {'size': size,
                      'snapshot_id': None,
                      'name': 'vol_' + str(i),
                      'description': None,
                      'volume_type_id': volume_type['id']}
            volume_params.append(volume)

        return {'num_volumes': len(volume_params),
                'vsa_id': 123,
                'volumes': volume_params}

    def _generate_default_service_states(self):
        service_states = {}
        for i in range(self.host_num):
            host = {}
            hostname = 'host_' + str(i)
            if hostname in self.exclude_host_list:
                continue

            host['volume'] = {'timestamp': utils.utcnow(),
                              'drive_qos_info': {}}

            for j in range(self.drive_type_start_ix,
                           self.drive_type_start_ix + self.drive_type_num):
                dtype = {}
                dtype['Name'] = 'name_' + str(j)
                dtype['DriveType'] = 'type_' + str(j)
                dtype['TotalDrives'] = 2 * (self.init_num_drives + i)
                dtype['DriveCapacity'] = vsa_sched.GB_TO_BYTES(1 + 100 * j)
                dtype['TotalCapacity'] = dtype['TotalDrives'] * \
                                            dtype['DriveCapacity']
                dtype['AvailableCapacity'] = (dtype['TotalDrives'] - i) * \
                                            dtype['DriveCapacity']
                dtype['DriveRpm'] = 7200
                dtype['DifCapable'] = 0
                dtype['SedCapable'] = 0
                dtype['PartitionDrive'] = {
                            'PartitionSize': 0,
                            'NumOccupiedPartitions': 0,
                            'NumFreePartitions': 0}
                dtype['FullDrive'] = {
                            'NumFreeDrives': dtype['TotalDrives'] - i,
                            'NumOccupiedDrives': i}
                host['volume']['drive_qos_info'][dtype['Name']] = dtype

            service_states[hostname] = host

        return service_states

    def _print_service_states(self):
        for host, host_val in self.service_states.iteritems():
            LOG.info(_("Host %s"), host)
            total_used = 0
            total_available = 0
            qos = host_val['volume']['drive_qos_info']

            for k, d in qos.iteritems():
                LOG.info("\t%s: type %s: drives (used %2d, total %2d) "\
                    "size %3d, total %4d, used %4d, avail %d",
                    k, d['DriveType'],
                    d['FullDrive']['NumOccupiedDrives'], d['TotalDrives'],
                    vsa_sched.BYTES_TO_GB(d['DriveCapacity']),
                    vsa_sched.BYTES_TO_GB(d['TotalCapacity']),
                    vsa_sched.BYTES_TO_GB(d['TotalCapacity'] - \
                        d['AvailableCapacity']),
                    vsa_sched.BYTES_TO_GB(d['AvailableCapacity']))

                total_used += vsa_sched.BYTES_TO_GB(d['TotalCapacity'] - \
                                    d['AvailableCapacity'])
                total_available += vsa_sched.BYTES_TO_GB(
                                        d['AvailableCapacity'])
            LOG.info("Host %s: used %d, avail %d",
                     host, total_used, total_available)

    def _set_service_states(self, host_num,
                            drive_type_start_ix, drive_type_num,
                            init_num_drives=10,
                            exclude_host_list=[]):
        self.host_num = host_num
        self.drive_type_start_ix = drive_type_start_ix
        self.drive_type_num = drive_type_num
        self.exclude_host_list = exclude_host_list
        self.init_num_drives = init_num_drives
        self.service_states = self._generate_default_service_states()

    def _get_service_states(self):
        return self.service_states

    def _fake_get_service_states(self):
        return self._get_service_states()

    def _fake_provision_volume(self, context, vol, vsa_id, availability_zone):
        global scheduled_volumes
        scheduled_volumes.append(dict(vol=vol,
                                      vsa_id=vsa_id,
                                      az=availability_zone))
        name = vol['name']
        host = vol['host']
        LOG.debug(_("Test: provision vol %(name)s on host %(host)s"),
                    locals())
        LOG.debug(_("\t vol=%(vol)s"), locals())

    def _fake_vsa_update(self, context, vsa_id, values):
        LOG.debug(_("Test: VSA update request: vsa_id=%(vsa_id)s "\
                    "values=%(values)s"), locals())

    def _fake_volume_create(self, context, options):
        LOG.debug(_("Test: Volume create: %s"), options)
        options['id'] = 123
        global global_volume
        global_volume = options
        return options

    def _fake_volume_get(self, context, volume_id):
        LOG.debug(_("Test: Volume get request: id=%(volume_id)s"), locals())
        global global_volume
        global_volume['id'] = volume_id
        global_volume['availability_zone'] = None
        return global_volume

    def _fake_volume_update(self, context, volume_id, values):
        LOG.debug(_("Test: Volume update request: id=%(volume_id)s "\
                    "values=%(values)s"), locals())
        global scheduled_volume
        scheduled_volume = {'id': volume_id, 'host': values['host']}

    def _fake_service_get_by_args(self, context, host, binary):
        return {'host': 'fake_host', 'disabled': False}

    def _fake_service_is_up_True(self, service):
        return True

    def _fake_service_is_up_False(self, service):
        return False

    def setUp(self, sched_class=None):
        super(VsaSchedulerTestCase, self).setUp()
        self.context = context.get_admin_context()

        if sched_class is None:
            self.sched = FakeVsaLeastUsedScheduler()
        else:
            self.sched = sched_class

        self.host_num = 10
        self.drive_type_num = 5

        self.stubs.Set(rpc, 'cast', fake_rpc_cast)
        self.stubs.Set(self.sched,
                        '_get_service_states', self._fake_get_service_states)
        self.stubs.Set(self.sched,
                        '_provision_volume', self._fake_provision_volume)
        self.stubs.Set(nova.db, 'vsa_update', self._fake_vsa_update)

        self.stubs.Set(nova.db, 'volume_get', self._fake_volume_get)
        self.stubs.Set(nova.db, 'volume_update', self._fake_volume_update)

        self.created_types_lst = []

    def tearDown(self):
        for name in self.created_types_lst:
            volume_types.purge(self.context, name)
        super(VsaSchedulerTestCase, self).tearDown()

    def test_vsa_sched_create_volumes_simple(self):
        global scheduled_volumes
        scheduled_volumes = []
        self._set_service_states(host_num=10,
                                 drive_type_start_ix=0,
                                 drive_type_num=5,
                                 init_num_drives=10,
                                 exclude_host_list=['host_1', 'host_3'])
        prev = self._generate_default_service_states()
        request_spec = self._get_vol_creation_request(num_vols=3, drive_ix=2)

        self.sched.schedule_create_volumes(self.context,
                                           request_spec,
                                           availability_zone=None)

        self.assertEqual(len(scheduled_volumes), 3)
        self.assertEqual(scheduled_volumes[0]['vol']['host'], 'host_0')
        self.assertEqual(scheduled_volumes[1]['vol']['host'], 'host_2')
        self.assertEqual(scheduled_volumes[2]['vol']['host'], 'host_4')

        cur = self._get_service_states()
        for host in ['host_0', 'host_2', 'host_4']:
            cur_dtype = cur[host]['volume']['drive_qos_info']['name_2']
            prev_dtype = prev[host]['volume']['drive_qos_info']['name_2']
            self.assertEqual(cur_dtype['DriveType'], prev_dtype['DriveType'])
            self.assertEqual(cur_dtype['FullDrive']['NumFreeDrives'],
                             prev_dtype['FullDrive']['NumFreeDrives'] - 1)
            self.assertEqual(cur_dtype['FullDrive']['NumOccupiedDrives'],
                             prev_dtype['FullDrive']['NumOccupiedDrives'] + 1)

    def test_vsa_sched_no_drive_type(self):
        self._set_service_states(host_num=10,
                                 drive_type_start_ix=0,
                                 drive_type_num=5,
                                 init_num_drives=1)
        request_spec = self._get_vol_creation_request(num_vols=1, drive_ix=6)
        self.assertRaises(exception.NoValidHost,
                          self.sched.schedule_create_volumes,
                                self.context,
                                request_spec,
                                availability_zone=None)

    def test_vsa_sched_no_enough_drives(self):
        global scheduled_volumes
        scheduled_volumes = []

        self._set_service_states(host_num=3,
                                 drive_type_start_ix=0,
                                 drive_type_num=1,
                                 init_num_drives=0)
        prev = self._generate_default_service_states()
        request_spec = self._get_vol_creation_request(num_vols=3, drive_ix=0)

        self.assertRaises(exception.NoValidHost,
                          self.sched.schedule_create_volumes,
                                self.context,
                                request_spec,
                                availability_zone=None)

        # check that everything was returned back
        cur = self._get_service_states()
        for k, v in prev.iteritems():
            self.assertEqual(prev[k]['volume']['drive_qos_info'],
                              cur[k]['volume']['drive_qos_info'])

    def test_vsa_sched_wrong_topic(self):
        self._set_service_states(host_num=1,
                                 drive_type_start_ix=0,
                                 drive_type_num=5,
                                 init_num_drives=1)
        states = self._get_service_states()
        new_states = {}
        new_states['host_0'] = {'compute': states['host_0']['volume']}
        self.service_states = new_states
        request_spec = self._get_vol_creation_request(num_vols=1, drive_ix=0)

        self.assertRaises(exception.NoValidHost,
                          self.sched.schedule_create_volumes,
                                self.context,
                                request_spec,
                                availability_zone=None)

    def test_vsa_sched_provision_volume(self):
        global global_volume
        global_volume = {}
        self._set_service_states(host_num=1,
                                 drive_type_start_ix=0,
                                 drive_type_num=1,
                                 init_num_drives=1)
        request_spec = self._get_vol_creation_request(num_vols=1, drive_ix=0)

        self.stubs.UnsetAll()
        self.stubs.Set(self.sched,
                        '_get_service_states', self._fake_get_service_states)
        self.stubs.Set(nova.db, 'volume_create', self._fake_volume_create)
        self.stubs.Set(nova.db, 'volume_update', self._fake_volume_update)
        self.stubs.Set(rpc, 'cast', fake_rpc_cast)

        self.sched.schedule_create_volumes(self.context,
                                           request_spec,
                                           availability_zone=None)

        self.assertEqual(request_spec['volumes'][0]['name'],
                         global_volume['display_name'])

    def test_vsa_sched_no_free_drives(self):
        self._set_service_states(host_num=1,
                                 drive_type_start_ix=0,
                                 drive_type_num=1,
                                 init_num_drives=1)
        request_spec = self._get_vol_creation_request(num_vols=1, drive_ix=0)

        self.sched.schedule_create_volumes(self.context,
                                           request_spec,
                                           availability_zone=None)

        cur = self._get_service_states()
        cur_dtype = cur['host_0']['volume']['drive_qos_info']['name_0']
        self.assertEqual(cur_dtype['FullDrive']['NumFreeDrives'], 1)

        new_request = self._get_vol_creation_request(num_vols=1, drive_ix=0)

        self.sched.schedule_create_volumes(self.context,
                                           request_spec,
                                           availability_zone=None)
        self._print_service_states()

        self.assertRaises(exception.NoValidHost,
                          self.sched.schedule_create_volumes,
                                self.context,
                                new_request,
                                availability_zone=None)

    def test_vsa_sched_forced_host(self):
        global scheduled_volumes
        scheduled_volumes = []

        self._set_service_states(host_num=10,
                                 drive_type_start_ix=0,
                                 drive_type_num=5,
                                 init_num_drives=10)

        request_spec = self._get_vol_creation_request(num_vols=3, drive_ix=2)

        self.assertRaises(exception.HostBinaryNotFound,
                          self.sched.schedule_create_volumes,
                                self.context,
                                request_spec,
                                availability_zone="nova:host_5")

        self.stubs.Set(nova.db,
                        'service_get_by_args', self._fake_service_get_by_args)
        self.stubs.Set(utils,
                        'service_is_up', self._fake_service_is_up_False)

        self.assertRaises(exception.WillNotSchedule,
                          self.sched.schedule_create_volumes,
                                self.context,
                                request_spec,
                                availability_zone="nova:host_5")

        self.stubs.Set(utils,
                        'service_is_up', self._fake_service_is_up_True)

        self.sched.schedule_create_volumes(self.context,
                                           request_spec,
                                           availability_zone="nova:host_5")

        self.assertEqual(len(scheduled_volumes), 3)
        self.assertEqual(scheduled_volumes[0]['vol']['host'], 'host_5')
        self.assertEqual(scheduled_volumes[1]['vol']['host'], 'host_5')
        self.assertEqual(scheduled_volumes[2]['vol']['host'], 'host_5')

    def test_vsa_sched_create_volumes_partition(self):
        global scheduled_volumes
        scheduled_volumes = []
        self._set_service_states(host_num=5,
                                 drive_type_start_ix=0,
                                 drive_type_num=5,
                                 init_num_drives=1,
                                 exclude_host_list=['host_0', 'host_2'])
        prev = self._generate_default_service_states()
        request_spec = self._get_vol_creation_request(num_vols=3,
                                                      drive_ix=3,
                                                      size=50)
        self.sched.schedule_create_volumes(self.context,
                                           request_spec,
                                           availability_zone=None)

        self.assertEqual(len(scheduled_volumes), 3)
        self.assertEqual(scheduled_volumes[0]['vol']['host'], 'host_1')
        self.assertEqual(scheduled_volumes[1]['vol']['host'], 'host_3')
        self.assertEqual(scheduled_volumes[2]['vol']['host'], 'host_4')

        cur = self._get_service_states()
        for host in ['host_1', 'host_3', 'host_4']:
            cur_dtype = cur[host]['volume']['drive_qos_info']['name_3']
            prev_dtype = prev[host]['volume']['drive_qos_info']['name_3']

            self.assertEqual(cur_dtype['DriveType'], prev_dtype['DriveType'])
            self.assertEqual(cur_dtype['FullDrive']['NumFreeDrives'],
                             prev_dtype['FullDrive']['NumFreeDrives'] - 1)
            self.assertEqual(cur_dtype['FullDrive']['NumOccupiedDrives'],
                             prev_dtype['FullDrive']['NumOccupiedDrives'] + 1)

            self.assertEqual(prev_dtype['PartitionDrive']
                                            ['NumOccupiedPartitions'], 0)
            self.assertEqual(cur_dtype['PartitionDrive']
                                            ['NumOccupiedPartitions'], 1)
            self.assertEqual(cur_dtype['PartitionDrive']
                                            ['NumFreePartitions'], 5)

            self.assertEqual(prev_dtype['PartitionDrive']
                                            ['NumFreePartitions'], 0)
            self.assertEqual(prev_dtype['PartitionDrive']
                                            ['PartitionSize'], 0)

    def test_vsa_sched_create_single_volume_az(self):
        global scheduled_volume
        scheduled_volume = {}

        def _fake_volume_get_az(context, volume_id):
            LOG.debug(_("Test: Volume get: id=%(volume_id)s"), locals())
            return {'id': volume_id, 'availability_zone': 'nova:host_3'}

        self.stubs.Set(nova.db, 'volume_get', _fake_volume_get_az)
        self.stubs.Set(nova.db,
                        'service_get_by_args', self._fake_service_get_by_args)
        self.stubs.Set(utils,
                        'service_is_up', self._fake_service_is_up_True)

        self.sched.schedule_create_volume(self.context,
                123, availability_zone=None)

        self.assertEqual(scheduled_volume['id'], 123)
        self.assertEqual(scheduled_volume['host'], 'host_3')

    def test_vsa_sched_create_single_non_vsa_volume(self):
        global scheduled_volume
        scheduled_volume = {}

        global global_volume
        global_volume = {}
        global_volume['volume_type_id'] = None

        self.assertRaises(exception.NoValidHost,
                          self.sched.schedule_create_volume,
                                self.context,
                                123,
                                availability_zone=None)

    def test_vsa_sched_create_single_volume(self):
        global scheduled_volume
        scheduled_volume = {}
        self._set_service_states(host_num=10,
                                 drive_type_start_ix=0,
                                 drive_type_num=5,
                                 init_num_drives=10,
                                 exclude_host_list=['host_0', 'host_1'])
        prev = self._generate_default_service_states()

        global global_volume
        global_volume = {}

        drive_ix = 2
        name = 'name_' + str(drive_ix)
        volume_types.create(self.context, name,
                    extra_specs={'type': 'vsa_drive',
                                 'drive_name': name,
                                 'drive_type': 'type_' + str(drive_ix),
                                 'drive_size': 1 + 100 * (drive_ix)})
        self.created_types_lst.append(name)
        volume_type = volume_types.get_volume_type_by_name(self.context, name)

        global_volume['volume_type_id'] = volume_type['id']
        global_volume['size'] = 0

        self.sched.schedule_create_volume(self.context,
                123, availability_zone=None)

        self.assertEqual(scheduled_volume['id'], 123)
        self.assertEqual(scheduled_volume['host'], 'host_2')


class VsaSchedulerTestCaseMostAvail(VsaSchedulerTestCase):

    def setUp(self):
        super(VsaSchedulerTestCaseMostAvail, self).setUp(
                    FakeVsaMostAvailCapacityScheduler())

    def tearDown(self):
        super(VsaSchedulerTestCaseMostAvail, self).tearDown()

    def test_vsa_sched_create_single_volume(self):
        global scheduled_volume
        scheduled_volume = {}
        self._set_service_states(host_num=10,
                                 drive_type_start_ix=0,
                                 drive_type_num=5,
                                 init_num_drives=10,
                                 exclude_host_list=['host_0', 'host_1'])
        prev = self._generate_default_service_states()

        global global_volume
        global_volume = {}

        drive_ix = 2
        name = 'name_' + str(drive_ix)
        volume_types.create(self.context, name,
                    extra_specs={'type': 'vsa_drive',
                                 'drive_name': name,
                                 'drive_type': 'type_' + str(drive_ix),
                                 'drive_size': 1 + 100 * (drive_ix)})
        self.created_types_lst.append(name)
        volume_type = volume_types.get_volume_type_by_name(self.context, name)

        global_volume['volume_type_id'] = volume_type['id']
        global_volume['size'] = 0

        self.sched.schedule_create_volume(self.context,
                123, availability_zone=None)

        self.assertEqual(scheduled_volume['id'], 123)
        self.assertEqual(scheduled_volume['host'], 'host_9')

    def test_vsa_sched_create_volumes_simple(self):
        global scheduled_volumes
        scheduled_volumes = []
        self._set_service_states(host_num=10,
                                 drive_type_start_ix=0,
                                 drive_type_num=5,
                                 init_num_drives=10,
                                 exclude_host_list=['host_1', 'host_3'])
        prev = self._generate_default_service_states()
        request_spec = self._get_vol_creation_request(num_vols=3, drive_ix=2)

        self._print_service_states()

        self.sched.schedule_create_volumes(self.context,
                                           request_spec,
                                           availability_zone=None)

        self.assertEqual(len(scheduled_volumes), 3)
        self.assertEqual(scheduled_volumes[0]['vol']['host'], 'host_9')
        self.assertEqual(scheduled_volumes[1]['vol']['host'], 'host_8')
        self.assertEqual(scheduled_volumes[2]['vol']['host'], 'host_7')

        cur = self._get_service_states()
        for host in ['host_9', 'host_8', 'host_7']:
            cur_dtype = cur[host]['volume']['drive_qos_info']['name_2']
            prev_dtype = prev[host]['volume']['drive_qos_info']['name_2']
            self.assertEqual(cur_dtype['DriveType'], prev_dtype['DriveType'])
            self.assertEqual(cur_dtype['FullDrive']['NumFreeDrives'],
                             prev_dtype['FullDrive']['NumFreeDrives'] - 1)
            self.assertEqual(cur_dtype['FullDrive']['NumOccupiedDrives'],
                             prev_dtype['FullDrive']['NumOccupiedDrives'] + 1)

    def test_vsa_sched_create_volumes_partition(self):
        global scheduled_volumes
        scheduled_volumes = []
        self._set_service_states(host_num=5,
                                 drive_type_start_ix=0,
                                 drive_type_num=5,
                                 init_num_drives=1,
                                 exclude_host_list=['host_0', 'host_2'])
        prev = self._generate_default_service_states()
        request_spec = self._get_vol_creation_request(num_vols=3,
                                                      drive_ix=3,
                                                      size=50)
        self.sched.schedule_create_volumes(self.context,
                                           request_spec,
                                           availability_zone=None)

        self.assertEqual(len(scheduled_volumes), 3)
        self.assertEqual(scheduled_volumes[0]['vol']['host'], 'host_4')
        self.assertEqual(scheduled_volumes[1]['vol']['host'], 'host_3')
        self.assertEqual(scheduled_volumes[2]['vol']['host'], 'host_1')

        cur = self._get_service_states()
        for host in ['host_1', 'host_3', 'host_4']:
            cur_dtype = cur[host]['volume']['drive_qos_info']['name_3']
            prev_dtype = prev[host]['volume']['drive_qos_info']['name_3']

            self.assertEqual(cur_dtype['DriveType'], prev_dtype['DriveType'])
            self.assertEqual(cur_dtype['FullDrive']['NumFreeDrives'],
                             prev_dtype['FullDrive']['NumFreeDrives'] - 1)
            self.assertEqual(cur_dtype['FullDrive']['NumOccupiedDrives'],
                             prev_dtype['FullDrive']['NumOccupiedDrives'] + 1)

            self.assertEqual(prev_dtype['PartitionDrive']
                                            ['NumOccupiedPartitions'], 0)
            self.assertEqual(cur_dtype['PartitionDrive']
                                            ['NumOccupiedPartitions'], 1)
            self.assertEqual(cur_dtype['PartitionDrive']
                                            ['NumFreePartitions'], 5)
            self.assertEqual(prev_dtype['PartitionDrive']
                                            ['NumFreePartitions'], 0)
            self.assertEqual(prev_dtype['PartitionDrive']
                                            ['PartitionSize'], 0)