Skip to content

Commit 75afedf

Browse files
committed
Allow overriding of module metadata files in integration tests (elastic#120427)
1 parent b2e8ed7 commit 75afedf

File tree

5 files changed

+59
-20
lines changed

5 files changed

+59
-20
lines changed

test/test-clusters/src/main/java/org/elasticsearch/test/cluster/local/AbstractLocalClusterFactory.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public abstract class AbstractLocalClusterFactory<S extends LocalClusterSpec, H
7777
private static final String TESTS_CLUSTER_FIPS_JAR_PATH_SYSPROP = "tests.cluster.fips.jars.path";
7878
private static final String TESTS_CLUSTER_DEBUG_ENABLED_SYSPROP = "tests.cluster.debug.enabled";
7979
private static final String ENABLE_DEBUG_JVM_ARGS = "-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=";
80+
private static final String ENTITLEMENT_POLICY_YAML = "entitlement-policy.yaml";
81+
private static final String PLUGIN_DESCRIPTOR_PROPERTIES = "plugin-descriptor.properties";
8082

8183
private final DistributionResolver distributionResolver;
8284

@@ -660,7 +662,7 @@ private void installPlugins() {
660662
.findFirst()
661663
.map(path -> {
662664
DefaultPluginInstallSpec installSpec = plugin.getValue();
663-
// Path the plugin archive with configured overrides if necessary
665+
// Patch the plugin archive with configured overrides if necessary
664666
if (installSpec.entitlementsOverride != null || installSpec.propertiesOverride != null) {
665667
Path target;
666668
try {
@@ -671,13 +673,13 @@ private void installPlugins() {
671673
ArchivePatcher patcher = new ArchivePatcher(path, target);
672674
if (installSpec.entitlementsOverride != null) {
673675
patcher.override(
674-
"entitlement-policy.yaml",
676+
ENTITLEMENT_POLICY_YAML,
675677
original -> installSpec.entitlementsOverride.apply(original).asStream()
676678
);
677679
}
678680
if (installSpec.propertiesOverride != null) {
679681
patcher.override(
680-
"plugin-descriptor.properties",
682+
PLUGIN_DESCRIPTOR_PROPERTIES,
681683
original -> installSpec.propertiesOverride.apply(original).asStream()
682684
);
683685
}
@@ -727,11 +729,11 @@ private void installModules() {
727729
.map(Path::of)
728730
.toList();
729731

730-
spec.getModules().forEach(module -> installModule(module, modulePaths));
732+
spec.getModules().forEach((module, spec) -> installModule(module, spec, modulePaths));
731733
}
732734
}
733735

734-
private void installModule(String moduleName, List<Path> modulePaths) {
736+
private void installModule(String moduleName, DefaultPluginInstallSpec installSpec, List<Path> modulePaths) {
735737
Path destination = distributionDir.resolve("modules").resolve(moduleName);
736738
if (Files.notExists(destination)) {
737739
Path modulePath = modulePaths.stream().filter(path -> path.endsWith(moduleName)).findFirst().orElseThrow(() -> {
@@ -741,7 +743,7 @@ private void installModule(String moduleName, List<Path> modulePaths) {
741743
? "project(xpackModule('" + moduleName.substring(7) + "'))"
742744
: "project(':modules:" + moduleName + "')";
743745

744-
throw new RuntimeException(
746+
return new RuntimeException(
745747
"Unable to locate module '"
746748
+ moduleName
747749
+ "'. Ensure you've added the following to the build script for project '"
@@ -756,20 +758,34 @@ private void installModule(String moduleName, List<Path> modulePaths) {
756758
});
757759

758760
IOUtils.syncWithCopy(modulePath, destination);
761+
try {
762+
if (installSpec.entitlementsOverride != null) {
763+
Path entitlementsFile = modulePath.resolve(ENTITLEMENT_POLICY_YAML);
764+
String original = Files.exists(entitlementsFile) ? Files.readString(entitlementsFile) : "";
765+
Path target = destination.resolve(ENTITLEMENT_POLICY_YAML);
766+
installSpec.entitlementsOverride.apply(original).writeTo(target);
767+
}
768+
if (installSpec.propertiesOverride != null) {
769+
Path propertiesFiles = modulePath.resolve(PLUGIN_DESCRIPTOR_PROPERTIES);
770+
String original = Files.exists(propertiesFiles) ? Files.readString(propertiesFiles) : "";
771+
Path target = destination.resolve(PLUGIN_DESCRIPTOR_PROPERTIES);
772+
installSpec.propertiesOverride.apply(original).writeTo(target);
773+
}
774+
} catch (IOException e) {
775+
throw new UncheckedIOException("Error patching module '" + moduleName + "'", e);
776+
}
759777

760-
// Install any extended plugins
778+
// Install any extended modules
761779
Properties pluginProperties = new Properties();
762780
try (
763-
InputStream in = new BufferedInputStream(
764-
new FileInputStream(modulePath.resolve("plugin-descriptor.properties").toFile())
765-
)
781+
InputStream in = new BufferedInputStream(new FileInputStream(modulePath.resolve(PLUGIN_DESCRIPTOR_PROPERTIES).toFile()))
766782
) {
767783
pluginProperties.load(in);
768784
String extendedProperty = pluginProperties.getProperty("extended.plugins");
769785
if (extendedProperty != null) {
770-
String[] extendedPlugins = extendedProperty.split(",");
771-
for (String plugin : extendedPlugins) {
772-
installModule(plugin, modulePaths);
786+
String[] extendedModules = extendedProperty.split(",");
787+
for (String module : extendedModules) {
788+
installModule(module, new DefaultPluginInstallSpec(), modulePaths);
773789
}
774790
}
775791
} catch (IOException e) {

test/test-clusters/src/main/java/org/elasticsearch/test/cluster/local/AbstractLocalSpecBuilder.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public abstract class AbstractLocalSpecBuilder<T extends LocalSpecBuilder<?>> im
3434
private final Map<String, String> settings = new HashMap<>();
3535
private final List<EnvironmentProvider> environmentProviders = new ArrayList<>();
3636
private final Map<String, String> environment = new HashMap<>();
37-
private final Set<String> modules = new HashSet<>();
37+
private final Map<String, DefaultPluginInstallSpec> modules = new HashMap<>();
3838
private final Map<String, DefaultPluginInstallSpec> plugins = new HashMap<>();
3939
private final Set<FeatureFlag> features = EnumSet.noneOf(FeatureFlag.class);
4040
private final List<SettingsProvider> keystoreProviders = new ArrayList<>();
@@ -123,11 +123,19 @@ DistributionType getDistributionType() {
123123

124124
@Override
125125
public T module(String moduleName) {
126-
this.modules.add(moduleName);
126+
this.modules.put(moduleName, new DefaultPluginInstallSpec());
127127
return cast(this);
128128
}
129129

130-
Set<String> getModules() {
130+
@Override
131+
public T module(String moduleName, Consumer<? super PluginInstallSpec> config) {
132+
DefaultPluginInstallSpec spec = new DefaultPluginInstallSpec();
133+
config.accept(spec);
134+
this.modules.put(moduleName, spec);
135+
return cast(this);
136+
}
137+
138+
Map<String, DefaultPluginInstallSpec> getModules() {
131139
return inherit(() -> parent.getModules(), modules);
132140
}
133141

test/test-clusters/src/main/java/org/elasticsearch/test/cluster/local/LocalClusterSpec.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public static class LocalNodeSpec {
9191
private final Map<String, String> settings;
9292
private final List<EnvironmentProvider> environmentProviders;
9393
private final Map<String, String> environment;
94-
private final Set<String> modules;
94+
private final Map<String, DefaultPluginInstallSpec> modules;
9595
private final Map<String, DefaultPluginInstallSpec> plugins;
9696
private final DistributionType distributionType;
9797
private final Set<FeatureFlag> features;
@@ -113,7 +113,7 @@ public LocalNodeSpec(
113113
Map<String, String> settings,
114114
List<EnvironmentProvider> environmentProviders,
115115
Map<String, String> environment,
116-
Set<String> modules,
116+
Map<String, DefaultPluginInstallSpec> modules,
117117
Map<String, DefaultPluginInstallSpec> plugins,
118118
DistributionType distributionType,
119119
Set<FeatureFlag> features,
@@ -175,7 +175,7 @@ public DistributionType getDistributionType() {
175175
return distributionType;
176176
}
177177

178-
public Set<String> getModules() {
178+
public Map<String, DefaultPluginInstallSpec> getModules() {
179179
return modules;
180180
}
181181

test/test-clusters/src/main/java/org/elasticsearch/test/cluster/local/LocalSpecBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ interface LocalSpecBuilder<T extends LocalSpecBuilder<?>> {
6969
*/
7070
T module(String moduleName);
7171

72+
/**
73+
* Ensure module is installed into the distribution when using the {@link DistributionType#INTEG_TEST} distribution. This is ignored
74+
* when the {@link DistributionType#DEFAULT} is being used.
75+
*/
76+
T module(String moduleName, Consumer<? super PluginInstallSpec> config);
77+
7278
/**
7379
* Ensure plugin is installed into the distribution.
7480
*/

test/test-clusters/src/main/java/org/elasticsearch/test/cluster/util/ArchivePatcher.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,24 @@ public Path patch() {
5050
ZipEntry entry = entries.nextElement();
5151
output.putNextEntry(entry);
5252
if (overrides.containsKey(entry.getName())) {
53+
Function<? super String, ? extends InputStream> override = overrides.remove(entry.getName());
5354
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input.getInputStream(entry)))) {
5455
String content = reader.lines().collect(Collectors.joining(System.lineSeparator()));
55-
overrides.get(entry.getName()).apply(content).transferTo(output);
56+
override.apply(content).transferTo(output);
5657
}
5758
} else {
5859
input.getInputStream(entry).transferTo(output);
5960
}
6061
output.closeEntry();
6162
}
63+
64+
for (Map.Entry<String, Function<? super String, ? extends InputStream>> override : overrides.entrySet()) {
65+
ZipEntry entry = new ZipEntry(override.getKey());
66+
output.putNextEntry(entry);
67+
override.getValue().apply("").transferTo(output);
68+
output.closeEntry();
69+
}
70+
6271
output.flush();
6372
output.finish();
6473
} catch (IOException e) {

0 commit comments

Comments
 (0)