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

[#390] improve(test): Consistency check in the graviton.conf file #408

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant slash '/' here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I removed the extra file separator.

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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it looks like we will only check the configs that exist in the conf file, if not, the check logic will be passed, is that what we expected?

"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;
}
}