Example #1
0
    def test_upload_java_app_with_no_appid(self):
        # add in mocks so that there is an appengine-web.xml, but with no appid set
        flexmock(os.path)
        os.path.should_call('exists')
        os.path.should_receive('exists').with_args(
            AppEngineHelper.get_app_yaml_location(
                self.app_dir)).and_return(False)
        appengine_web_xml_location = AppEngineHelper.get_appengine_web_xml_location(
            self.app_dir)
        os.path.should_receive('exists').with_args(
            AppEngineHelper.get_appengine_web_xml_location(
                self.app_dir)).and_return(True)
        flexmock(AppEngineHelper).should_receive(
            'get_app_id_from_app_config').and_return('app_id')
        flexmock(AppEngineHelper).should_receive(
            'get_app_runtime_from_app_config').and_return('runtime')
        flexmock(LocalState).should_receive('get_secret_key').and_return()

        # mock out reading the app.yaml file
        builtins = flexmock(sys.modules['__builtin__'])
        builtins.should_call('open')  # set the fall-through

        fake_appengine_web_xml = flexmock(name="fake_appengine_web_xml")
        fake_appengine_web_xml.should_receive('read').and_return(
            "<baz></baz>\n" + "<application></application>")
        builtins.should_receive('open').with_args(appengine_web_xml_location, 'r') \
          .and_return(fake_appengine_web_xml)

        argv = ["--keyname", self.keyname, "--file", self.app_dir]
        options = ParseArgs(argv, self.function).args
        self.assertRaises(AppEngineConfigException, AppScaleTools.upload_app,
                          options)
  def test_upload_java_app_with_no_appid(self):
    # add in mocks so that there is an appengine-web.xml, but with no appid set
    flexmock(os.path)
    os.path.should_call('exists')
    os.path.should_receive('exists').with_args(
      AppEngineHelper.get_app_yaml_location(self.app_dir)).and_return(False)
    appengine_web_xml_location = AppEngineHelper.get_appengine_web_xml_location(
      self.app_dir)
    os.path.should_receive('exists').with_args(
      AppEngineHelper.get_appengine_web_xml_location(self.app_dir)).and_return(True)
    flexmock(AppEngineHelper).should_receive('get_app_id_from_app_config').and_return('app_id')
    flexmock(AppEngineHelper).should_receive('get_app_runtime_from_app_config').and_return('runtime')
    flexmock(LocalState).should_receive('get_secret_key').and_return()

    # mock out reading the app.yaml file
    builtins = flexmock(sys.modules['__builtin__'])
    builtins.should_call('open')  # set the fall-through

    fake_appengine_web_xml = flexmock(name="fake_appengine_web_xml")
    fake_appengine_web_xml.should_receive('read').and_return("<baz></baz>\n" +
      "<application></application>")
    builtins.should_receive('open').with_args(appengine_web_xml_location, 'r') \
      .and_return(fake_appengine_web_xml)

    argv = [
      "--keyname", self.keyname,
      "--file", self.app_dir
    ]
    options = ParseArgs(argv, self.function).args
    self.assertRaises(AppEngineConfigException, AppScaleTools.upload_app, options)
  def test_upload_app_with_non_alpha_appid(self):
    # add in mocks so that there is an app.yaml, but with no appid set
    flexmock(os.path)
    os.path.should_call('exists')
    app_yaml_location = AppEngineHelper.get_app_yaml_location(self.app_dir)
    os.path.should_receive('exists').with_args(app_yaml_location) \
      .and_return(True)

    # mock out reading the app.yaml file
    builtins = flexmock(sys.modules['__builtin__'])
    builtins.should_call('open')  # set the fall-through

    fake_app_yaml = flexmock(name="fake_app_yaml")
    fake_app_yaml.should_receive('read').and_return(yaml.dump({
      'application' : 'baz*',
      'runtime' : 'python'
    }))
    builtins.should_receive('open').with_args(app_yaml_location, 'r') \
      .and_return(fake_app_yaml)

    argv = [
      "--keyname", self.keyname,
      "--file", self.app_dir
    ]
    options = ParseArgs(argv, self.function).args
    self.assertRaises(AppEngineConfigException, AppScaleTools.upload_app, options)
Example #4
0
    def test_upload_app_with_non_alpha_appid(self):
        # add in mocks so that there is an app.yaml, but with no appid set
        flexmock(os.path)
        os.path.should_call('exists')
        app_yaml_location = AppEngineHelper.get_app_yaml_location(self.app_dir)
        os.path.should_receive('exists').with_args(app_yaml_location) \
          .and_return(True)

        # mock out reading the app.yaml file
        builtins = flexmock(sys.modules['__builtin__'])
        builtins.should_call('open')  # set the fall-through

        fake_app_yaml = flexmock(name="fake_app_yaml")
        fake_app_yaml.should_receive('read').and_return(
            yaml.dump({
                'application': 'baz*',
                'runtime': 'python'
            }))
        builtins.should_receive('open').with_args(app_yaml_location, 'r') \
          .and_return(fake_app_yaml)

        argv = ["--keyname", self.keyname, "--file", self.app_dir]
        options = ParseArgs(argv, self.function).args
        self.assertRaises(AppEngineConfigException, AppScaleTools.upload_app,
                          options)
Example #5
0
    def test_upload_app_with_no_app_yaml_or_appengine_web_xml(self):
        # all app engine apps must have a config file - abort if we can't find one

        # add in mocks so that the config files aren't found
        flexmock(os.path)
        os.path.should_call('exists')
        os.path.should_receive('exists').with_args(
            AppEngineHelper.get_app_yaml_location(
                self.app_dir)).and_return(False)
        os.path.should_receive('exists').with_args(
          AppEngineHelper.get_appengine_web_xml_location(self.app_dir)) \
          .and_return(False)

        argv = ["--keyname", self.keyname, "--file", self.app_dir]
        options = ParseArgs(argv, self.function).args
        self.assertRaises(AppEngineConfigException, AppScaleTools.upload_app,
                          options)
  def test_upload_app_with_no_app_yaml_or_appengine_web_xml(self):
    # all app engine apps must have a config file - abort if we can't find one

    # add in mocks so that the config files aren't found
    flexmock(os.path)
    os.path.should_call('exists')
    os.path.should_receive('exists').with_args(
      AppEngineHelper.get_app_yaml_location(self.app_dir)).and_return(False)
    os.path.should_receive('exists').with_args(
      AppEngineHelper.get_appengine_web_xml_location(self.app_dir)) \
      .and_return(False)

    argv = [
      "--keyname", self.keyname,
      "--file", self.app_dir
    ]
    options = ParseArgs(argv, self.function).args
    self.assertRaises(AppEngineConfigException, AppScaleTools.upload_app, options)
 def test_is_valid_ipv4_address(self):
   to_test = {'192.168.33.10': True,
              '10.10.1.0': True,
              'node-1': False,
              '2001:0DB8:AC10:FE01::': False}
   for test_ip, should_be_valid in to_test.iteritems():
     self.assertEqual(AppEngineHelper.is_valid_ipv4_address(test_ip),
                      should_be_valid,
                      '{} should be {}'.format(test_ip, should_be_valid))
    def test_java_good_sdk_version(self):
        target_jar = AppEngineHelper.JAVA_SDK_JAR_PREFIX + '-' \
          + AppEngineHelper.SUPPORTED_SDK_VERSION + '.jar'
        good_jars = ['test.jar', target_jar]

        aeh = flexmock(AppEngineHelper)
        aeh.should_receive('get_appengine_lib_locations').and_return(['blah'])
        flexmock(os)
        os.should_receive('listdir').and_return(good_jars)
        self.assertEquals(False, AppEngineHelper.is_sdk_mismatch(''))
  def test_java_good_sdk_version(self):
    target_jar = AppEngineHelper.JAVA_SDK_JAR_PREFIX + '-' \
      + AppEngineHelper.SUPPORTED_SDK_VERSION + '.jar'
    good_jars = ['test.jar', target_jar]

    aeh = flexmock(AppEngineHelper)
    aeh.should_receive('get_appengine_lib_locations').and_return(['blah'])
    flexmock(os)
    os.should_receive('listdir').and_return(good_jars)
    self.assertEquals(False, AppEngineHelper.is_sdk_mismatch(''))
Example #10
0
 def test_is_valid_ipv4_address(self):
     to_test = {
         '192.168.33.10': True,
         '10.10.1.0': True,
         'node-1': False,
         '2001:0DB8:AC10:FE01::': False
     }
     for test_ip, should_be_valid in to_test.iteritems():
         self.assertEqual(
             AppEngineHelper.is_valid_ipv4_address(test_ip),
             should_be_valid,
             '{} should be {}'.format(test_ip, should_be_valid))
Example #11
0
 def get_ips_from_options(ips):
   """ Gets ips from run time options and validates that they are valid ip
   addresses.
   Args:
     ips: A list or dict containing the ips attribute of the run time options.
   Raises:
     AssertionError if any ip addresses are not valid.
   """
   try:
     all_ips = set(ips.values())
   except AttributeError:
     all_ips = set()
     for node_set in ips:
       all_ips.update(node_set['nodes'] if isinstance(node_set['nodes'], list)\
                      else [node_set['nodes']])
   assert all(AppEngineHelper.is_valid_ipv4_address(ip) for ip in all_ips), \
     'Invalid IP address in {}'.format(all_ips)
   return all_ips
Example #12
0
 def get_ips_from_options(ips):
     """ Gets ips from run time options and validates that they are valid ip
 addresses.
 Args:
   ips: A list or dict containing the ips attribute of the run time options.
 Raises:
   AssertionError if any ip addresses are not valid.
 """
     try:
         all_ips = set(ips.values())
     except AttributeError:
         all_ips = set()
         for node_set in ips:
             all_ips.update(node_set['nodes'] if isinstance(node_set['nodes'], list)\
                            else [node_set['nodes']])
     assert all(AppEngineHelper.is_valid_ipv4_address(ip) for ip in all_ips), \
       'Invalid IP address in {}'.format(all_ips)
     return all_ips
  def upload_app(cls, options):
    """Uploads the given App Engine application into AppScale.

    Args:
      options: A Namespace that has fields for each parameter that can be
        passed in via the command-line interface.
    Returns:
      A tuple containing the host and port where the application is serving
        traffic from.
    """
    custom_service_yaml = None
    if cls.TAR_GZ_REGEX.search(options.file):
      file_location = LocalState.extract_tgz_app_to_dir(options.file)
      created_dir = True
      version = Version.from_tar_gz(options.file)
    elif cls.ZIP_REGEX.search(options.file):
      file_location = LocalState.extract_zip_app_to_dir(options.file)
      created_dir = True
      version = Version.from_zip(options.file)
    elif os.path.isdir(options.file):
      file_location = options.file
      created_dir = False
      version = Version.from_directory(options.file)
    elif options.file.endswith('.yaml'):
      file_location = os.path.dirname(options.file)
      created_dir = False
      version = Version.from_yaml_file(options.file)
      custom_service_yaml = options.file
    else:
      raise AppEngineConfigException('{0} is not a tar.gz file, a zip file, ' \
        'or a directory. Please try uploading either a tar.gz file, a zip ' \
        'file, or a directory.'.format(options.file))

    if options.project:
      if version.runtime == 'java':
        raise BadConfigurationException("AppScale doesn't support --project for"
          "Java yet. Please specify the application id in appengine-web.xml.")

      version.project_id = options.project

    if version.project_id is None:
      if version.config_type == 'app.yaml':
        message = 'Specify --project or define "application" in your app.yaml'
      else:
        message = 'Define "application" in your appengine-web.xml'

      raise AppEngineConfigException(message)

    # Let users know that versions are not supported yet.
    AppEngineHelper.warn_if_version_defined(version, options.test)

    AppEngineHelper.validate_app_id(version.project_id)

    extras = {}
    if version.runtime == 'go':
      extras = LocalState.get_extra_go_dependencies(options.file, options.test)

    if (version.runtime == 'java'
        and AppEngineHelper.is_sdk_mismatch(file_location)):
      AppScaleLogger.warn(
        'AppScale did not find the correct SDK jar versions in your app. The '
        'current supported SDK version is '
        '{}.'.format(AppEngineHelper.SUPPORTED_SDK_VERSION))

    head_node_public_ip = LocalState.get_host_with_role(
      options.keyname, 'shadow')
    secret_key = LocalState.get_secret_key(options.keyname)
    admin_client = AdminClient(head_node_public_ip, secret_key)

    remote_file_path = RemoteHelper.copy_app_to_host(
      file_location, version.project_id, options.keyname,
      extras, custom_service_yaml)

    AppScaleLogger.log(
      'Deploying service {} for {}'.format(version.service_id,
                                           version.project_id))
    operation_id = admin_client.create_version(version, remote_file_path)

    # now that we've told the AppController to start our app, find out what port
    # the app is running on and wait for it to start serving
    AppScaleLogger.log("Please wait for your app to start serving.")

    deadline = time.time() + cls.MAX_OPERATION_TIME
    while True:
      if time.time() > deadline:
        raise AppScaleException('The deployment operation took too long.')
      operation = admin_client.get_operation(version.project_id, operation_id)
      if not operation['done']:
        time.sleep(1)
        continue

      if 'error' in operation:
        raise AppScaleException(operation['error']['message'])
      version_url = operation['response']['versionUrl']
      break

    AppScaleLogger.success(
      'Your app can be reached at the following URL: {}'.format(version_url))

    if created_dir:
      shutil.rmtree(file_location)

    match = re.match('http://(.+):(\d+)', version_url)
    login_host = match.group(1)
    http_port = int(match.group(2))
    return login_host, http_port
 def test_java_bad_sdk_version(self):
   bad_jars = ['test.jar', 'appengine-api-1.0-sdk-1.7.3.jar']
   flexmock(os)
   os.should_receive('listdir').and_return(bad_jars)
   self.assertEquals(True, AppEngineHelper.is_sdk_mismatch(''))
  def test_upload_php_app_successfully(self):
    app_dir = '/tmp/appscale-app-1234'

    # add in mocks so that the gzip'ed file gets extracted to /tmp
    # as well as for removing it later
    flexmock(os)
    os.should_receive('mkdir').with_args(app_dir) \
      .and_return(True)
    flexmock(shutil)
    shutil.should_receive('rmtree').with_args(app_dir).and_return()

    local_state = flexmock(LocalState)
    local_state.should_receive('shell')\
      .with_args(re.compile('tar zxvf'),False)\
      .and_return()

    # add in mocks so that there is an app.yaml, but with no appid set
    flexmock(os.path)
    os.path.should_call('exists')
    app_yaml_location = AppEngineHelper.get_app_yaml_location(app_dir)
    os.path.should_receive('exists').with_args(app_yaml_location) \
      .and_return(True)

    # mock out reading the app.yaml file
    builtins = flexmock(sys.modules['__builtin__'])
    builtins.should_call('open')  # set the fall-through

    fake_app_yaml = flexmock(name="fake_app_yaml")
    fake_app_yaml.should_receive('read').and_return(yaml.dump({
      'application' : 'baz',
      'runtime' : 'php'
    }))
    builtins.should_receive('open').with_args(app_yaml_location, 'r') \
      .and_return(fake_app_yaml)

    # Mock out service host and port
    app_data = {'owner' : '*****@*****.**',
      'hosts' : {'192.168.1.1' : { 'http' : 8080, 'https' : 4380 }}}
    app_stats_data = {'apps': {'baz': {'http': 8080, 'language': 'python27',
      'total_reqs': 'no_change', 'appservers': 1, 'https': 4380, 'reqs_enqueued': None}}}

    # mock out the SOAP call to the AppController and assume it succeeded
    fake_appcontroller = flexmock(name='fake_appcontroller')
    fake_appcontroller.should_receive('status').with_args('the secret') \
      .and_return('Database is at public1')
    fake_appcontroller.should_receive('done_uploading').with_args('baz',
      '/opt/appscale/apps/baz.tar.gz', 'the secret').and_return()
    fake_appcontroller.should_receive('update').with_args(['baz'],
      'the secret').and_return()
    fake_appcontroller.should_receive('is_app_running').with_args('baz',
      'the secret').and_return(False).and_return(True)
    fake_appcontroller.should_receive('does_user_exist').with_args(
      '*****@*****.**', 'the secret').and_return('true')
    fake_appcontroller.should_receive('does_user_exist').with_args(
      'a@public1', 'the secret').and_return('true')
    fake_appcontroller.should_receive('does_app_exist').with_args(
      'baz', 'the secret').and_return(json.dumps(app_data))
    fake_appcontroller.should_receive('get_app_data').with_args(
      'baz', 'the secret').and_return(json.dumps(app_data))
    fake_appcontroller.should_receive('get_all_stats').with_args(
      'the secret').and_return(json.dumps(app_stats_data))
    flexmock(SOAPpy)
    SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@a.com")
    flexmock(getpass)
    getpass.should_receive('getpass').and_return('aaaaaa')

    # mock out making the remote app directory
    local_state.should_receive('shell') \
      .with_args(re.compile('^ssh'), False, 5, stdin=re.compile('^mkdir -p')) \
      .and_return()

    # and mock out tarring and copying the app
    local_state.should_receive('shell') \
      .with_args(re.compile('tar -czf'), False) \
      .and_return()

    local_state.should_receive('shell') \
      .with_args(re.compile('/tmp/appscale-app-baz.tar.gz'), False, 5) \
      .and_return()

    # as well as removing the tar'ed app once we're done copying it
    flexmock(os)
    os.should_receive('remove').with_args('/tmp/appscale-app-baz-1234.tar.gz') \
      .and_return()

    os.should_receive('listdir').and_return(['app.yaml','index.py'])

    # and slap in a mock that says the app comes up after waiting for it
    # three times
    fake_socket = flexmock(name='fake_socket')
    fake_socket.should_receive('connect').with_args(('public1',
      8080)).and_raise(Exception).and_raise(Exception) \
      .and_return(None)
    flexmock(socket)
    socket.should_receive('socket').and_return(fake_socket)

    argv = [
      "--keyname", self.keyname,
      "--file", self.app_dir + ".tar.gz"
    ]
    options = ParseArgs(argv, self.function).args
    (host, port) = AppScaleTools.upload_app(options)
    self.assertEquals('public1', host)
    self.assertEquals(8080, port) 
  def test_upload_app_when_app_admin_not_this_user(self):
    # we don't let you upload an app if the appid is registered to someone else,
    # so abort

    # add in mocks so that there is an app.yaml, but with no appid set
    flexmock(os.path)
    os.path.should_call('exists')
    app_yaml_location = AppEngineHelper.get_app_yaml_location(self.app_dir)
    os.path.should_receive('exists').with_args(app_yaml_location) \
      .and_return(True)

    # mock out reading the app.yaml file
    builtins = flexmock(sys.modules['__builtin__'])
    builtins.should_call('open')  # set the fall-through

    fake_app_yaml = flexmock(name="fake_app_yaml")
    fake_app_yaml.should_receive('read').and_return(yaml.dump({
      'application' : 'baz',
      'runtime' : 'python27'
    }))
    builtins.should_receive('open').with_args(app_yaml_location, 'r') \
      .and_return(fake_app_yaml)

    # Mock out service host and port
    app_data = {'owner' : '*****@*****.**','hosts' : { }}
    app_data_not_admin = {'owner': '*****@*****.**'}

    # mock out the SOAP call to the AppController and assume it succeeded
    fake_appcontroller = flexmock(name='fake_appcontroller')
    fake_appcontroller.should_receive('status').with_args('the secret') \
      .and_return('Database is at public1')
    fake_appcontroller.should_receive('does_user_exist').with_args(
      '*****@*****.**', 'the secret').and_return('true')
    fake_appcontroller.should_receive('does_user_exist').with_args(
      'a@public1', 'the secret').and_return('true')
    fake_appcontroller.should_receive('does_app_exist').with_args(
      'baz', 'the secret').and_return(json.dumps(app_data))
    fake_appcontroller.should_receive('get_app_data').with_args(
      'baz', 'the secret').and_return(json.dumps(app_data_not_admin))
    flexmock(SOAPpy)
    SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@a.com")
    flexmock(getpass)
    getpass.should_receive('getpass').and_return('aaaaaa')

    argv = [
      "--keyname", self.keyname,
      "--file", self.app_dir
    ]
    options = ParseArgs(argv, self.function).args
    self.assertRaises(AppScaleException, AppScaleTools.upload_app, options)
Example #17
0
    def test_upload_app_when_app_exists_on_virt_cluster(self):
        # we do let you upload an app if it's already running

        # add in mocks so that there is an app.yaml with an appid set
        flexmock(os.path)
        os.path.should_call('exists')
        app_yaml_location = AppEngineHelper.get_app_yaml_location(self.app_dir)
        os.path.should_receive('exists').with_args(app_yaml_location) \
          .and_return(True)

        # mock out reading the app.yaml file
        builtins = flexmock(sys.modules['__builtin__'])
        builtins.should_call('open')  # set the fall-through

        fake_app_yaml = flexmock(name="fake_app_yaml")
        fake_app_yaml.should_receive('read').and_return(
            yaml.dump({
                'application': 'baz',
                'runtime': 'python27'
            }))
        builtins.should_receive('open').with_args(app_yaml_location, 'r') \
          .and_return(fake_app_yaml)

        # Mock out service host and port
        app_data = {
            'owner': '*****@*****.**',
            'hosts': {
                '192.168.1.1': {
                    'http': 8080,
                    'https': 4380
                }
            }
        }
        app_stats_data = {
            'apps': {
                'baz': {
                    'http': 8080,
                    'language': 'python27',
                    'total_reqs': 'no_change',
                    'appservers': 1,
                    'https': 4380,
                    'reqs_enqueued': None
                }
            }
        }

        # mock out the SOAP call to the AppController and assume it succeeded
        fake_appcontroller = flexmock(name='fake_appcontroller')
        fake_appcontroller.should_receive('status').with_args('the secret') \
          .and_return('Database is at public1')
        fake_appcontroller.should_receive('done_uploading').with_args(
            'baz', '/opt/appscale/apps/baz.tar.gz',
            'the secret').and_return('OK')
        fake_appcontroller.should_receive('update').with_args(
            ['baz'], 'the secret').and_return('OK')
        fake_appcontroller.should_receive('does_user_exist').with_args(
            '*****@*****.**', 'the secret').and_return('true')
        fake_appcontroller.should_receive('does_user_exist').with_args(
            'a@public1', 'the secret').and_return('true')
        fake_appcontroller.should_receive('does_app_exist').with_args(
            'baz', 'the secret').and_return(json.dumps(app_data))
        fake_appcontroller.should_receive('get_app_data').with_args(
            'baz', 'the secret').and_return(json.dumps(app_data))
        fake_appcontroller.should_receive('get_all_stats').with_args(
            'the secret').and_return(json.dumps(app_stats_data))
        flexmock(SOAPpy)
        SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@a.com")
        flexmock(getpass)
        getpass.should_receive('getpass').and_return('aaaaaa')

        # mock out making the remote app directory
        flexmock(subprocess)
        subprocess.should_receive('Popen').with_args(re.compile('mkdir -p'),
          shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \
          .and_return(self.success)

        # and mock out tarring and copying the app
        subprocess.should_receive('Popen').with_args(re.compile('tar -czhf'),
          shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \
          .and_return(self.success)

        subprocess.should_receive('Popen').with_args(re.compile(
          '/tmp/appscale-app-baz.tar.gz'),
          shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \
          .and_return(self.success)

        # as well as removing the tar'ed app once we're done copying it
        flexmock(os)
        os.should_receive('remove').with_args('/tmp/appscale-app-baz-1234.tar.gz') \
          .and_return()

        # and slap in a mock that says the app comes up after waiting for it
        # three times
        fake_socket = flexmock(name='fake_socket')
        fake_socket.should_receive('connect').with_args(('public1',
          8080)).and_raise(Exception).and_raise(Exception) \
          .and_return(None)
        flexmock(socket)
        socket.should_receive('socket').and_return(fake_socket)

        argv = ["--keyname", self.keyname, "--file", self.app_dir]
        options = ParseArgs(argv, self.function).args
        (host, port) = AppScaleTools.upload_app(options)
        self.assertEquals('public1', host)
        self.assertEquals(8080, port)
Example #18
0
    def test_upload_app_when_app_admin_not_this_user(self):
        # we don't let you upload an app if the appid is registered to someone else,
        # so abort

        # add in mocks so that there is an app.yaml, but with no appid set
        flexmock(os.path)
        os.path.should_call('exists')
        app_yaml_location = AppEngineHelper.get_app_yaml_location(self.app_dir)
        os.path.should_receive('exists').with_args(app_yaml_location) \
          .and_return(True)

        # mock out reading the app.yaml file
        builtins = flexmock(sys.modules['__builtin__'])
        builtins.should_call('open')  # set the fall-through

        fake_app_yaml = flexmock(name="fake_app_yaml")
        fake_app_yaml.should_receive('read').and_return(
            yaml.dump({
                'application': 'baz',
                'runtime': 'python27'
            }))
        builtins.should_receive('open').with_args(app_yaml_location, 'r') \
          .and_return(fake_app_yaml)

        # Mock out service host and port
        app_data = {'owner': '*****@*****.**', 'hosts': {}}
        app_data_not_admin = {'owner': '*****@*****.**'}

        # mock out the SOAP call to the AppController and assume it succeeded
        fake_appcontroller = flexmock(name='fake_appcontroller')
        fake_appcontroller.should_receive('status').with_args('the secret') \
          .and_return('Database is at public1')
        fake_appcontroller.should_receive('does_user_exist').with_args(
            '*****@*****.**', 'the secret').and_return('true')
        fake_appcontroller.should_receive('does_user_exist').with_args(
            'a@public1', 'the secret').and_return('true')
        fake_appcontroller.should_receive('does_app_exist').with_args(
            'baz', 'the secret').and_return(json.dumps(app_data))
        fake_appcontroller.should_receive('get_app_data').with_args(
            'baz', 'the secret').and_return(json.dumps(app_data_not_admin))
        flexmock(SOAPpy)
        SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@a.com")
        flexmock(getpass)
        getpass.should_receive('getpass').and_return('aaaaaa')

        argv = ["--keyname", self.keyname, "--file", self.app_dir]
        options = ParseArgs(argv, self.function).args
        self.assertRaises(AppScaleException, AppScaleTools.upload_app, options)
 def test_java_bad_sdk_version(self):
     bad_jars = ['test.jar', 'appengine-api-1.0-sdk-1.7.3.jar']
     flexmock(os)
     os.should_receive('listdir').and_return(bad_jars)
     self.assertEquals(True, AppEngineHelper.is_sdk_mismatch(''))
Example #20
0
    def test_upload_php_app_successfully(self):
        app_dir = '/tmp/appscale-app-1234'

        # add in mocks so that the gzip'ed file gets extracted to /tmp
        # as well as for removing it later
        flexmock(os)
        os.should_receive('mkdir').with_args(app_dir) \
          .and_return(True)
        flexmock(shutil)
        shutil.should_receive('rmtree').with_args(app_dir).and_return()

        local_state = flexmock(LocalState)
        local_state.should_receive('shell')\
          .with_args(re.compile('tar zxvf'),False)\
          .and_return()

        # add in mocks so that there is an app.yaml, but with no appid set
        flexmock(os.path)
        os.path.should_call('exists')
        app_yaml_location = AppEngineHelper.get_app_yaml_location(app_dir)
        os.path.should_receive('exists').with_args(app_yaml_location) \
          .and_return(True)

        # mock out reading the app.yaml file
        builtins = flexmock(sys.modules['__builtin__'])
        builtins.should_call('open')  # set the fall-through

        fake_app_yaml = flexmock(name="fake_app_yaml")
        fake_app_yaml.should_receive('read').and_return(
            yaml.dump({
                'application': 'baz',
                'runtime': 'php'
            }))
        builtins.should_receive('open').with_args(app_yaml_location, 'r') \
          .and_return(fake_app_yaml)

        # Mock out service host and port
        app_data = {
            'owner': '*****@*****.**',
            'hosts': {
                '192.168.1.1': {
                    'http': 8080,
                    'https': 4380
                }
            }
        }
        app_stats_data = {
            'apps': {
                'baz': {
                    'http': 8080,
                    'language': 'python27',
                    'total_reqs': 'no_change',
                    'appservers': 1,
                    'https': 4380,
                    'reqs_enqueued': None
                }
            }
        }

        remote_tarball = '/opt/appscale/apps/baz.tar.gz'

        # mock out the SOAP call to the AppController and assume it succeeded
        fake_appcontroller = flexmock(name='fake_appcontroller')
        fake_appcontroller.should_receive('status').with_args('the secret') \
          .and_return('Database is at public1')
        fake_appcontroller.should_receive('done_uploading').with_args(
            'baz', remote_tarball, 'the secret').and_return()
        fake_appcontroller.should_receive('update').with_args(
            ['baz'], 'the secret').and_return()
        fake_appcontroller.should_receive('does_user_exist').with_args(
            '*****@*****.**', 'the secret').and_return('true')
        fake_appcontroller.should_receive('does_user_exist').with_args(
            'a@public1', 'the secret').and_return('true')
        fake_appcontroller.should_receive('does_app_exist').with_args(
            'baz', 'the secret').and_return(json.dumps(app_data))
        fake_appcontroller.should_receive('get_app_data').with_args(
            'baz', 'the secret').and_return(json.dumps(app_data))
        fake_appcontroller.should_receive('get_all_stats').with_args(
            'the secret').and_return(json.dumps(app_stats_data))
        flexmock(SOAPpy)
        SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@a.com")
        flexmock(getpass)
        getpass.should_receive('getpass').and_return('aaaaaa')

        # mock out making the remote app directory
        local_state.should_receive('shell') \
          .with_args(re.compile('^ssh'), False, 5, stdin=re.compile('^mkdir -p')) \
          .and_return()

        # and mock out tarring and copying the app
        local_state.should_receive('shell') \
          .with_args(re.compile('tar -czf'), False) \
          .and_return()

        local_state.should_receive('shell') \
          .with_args(re.compile('/tmp/appscale-app-baz.tar.gz'), False, 5) \
          .and_return()

        # as well as removing the tar'ed app once we're done copying it
        flexmock(os)
        os.should_receive('remove').with_args('/tmp/appscale-app-baz-1234.tar.gz') \
          .and_return()

        os.should_receive('listdir').and_return(['app.yaml', 'index.py'])

        # and slap in a mock that says the app comes up after waiting for it
        # three times
        fake_socket = flexmock(name='fake_socket')
        fake_socket.should_receive('connect').with_args(('public1',
          8080)).and_raise(Exception).and_raise(Exception) \
          .and_return(None)
        flexmock(socket)
        socket.should_receive('socket').and_return(fake_socket)

        flexmock(RemoteHelper).should_receive('copy_app_to_host').\
          and_return(remote_tarball)

        argv = ["--keyname", self.keyname, "--file", self.app_dir + ".tar.gz"]
        options = ParseArgs(argv, self.function).args
        (host, port) = AppScaleTools.upload_app(options)
        self.assertEquals('public1', host)
        self.assertEquals(8080, port)