def testClientUpdateSave(self): """Test that a client host is capable of saving its attributes. Create a client host, set its attributes and verify that the attributes are saved properly by recreating a server host and checking them. @raises AssertionError: If the server host has the wrong attributes. """ hostname = 'h1' db_host = self.db_helper.create_host(hostname, leased=True) server_host_dict = rdb_hosts.RDBServerHostWrapper( db_host).wire_format() client_host = rdb_hosts.RDBClientHostWrapper(**server_host_dict) host_data = {'hostname': hostname, 'id': db_host.id} default_values = rdb_models.AbstractHostModel.provide_default_values( host_data) for k, v in default_values.iteritems(): self.assertTrue(server_host_dict[k] == v) updated_client_fields = { 'locked': True, 'leased': False, 'status': 'FakeStatus', 'invalid': True, 'protection': 1, 'dirty': True, } client_host.__dict__.update(updated_client_fields) client_host.save() updated_server_host = rdb_hosts.RDBServerHostWrapper( self.db_helper.get_host(hostname=hostname)[0]).wire_format() for k, v in updated_client_fields.iteritems(): self.assertTrue(updated_server_host[k] == v)
def testPlatformAndLabels(self): """Test that a client host returns the right platform and labels. @raises AssertionError: If client host cannot return the right platform and labels. """ platform_name = 'x86' label_names = ['a', 'b'] self.db_helper.create_label(name=platform_name, platform=True) server_host = rdb_hosts.RDBServerHostWrapper( self.db_helper.create_host('h1', deps=set(label_names + [platform_name]))) client_host = rdb_hosts.RDBClientHostWrapper( **server_host.wire_format()) platform, labels = client_host.platform_and_labels() self.assertTrue(platform == platform_name) self.assertTrue(set(labels) == set(label_names))
def get_hosts(host_ids): """Get information about the hosts with ids in host_ids. get_hosts is different from acquire_hosts in that it is completely oblivious to the leased state of a host. @param host_ids: A list of host_ids. @return: A list of rdb_hosts.RDBClientHostWrapper objects. @raises RDBException: If something goes wrong in making the request. """ request_manager = rdb_requests.BaseHostRequestManager( rdb_requests.HostRequest, rdb.get_hosts) for host_id in host_ids: request_manager.add_request(host_id=host_id) hosts = [] for host in request_manager.response(): hosts.append(rdb_hosts.RDBClientHostWrapper(**host) if host else None) return hosts
def testWireFormat(self): """Test that we can create a client host with the server host's fields. Get the wire_format fields of an RDBServerHostWrapper and use them to create an RDBClientHostWrapper. @raises AssertionError: If the labels and acls don't match up after going through the complete wire_format conversion, of the bare wire_format conversion also converts labels and acls. @raises RDBException: If some critical fields were lost during wire_format conversion, as we won't be able to construct the RDBClientHostWrapper. """ labels = set(['a', 'b', 'c']) acls = set(['d', 'e']) server_host = rdb_hosts.RDBServerHostWrapper( self.db_helper.create_host('h1', deps=labels, acls=acls)) acl_ids = set([ aclgroup.id for aclgroup in self.db_helper.get_acls(name__in=acls) ]) label_ids = set( [label.id for label in self.db_helper.get_labels(name__in=labels)]) # The RDBServerHostWrapper keeps ids of labels/acls to perform # comparison operations within the rdb, but converts labels to # strings because this is the format the scheduler expects them in. self.assertTrue( set(server_host.labels) == label_ids and set(server_host.acls) == acl_ids) formatted_server_host = server_host.wire_format() client_host = rdb_hosts.RDBClientHostWrapper(**formatted_server_host) self.assertTrue( set(client_host.labels) == labels and set(client_host.acls) == acl_ids) bare_formatted_server_host = server_host.wire_format( unwrap_foreign_keys=False) self.assertTrue( bare_formatted_server_host.get('labels') is None and bare_formatted_server_host.get('acls') is None)
def acquire_hosts(queue_entries, suite_min_duts=None): """Acquire hosts for the list of queue_entries. The act of acquisition involves leasing a host from the rdb. @param queue_entries: A list of queue_entries that need hosts. @param suite_min_duts: A dictionary that maps suite job id to the minimum number of duts required. @yield: An rdb_hosts.RDBClientHostWrapper for each host acquired on behalf of a queue_entry, or None if a host wasn't found. @raises RDBException: If something goes wrong making the request. """ job_query_manager = JobQueryManager(queue_entries, suite_min_duts) request_manager = rdb_requests.BaseHostRequestManager( rdb_requests.AcquireHostRequest, rdb.rdb_host_request_dispatcher) for entry in queue_entries: request_manager.add_request(**job_query_manager.get_job_info(entry)) for host in request_manager.response(): yield (rdb_hosts.RDBClientHostWrapper(**host) if host else None)
def testUpdateField(self): """Test that update field on the client host works as expected. @raises AssertionError: If a bad update is processed without an exception, of a good update isn't processed as expected. """ hostname = 'h1' db_host = self.db_helper.create_host(hostname, dirty=False) server_host_dict = rdb_hosts.RDBServerHostWrapper( db_host).wire_format() client_host = rdb_hosts.RDBClientHostWrapper(**server_host_dict) self.assertRaises(rdb_utils.RDBException, client_host.update_field, *('id', 'fakeid')) self.assertRaises(rdb_utils.RDBException, client_host.update_field, *('Nonexist', 'Nonexist')) client_host.update_field('dirty', True) self.assertTrue( self.db_helper.get_host(hostname=hostname)[0].dirty == True and client_host.dirty == True) new_status = 'newstatus' client_host.set_status(new_status) self.assertTrue( self.db_helper.get_host(hostname=hostname)[0].status == new_status and client_host.status == new_status)
def testBatchGetHosts(self): """Test getting hosts. Verify that: 1. We actually call get_hosts on the query_manager for a batched_get_hosts request. 2. The hosts returned are matched up correctly with requests, and each request gets exactly one response. 3. The hosts returned have all the fields needed to create an RDBClientHostWrapper, in spite of having gone through the to_wire process of serialization in get_response. """ fake_hosts = [] for host_id in range(1, 4): self.get_hosts_manager.add_request(host_id=host_id) fake_hosts.append( rdb_testing_utils.FakeHost('host%s' % host_id, host_id)) self.handler.host_query_manager.get_hosts = mock.MagicMock( return_value=fake_hosts) self.handler.batch_get_hosts(self.get_hosts_manager.request_queue) for request, hosts in self.handler.get_response().iteritems(): self.assertTrue(len(hosts) == 1) client_host = rdb_hosts.RDBClientHostWrapper(**hosts[0]) self.assertTrue(request.host_id == client_host.id)