-
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.
feat(ci): run some end to end tests on CI (#18)
* refactor: move tests to more appropriate package * refactor: split process vs. input stream handling of powermetrics input * chore(tests): add CI-specific tests that can run against resources … instead of running the real process, which isn't possible in CI because this requires sudo access. * feat(ci): only activate CI-specific tests * refactor: clean-up
- Loading branch information
Showing
13 changed files
with
193 additions
and
68 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
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
43 changes: 43 additions & 0 deletions
43
...a/io/github/metacosm/power/sensors/macos/powermetrics/ProcessMacOSPowermetricsSensor.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,43 @@ | ||
package io.github.metacosm.power.sensors.macos.powermetrics; | ||
|
||
import java.io.InputStream; | ||
|
||
public class ProcessMacOSPowermetricsSensor extends MacOSPowermetricsSensor { | ||
private Process powermetrics; | ||
|
||
public ProcessMacOSPowermetricsSensor() { | ||
// extract metadata | ||
try { | ||
final var exec = new ProcessBuilder() | ||
.command("sudo", "powermetrics", "--samplers", "cpu_power", "-i", "10", "-n", "1") | ||
.start(); | ||
initMetadata(exec.getInputStream()); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Couldn't execute powermetrics to extract metadata", e); | ||
} | ||
} | ||
|
||
public void start(long frequency) throws Exception { | ||
if (!isStarted()) { | ||
// it takes some time for the external process in addition to the sampling time so adjust the sampling frequency to account for this so that at most one measure occurs during the sampling time window | ||
final var freq = Long.toString(frequency - 50); | ||
powermetrics = new ProcessBuilder().command("sudo", "powermetrics", "--samplers", "cpu_power,tasks", | ||
"--show-process-samp-norm", "--show-process-gpu", "-i", freq).start(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isStarted() { | ||
return powermetrics != null && powermetrics.isAlive(); | ||
} | ||
|
||
@Override | ||
protected InputStream getInputStream() { | ||
return powermetrics.getInputStream(); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
powermetrics.destroy(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
.../io/github/metacosm/power/sensors/macos/powermetrics/ResourceMacOSPowermetricsSensor.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,30 @@ | ||
package io.github.metacosm.power.sensors.macos.powermetrics; | ||
|
||
import java.io.InputStream; | ||
|
||
public class ResourceMacOSPowermetricsSensor extends MacOSPowermetricsSensor { | ||
private final String resourceName; | ||
private boolean started; | ||
|
||
public ResourceMacOSPowermetricsSensor(String resourceName) { | ||
this.resourceName = resourceName; | ||
initMetadata(getInputStream()); | ||
} | ||
|
||
@Override | ||
protected InputStream getInputStream() { | ||
return Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName); | ||
} | ||
|
||
@Override | ||
public boolean isStarted() { | ||
return started; | ||
} | ||
|
||
@Override | ||
public void start(long samplingFrequencyInMillis) { | ||
if (!started) { | ||
started = true; | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
server/src/test/java/io/github/metacosm/power/CIPowerResourceTest.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,53 @@ | ||
package io.github.metacosm.power; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.Set; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.junit.TestProfile; | ||
|
||
@QuarkusTest | ||
@TestProfile(CIQuarkusTestProfile.class) | ||
public class CIPowerResourceTest { | ||
|
||
protected long getPid() { | ||
return 29419; | ||
} | ||
|
||
@Test | ||
public void testPowerEndpoint() { | ||
final var pid = getPid(); | ||
given() | ||
.when().get("/power/" + pid) | ||
.then() | ||
.statusCode(200); | ||
} | ||
|
||
@Test | ||
public void testMacOSAppleSiliconMetadataEndpoint() { | ||
final var metadata = given() | ||
.when().get("/power/metadata") | ||
.then() | ||
.statusCode(200) | ||
.extract().body().as(SensorMetadata.class); | ||
assertEquals(4, metadata.componentCardinality()); | ||
assertTrue(metadata.documentation().contains("powermetrics")); | ||
assertTrue(metadata.components().keySet().containsAll(Set.of("CPU", "GPU", "ANE", "cpuShare"))); | ||
|
||
final var cpu = metadata.metadataFor("CPU"); | ||
assertEquals(0, cpu.index()); | ||
assertEquals("CPU", cpu.name()); | ||
assertEquals("mW", cpu.unit()); | ||
assertTrue(cpu.isAttributed()); | ||
|
||
final var cpuShare = metadata.metadataFor("cpuShare"); | ||
assertEquals(3, cpuShare.index()); | ||
assertEquals("cpuShare", cpuShare.name()); | ||
assertEquals("decimal percentage", cpuShare.unit()); | ||
assertFalse(cpuShare.isAttributed()); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
server/src/test/java/io/github/metacosm/power/CIQuarkusTestProfile.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,13 @@ | ||
package io.github.metacosm.power; | ||
|
||
import java.util.Set; | ||
|
||
import io.quarkus.test.junit.QuarkusTestProfile; | ||
|
||
public class CIQuarkusTestProfile implements QuarkusTestProfile { | ||
|
||
@Override | ||
public Set<String> tags() { | ||
return Set.of("ci"); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
server/src/test/java/io/github/metacosm/power/MockPowerMeasurer.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,13 @@ | ||
package io.github.metacosm.power; | ||
|
||
import io.quarkus.test.Mock; | ||
|
||
@Mock | ||
@SuppressWarnings("unused") | ||
public class MockPowerMeasurer extends PowerMeasurer { | ||
|
||
@Override | ||
protected long validPIDOrFail(String pid) { | ||
return Long.parseLong(pid); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
server/src/test/java/io/github/metacosm/power/MockPowerSensor.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,12 @@ | ||
package io.github.metacosm.power; | ||
|
||
import io.github.metacosm.power.sensors.macos.powermetrics.ResourceMacOSPowermetricsSensor; | ||
import io.quarkus.test.Mock; | ||
|
||
@Mock | ||
@SuppressWarnings("unused") | ||
public class MockPowerSensor extends ResourceMacOSPowermetricsSensor { | ||
public MockPowerSensor() { | ||
super("sonoma-m1max.txt"); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...a/io/github/metacosm/PowerResourceIT.java → ...ithub/metacosm/power/PowerResourceIT.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
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
Oops, something went wrong.