# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# 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 Scheduler
"""
import logging

from twisted.internet import defer

from nova import db
from nova import flags
from nova import service
from nova import test
from nova import utils
from nova.auth import manager as auth_manager
from nova.scheduler import manager


FLAGS = flags.FLAGS


class SchedulerTestCase(test.TrialTestCase):
    """Test case for scheduler"""
    def setUp(self):  # pylint: disable-msg=C0103
        super(SchedulerTestCase, self).setUp()
        self.flags(connection_type='fake',
                   scheduler_driver='nova.scheduler.simple.SimpleScheduler')
        self.scheduler = manager.SchedulerManager()
        self.context = None
        self.manager = auth_manager.AuthManager()
        self.user = self.manager.create_user('fake', 'fake', 'fake')
        self.project = self.manager.create_project('fake', 'fake', 'fake')
        self.context = None

    def tearDown(self):  # pylint: disable-msg=C0103
        self.manager.delete_user(self.user)
        self.manager.delete_project(self.project)

    def _create_instance(self):
        """Create a test instance"""
        inst = {}
        inst['image_id'] = 'ami-test'
        inst['reservation_id'] = 'r-fakeres'
        inst['launch_time'] = '10'
        inst['user_id'] = self.user.id
        inst['project_id'] = self.project.id
        inst['instance_type'] = 'm1.tiny'
        inst['mac_address'] = utils.generate_mac()
        inst['ami_launch_index'] = 0
        return db.instance_create(self.context, inst)

    def test_hosts_are_up(self):
        # NOTE(vish): constructing service without create method
        #             because we are going to use it without queue
        service1 = service.Service('host1',
                                   'nova-compute',
                                   'compute',
                                   FLAGS.compute_manager)
        service2 = service.Service('host2',
                                   'nova-compute',
                                   'compute',
                                   FLAGS.compute_manager)
        service1.report_state()
        service2.report_state()
        hosts = self.scheduler.driver.hosts_up(self.context, 'compute')
        self.assertEqual(len(hosts), 2)

    def test_least_busy_host_gets_instance(self):
        # NOTE(vish): constructing service without create method
        #             because we are going to use it without queue
        service1 = service.Service('host1',
                                   'nova-compute',
                                   'compute',
                                   FLAGS.compute_manager)
        service2 = service.Service('host2',
                                   'nova-compute',
                                   'compute',
                                   FLAGS.compute_manager)
        service1.report_state()
        service2.report_state()
        instance_id = self._create_instance()
        FLAGS.host = 'host1'
        service1.run_instance(self.context,
                              instance_id)
        print type(self.scheduler.driver)
        host = self.scheduler.driver.pick_compute_host(self.context,
                                                       instance_id)
        self.assertEqual(host, 'host2')

@@ -1047,29 +1047,119 @@
 ger%0A
-%0A%0AFLAGS = flags.FLAGS
+from nova.scheduler import driver%0A%0A%0AFLAGS = flags.FLAGS%0Aflags.DECLARE('max_instances', 'nova.scheduler.simple')
 %0A%0A%0Ac
@@ -1372,24 +1372,60 @@
 ype='fake',%0A
+                   max_instances=4,%0A
             
@@ -2969,32 +2969,144 @@
 ompute_manager)%0A
+        hosts = self.scheduler.driver.hosts_up(self.context, 'compute')%0A        self.assertEqual(len(hosts), 0)%0A
         service1
@@ -3320,140 +3320,8 @@
 f):%0A
-        # NOTE(vish): constructing service without create method%0A        #             because we are going to use it without queue%0A
     
@@ -3836,28 +3836,233 @@
     
-FLAGS.host =
+service1.run_instance(self.context, instance_id)%0A        host = self.scheduler.driver.pick_compute_host(self.context,%0A                                                       instance_id)%0A        self.assertEqual(host,
  'host
-1'
+2')
 %0A   
@@ -4067,35 +4067,41 @@
        service1.
-run
+terminate
 _instance(self.c
@@ -4107,39 +4107,265 @@
 context,
-%0A                      
+ instance_id)%0A%0A    def test_too_many_instances(self):%0A        service1 = service.Service('host',%0A                                  'nova-compute',%0A                                  'compute',%0A                                  FLAGS.compute_manager)%0A
         
@@ -4367,33 +4367,38 @@
      instance_id
-)
+s = %5B%5D
 %0A        print t
@@ -4394,56 +4394,293 @@
     
-print type(self.scheduler.driver)%0A        host =
+for index in xrange(FLAGS.max_instances):%0A            instance_ids.append(self._create_instance())%0A            service1.run_instance(self.context, instance_ids%5Bindex%5D)%0A        instance_id = self._create_instance()%0A        self.assertRaises(driver.NoValidHost,%0A                         
  sel
@@ -4711,37 +4711,24 @@
 compute_host
-(self.context
 ,%0A          
@@ -4735,35 +4735,46 @@
                 
-   
+self.context,%0A
                 
@@ -4808,36 +4808,104 @@
     
-self.assertEqual(host, 'host2'
+for instance_id in instance_ids:%0A            service1.terminate_instance(self.context, instance_id
 )%0A

fix test to specify host