-
Notifications
You must be signed in to change notification settings - Fork 304
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
server/src/test/java/com/datastrato/graviton/server/TestServerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant slash '/' here
There was a problem hiding this comment.
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.