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

Feature/firebase extra logging #729

Merged
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 @@ -10,6 +10,8 @@
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;

import io.quarkiverse.googlecloudservices.firebase.deployment.testcontainers.json.Emulators;
Expand All @@ -21,6 +23,8 @@
*/
class CustomFirebaseConfigReader {

private static final Logger LOGGER = LoggerFactory.getLogger(CustomFirebaseConfigReader.class);

private final ObjectMapper objectMapper = new ObjectMapper();

/**
Expand Down Expand Up @@ -110,6 +114,8 @@ private Map<FirebaseEmulatorContainer.Emulator, FirebaseEmulatorContainer.Expose
map = Map.copyOf(map);
}

LOGGER.debug("Found the following emulators configured in firebase.json {}", map.keySet());

return map;
}

Expand Down Expand Up @@ -137,6 +143,9 @@ private FirebaseEmulatorContainer.FirestoreConfig readFirestore(Object firestore
.ofNullable(firestoreMap.get("indexes"))
.map(f -> this.resolvePath(f, customFirebaseJson));

LOGGER.debug("Firestore configured with rules file {}", rulesFile);
LOGGER.debug("Firestore configured with indexes file {}", indexesFile);

return new FirebaseEmulatorContainer.FirestoreConfig(
rulesFile,
indexesFile);
Expand All @@ -154,6 +163,8 @@ private FirebaseEmulatorContainer.HostingConfig readHosting(Object hosting, Path
.ofNullable(hostingMap.get("public"))
.map(f -> this.resolvePath(f, customFirebaseJson));

LOGGER.debug("Hosting configured with public directory {}", publicDir);

return new FirebaseEmulatorContainer.HostingConfig(
publicDir);
} else {
Expand All @@ -170,6 +181,8 @@ private FirebaseEmulatorContainer.StorageConfig readStorage(Object storage, Path
.ofNullable(storageMap.get("rules"))
.map(f -> this.resolvePath(f, customFirebaseJson));

LOGGER.debug("Storage configured with rules file {}", rulesFile);

return new FirebaseEmulatorContainer.StorageConfig(
rulesFile);
} else {
Expand All @@ -192,6 +205,9 @@ private FirebaseEmulatorContainer.FunctionsConfig readFunctions(Object functions
.map(String[].class::cast)
.orElse(new String[0]);

LOGGER.debug("Functions will be read from source directory {}", functionsPath);
LOGGER.debug("Functions configured with ignore file {}", (Object) ignores);

return new FirebaseEmulatorContainer.FunctionsConfig(
functionsPath,
ignores);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ public FirebaseConfigBuilder withFirebaseConfig() {
public EmulatorConfig buildConfig() {
if (firebaseConfig == null) {
// Try to autoload the firebase.json configuration
var defaultFirebaseJson = new File("firebase.json").getAbsoluteFile().toPath();
var defaultFirebaseJson = new File("firebase.json").toPath();

LOGGER.info("Trying to automatically read firebase config from {}", defaultFirebaseJson);

Expand Down Expand Up @@ -921,6 +921,8 @@ public FirebaseEmulatorContainer(EmulatorConfig emulatorConfig) {
.map(Path::toString)
.orElse(new File(FirebaseJsonBuilder.FIREBASE_HOSTING_SUBPATH).getAbsolutePath());

LOGGER.debug("Mounting {} to the container hosting path", hostingPath);

// Mount volume for static hosting content
this.withFileSystemBind(hostingPath, containerHostingPath(emulatorConfig), BindMode.READ_ONLY);
}
Expand All @@ -933,39 +935,62 @@ public FirebaseEmulatorContainer(EmulatorConfig emulatorConfig) {
.map(Path::toString)
.orElse(new File(FirebaseJsonBuilder.FIREBASE_FUNCTIONS_SUBPATH).getAbsolutePath());

LOGGER.debug("Mounting {} to the container functions sources path", functionsPath);

// Mount volume for functions
this.withFileSystemBind(functionsPath, containerFunctionsPath(emulatorConfig), BindMode.READ_ONLY);
}
}

static String containerHostingPath(EmulatorConfig emulatorConfig) {
var hostingPath = emulatorConfig.firebaseConfig().hostingConfig().hostingContentDir();
if (emulatorConfig.customFirebaseJson().isPresent()) {
var firebaseJsonDir = emulatorConfig.customFirebaseJson().get().getParent();
hostingPath = hostingPath.map(path -> path.subpath(firebaseJsonDir.getNameCount(), path.getNameCount()));
}
var hostingPath = relativizeToFirebaseJson(
emulatorConfig.firebaseConfig().hostingConfig().hostingContentDir(),
emulatorConfig);

String containerHostingPath;
if (hostingPath.isPresent()) {
var path = hostingPath.get();
if (path.isAbsolute()) {
return FIREBASE_HOSTING_PATH;
containerHostingPath = FIREBASE_HOSTING_PATH;
} else {
return FIREBASE_ROOT + "/" + hostingPath.get();
containerHostingPath = FIREBASE_ROOT + "/" + hostingPath.get();
}
} else {
return FIREBASE_HOSTING_PATH;
containerHostingPath = FIREBASE_HOSTING_PATH;
}

LOGGER.debug("Container hosting path is {}", containerHostingPath);

return containerHostingPath;
}

static String containerFunctionsPath(EmulatorConfig emulatorConfig) {
var functionsPath = emulatorConfig.firebaseConfig().functionsConfig().functionsPath();
if (emulatorConfig.customFirebaseJson().isPresent()) {
var firebaseJsonDir = emulatorConfig.customFirebaseJson().get().getParent();
functionsPath = functionsPath.map(path -> path.subpath(firebaseJsonDir.getNameCount(), path.getNameCount()));
}
return FIREBASE_ROOT + "/" + functionsPath
var functionsPath = relativizeToFirebaseJson(
emulatorConfig.firebaseConfig().functionsConfig().functionsPath(),
emulatorConfig);

var containerFunctionsPath = FIREBASE_ROOT + "/" + functionsPath
.map(Path::toString)
.orElse(FirebaseJsonBuilder.FIREBASE_FUNCTIONS_SUBPATH);

LOGGER.debug("Container functions path is {}", containerFunctionsPath);

return containerFunctionsPath;
}

private static Optional<Path> relativizeToFirebaseJson(Optional<Path> filePath, EmulatorConfig emulatorConfig) {
if (emulatorConfig.customFirebaseJson().isPresent()) {
var firebaseJsonFile = emulatorConfig.customFirebaseJson().get();
var nameCount = firebaseJsonFile.getParent() == null ? 0 : firebaseJsonFile.getParent().getNameCount();

var result = filePath.map(path -> path.subpath(nameCount, path.getNameCount()));

LOGGER.debug("Resolved path to be {} relative to the firebase.json file", result);

return result;
} else {
return filePath;
}
}

private static class FirebaseDockerBuilder {
Expand Down Expand Up @@ -1036,33 +1061,44 @@ private void validateConfiguration() {
}

if (emulatorConfig.customFirebaseJson.isPresent()) {
var hostingDirIsAbsolute = emulatorConfig.firebaseConfig.hostingConfig.hostingContentDir
var hostingDir = emulatorConfig.firebaseConfig.hostingConfig.hostingContentDir;

var hostingDirIsAbsolute = hostingDir
.map(Path::isAbsolute)
.orElse(false);

LOGGER.debug("Checking if path {} is absolute --> {}", hostingDir, hostingDirIsAbsolute);

if (hostingDirIsAbsolute) {
throw new IllegalStateException(
"When using a custom firebase.json, the hosting path must be relative to the firebase.json file");
}

var firebasePath = emulatorConfig.customFirebaseJson.get().toAbsolutePath().getParent();

var hostingDirIsChildOfFirebaseJsonParent = emulatorConfig.firebaseConfig.hostingConfig.hostingContentDir
var hostingDirIsChildOfFirebaseJsonParent = hostingDir
.map(Path::toAbsolutePath)
.map(h -> h.startsWith(firebasePath))
.orElse(true);

LOGGER.debug("Checking if the hosting path {} is relative to the firebase.json file --> {}", hostingDir,
hostingDirIsChildOfFirebaseJsonParent);

if (!hostingDirIsChildOfFirebaseJsonParent) {
throw new IllegalStateException(
"When using a custom firebase.json, the hosting path must be in the same subtree as the firebase.json file");
}
}

if (emulatorConfig.firebaseConfig.functionsConfig.functionsPath.isPresent()) {
var functionsDirIsAbsolute = emulatorConfig.firebaseConfig.functionsConfig.functionsPath
var functionsDir = emulatorConfig.firebaseConfig.functionsConfig.functionsPath;
var functionsDirIsAbsolute = functionsDir
.map(Path::isAbsolute)
.orElse(false);

LOGGER.debug("Checking if the functions sources dir {} is absolute --> {}", functionsDir,
functionsDirIsAbsolute);

if (functionsDirIsAbsolute) {
throw new IllegalStateException("Functions path cannot be absolute");
}
Expand Down
Loading