Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #355 Explicitly passing --project argument when starting emulator #923

Merged
merged 4 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.cloud.datastore.testing;

import static com.google.common.base.MoreObjects.firstNonNull;

import com.google.api.core.InternalApi;
import com.google.cloud.NoCredentials;
import com.google.cloud.ServiceOptions;
Expand Down Expand Up @@ -68,7 +70,9 @@ public class LocalDatastoreHelper extends BaseEmulatorHelper<DatastoreOptions> {

// Common settings
private static final String CONSISTENCY_FLAG = "--consistency=";
private static final String PROJECT_FLAG = "--project=";
private static final double DEFAULT_CONSISTENCY = 0.9;
private static final String DEFAULT_PROJECT_ID = PROJECT_ID_PREFIX + UUID.randomUUID();

private static final Logger LOGGER = Logger.getLogger(LocalDatastoreHelper.class.getName());

Expand All @@ -90,6 +94,7 @@ public static class Builder {
private int port;
private Path dataDir;
private boolean storeOnDisk = true;
private String projectId;

private Builder() {}

Expand All @@ -109,6 +114,11 @@ public Builder setPort(int port) {
return this;
}

public Builder setProjectId(String projectId) {
this.projectId = projectId;
return this;
}

public Builder setDataDir(Path dataDir) {
this.dataDir = dataDir;
return this;
Expand All @@ -129,7 +139,8 @@ private LocalDatastoreHelper(Builder builder) {
super(
"datastore",
builder.port > 0 ? builder.port : BaseEmulatorHelper.findAvailablePort(DEFAULT_PORT),
PROJECT_ID_PREFIX + UUID.randomUUID().toString());
firstNonNull(builder.projectId, DEFAULT_PROJECT_ID));
String projectId = firstNonNull(builder.projectId, DEFAULT_PROJECT_ID);
this.consistency = builder.consistency > 0 ? builder.consistency : DEFAULT_CONSISTENCY;
this.gcdPath = builder.dataDir;
this.storeOnDisk = builder.storeOnDisk;
Expand All @@ -140,6 +151,7 @@ private LocalDatastoreHelper(Builder builder) {
List<String> gcloudCommand = new ArrayList<>(Arrays.asList(GCLOUD_CMD_TEXT.split(" ")));
gcloudCommand.add(GCLOUD_CMD_PORT_FLAG + "localhost:" + getPort());
gcloudCommand.add(CONSISTENCY_FLAG + builder.consistency);
gcloudCommand.add(PROJECT_FLAG + projectId);
if (!builder.storeOnDisk) {
gcloudCommand.add("--no-store-on-disk");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ public void testCreateWithBuilder() {
assertTrue(incompleteHelper.getProjectId().startsWith(PROJECT_ID_PREFIX));
}

@Test
public void testCreateWithCustomProjectId() {
String customProjectId = "custom-project-id";
LocalDatastoreHelper helper =
LocalDatastoreHelper.newBuilder().setProjectId(customProjectId).build();
assertEquals(customProjectId, helper.getProjectId());
}

@Test
public void testCreateWithToBuilder() throws IOException {
LocalDatastoreHelper helper =
Expand Down