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 up testing for missing metadata files #727

Merged
merged 2 commits into from
Sep 7, 2023
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
3 changes: 2 additions & 1 deletion bin/sequencer
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ if [[ -d $site_path ]]; then
elif [[ -f $site_path ]]; then
echo Using site config file $site_path
site_model=`jq -r .site_model $site_path`
[[ $site_model == null ]] && site_model=$(dirname $site_path)
[[ $site_model == null ]] && site_model=.
[[ $site_model =~ ^/ ]] || site_model=$(realpath $(dirname $site_path)/$site_model)
project_id=`jq -r .project_id $site_path`
[[ $project_id == null ]] && project_id=
device_id=`jq -r .device_id $site_path`
Expand Down
2 changes: 2 additions & 0 deletions bin/test_regclean
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ cat $pubber_config
echo Corrupting site model to check error handling...
mkdir -p $site_path/devices/XXX-1
echo { > $site_path/devices/XXX-1/metadata.json
mkdir -p $site_path/devices/XXX-2/out
echo hello > $site_path/devices/XXX-2/out/exceptions.txt

echo Clean out the registry to make sure devices get removed...
bin/registrar $site_arg $registrar_project -d
Expand Down
20 changes: 14 additions & 6 deletions common/src/main/java/com/google/udmi/util/SiteModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -121,18 +122,25 @@ private static boolean validDeviceDirectory(String dirName) {
}

public static Metadata loadDeviceMetadata(String sitePath, String deviceId, Class<?> container) {
Preconditions.checkState(sitePath != null, "sitePath not defined");
File deviceDir = getDeviceDir(sitePath, deviceId);
File deviceMetadataFile = new File(deviceDir, "metadata.json");
Metadata metadata = captureLoadErrors(deviceMetadataFile);
if (metadata != null) {
try {
Preconditions.checkState(sitePath != null, "sitePath not defined");
File deviceDir = getDeviceDir(sitePath, deviceId);
File deviceMetadataFile = new File(deviceDir, "metadata.json");
if (!deviceMetadataFile.exists()) {
return new MetadataException(deviceMetadataFile, new FileNotFoundException());
}
Metadata metadata = requireNonNull(captureLoadErrors(deviceMetadataFile), "bad metadata");

// Missing arrays are automatically parsed to an empty list, which is not what
// we want, so hacky go through and convert an empty list to null.
if (metadata.gateway != null && metadata.gateway.proxy_ids.isEmpty()) {
metadata.gateway.proxy_ids = null;
}

return metadata;
} catch (Exception e) {
throw new RuntimeException("While loading device metadata for " + deviceId, e);
}
return metadata;
}

private static Metadata captureLoadErrors(File deviceMetadataFile) {
Expand Down