Skip to content

Commit

Permalink
[#390] improve(test): Consistency check in the graviton.conf file (#408)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

+ Add ServerConfigTest to keep consistency check in the graviton.conf
file.
+ Use the JAVA reflect method to get all property names from
`ServerConfig.java` and `Configs.java` files.

### Why are the changes needed?

Because property names in the `graviton.conf.template` file need to be
kept consistent with `com/datastrato/graviton/Configs.java` and
`com/datastrato/graviton/server/ServerConfig.java`,
Otherwise, Graviton will not be able to read `graviton.conf` file
correctly.

Fix: #390 

### Does this PR introduce _any_ user-facing change?

N/A

### How was this patch tested?

Added `ServerConfigTest` in the test cases.
  • Loading branch information
xunliu authored Sep 18, 2023
1 parent 9645528 commit 0d00735
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion conf/graviton.conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ graviton.entity.store = kv
# The RocksDB entity store
graviton.entity.store.kv = RocksDBKvBackend
# The RocksDB backend path for entity store
graviton.entity.store.kv.rocskdb.path = /tmp/graviton
graviton.entity.store.kv.rocksdbPath = /tmp/graviton

# THE CONFIGURATION FOR GRAVITON CATALOG
# The interval in milliseconds to evict the catalog cache
Expand Down
4 changes: 4 additions & 0 deletions server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,7 @@ tasks.named("build") {
throw GradleException("$propertiesFile file not generated!")
}
}

tasks.test {
environment("GRAVITON_ROOT_DIR", rootDir.path)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2023 Datastrato.
* This software is licensed under the Apache License version 2.
*/
package com.datastrato.graviton.server;

import com.datastrato.graviton.Configs;
import com.datastrato.graviton.config.ConfigEntry;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class TestServerConfig {
@Test
public void checkGravitonConfFile()
throws NoSuchFieldException, IllegalAccessException, IOException {
// Load all program config keys from `ServerConfig` and `Configs` into a map
Map<String, String> configKeyMap = new HashMap<>();
configKeyMap.putAll(getConfigEntryFromClass(ServerConfig.class));
configKeyMap.putAll(getConfigEntryFromClass(Configs.class));

// Load all config keys from `graviton.conf.template` into a map
Properties properties = new Properties();
String confFile =
System.getenv("GRAVITON_ROOT_DIR")
+ File.separator
+ "conf"
+ File.separator
+ "graviton.conf.template";
InputStream in = Files.newInputStream(new File(confFile).toPath());
properties.load(in);

// Check if all config keys from `graviton.conf.template` are defined in `ServerConfig` and
// `Configs`
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
String propKey = (String) entry.getKey();
Assertions.assertTrue(
configKeyMap.containsKey(propKey),
"Config key " + propKey + " is not defined in ConfigEntry");
}
}

// Get all ConfigEntry member variables from a config class
private Map<String, String> getConfigEntryFromClass(Class<?> configClazz)
throws NoSuchFieldException, IllegalAccessException {
Map<String, String> configKeyMap = new HashMap<>();
// Get all fields
Field[] fields = configClazz.getDeclaredFields();
for (Field field : fields) {
String fieldName = field.getName();
Class<?> fieldType = field.getType();

if (!(fieldType == ConfigEntry.class)) {
continue;
}
Field memberConfigEntry = configClazz.getDeclaredField(fieldName);
memberConfigEntry.setAccessible(true);

// Get all ConfigEntry member variables
ConfigEntry<?> configEntry = (ConfigEntry<?>) memberConfigEntry.get(null);
String configEntryKey = configEntry.getKey();
configKeyMap.put(configEntryKey, fieldName);
}
return configKeyMap;
}
}

0 comments on commit 0d00735

Please sign in to comment.