mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
fix(OriginExecution): apply execution policy to process launch
This commit is contained in:
@@ -2,6 +2,7 @@ package work.slhaf.partner.core.action.runner.execution;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import work.slhaf.partner.core.action.runner.policy.WrappedLaunchSpec;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
@@ -105,6 +106,32 @@ class CommandExecutionServiceTest {
|
||||
Assertions.assertEquals("oops", session.getStderrBuffer().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExecWrappedLaunchSpecAppliesWorkingDirectory(@org.junit.jupiter.api.io.TempDir Path tempDir) {
|
||||
CommandExecutionService.Result result = service.exec(new WrappedLaunchSpec(
|
||||
"sh",
|
||||
List.of("-lc", "pwd"),
|
||||
tempDir.toString(),
|
||||
System.getenv()
|
||||
));
|
||||
|
||||
Assertions.assertTrue(result.isOk());
|
||||
Assertions.assertEquals(tempDir.toString(), result.getTotal());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExecWrappedLaunchSpecAppliesEnvironmentOverride() {
|
||||
CommandExecutionService.Result result = service.exec(new WrappedLaunchSpec(
|
||||
"sh",
|
||||
List.of("-lc", "printf '%s' \"$PARTNER_TEST_ENV\""),
|
||||
null,
|
||||
Map.of("PARTNER_TEST_ENV", "applied")
|
||||
));
|
||||
|
||||
Assertions.assertTrue(result.isOk());
|
||||
Assertions.assertEquals("applied", result.getTotal());
|
||||
}
|
||||
|
||||
private void waitForBufferContains(StringBuilder buffer, String expected) throws InterruptedException {
|
||||
long deadline = System.currentTimeMillis() + 2000;
|
||||
while (System.currentTimeMillis() < deadline) {
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package work.slhaf.partner.core.action.runner.execution;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import work.slhaf.partner.core.action.entity.MetaAction;
|
||||
import work.slhaf.partner.core.action.runner.policy.ExecutionPolicy;
|
||||
import work.slhaf.partner.core.action.runner.policy.ExecutionPolicyRegistry;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
class OriginExecutionServiceTest {
|
||||
|
||||
private static String originalUserHome;
|
||||
|
||||
@BeforeAll
|
||||
static void prepareTestHome() throws IOException {
|
||||
originalUserHome = System.getProperty("user.home");
|
||||
Path tempHome = Files.createTempDirectory("partner-test-home");
|
||||
System.setProperty("user.home", tempHome.toString());
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void restoreUserHome() {
|
||||
if (originalUserHome != null) {
|
||||
System.setProperty("user.home", originalUserHome);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOriginExecutionServiceAppliesExecutionPolicyEnvironment(@TempDir Path tempDir) throws IOException {
|
||||
Path script = tempDir.resolve("print_env.py");
|
||||
Files.writeString(script, "import os\nprint(os.getenv('PARTNER_ORIGIN_TEST', ''), end='')\n");
|
||||
|
||||
ExecutionPolicy originalPolicy = new ExecutionPolicy(
|
||||
ExecutionPolicy.Mode.DIRECT,
|
||||
"direct",
|
||||
ExecutionPolicy.Network.ENABLE,
|
||||
true,
|
||||
Map.of(),
|
||||
null,
|
||||
Set.of(),
|
||||
Set.of()
|
||||
);
|
||||
ExecutionPolicyRegistry.INSTANCE.updatePolicy(new ExecutionPolicy(
|
||||
ExecutionPolicy.Mode.DIRECT,
|
||||
"direct",
|
||||
ExecutionPolicy.Network.ENABLE,
|
||||
false,
|
||||
Map.of("PARTNER_ORIGIN_TEST", "origin-applied"),
|
||||
null,
|
||||
Set.of(),
|
||||
Set.of()
|
||||
));
|
||||
|
||||
try {
|
||||
var prepared = ExecutionPolicyRegistry.INSTANCE.prepare(List.of("python3", script.toString()));
|
||||
Assertions.assertEquals("origin-applied", prepared.getEnvironment().get("PARTNER_ORIGIN_TEST"));
|
||||
var directExec = CommandExecutionService.INSTANCE.exec(prepared);
|
||||
Assertions.assertTrue(directExec.isOk());
|
||||
Assertions.assertEquals("origin-applied", directExec.getTotal());
|
||||
OriginExecutionService service = new OriginExecutionService();
|
||||
MetaAction metaAction = new MetaAction("run", false, "python3", MetaAction.Type.ORIGIN, script.toString());
|
||||
var response = service.run(metaAction);
|
||||
Assertions.assertTrue(response.isOk());
|
||||
Assertions.assertEquals("origin-applied", response.getData());
|
||||
} finally {
|
||||
ExecutionPolicyRegistry.INSTANCE.updatePolicy(originalPolicy);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user