Skip to content

Commit 0837311

Browse files
authored
Make entitlement IT tests reflective (#121355) (#121418)
This commit adds an EntitlementTest annotation that can be used on classes containing test actions for entitlements. The annotation mirrors the parameters of CheckAction. Only file check actions are currently converted, the rest can be moved and annotated as followups. Note that the check action name is simply the method name, no fancy name manipulation is done.
1 parent fb98d84 commit 0837311

File tree

3 files changed

+223
-135
lines changed

3 files changed

+223
-135
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.entitlement.qa.test;
11+
12+
import java.lang.annotation.ElementType;
13+
import java.lang.annotation.Retention;
14+
import java.lang.annotation.RetentionPolicy;
15+
import java.lang.annotation.Target;
16+
17+
@Target(ElementType.METHOD)
18+
@Retention(RetentionPolicy.RUNTIME)
19+
public @interface EntitlementTest {
20+
enum ExpectedAccess {
21+
PLUGINS,
22+
ES_MODULES_ONLY,
23+
ALWAYS_DENIED
24+
}
25+
26+
ExpectedAccess expectedAccess();
27+
28+
int fromJavaVersion() default -1;
29+
}

libs/entitlement/qa/entitlement-test-plugin/src/main/java/org/elasticsearch/entitlement/qa/test/FileCheckActions.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.nio.file.attribute.UserPrincipal;
2323
import java.util.Scanner;
2424

25+
import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.PLUGINS;
26+
2527
@SuppressForbidden(reason = "Explicitly checking APIs that are forbidden")
2628
class FileCheckActions {
2729

@@ -43,38 +45,47 @@ private static Path readWriteFile() {
4345
return testRootDir.resolve("read_write_file");
4446
}
4547

48+
@EntitlementTest(expectedAccess = PLUGINS)
4649
static void createScannerFile() throws FileNotFoundException {
4750
new Scanner(readFile().toFile());
4851
}
4952

53+
@EntitlementTest(expectedAccess = PLUGINS)
5054
static void createScannerFileWithCharset() throws IOException {
5155
new Scanner(readFile().toFile(), StandardCharsets.UTF_8);
5256
}
5357

58+
@EntitlementTest(expectedAccess = PLUGINS)
5459
static void createScannerFileWithCharsetName() throws FileNotFoundException {
5560
new Scanner(readFile().toFile(), "UTF-8");
5661
}
5762

63+
@EntitlementTest(expectedAccess = PLUGINS)
5864
static void createFileOutputStreamString() throws IOException {
5965
new FileOutputStream(readWriteFile().toString()).close();
6066
}
6167

68+
@EntitlementTest(expectedAccess = PLUGINS)
6269
static void createFileOutputStreamStringWithAppend() throws IOException {
6370
new FileOutputStream(readWriteFile().toString(), false).close();
6471
}
6572

73+
@EntitlementTest(expectedAccess = PLUGINS)
6674
static void createFileOutputStreamFile() throws IOException {
6775
new FileOutputStream(readWriteFile().toFile()).close();
6876
}
6977

78+
@EntitlementTest(expectedAccess = PLUGINS)
7079
static void createFileOutputStreamFileWithAppend() throws IOException {
7180
new FileOutputStream(readWriteFile().toFile(), false).close();
7281
}
7382

83+
@EntitlementTest(expectedAccess = PLUGINS)
7484
static void filesProbeContentType() throws IOException {
7585
Files.probeContentType(readFile());
7686
}
7787

88+
@EntitlementTest(expectedAccess = PLUGINS)
7889
static void filesSetOwner() throws IOException {
7990
UserPrincipal owner = EntitledActions.getFileOwner(readWriteFile());
8091
Files.setOwner(readWriteFile(), owner); // set to existing owner, just trying to execute the method

0 commit comments

Comments
 (0)