New configuration handling. Incomplete manual config ends with error.… (#15784)

* New configuration handling. Incomplete manual config ends with error. Empty config allows to try different config options.

* Trying to reflect functionality responsibilities with better naming
This commit is contained in:
luk-kaminski
2023-07-04 13:56:22 +02:00
committed by GitHub
parent 8de177df0e
commit a8f29b0431
9 changed files with 399 additions and 7 deletions

View File

@@ -0,0 +1,38 @@
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
package org.graylog.datanode.configuration.verification;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class ConfigPropertyTest {
@Test
void testRequiresPropertyNameToBeProvided() {
assertThrows(IllegalArgumentException.class, () -> new ConfigProperty(null, "value"));
assertThrows(IllegalArgumentException.class, () -> new ConfigProperty("", "value"));
}
@Test
void testHoldsProperties() {
final ConfigProperty configProperty = new ConfigProperty("name", "value");
assertEquals("name", configProperty.propertyName());
assertEquals("value", configProperty.propertyValue());
}
}

View File

@@ -0,0 +1,123 @@
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
package org.graylog.datanode.configuration.verification;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import static org.graylog.datanode.configuration.verification.ConfigSectionCompleteness.COMPLETE;
import static org.graylog.datanode.configuration.verification.ConfigSectionCompleteness.INCOMPLETE;
import static org.graylog.datanode.configuration.verification.ConfigSectionCompleteness.MISSING;
import static org.junit.jupiter.api.Assertions.assertEquals;
class ConfigSectionCompletenessVerifierTest {
private static Path existingFile;
@BeforeAll
static void setUp(@TempDir Path tmpDir) throws IOException {
existingFile = tmpDir.resolve("lalala.txt");
Files.createFile(existingFile);
}
@Test
void testReturnsOkOnNullRequirements() {
assertEquals(COMPLETE, new ConfigSectionCompletenessVerifier().verifyConfigSectionCompleteness(null));
}
@Test
void testReturnsOkOnEmptyRequirements() {
assertEquals(COMPLETE, new ConfigSectionCompletenessVerifier().verifyConfigSectionCompleteness(new ConfigSectionRequirements(null, null)));
assertEquals(COMPLETE, new ConfigSectionCompletenessVerifier().verifyConfigSectionCompleteness(new ConfigSectionRequirements(List.of(), List.of())));
}
@Test
void testReturnsOkIfAllRequirementsMet() {
assertEquals(COMPLETE, new ConfigSectionCompletenessVerifier().verifyConfigSectionCompleteness(new ConfigSectionRequirements(
List.of(new ConfigProperty("importantProperty", "I am present"),
new ConfigProperty("whatever", "Mee too!")),
List.of(existingFile)
)));
}
@Test
void testReturnsIncompleteIfSomePropertiesMissing() {
assertEquals(INCOMPLETE, new ConfigSectionCompletenessVerifier().verifyConfigSectionCompleteness(new ConfigSectionRequirements(
List.of(new ConfigProperty("importantProperty", "I am present"),
new ConfigProperty("whatever", "Mee too!"),
new ConfigProperty("nvmd", "")
),
List.of()
)));
}
@Test
void testReturnsIncompleteIfSomePropertiesNull() {
assertEquals(INCOMPLETE, new ConfigSectionCompletenessVerifier().verifyConfigSectionCompleteness(new ConfigSectionRequirements(
List.of(new ConfigProperty("importantProperty", "I am present"),
new ConfigProperty("whatever", "Mee too!"),
new ConfigProperty("nvmd", null)
),
List.of()
)));
}
@Test
void testReturnsIncompleteIfNullFilePathPresent() {
assertEquals(INCOMPLETE, new ConfigSectionCompletenessVerifier().verifyConfigSectionCompleteness(new ConfigSectionRequirements(
List.of(new ConfigProperty("importantProperty", "I am present"),
new ConfigProperty("whatever", "Mee too!")),
Arrays.asList(existingFile, null)
)));
}
@Test
void testReturnsIncompleteIfNonExistingFilePathPresent() {
assertEquals(INCOMPLETE, new ConfigSectionCompletenessVerifier().verifyConfigSectionCompleteness(new ConfigSectionRequirements(
List.of(new ConfigProperty("importantProperty", "I am present"),
new ConfigProperty("whatever", "Mee too!")),
List.of(existingFile, Path.of("I_am_not_at_home.txt"))
)));
}
@Test
void testReturnsMissingIfAllPropertiesRequirementsAreNotMet() {
assertEquals(MISSING, new ConfigSectionCompletenessVerifier().verifyConfigSectionCompleteness(new ConfigSectionRequirements(
Arrays.asList(new ConfigProperty("importantProperty", ""),
new ConfigProperty("whatever", ""),
new ConfigProperty("x", null)),
Arrays.asList(null, null, Path.of("I_am_not_at_home.txt"))
)));
assertEquals(MISSING, new ConfigSectionCompletenessVerifier().verifyConfigSectionCompleteness(new ConfigSectionRequirements(
List.of(),
List.of(Path.of("I_am_not_at_home.txt"))
)));
assertEquals(MISSING, new ConfigSectionCompletenessVerifier().verifyConfigSectionCompleteness(new ConfigSectionRequirements(
List.of(new ConfigProperty("x", "")),
List.of()
)));
}
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
package org.graylog.datanode.configuration.verification;
import org.junit.jupiter.api.Test;
import java.nio.file.Path;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
class ConfigSectionRequirementsTest {
@Test
void testReplacesNullValuesWithEmptyCollectionsDuringConstruction() {
ConfigSectionRequirements toTest = new ConfigSectionRequirements(null, null);
assertNotNull(toTest.requiredStringProperties());
assertNotNull(toTest.requiredFiles());
assertTrue(toTest.requiredFiles().isEmpty());
assertTrue(toTest.requiredStringProperties().isEmpty());
}
@Test
void testRequirementCounting() {
assertEquals(0, new ConfigSectionRequirements(null, null).requirementsCount());
assertEquals(0, new ConfigSectionRequirements(List.of(), null).requirementsCount());
assertEquals(0, new ConfigSectionRequirements(null, List.of()).requirementsCount());
assertEquals(1, new ConfigSectionRequirements(null, List.of(Path.of("whatever"))).requirementsCount());
assertEquals(1, new ConfigSectionRequirements(
List.of(new ConfigProperty("importantProperty", "whatever")),
List.of()).requirementsCount());
assertEquals(3, new ConfigSectionRequirements(
List.of(new ConfigProperty("importantProperty", "whatever"),
new ConfigProperty("importantProperty2", "smthelse")),
List.of(Path.of("secret.file"))).requirementsCount());
}
@Test
void testRequirementList() {
final List<String> requirementsList = new ConfigSectionRequirements(
List.of(new ConfigProperty("importantProperty", "whatever"),
new ConfigProperty("importantProperty2", "smthelse")),
List.of(Path.of("secret.file"))).requirementsList();
assertEquals(3, requirementsList.size());
assertTrue(requirementsList.get(0).startsWith("importantProperty"));
assertTrue(requirementsList.get(1).startsWith("importantProperty2"));
assertTrue(requirementsList.get(2).contains("secret.file"));
}
}