forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow overriding of plugin metadata files in integration tests (elast…
- Loading branch information
1 parent
70d0621
commit 26d6261
Showing
7 changed files
with
190 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...clusters/src/main/java/org/elasticsearch/test/cluster/local/DefaultPluginInstallSpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.test.cluster.local; | ||
|
||
import org.elasticsearch.test.cluster.util.resource.Resource; | ||
|
||
import java.util.function.Function; | ||
|
||
public class DefaultPluginInstallSpec implements PluginInstallSpec { | ||
Function<? super String, ? extends Resource> propertiesOverride; | ||
Function<? super String, ? extends Resource> entitlementsOverride; | ||
|
||
@Override | ||
public PluginInstallSpec withPropertiesOverride(Function<? super String, ? extends Resource> override) { | ||
this.propertiesOverride = override; | ||
return this; | ||
} | ||
|
||
@Override | ||
public PluginInstallSpec withEntitlementsOverride(Function<? super String, ? extends Resource> override) { | ||
this.entitlementsOverride = override; | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
test/test-clusters/src/main/java/org/elasticsearch/test/cluster/local/PluginInstallSpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.test.cluster.local; | ||
|
||
import org.elasticsearch.test.cluster.util.resource.Resource; | ||
|
||
import java.util.function.Function; | ||
|
||
public interface PluginInstallSpec { | ||
|
||
/** | ||
* Override bundled plugin properties file with the given {@link Resource}. The provided override function receives the original | ||
* file content as function argument. | ||
* | ||
* @param override function returning resource used to override bundled properties file | ||
*/ | ||
PluginInstallSpec withPropertiesOverride(Function<? super String, ? extends Resource> override); | ||
|
||
/** | ||
* Override bundled entitlements policy file with the given {@link Resource}. The provided override function receives the original | ||
* file content as function argument. | ||
* | ||
* @param override function returning resource used to override bundled entitlements policy file | ||
*/ | ||
PluginInstallSpec withEntitlementsOverride(Function<? super String, ? extends Resource> override); | ||
} |
70 changes: 70 additions & 0 deletions
70
test/test-clusters/src/main/java/org/elasticsearch/test/cluster/util/ArchivePatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.test.cluster.util; | ||
|
||
import java.io.BufferedOutputStream; | ||
import java.io.BufferedReader; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Path; | ||
import java.util.Enumeration; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipFile; | ||
import java.util.zip.ZipOutputStream; | ||
|
||
public class ArchivePatcher { | ||
private final Path original; | ||
private final Path target; | ||
private final Map<String, Function<? super String, ? extends InputStream>> overrides = new HashMap<>(); | ||
|
||
public ArchivePatcher(Path original, Path target) { | ||
this.original = original; | ||
this.target = target; | ||
} | ||
|
||
public void override(String filename, Function<? super String, ? extends InputStream> override) { | ||
this.overrides.put(filename, override); | ||
} | ||
|
||
public Path patch() { | ||
try ( | ||
ZipFile input = new ZipFile(original.toFile()); | ||
ZipOutputStream output = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(target.toFile()))) | ||
) { | ||
Enumeration<? extends ZipEntry> entries = input.entries(); | ||
while (entries.hasMoreElements()) { | ||
ZipEntry entry = entries.nextElement(); | ||
output.putNextEntry(entry); | ||
if (overrides.containsKey(entry.getName())) { | ||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input.getInputStream(entry)))) { | ||
String content = reader.lines().collect(Collectors.joining(System.lineSeparator())); | ||
overrides.get(entry.getName()).apply(content).transferTo(output); | ||
} | ||
} else { | ||
input.getInputStream(entry).transferTo(output); | ||
} | ||
output.closeEntry(); | ||
} | ||
output.flush(); | ||
output.finish(); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("Failed to patch archive", e); | ||
} | ||
|
||
return target; | ||
} | ||
} |