Skip to content

Commit ee2d164

Browse files
committed
Allow overriding of module metadata files in integration tests (elastic#120427)
1 parent 054cc62 commit ee2d164

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

7779
private final DistributionResolver distributionResolver;
7880

@@ -611,7 +613,7 @@ private void installPlugins() {
611613
.findFirst()
612614
.map(path -> {
613615
DefaultPluginInstallSpec installSpec = plugin.getValue();
614-
// Path the plugin archive with configured overrides if necessary
616+
// Patch the plugin archive with configured overrides if necessary
615617
if (installSpec.entitlementsOverride != null || installSpec.propertiesOverride != null) {
616618
Path target;
617619
try {
@@ -622,13 +624,13 @@ private void installPlugins() {
622624
ArchivePatcher patcher = new ArchivePatcher(path, target);
623625
if (installSpec.entitlementsOverride != null) {
624626
patcher.override(
625-
"entitlement-policy.yaml",
627+
ENTITLEMENT_POLICY_YAML,
626628
original -> installSpec.entitlementsOverride.apply(original).asStream()
627629
);
628630
}
629631
if (installSpec.propertiesOverride != null) {
630632
patcher.override(
631-
"plugin-descriptor.properties",
633+
PLUGIN_DESCRIPTOR_PROPERTIES,
632634
original -> installSpec.propertiesOverride.apply(original).asStream()
633635
);
634636
}
@@ -678,11 +680,11 @@ private void installModules() {
678680
.map(Path::of)
679681
.toList();
680682

681-
spec.getModules().forEach(module -> installModule(module, modulePaths));
683+
spec.getModules().forEach((module, spec) -> installModule(module, spec, modulePaths));
682684
}
683685
}
684686

685-
private void installModule(String moduleName, List<Path> modulePaths) {
687+
private void installModule(String moduleName, DefaultPluginInstallSpec installSpec, List<Path> modulePaths) {
686688
Path destination = distributionDir.resolve("modules").resolve(moduleName);
687689
if (Files.notExists(destination)) {
688690
Path modulePath = modulePaths.stream().filter(path -> path.endsWith(moduleName)).findFirst().orElseThrow(() -> {
@@ -692,7 +694,7 @@ private void installModule(String moduleName, List<Path> modulePaths) {
692694
? "project(xpackModule('" + moduleName.substring(7) + "'))"
693695
: "project(':modules:" + moduleName + "')";
694696

695-
throw new RuntimeException(
697+
return new RuntimeException(
696698
"Unable to locate module '"
697699
+ moduleName
698700
+ "'. Ensure you've added the following to the build script for project '"
@@ -707,20 +709,34 @@ private void installModule(String moduleName, List<Path> modulePaths) {
707709
});
708710

709711
IOUtils.syncWithCopy(modulePath, destination);
712+
try {
713+
if (installSpec.entitlementsOverride != null) {
714+
Path entitlementsFile = modulePath.resolve(ENTITLEMENT_POLICY_YAML);
715+
String original = Files.exists(entitlementsFile) ? Files.readString(entitlementsFile) : "";
716+
Path target = destination.resolve(ENTITLEMENT_POLICY_YAML);
717+
installSpec.entitlementsOverride.apply(original).writeTo(target);
718+
}
719+
if (installSpec.propertiesOverride != null) {
720+
Path propertiesFiles = modulePath.resolve(PLUGIN_DESCRIPTOR_PROPERTIES);
721+
String original = Files.exists(propertiesFiles) ? Files.readString(propertiesFiles) : "";
722+
Path target = destination.resolve(PLUGIN_DESCRIPTOR_PROPERTIES);
723+
installSpec.propertiesOverride.apply(original).writeTo(target);
724+
}
725+
} catch (IOException e) {
726+
throw new UncheckedIOException("Error patching module '" + moduleName + "'", e);
727+
}
710728

711-
// Install any extended plugins
729+
// Install any extended modules
712730
Properties pluginProperties = new Properties();
713731
try (
714-
InputStream in = new BufferedInputStream(
715-
new FileInputStream(modulePath.resolve("plugin-descriptor.properties").toFile())
716-
)
732+
InputStream in = new BufferedInputStream(new FileInputStream(modulePath.resolve(PLUGIN_DESCRIPTOR_PROPERTIES).toFile()))
717733
) {
718734
pluginProperties.load(in);
719735
String extendedProperty = pluginProperties.getProperty("extended.plugins");
720736
if (extendedProperty != null) {
721-
String[] extendedPlugins = extendedProperty.split(",");
722-
for (String plugin : extendedPlugins) {
723-
installModule(plugin, modulePaths);
737+
String[] extendedModules = extendedProperty.split(",");
738+
for (String module : extendedModules) {
739+
installModule(module, new DefaultPluginInstallSpec(), modulePaths);
724740
}
725741
}
726742
} 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
@@ -90,7 +90,7 @@ public static class LocalNodeSpec {
9090
private final Map<String, String> settings;
9191
private final List<EnvironmentProvider> environmentProviders;
9292
private final Map<String, String> environment;
93-
private final Set<String> modules;
93+
private final Map<String, DefaultPluginInstallSpec> modules;
9494
private final Map<String, DefaultPluginInstallSpec> plugins;
9595
private final DistributionType distributionType;
9696
private final Set<FeatureFlag> features;
@@ -112,7 +112,7 @@ public LocalNodeSpec(
112112
Map<String, String> settings,
113113
List<EnvironmentProvider> environmentProviders,
114114
Map<String, String> environment,
115-
Set<String> modules,
115+
Map<String, DefaultPluginInstallSpec> modules,
116116
Map<String, DefaultPluginInstallSpec> plugins,
117117
DistributionType distributionType,
118118
Set<FeatureFlag> features,
@@ -174,7 +174,7 @@ public DistributionType getDistributionType() {
174174
return distributionType;
175175
}
176176

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

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)