Lock the hosts on creation to avoid a race where the scheduler uses
the host before it gets ACL'ed.

Signed-off-by: Jean-Marc Eurin <jmeurin@google.com>



git-svn-id: http://test.kernel.org/svn/autotest/trunk@2544 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/cli/host.py b/cli/host.py
index ef6ed91..b3ea081 100755
--- a/cli/host.py
+++ b/cli/host.py
@@ -374,7 +374,7 @@
         super(host_create, self).__init__()
         self.parser.add_option('-l', '--lock',
                                help='Create the hosts as locked',
-                               action='store_true')
+                               action='store_true', default=False)
         self.parser.add_option('-u', '--unlock',
                                help='Create the hosts as '
                                'unlocked (default)',
@@ -401,11 +401,15 @@
         (options, leftover) = super(host_create, self).parse(flists)
 
         self._parse_lock_options(options)
+        self.locked = options.lock
         self.platform = getattr(options, 'platform', None)
         return (options, leftover)
 
 
     def _execute_add_one_host(self, host):
+        # Always add the hosts as locked to avoid the host
+        # being picked up by the scheduler before it's ACL'ed 
+        self.data['locked'] = True
         self.execute_rpc('add_host', hostname=host,
                          status="Ready", **self.data)
 
@@ -440,6 +444,10 @@
         if len(success):
             for acl in self.acls:
                 self.execute_rpc('acl_group_add_hosts', id=acl, hosts=success)
+
+            if not self.locked:
+                for host in success:
+                    self.execute_rpc('modify_host', id=host, locked=False)
         return success
 
 
diff --git a/cli/host_unittest.py b/cli/host_unittest.py
index 5af8bec..9539431 100755
--- a/cli/host_unittest.py
+++ b/cli/host_unittest.py
@@ -1198,5 +1198,44 @@
                      out_words_ok=['host0', 'host1'])
 
 
+    def test_execute_create_muliple_hosts_unlocked(self):
+        self.run_cmd(argv=['atest', 'host', 'create',
+                           '-b', 'label0', '--acls', 'acl0', 'host0', 'host1',
+                           '--ignore_site_file'],
+                     rpcs=[('get_labels', {'name': 'label0'},
+                            True,
+                            [{u'id': 4,
+                              u'platform': 0,
+                              u'name': u'label0',
+                              u'invalid': 0,
+                              u'kernel_config': u''}]),
+                           ('get_acl_groups', {'name': 'acl0'},
+                            True, []),
+                           ('add_acl_group', {'name': 'acl0'},
+                            True, 5),
+                           ('add_host', {'hostname': 'host1',
+                                         'status': 'Ready',
+                                         'locked': True},
+                            True, 42),
+                           ('host_add_labels', {'id': 'host1',
+                                                'labels': ['label0']},
+                            True, None),
+                           ('add_host', {'hostname': 'host0',
+                                         'status': 'Ready',
+                                         'locked': True},
+                            True, 42),
+                           ('host_add_labels', {'id': 'host0',
+                                                'labels': ['label0']},
+                            True, None),
+                           ('acl_group_add_hosts',
+                            {'id': 'acl0', 'hosts': ['host1', 'host0']},
+                            True, None),
+                           ('modify_host', {'id': 'host1', 'locked': False},
+                            True, None),
+                           ('modify_host', {'id': 'host0', 'locked': False},
+                            True, None)],
+                     out_words_ok=['host0', 'host1'])
+
+
 if __name__ == '__main__':
     unittest.main()