Skip to content

Commit fb9b55c

Browse files
authored
Allow overriding of module metadata files in integration tests (#120427) (#120428)
1 parent 2b9580f commit fb9b55c

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
@@ -78,6 +78,8 @@ public abstract class AbstractLocalClusterFactory<S extends LocalClusterSpec, H
7878
private static final String TESTS_CLUSTER_FIPS_JAR_PATH_SYSPROP = "tests.cluster.fips.jars.path";
7979
private static final String TESTS_CLUSTER_DEBUG_ENABLED_SYSPROP = "tests.cluster.debug.enabled";
8080
private static final String ENABLE_DEBUG_JVM_ARGS = "-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=";
81+
private static final String ENTITLEMENT_POLICY_YAML = "entitlement-policy.yaml";
82+
private static final String PLUGIN_DESCRIPTOR_PROPERTIES = "plugin-descriptor.properties";
8183

8284
private final DistributionResolver distributionResolver;
8385

@@ -662,7 +664,7 @@ private void installPlugins() {
662664
.findFirst()
663665
.map(path -> {
664666
DefaultPluginInstallSpec installSpec = plugin.getValue();
665-
// Path the plugin archive with configured overrides if necessary
667+
// Patch the plugin archive with configured overrides if necessary
666668
if (installSpec.entitlementsOverride != null || installSpec.propertiesOverride != null) {
667669
Path target;
668670
try {
@@ -673,13 +675,13 @@ private void installPlugins() {
673675
ArchivePatcher patcher = new ArchivePatcher(path, target);
674676
if (installSpec.entitlementsOverride != null) {
675677
patcher.override(
676-
"entitlement-policy.yaml",
678+
ENTITLEMENT_POLICY_YAML,
677679
original -> installSpec.entitlementsOverride.apply(original).asStream()
678680
);
679681
}
680682
if (installSpec.propertiesOverride != null) {
681683
patcher.override(
682-
"plugin-descriptor.properties",
684+
PLUGIN_DESCRIPTOR_PROPERTIES,
683685
original -> installSpec.propertiesOverride.apply(original).asStream()
684686
);
685687
}
@@ -729,11 +731,11 @@ private void installModules() {
729731
.map(Path::of)
730732
.toList();
731733

732-
spec.getModules().forEach(module -> installModule(module, modulePaths));
734+
spec.getModules().forEach((module, spec) -> installModule(module, spec, modulePaths));
733735
}
734736
}
735737

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

746-
throw new RuntimeException(
748+
return new RuntimeException(
747749
"Unable to locate module '"
748750
+ moduleName
749751
+ "'. Ensure you've added the following to the build script for project '"
@@ -758,20 +760,34 @@ private void installModule(String moduleName, List<Path> modulePaths) {
758760
});
759761

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

762-
// Install any extended plugins
780+
// Install any extended modules
763781
Properties pluginProperties = new Properties();
764782
try (
765-
InputStream in = new BufferedInputStream(
766-
new FileInputStream(modulePath.resolve("plugin-descriptor.properties").toFile())
767-
)
783+
InputStream in = new BufferedInputStream(new FileInputStream(modulePath.resolve(PLUGIN_DESCRIPTOR_PROPERTIES).toFile()))
768784
) {
769785
pluginProperties.load(in);
770786
String extendedProperty = pluginProperties.getProperty("extended.plugins");
771787
if (extendedProperty != null) {
772-
String[] extendedPlugins = extendedProperty.split(",");
773-
for (String plugin : extendedPlugins) {
774-
installModule(plugin, modulePaths);
788+
String[] extendedModules = extendedProperty.split(",");
789+
for (String module : extendedModules) {
790+
installModule(module, new DefaultPluginInstallSpec(), modulePaths);
775791
}
776792
}
777793
} 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)