Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Test quota project #3245

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -25,7 +25,10 @@ tmp
*.iml
*.ipr
*.iws
.ijwb

# Showcase src directories
showcase-protos
showcase-protos-patched
google
googleapis
2 changes: 2 additions & 0 deletions showcase/java/build.gradle
Expand Up @@ -41,7 +41,9 @@ repositories {
dependencies {
implementation 'com.google.cloud:gapic-google-cloud-showcase-v1beta1:0.0.0-SNAPSHOT'
implementation "io.grpc:grpc-netty-shaded:1.13.1"
compile 'com.google.api:gax:1.57.0'

testCompile 'com.google.api:gax:1.57.0'
testImplementation 'junit:junit:4.13'
testImplementation "com.google.truth:truth:1.0"
}
Expand Up @@ -19,12 +19,17 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.rpc.AbortedException;
import com.google.api.gax.rpc.ApiStreamObserver;
import com.google.api.gax.rpc.BidiStreamObserver;
import com.google.api.gax.rpc.ClientStream;
import com.google.api.gax.rpc.HeaderProvider;
import com.google.api.gax.rpc.ServerStream;
import com.google.api.gax.rpc.StreamController;
import com.google.auth.Credentials;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.protobuf.Duration;
import com.google.rpc.Code;
import com.google.rpc.Status;
Expand All @@ -36,14 +41,20 @@
import com.google.showcase.v1beta1.EchoSettings;
import com.google.showcase.v1beta1.ExpandRequest;
import io.grpc.StatusRuntimeException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -54,13 +65,17 @@
public class ShowcaseTest {

private EchoClient client;
private ShowcaseTransportChannelProvider channelProvider;

@Before
public void setup() throws Exception {
String host = System.getenv("HOST");
if (host == null) host = "localhost";
String port = System.getenv("PORT");
if (port == null) port = "7469";
channelProvider =
new ShowcaseTransportChannelProvider(
host, Integer.parseInt(port), new ShowcaseHeaderProvider());

// init client for all tests
client =
Expand Down Expand Up @@ -258,4 +273,121 @@ public void block() {
.build());
assertThat(result.getContent()).isEqualTo("Hello, World!");
}

static GoogleCredentials loadCredentials(String credentialFile) {
try {
InputStream keyStream = new ByteArrayInputStream(credentialFile.getBytes());
return GoogleCredentials.fromStream(keyStream);
} catch (IOException e) {
fail("Couldn't create fake JSON credentials.");
}
return null;
}

@Test
public void quotaProjectIdTest() throws Exception {
final String QUOTA_PROJECT_ID_KEY = "x-google-user-project";
final String QUOTA_PROJECT_ID = "quota_project_id";
final String QUOTA_PROJECT_ID_FROM_HEADER_VALUE = "quota_project_id_from_headers";
final String QUOTA_PROJECT_ID_FROM_CREDENTIALS_VALUE = "quota_project_id_from_credentials";
final String JSON_KEY_QUOTA_PROJECT_ID =
"{\n"
+ " \"private_key_id\": \"somekeyid\",\n"
+ " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS"
+ "kAgEAAoIBAQC+K2hSuFpAdrJI\\nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHg"
+ "aR\\n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\\nQP/9dJfIkIDJ9Fw9N4"
+ "Bhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nknddadwkwewcVxHFhcZJO+XWf6ofLUXpRwiT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t\\nkndj5uNl5SiuVxHFhcZJO+XWf6ofLUregtevZakGMn8EE1uVa"
+ "2AY7eafmoU/nZPT\\n00YB0TBATdCbn/nBSuKDESkhSg9s2GEKQZG5hBmL5uCMfo09z3SfxZIhJdlerreP\\nJ7gSi"
+ "dI12N+EZxYd4xIJh/HFDgp7RRO87f+WJkofMQKBgGTnClK1VMaCRbJZPriw\\nEfeFCoOX75MxKwXs6xgrw4W//AYG"
+ "GUjDt83lD6AZP6tws7gJ2IwY/qP7+lyhjEqN\\nHtfPZRGFkGZsdaksdlaksd323423d+15/UvrlRSFPNj1tWQmNKk"
+ "XyRDW4IG1Oa2p\\nrALStNBx5Y9t0/LQnFI4w3aG\\n-----END PRIVATE KEY-----\\n\",\n"
+ " \"project_id\": \"someprojectid\",\n"
+ " \"client_email\": \"someclientid@developer.gserviceaccount.com\",\n"
+ " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n"
+ " \"type\": \"service_account\",\n"
+ " \"quota_project_id\": \""
+ QUOTA_PROJECT_ID_FROM_CREDENTIALS_VALUE
+ "\"\n"
+ "}";
final GoogleCredentials credentialsWithQuotaProject =
loadCredentials(JSON_KEY_QUOTA_PROJECT_ID);
final CredentialsProvider credentialsProviderWithQuota =
new CredentialsProvider() {
@Override
public Credentials getCredentials() throws IOException {
return credentialsWithQuotaProject;
}
};
final HeaderProvider headerProviderWithQuota =
new HeaderProvider() {
@Override
public Map<String, String> getHeaders() {
return Collections.singletonMap(
QUOTA_PROJECT_ID_KEY, QUOTA_PROJECT_ID_FROM_HEADER_VALUE);
}
};

EchoSettings settingsSetQuota =
EchoSettings.newBuilder()
.setTransportChannelProvider(channelProvider)
.setCredentialsProvider(NoCredentialsProvider.create())
.setQuotaProjectId(QUOTA_PROJECT_ID)
.build();
EchoSettings settingsHeader =
EchoSettings.newBuilder()
.setTransportChannelProvider(channelProvider)
.setCredentialsProvider(NoCredentialsProvider.create())
.setHeaderProvider(headerProviderWithQuota)
.build();
EchoSettings settingsHeaderAndQuota =
EchoSettings.newBuilder()
.setTransportChannelProvider(channelProvider)
.setCredentialsProvider(NoCredentialsProvider.create())
.setHeaderProvider(headerProviderWithQuota)
.setQuotaProjectId(QUOTA_PROJECT_ID)
.build();
EchoSettings settingsCredentials =
EchoSettings.newBuilder()
.setTransportChannelProvider(channelProvider)
.setCredentialsProvider(credentialsProviderWithQuota)
.build();
EchoSettings settingsCredentialsAndQuota =
EchoSettings.newBuilder()
.setTransportChannelProvider(channelProvider)
.setCredentialsProvider(credentialsProviderWithQuota)
.setQuotaProjectId(QUOTA_PROJECT_ID)
.build();
EchoSettings settingsQuotaAll =
EchoSettings.newBuilder()
.setTransportChannelProvider(channelProvider)
.setCredentialsProvider(credentialsProviderWithQuota)
.setQuotaProjectId(QUOTA_PROJECT_ID)
.setHeaderProvider(headerProviderWithQuota)
.build();

validQuotaProjectId(settingsSetQuota, QUOTA_PROJECT_ID);
validQuotaProjectId(settingsHeader, QUOTA_PROJECT_ID_FROM_HEADER_VALUE);
validQuotaProjectId(settingsHeaderAndQuota, QUOTA_PROJECT_ID);
validQuotaProjectId(settingsCredentials, QUOTA_PROJECT_ID_FROM_CREDENTIALS_VALUE);
validQuotaProjectId(settingsCredentialsAndQuota, QUOTA_PROJECT_ID);
validQuotaProjectId(client.getSettings(), null);
validQuotaProjectId(settingsQuotaAll, QUOTA_PROJECT_ID);
}

private void validQuotaProjectId(EchoSettings settings, String expectId) throws Exception {
EchoClient clientSetQuota = EchoClient.create(settings);
Assert.assertEquals(clientSetQuota.getSettings().getQuotaProjectId(), expectId);
}
}