def register(self, deployment_id): """ Allows users to register their AppScale deployment with the AppScale Portal. Raises: AppScaleException: If the deployment has already been registered. """ appscale_yaml = yaml.safe_load(self.read_appscalefile()) if 'keyname' in appscale_yaml: keyname = appscale_yaml['keyname'] else: keyname = 'appscale' nodes = self.get_nodes(keyname) head_node = self.get_head_node(nodes) if RegistrationHelper.appscale_has_deployment_id(head_node, keyname): existing_id = RegistrationHelper.get_deployment_id(head_node, keyname) if existing_id != deployment_id: raise AppScaleException( 'This deployment has already been registered with a different ID.') if 'infrastructure' in appscale_yaml: deployment_type = 'cloud' else: deployment_type = 'cluster' deployment = RegistrationHelper.update_deployment(deployment_type, nodes, deployment_id) RegistrationHelper.set_deployment_id(head_node, keyname, deployment_id) AppScaleLogger.success( 'Registration complete for AppScale deployment {0}.' .format(deployment['name']))
def test_set_deployment_id(self): head_node = 'boo' keyname = 'bar' flexmock(LocalState).should_receive('get_secret_key').and_return() deployment_id = 'blarg' # Given the deployment ID, the function should return successfully. flexmock(AppControllerClient).should_receive('set_deployment_id')\ .with_args(deployment_id).and_return() RegistrationHelper.set_deployment_id(head_node, keyname, deployment_id)
def test_appscale_has_deployment_id(self): head_node = 'boo' keyname = 'bar' flexmock(LocalState).should_receive('get_secret_key').and_return('baz') # When the AppControllerClient returns True, the function should # return True. flexmock(AppControllerClient).should_receive('deployment_id_exists')\ .and_return(True) self.assertEqual( RegistrationHelper.appscale_has_deployment_id(head_node, keyname), True) # When the AppControllerClient returns False, the function should # return False. flexmock(AppControllerClient).should_receive('deployment_id_exists')\ .and_return(False) self.assertEqual( RegistrationHelper.appscale_has_deployment_id(head_node, keyname), False)
def test_get_deployment_id(self): head_node = 'boo' keyname = 'bar' flexmock(LocalState).should_receive('get_secret_key').and_return('baz') deployment_id = 'blarg' # The function should return what the AppControllerClient returns. flexmock(AppControllerClient).should_receive('get_deployment_id')\ .and_return(deployment_id) self.assertEqual( RegistrationHelper.get_deployment_id(head_node, keyname), deployment_id)
def test_update_deployment(self): deployment = { 'deployment_id': 'boo', 'deployment_type': 'cluster', 'nodes': [{ 'public_ip': 'public1', 'jobs': ['appengine'] }] } # When the portal returns a HTTP_NOTFOUND, the tools should raise # an AppScaleException. http_error = urllib2.HTTPError( 'boo', RegistrationHelper.HTTP_NOTFOUND, 'bar', 'baz', flexmock(read=lambda: 'blarg', readline=lambda: 'blarg')) flexmock(urllib2).should_receive('urlopen').and_raise(http_error) with self.assertRaises(AppScaleException): RegistrationHelper.update_deployment(deployment['deployment_type'], deployment['nodes'], deployment['deployment_id']) # When the portal returns a HTTP_BADREQUEST, the tools should raise an # AppScaleException. http_error = urllib2.HTTPError( 'boo', RegistrationHelper.HTTP_BADREQUEST, 'bar', 'baz', flexmock(read=lambda: 'blarg', readline=lambda: 'blarg')) flexmock(urllib2).should_receive('urlopen').and_raise(http_error) with self.assertRaises(AppScaleException): RegistrationHelper.update_deployment(deployment['deployment_type'], deployment['nodes'], deployment['deployment_id']) # When the POST to the server completes, the function should return a # dictionary with the deployment info. flexmock(urllib2).should_receive('urlopen')\ .and_return(flexmock(read=lambda: json.dumps(deployment))) self.assertEqual( deployment, RegistrationHelper.update_deployment(deployment['deployment_type'], deployment['nodes'], deployment['deployment_id']))
def test_update_deployment(self): deployment = { 'deployment_id': 'boo', 'deployment_type': 'cluster', 'nodes': [{'public_ip': 'public1', 'jobs': ['appengine']}] } # When the portal returns a HTTP_NOTFOUND, the tools should raise # an AppScaleException. http_error = urllib2.HTTPError('boo', RegistrationHelper.HTTP_NOTFOUND, 'bar', 'baz', flexmock(read=lambda: 'blarg', readline=lambda: 'blarg')) flexmock(urllib2).should_receive('urlopen').and_raise(http_error) with self.assertRaises(AppScaleException): RegistrationHelper.update_deployment(deployment['deployment_type'], deployment['nodes'], deployment['deployment_id']) # When the portal returns a HTTP_BADREQUEST, the tools should raise an # AppScaleException. http_error = urllib2.HTTPError('boo', RegistrationHelper.HTTP_BADREQUEST, 'bar', 'baz', flexmock(read=lambda: 'blarg', readline=lambda: 'blarg')) flexmock(urllib2).should_receive('urlopen').and_raise(http_error) with self.assertRaises(AppScaleException): RegistrationHelper.update_deployment(deployment['deployment_type'], deployment['nodes'], deployment['deployment_id']) # When the POST to the server completes, the function should return a # dictionary with the deployment info. flexmock(urllib2).should_receive('urlopen')\ .and_return(flexmock(read=lambda: json.dumps(deployment))) self.assertEqual( deployment, RegistrationHelper.update_deployment( deployment['deployment_type'], deployment['nodes'], deployment['deployment_id'] ) )