Skip to content

Commit

Permalink
Closes #1318 - Only accept Maps as Config-Yaml content (#1320)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronweissler authored Mar 1, 2022
1 parent a89a1b7 commit b0e71e1
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import rocks.inspectit.ocelot.mappings.model.AgentMapping;
import rocks.inspectit.ocelot.utils.CancellableTask;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -172,7 +169,7 @@ private Object loadAndMergeYaml(AbstractFileAccessor fileAccessor, Object toMerg
String src = fileAccessor.readConfigurationFile(path).orElse("");

try {
Object loadedYaml = yaml.load(src);
Map<String, Object> loadedYaml = yaml.load(src);
if (toMerge == null) {
return loadedYaml;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,81 @@ public void loadWithException() {

List<AgentConfiguration> configurationList = configurations.getValue();
assertThat(configurationList).hasSize(1);
assertThat(configurationList)
.element(0)
assertThat(configurationList).element(0)
.extracting(AgentConfiguration::getConfigYaml)
.isEqualTo("{key: valid}\n");
}

@Test
public void loadWithExceptionOnlyString() {
FileInfo fileInfo = mock(FileInfo.class);
when(fileInfo.getAbsoluteFilePaths(any())).thenReturn(Stream.of("/test.yml"), Stream.of("/test.yml"));
when(workspaceAccessor.agentMappingsExist()).thenReturn(true);
when(workspaceAccessor.configurationFileExists(anyString())).thenReturn(true);
when(workspaceAccessor.configurationFileIsDirectory(anyString())).thenReturn(true);
when(workspaceAccessor.listConfigurationFiles(anyString())).thenReturn(Collections.singletonList(fileInfo));
// the first call will return an invalid file only containing a string
when(workspaceAccessor.readConfigurationFile(anyString())).thenReturn(Optional.of("onlystring"), Optional.of("key: valid"));

AgentMapping mapping = AgentMapping.builder()
.name("test")
.source("/test")
.sourceBranch(Branch.WORKSPACE)
.build();
AgentMapping mapping2 = AgentMapping.builder()
.name("test2")
.source("/test2")
.sourceBranch(Branch.WORKSPACE)
.build();
doReturn(Arrays.asList(mapping, mapping2)).when(serializer).readAgentMappings(any());

MutableObject<List<AgentConfiguration>> configurations = new MutableObject<>();
Consumer<List<AgentConfiguration>> consumer = configurations::setValue;

AgentConfigurationReloadTask task = new AgentConfigurationReloadTask(serializer, fileManager, consumer);

task.run();

List<AgentConfiguration> configurationList = configurations.getValue();
assertThat(configurationList).hasSize(1);
assertThat(configurationList).element(0)
.extracting(AgentConfiguration::getConfigYaml)
.isEqualTo("{key: valid}\n");
}

@Test
public void loadWithExceptionOnlyList() {
FileInfo fileInfo = mock(FileInfo.class);
when(fileInfo.getAbsoluteFilePaths(any())).thenReturn(Stream.of("/test.yml"), Stream.of("/test.yml"));
when(workspaceAccessor.agentMappingsExist()).thenReturn(true);
when(workspaceAccessor.configurationFileExists(anyString())).thenReturn(true);
when(workspaceAccessor.configurationFileIsDirectory(anyString())).thenReturn(true);
when(workspaceAccessor.listConfigurationFiles(anyString())).thenReturn(Collections.singletonList(fileInfo));
// the first call will return an invalid file only containing a list
when(workspaceAccessor.readConfigurationFile(anyString())).thenReturn(Optional.of("- listentry1\n listentry2"), Optional.of("key: valid"));

AgentMapping mapping = AgentMapping.builder()
.name("test")
.source("/test")
.sourceBranch(Branch.WORKSPACE)
.build();
AgentMapping mapping2 = AgentMapping.builder()
.name("test2")
.source("/test2")
.sourceBranch(Branch.WORKSPACE)
.build();
doReturn(Arrays.asList(mapping, mapping2)).when(serializer).readAgentMappings(any());

MutableObject<List<AgentConfiguration>> configurations = new MutableObject<>();
Consumer<List<AgentConfiguration>> consumer = configurations::setValue;

AgentConfigurationReloadTask task = new AgentConfigurationReloadTask(serializer, fileManager, consumer);

task.run();

List<AgentConfiguration> configurationList = configurations.getValue();
assertThat(configurationList).hasSize(1);
assertThat(configurationList).element(0)
.extracting(AgentConfiguration::getConfigYaml)
.isEqualTo("{key: valid}\n");
}
Expand Down Expand Up @@ -131,8 +204,7 @@ public void yamlWithTab() {
.sourceBranch(Branch.WORKSPACE)
.build();

assertThatExceptionOfType(AgentConfigurationReloadTask.InvalidConfigurationFileException.class)
.isThrownBy(() -> reloadTask.loadConfigForMapping(mapping))
assertThatExceptionOfType(AgentConfigurationReloadTask.InvalidConfigurationFileException.class).isThrownBy(() -> reloadTask.loadConfigForMapping(mapping))
.withMessage("The configuration file '/test.yml' is invalid and cannot be parsed.");
}
}
Expand All @@ -142,19 +214,14 @@ class LoadConfigForMapping {

@Test
void noSourcesSpecified() throws IOException {
String result = reloadTask.loadConfigForMapping(
AgentMapping.builder()
.build());
String result = reloadTask.loadConfigForMapping(AgentMapping.builder().build());

assertThat(result).isEmpty();
}

@Test
void liveBranchSpecified() throws IOException {
AgentMapping mapping = AgentMapping.builder()
.source("a.yml")
.sourceBranch(Branch.LIVE)
.build();
AgentMapping mapping = AgentMapping.builder().source("a.yml").sourceBranch(Branch.LIVE).build();

doReturn(true).when(liveAccessor).configurationFileExists("a.yml");
doReturn(false).when(liveAccessor).configurationFileIsDirectory("a.yml");
Expand All @@ -170,12 +237,11 @@ void nonExistingSourcesSpecified() throws IOException {
doReturn(false).when(workspaceAccessor).configurationFileExists("a.yml");
doReturn(false).when(workspaceAccessor).configurationFileExists("some/folder");

String result = reloadTask.loadConfigForMapping(
AgentMapping.builder()
.source("a.yml")
.source("/some/folder")
.sourceBranch(Branch.WORKSPACE)
.build());
String result = reloadTask.loadConfigForMapping(AgentMapping.builder()
.source("a.yml")
.source("/some/folder")
.sourceBranch(Branch.WORKSPACE)
.build());

assertThat(result).isEmpty();
}
Expand All @@ -186,14 +252,13 @@ void nonYamlIgnored() throws IOException {
doReturn(false).when(workspaceAccessor).configurationFileIsDirectory(any());
doReturn(Optional.of("")).when(workspaceAccessor).readConfigurationFile(any());

String result = reloadTask.loadConfigForMapping(
AgentMapping.builder()
.source("a.yml")
.source("b.YmL")
.source("c.yaml")
.source("d.txt")
.sourceBranch(Branch.WORKSPACE)
.build());
String result = reloadTask.loadConfigForMapping(AgentMapping.builder()
.source("a.yml")
.source("b.YmL")
.source("c.yaml")
.source("d.txt")
.sourceBranch(Branch.WORKSPACE)
.build());

assertThat(result).isEmpty();
verify(workspaceAccessor).readConfigurationFile("a.yml");
Expand All @@ -209,11 +274,10 @@ void leadingSlashesInSourcesRemoved() throws IOException {

lenient().doThrow(new RuntimeException()).when(workspaceAccessor).configurationFileExists(startsWith("/"));

reloadTask.loadConfigForMapping(
AgentMapping.builder()
.source("/a.yml")
.sourceBranch(Branch.WORKSPACE)
.build());
reloadTask.loadConfigForMapping(AgentMapping.builder()
.source("/a.yml")
.sourceBranch(Branch.WORKSPACE)
.build());

verify(workspaceAccessor).configurationFileExists(eq("a.yml"));
}
Expand All @@ -226,20 +290,13 @@ void priorityRespected() throws IOException {
doReturn(true).when(workspaceAccessor).configurationFileIsDirectory("folder");
doReturn(false).when(workspaceAccessor).configurationFileIsDirectory("z.yml");

List<FileInfo> fileInfos = Arrays.asList(
FileInfo.builder()
.type(FileInfo.Type.FILE)
.name("b.yml")
.build(),
FileInfo.builder()
.type(FileInfo.Type.FILE)
.name("a.yml")
.build(),
FileInfo.builder()
.type(FileInfo.Type.FILE)
.name("somethingelse")
.build()
);
List<FileInfo> fileInfos = Arrays.asList(FileInfo.builder()
.type(FileInfo.Type.FILE)
.name("b.yml")
.build(), FileInfo.builder().type(FileInfo.Type.FILE).name("a.yml").build(), FileInfo.builder()
.type(FileInfo.Type.FILE)
.name("somethingelse")
.build());

when(workspaceAccessor.listConfigurationFiles("folder")).thenReturn(fileInfos);

Expand All @@ -248,12 +305,11 @@ void priorityRespected() throws IOException {
doReturn(Optional.of("{ val1: b, val2: b, val3: b}")).when(workspaceAccessor)
.readConfigurationFile("folder/b.yml");

String result = reloadTask.loadConfigForMapping(
AgentMapping.builder()
.source("/z.yml")
.source("/folder")
.sourceBranch(Branch.WORKSPACE)
.build());
String result = reloadTask.loadConfigForMapping(AgentMapping.builder()
.source("/z.yml")
.source("/folder")
.sourceBranch(Branch.WORKSPACE)
.build());

assertThat(result).isEqualTo("{val1: z, val2: a, val3: b}\n");
verify(workspaceAccessor, never()).readConfigurationFile("folder/somethingelse");
Expand Down

0 comments on commit b0e71e1

Please sign in to comment.