Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Windows build, fix NPE adding agent jar #227

Merged
merged 4 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Build
on:
pull_request:
schedule:
- cron: "0 0 * * 0"

jobs:
test:
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest, windows-latest, macos-latest ]

name: Test ${{ matrix.os }}
runs-on: ${{ matrix.os }}
timeout-minutes: 45
steps:
- name: Fetch Sources
uses: actions/checkout@v4

- name: Gradle Wrapper Validation
uses: gradle/wrapper-validation-action@v1

- name: Setup Java
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 17

- name: Install Bash on macOS
if: runner.os == 'macOS'
run: brew install bash

- name: Run Tests
shell: bash
run: |
set -e
bin/disable-gradle-daemon
./gradlew -i check integrationTest
bin/test
4 changes: 2 additions & 2 deletions agent/bin/test_install
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function install_petclinic (
git checkout "${oldVersion}"
;;
esac
./mvnw --quiet package -Dmaven.test.skip=true
./mvnw --quiet package -DskipTests -Dcheckstyle.skip=true -Dspring-javaformat.skip=true
else
case "${JAVA_VERSION}" in
1.8*)
Expand All @@ -37,7 +37,7 @@ function install_petclinic (
echo "No old version, not packaging in ${pkg}"
;;
17.*)
./mvnw --quiet package -Dmaven.test.skip=true
./mvnw --quiet package -DskipTests -Dcheckstyle.skip=true -Dspring-javaformat.skip=true
;;
esac
fi
Expand Down
1 change: 1 addition & 0 deletions agent/bin/test_run
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ report_failure() {

trap 'report_failure $LINENO' ERR

set -x
./test/petclinic/test
if [[ $JAVA_VERSION == 17.* ]]; then
./test/petclinic-fw/test
Expand Down
1 change: 1 addition & 0 deletions agent/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ dependencies {
testImplementation 'org.junit.vintage:junit-vintage-engine'

testImplementation 'com.github.stefanbirkner:system-rules:1.19.0'
testImplementation 'com.github.stefanbirkner:system-lambda:1.2.1'
testImplementation "org.mockito:mockito-core:[3.4,4)"
testImplementation 'com.github.marschall:memoryfilesystem:2.6.1'
}
Expand Down
54 changes: 38 additions & 16 deletions agent/src/main/java/com/appland/appmap/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -45,22 +48,7 @@ public static void premain(String agentArgs, Instrumentation inst) {
logger.debug("System properties: {}", System.getProperties());
logger.debug(new Exception(), "whereAmI");

URL jarURL = Agent.class.getProtectionDomain().getCodeSource().getLocation();
Path agentJar = Paths.get(jarURL.getPath());
// During testing of the agent itself, classes get loaded from a directory.
// The rest of the time (i.e. when it's actually deployed), they'll always
// come from a jar file.
JarFile jarFile = null;
if (!Files.isDirectory(agentJar)) {
try {
jarFile = new JarFile(Paths.get(jarURL.getPath()).toFile());
inst.appendToSystemClassLoaderSearch(jarFile);
} catch (IOException e) {
System.err.println("Failed to load the agent jar");
e.printStackTrace();
System.exit(1);
}
}
addAgentJar(inst);
inst.addTransformer(new ClassFileTransformer());

try {
Expand Down Expand Up @@ -113,6 +101,40 @@ public static void premain(String agentArgs, Instrumentation inst) {
}
}

private static void addAgentJar(Instrumentation inst) {
ProtectionDomain protectionDomain = Agent.class.getProtectionDomain();
CodeSource codeSource;
URL jarURL;
if (((codeSource = protectionDomain.getCodeSource()) == null)
|| ((jarURL = codeSource.getLocation()) == null)) {
// Nothing we can do if we can't locate the agent jar
return;
}

Path agentJar = null;
try {
agentJar = Paths.get(jarURL.toURI());
} catch (URISyntaxException e) {
// Doesn't seem like this should ever happen....
System.err.println("Failed getting path to agent jar");
e.printStackTrace();
System.exit(1);
}
// During testing of the agent itself, classes get loaded from a directory.
// The rest of the time (i.e. when it's actually deployed), they'll always
// come from a jar file.
JarFile jarFile = null;
if (!Files.isDirectory(agentJar)) {
try {
jarFile = new JarFile(agentJar.toFile());
inst.appendToSystemClassLoaderSearch(jarFile);
} catch (IOException e) {
System.err.println("Failed to load the agent jar");
e.printStackTrace();
System.exit(1);
}
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.springframework.samples.petclinic.web;

import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
class ExitController {
@DeleteMapping("/exit")
public void exit() {
System.exit(0);
}
}
59 changes: 23 additions & 36 deletions agent/src/test/java/com/appland/appmap/config/AppMapConfigTest.java
Original file line number Diff line number Diff line change
@@ -1,49 +1,36 @@
package com.appland.appmap.config;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemErr;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.io.TempDir;

public class AppMapConfigTest {

private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private final PrintStream originalErr = System.err;

@Rule
public TemporaryFolder tmpdir = new TemporaryFolder();

@Before
public void setUpStreams() {
System.setErr(new PrintStream(errContent));
}

@After
public void restoreStreams() {
System.setErr(originalErr);
}
@TempDir
Path tmpdir;

@Test
public void loadBadDirectory() {
// Trying to load appmap.yml in non-existent directory shouldn't work.
@DisabledOnOs(OS.WINDOWS)
public void loadBadDirectory() throws Exception {
// Trying to load appmap.yml in non-existent directory (that can't be created)
// shouldn't work.
Path badDir = Paths.get("/no-such-dir");
assertFalse(Files.exists(badDir));
AppMapConfig.load(badDir.resolve("appmap.yml"), false);
assertNotNull(errContent.toString());
assertTrue(errContent.toString().contains("file not found"));
String actualErr = tapSystemErr(() -> AppMapConfig.load(badDir.resolve("appmap.yml"), false));
assertNotNull(actualErr.toString());
assertTrue(actualErr.contains("file not found"));
}

@Test
Expand All @@ -67,13 +54,13 @@ public void preservesExisting() throws IOException {
}

@Test
public void requiresExisting() throws IOException {
public void requiresExisting() throws Exception {
Path configFile = Paths.get(System.getProperty("java.io.tmpdir"), "appmap.yml");
Files.deleteIfExists(configFile);

AppMapConfig.load(configFile, true);
assertNotNull(errContent.toString());
assertTrue(errContent.toString().contains("file not found"));
String actualErr = tapSystemErr(() -> AppMapConfig.load(configFile, true));
assertNotNull(actualErr.toString());
assertTrue(actualErr.contains("file not found"));
}

// If a non-existent config file in a subdirectory is specified, the
Expand All @@ -91,7 +78,7 @@ public void loadParent() {

@Test
public void hasAppmapDir() throws Exception {
Path configFile = tmpdir.newFile("appmap.yml").toPath();
Path configFile = tmpdir.resolve("appmap.yml");
final String contents = "appmap_dir: /appmap\n";
Files.write(configFile, contents.getBytes());
AppMapConfig.load(configFile, true);
Expand Down
Loading