def test_copy_deployment_credentials_in_cloud(self):
    options = flexmock(
      keyname='key1',
      infrastructure='ec2',
      verbose=True,
    )

    local_state = flexmock(LocalState)
    remote_helper = flexmock(RemoteHelper)
    local_state.should_receive('get_secret_key_location').and_return()
    local_state.should_receive('get_key_path_from_name').and_return()
    local_state.should_receive('get_certificate_location').and_return()
    local_state.should_receive('get_private_key_location').and_return()

    remote_helper.should_receive('scp').and_return()
    local_state.should_receive('generate_ssl_cert').and_return()
    popen_object = flexmock(communicate=lambda: ['hash_id'])
    flexmock(subprocess).should_receive('Popen').and_return(popen_object)
    remote_helper.should_receive('ssh').and_return()
    flexmock(AppScaleLogger).should_receive('log').and_return()

    RemoteHelper.copy_deployment_credentials('public1', options)

    flexmock(GCEAgent).should_receive('get_secrets_type').\
      and_return(CredentialTypes.OAUTH)
    flexmock(os.path).should_receive('exists').and_return(True)

    options = flexmock(
      keyname='key1',
      infrastructure='gce',
      verbose=True,
    )
    local_state.should_receive('get_oauth2_storage_location').and_return()

    RemoteHelper.copy_deployment_credentials('public1', options)
    def test_copy_deployment_credentials_in_cloud(self):
        # mock out the scp'ing to public1 and assume they succeed
        local_state = flexmock(LocalState)
        local_state.should_receive("shell").with_args(re.compile("^scp .*secret.key"), True, 5).and_return().ordered()

        local_state.should_receive("shell").with_args(re.compile("^scp .*ssh.key"), True, 5).and_return().ordered()

        # mock out generating the private key
        local_state = flexmock(LocalState)
        local_state.should_receive("shell").with_args(re.compile("^openssl"), True, stdin=None).and_return().ordered()

        local_state.should_receive("shell").with_args(re.compile("^scp .*mycert.pem"), True, 5).and_return().ordered()

        local_state.should_receive("shell").with_args(re.compile("^scp .*mykey.pem"), True, 5).and_return().ordered()

        # next, mock out copying the private key and certificate
        local_state.should_receive("shell").with_args(
            re.compile("^ssh"), True, 5, stdin=re.compile("^mkdir -p")
        ).and_return().ordered()

        local_state.should_receive("shell").with_args(
            re.compile("^scp .*cloud1/mycert.pem"), True, 5
        ).and_return().ordered()

        local_state.should_receive("shell").with_args(
            re.compile("^scp .*cloud1/mykey.pem"), True, 5
        ).and_return().ordered()

        options = flexmock(name="options", keyname="bookey", infrastructure="ec2", verbose=True)
        RemoteHelper.copy_deployment_credentials("public1", options)
    def test_copy_deployment_credentials_in_cloud(self):
        options = flexmock(
            keyname='key1',
            infrastructure='ec2',
            verbose=True,
        )

        local_state = flexmock(LocalState)
        remote_helper = flexmock(RemoteHelper)
        local_state.should_receive('get_secret_key_location').and_return()
        local_state.should_receive('get_key_path_from_name').and_return()
        local_state.should_receive('get_certificate_location').and_return()
        local_state.should_receive('get_private_key_location').and_return()

        remote_helper.should_receive('scp').and_return()
        local_state.should_receive('generate_ssl_cert').and_return()
        popen_object = flexmock(communicate=lambda: ['hash_id'])
        flexmock(subprocess).should_receive('Popen').and_return(popen_object)
        remote_helper.should_receive('ssh').and_return()
        flexmock(AppScaleLogger).should_receive('log').and_return()

        RemoteHelper.copy_deployment_credentials('public1', options)

        options = flexmock(
            keyname='key1',
            infrastructure='gce',
            verbose=True,
        )
        local_state.should_receive('get_oauth2_storage_location').and_return()

        RemoteHelper.copy_deployment_credentials('public1', options)
 def test_copy_deployment_credentials_in_cloud(self):
   # mock out the scp'ing to public1 and assume they succeed
   local_state = flexmock(LocalState)
   local_state.should_receive('shell').and_return().ordered()
 
   options = flexmock(name='options', keyname='bookey', infrastructure='ec2',
     verbose=True)
   RemoteHelper.copy_deployment_credentials('public1', options)
  def test_copy_deployment_credentials_in_cloud(self):
    # mock out the scp'ing to public1 and assume they succeed
    subprocess.should_receive('Popen').with_args(re.compile('secret.key'),
      shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \
      .and_return(self.success)

    subprocess.should_receive('Popen').with_args(re.compile('ssh.key'),
      shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \
      .and_return(self.success)

    # mock out generating the private key
    flexmock(M2Crypto.RSA)
    fake_rsa_key = flexmock(name='fake_rsa_key')
    fake_rsa_key.should_receive('save_key').with_args(
      LocalState.get_private_key_location('bookey'), None)
    M2Crypto.RSA.should_receive('gen_key').and_return(fake_rsa_key)

    flexmock(M2Crypto.EVP)
    fake_pkey = flexmock(name='fake_pkey')
    fake_pkey.should_receive('assign_rsa').with_args(fake_rsa_key).and_return()
    M2Crypto.EVP.should_receive('PKey').and_return(fake_pkey)

    # and mock out generating the certificate
    flexmock(M2Crypto.X509)
    fake_cert = flexmock(name='fake_x509')
    fake_cert.should_receive('set_pubkey').with_args(fake_pkey).and_return()
    fake_cert.should_receive('set_subject')
    fake_cert.should_receive('set_issuer_name')
    fake_cert.should_receive('set_not_before')
    fake_cert.should_receive('set_not_after')
    fake_cert.should_receive('sign').with_args(fake_pkey, md="sha256")
    fake_cert.should_receive('save_pem').with_args(
      LocalState.get_certificate_location('bookey'))
    M2Crypto.X509.should_receive('X509').and_return(fake_cert)

    # next, mock out copying the private key and certificate
    subprocess.should_receive('Popen').with_args(re.compile('mycert.pem'),
      shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \
      .and_return(self.success)

    subprocess.should_receive('Popen').with_args(re.compile('mykey.pem'),
      shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \
      .and_return(self.success)

    subprocess.should_receive('Popen').with_args(re.compile('mkdir -p'),
      shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \
      .and_return(self.success)

    options = flexmock(name='options', keyname='bookey', infrastructure='ec2',
      verbose=True)
    RemoteHelper.copy_deployment_credentials('public1', options)
    def test_copy_deployment_credentials_in_cloud(self):
        # mock out the scp'ing to public1 and assume they succeed
        local_state = flexmock(LocalState)
        local_state.should_receive('shell')\
          .with_args(re.compile('^scp .*secret.key'),True,5)\
          .and_return().ordered()

        local_state.should_receive('shell')\
          .with_args(re.compile('^scp .*ssh.key'),True,5)\
          .and_return().ordered()

        # mock out generating the private key
        local_state = flexmock(LocalState)
        local_state.should_receive('shell')\
          .with_args(re.compile('^openssl'),True, stdin=None)\
          .and_return().ordered()

        local_state.should_receive('shell')\
          .with_args(re.compile('^scp .*mycert.pem'),True,5)\
          .and_return().ordered()

        local_state.should_receive('shell')\
          .with_args(re.compile('^scp .*mykey.pem'),True,5)\
          .and_return().ordered()

        # next, mock out copying the private key and certificate
        local_state.should_receive('shell')\
          .with_args(re.compile('^ssh'),True,5,stdin=re.compile('^mkdir -p'))\
          .and_return().ordered()

        local_state.should_receive('shell')\
          .with_args(re.compile('^scp .*cloud1/mycert.pem'),True,5)\
          .and_return().ordered()

        local_state.should_receive('shell')\
          .with_args(re.compile('^scp .*cloud1/mykey.pem'),True,5)\
          .and_return().ordered()

        options = flexmock(name='options',
                           keyname='bookey',
                           infrastructure='ec2',
                           verbose=True)
        RemoteHelper.copy_deployment_credentials('public1', options)
  def test_copy_deployment_credentials_in_cloud(self):
    # mock out the scp'ing to public1 and assume they succeed
    local_state = flexmock(LocalState)
    local_state.should_receive('shell')\
      .with_args(re.compile('^scp .*secret.key'),True,5)\
      .and_return().ordered()

    local_state.should_receive('shell')\
      .with_args(re.compile('^scp .*ssh.key'),True,5)\
      .and_return().ordered()

    # mock out generating the private key
    local_state = flexmock(LocalState)
    local_state.should_receive('shell')\
      .with_args(re.compile('^openssl'),True, stdin=None)\
      .and_return().ordered()

    local_state.should_receive('shell')\
      .with_args(re.compile('^scp .*mycert.pem'),True,5)\
      .and_return().ordered()

    local_state.should_receive('shell')\
      .with_args(re.compile('^scp .*mykey.pem'),True,5)\
      .and_return().ordered()

    # next, mock out copying the private key and certificate
    local_state.should_receive('shell')\
      .with_args(re.compile('^ssh'),True,5,stdin=re.compile('^mkdir -p'))\
      .and_return().ordered()

    local_state.should_receive('shell')\
      .with_args(re.compile('^scp .*cloud1/mycert.pem'),True,5)\
      .and_return().ordered()

    local_state.should_receive('shell')\
      .with_args(re.compile('^scp .*cloud1/mykey.pem'),True,5)\
      .and_return().ordered()

    options = flexmock(name='options', keyname='bookey', infrastructure='ec2',
      verbose=True)
    RemoteHelper.copy_deployment_credentials('public1', options)