Skip to content

Commit

Permalink
fix: #355 Explicitly passing --project argument when starting emulator (
Browse files Browse the repository at this point in the history
#923)

* Running gcloud datastore emulator with --project argument

* Allow user to pass a custom project id when creating a new LocalDatastoreHelper instance

* Creating a local variable to increase the readability and emphasizing on the fact that project id not nullable now

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
jainsahab and gcf-owl-bot[bot] authored Dec 1, 2022
1 parent 1e7a0a9 commit ef4065d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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

0 comments on commit ef4065d

Please sign in to comment.