def create_mock_mesos_offer(cpus, max_cores, is_windows, hostname): return Offer( "uri", "fid", "sid", create_mock_mesos_offer_aux(cpus, max_cores, is_windows, hostname))
def __register(self): python_version = sys.version_info.major headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } subscribe = { "type": "SUBSCRIBE", "subscribe": { "framework_info": { "user": self.frameworkUser, "name": self.frameworkName } } } if self.frameworkRole: subscribe['subscribe']['framework_info'][ 'role'] = self.frameworkRole if self.capabilities: subscribe['subscribe']['framework_info'][ 'capabilities'] = self.capabilities if self.failover_timeout: subscribe['subscribe']['framework_info'][ 'failover_timeout'] = self.failover_timeout if self.authenticate: subscribe['subscribe']['framework_info'][ 'principal'] = self.principal credentials = [{ 'principal': self.principal, 'secret': self.secret }] subscribe['subscribe']['credentials'] = credentials if self.frameworkId: subscribe['subscribe']['framework_info']['id'] = { 'value': self.frameworkId } subscribe['framework_id'] = {'value': self.frameworkId} ok = False self.long_pool = None while (not ok) and self.mesos_url_index < len(self.mesos_urls): try: self.mesos_url = self.mesos_urls[self.mesos_url_index] if self.mesos_url.startswith('zk://'): self.logger.debug( 'Use zookeeper url, try to detect master') zk_info = self.mesos_url.replace('zk://', '').split('/') zk_url = self.__zk_detect(zk_info[0], '/'.join(zk_info[1:])) if zk_url is None: raise Exception('Could not detect master in zookeeper') self.mesos_url = zk_url self.logger.warn('Try to connect to master: %s' % (self.mesos_url)) self.long_pool = requests.post(self.mesos_url + '/api/v1/scheduler', json.dumps(subscribe), stream=True, headers=headers) self.logger.debug("Subscribe HTTP answer: " + str(self.long_pool.status_code)) if self.long_pool.status_code == 307: # Not leader, reconnect to leader self.logger.info("Not master, connect to " + self.long_pool.headers['Location']) if 'Location' in self.long_pool.headers: self.mesos_url = self.long_pool.headers['Location'] self.long_pool = requests.post(self.mesos_url + '/api/v1/scheduler', json.dumps(subscribe), stream=True, headers=headers) ok = True self.mesos_url_index = 0 except Exception as e: self.mesos_url_index += 1 self.logger.exception('Mesos:Subscribe:Failed for %s: %s' % (self.mesos_url, str(e))) if self.long_pool is not None: if not self.long_pool.status_code == 200: self.logger.error('Mesos:Subscribe:Error: ' + str(self.long_pool.text)) return False else: self.logger.error( 'Mesos:Subscribe:Error: Failed to connect to a mesos master') return False self.streamId = self.long_pool.headers['Mesos-Stream-Id'] if self.disconnected: self.__event_reconnected() self.disconnected = False first_line = True for line in self.long_pool.iter_lines(): if self.stop or self.disconnect: if self.stop and self.driver: self.driver.tearDown() break # filter out keep-alive new lines if first_line: count_bytes = int(line) first_line = False continue else: if python_version == 3: line = line.decode('UTF-8') body = json.loads(line[:count_bytes]) self.logger.debug('Mesos:Event:%s' % (str(body['type']))) self.logger.debug('Mesos:Message:' + str(body)) if body['type'] == 'SUBSCRIBED': self.frameworkId = body['subscribed']['framework_id'][ 'value'] self.logger.info('Mesos:Subscribe:Framework-Id:' + self.frameworkId) self.logger.info('Mesos:Subscribe:Stream-Id:' + self.streamId) if 'master_info' in body['subscribed']: self.master_info = body['subscribed']['master_info'] self.__event_subscribed() elif body['type'] == 'OFFERS': mesos_offers = body['offers']['offers'] offers = [] for mesos_offer in mesos_offers: offers.append( Offer(self.mesos_url, frameworkId=self.frameworkId, streamId=self.streamId, mesosOffer=mesos_offer)) self.__event_offers(offers) elif body['type'] == 'UPDATE': mesos_update = body['update'] update_event = Update(self.mesos_url, frameworkId=self.frameworkId, streamId=self.streamId, mesosUpdate=mesos_update) update_event.ack() self.__event_update(mesos_update) elif body['type'] == 'ERROR': self.logger.error('Mesos:Error:' + body['error']['message']) self.__event_error(body['error']['message']) elif body['type'] == 'RESCIND': self.__event_callback(body['type'], body['rescind']) elif body['type'] == 'MESSAGE': self.__event_callback(body['type'], body['message']) elif body['type'] == 'FAILURE': self.__event_callback(body['type'], body['failure']) elif body['type'] == 'HEARTBEAT': self.logger.debug('Mesos:Heartbeat') else: self.logger.warn('%s event no yet implemented' % (str(body['type']))) if line[count_bytes:]: count_bytes = int(line[count_bytes:]) return True
def test_offer_match_against_matched_and_unmatched(self, register_agents, add_new_offers, add_new_offer_count, decline): """Validate resource reservations that don't match with ACCEPTED_RESOURCE_ROLE are ignored""" settings.ACCEPTED_RESOURCE_ROLE = 'service-account' offers_json = { u'offers': { u'offers': [{ u'domain': { u'fault_domain': { u'region': { u'name': u'us-east-2' }, u'zone': { u'name': u'us-east-2a' } } }, u'url': { u'path': u'/slave(1)', u'scheme': u'http', u'address': { u'ip': u'172.12.4.140', u'hostname': u'172.12.4.140', u'port': 5051 } }, u'hostname': u'172.12.4.140', u'framework_id': { u'value': u'6777f785-2d17-4a2c-9e06-2bf56efa3417-0003' }, u'agent_id': { u'value': u'6777f785-2d17-4a2c-9e06-2bf56efa3417-S3' }, u'id': { u'value': u'6777f785-2d17-4a2c-9e06-2bf56efa3417-O1693' }, u'resources': [{ u'role': u'service-account', u'scalar': { u'value': 4.0 }, u'type': u'SCALAR', u'name': u'cpus', u'reservation': { u'principal': u'service-account' } }, { u'ranges': { u'range': [{ u'begin': 1025, u'end': 2180 }, { u'begin': 2182, u'end': 3887 }, { u'begin': 3889, u'end': 5049 }, { u'begin': 5052, u'end': 8079 }, { u'begin': 8082, u'end': 8180 }, { u'begin': 8182, u'end': 32000 }] }, u'role': u'service-account', u'type': u'RANGES', u'name': u'ports', u'reservation': { u'principal': u'service-account' } }, { u'role': u'service-account', u'scalar': { u'value': 119396.0 }, u'type': u'SCALAR', u'name': u'disk', u'reservation': { u'principal': u'service-account' } }, { u'role': u'service-account', u'scalar': { u'value': 14861.0 }, u'type': u'SCALAR', u'name': u'mem', u'reservation': { u'principal': u'service-account' } }] }, { u'domain': { u'fault_domain': { u'region': { u'name': u'us-east-2' }, u'zone': { u'name': u'us-east-2a' } } }, u'url': { u'path': u'/slave(1)', u'scheme': u'http', u'address': { u'ip': u'172.12.4.140', u'hostname': u'172.12.4.140', u'port': 5051 } }, u'hostname': u'172.12.4.140', u'framework_id': { u'value': u'6777f785-2d17-4a2c-9e06-2bf56efa3417-0003' }, u'agent_id': { u'value': u'6777f785-2d17-4a2c-9e06-2bf56efa3417-S3' }, u'id': { u'value': u'6777f785-2d17-4a2c-9e06-2bf56efa3417-O1693' }, u'resources': [{ u'role': u'*', u'scalar': { u'value': 4.0 }, u'type': u'SCALAR', u'name': u'cpus' }, { u'ranges': { u'range': [{ u'begin': 1025, u'end': 2180 }, { u'begin': 2182, u'end': 3887 }, { u'begin': 3889, u'end': 5049 }, { u'begin': 5052, u'end': 8079 }, { u'begin': 8082, u'end': 8180 }, { u'begin': 8182, u'end': 32000 }] }, u'role': u'*', u'type': u'RANGES', u'name': u'ports' }, { u'role': u'*', u'scalar': { u'value': 119396.0 }, u'type': u'SCALAR', u'name': u'disk' }, { u'role': u'*', u'scalar': { u'value': 14861.0 }, u'type': u'SCALAR', u'name': u'mem' }] }] }, u'type': u'OFFERS' } offers = [ Offer('http://127.0.0.1', '', '', x) for x in offers_json['offers']['offers'] ] ScaleScheduler().offers(offers) register_agents.called_with( ['6777f785-2d17-4a2c-9e06-2bf56efa3417-S3']) add_new_offer_count.called_with(1) self.assertEquals(add_new_offers.call_count, 1) self.assertTrue(decline.called)