From 9b494eb0a3a7c180642e7e6de37cff74b7cfa78b Mon Sep 17 00:00:00 2001 From: Gavin Date: Thu, 28 Mar 2024 20:38:48 -0700 Subject: [PATCH] Add git commit to status() method & build --- Dockerfile | 2 + build.gradle | 11 +++++- src/us/kbase/workspace/WorkspaceServer.java | 2 + .../kbase/workspace/gitcommit/GitCommit.java | 38 +++++++++++++++++++ .../HandleAndBytestreamIntegrationTest.java | 32 ++++------------ .../test/kbase/JSONRPCLayerTest.java | 36 ++++++++++-------- .../kbase/SampleServiceIntegrationTest.java | 10 ++--- 7 files changed, 85 insertions(+), 46 deletions(-) create mode 100644 src/us/kbase/workspace/gitcommit/GitCommit.java diff --git a/Dockerfile b/Dockerfile index d46802d8..84b9b80e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,6 +20,8 @@ COPY docsource /tmp/workspace/docsource/ COPY lib /tmp/workspace/lib/ COPY src /tmp/workspace/src/ COPY war /tmp/workspace/war/ +# for the git commit +COPY .git /tmp/workspace/.git/ RUN ./gradlew war FROM ubuntu:18.04 diff --git a/build.gradle b/build.gradle index 6cce29b8..fd96d5f7 100644 --- a/build.gradle +++ b/build.gradle @@ -30,7 +30,6 @@ var IN_JAR_JAVA_DOC_DIR = "$IN_JAR_DOC_DIR/javadoc" var LOC_WS_SPEC = "$rootDir/workspace.spec" var LOC_DOC_HTML = "$rootDir/docshtml" -// TODO NOW handle the git commit the same way as auth does - commit 7 // TODO NOW delete build.xml and Makefile - commit 8 // TODO NOW run tests from Eclipse w/o specifying classpath manually & remove sourceSets & claspath - commit 9 // TODO NOW update any ant refs in docs to gradle & the update schema script build & location @@ -38,11 +37,21 @@ var LOC_DOC_HTML = "$rootDir/docshtml" repositories { mavenCentral() } + +task buildGitCommitFile { + doLast { + def commitId = grgit.head().id + // is there a variable for builddir/classes/java/main? + file("$buildDir/classes/java/main/us/kbase/workspace/gitcommit/gitcommit").text = commitId + } +} + compileJava { // build needs to be java 8 compatible so jars can be used in java 8 projects // TODO BUILD remove when we no longer support java 8, use `options.release = 11` if needed java.sourceCompatibility = JavaVersion.VERSION_1_8 java.targetCompatibility = JavaVersion.VERSION_1_8 + finalizedBy buildGitCommitFile } javadoc { diff --git a/src/us/kbase/workspace/WorkspaceServer.java b/src/us/kbase/workspace/WorkspaceServer.java index 64c98637..eb4ee4a4 100644 --- a/src/us/kbase/workspace/WorkspaceServer.java +++ b/src/us/kbase/workspace/WorkspaceServer.java @@ -70,6 +70,7 @@ import us.kbase.workspace.database.WorkspaceObjectData; import us.kbase.workspace.database.WorkspaceUser; import us.kbase.workspace.database.WorkspaceUserMetadata; +import us.kbase.workspace.gitcommit.GitCommit; import us.kbase.workspace.kbase.InitWorkspaceServer.InitReporter; import us.kbase.workspace.kbase.InitWorkspaceServer; import us.kbase.workspace.kbase.InitWorkspaceServer.WorkspaceInitResults; @@ -1766,6 +1767,7 @@ public Map status() { returnVal.put("dependencies", dstate); returnVal.put("version", VERSION); returnVal.put("git_url", GIT); + returnVal.put("git_commit", GitCommit.COMMIT); returnVal.put("freemem", Runtime.getRuntime().freeMemory()); returnVal.put("totalmem", Runtime.getRuntime().totalMemory()); returnVal.put("maxmem", Runtime.getRuntime().maxMemory()); diff --git a/src/us/kbase/workspace/gitcommit/GitCommit.java b/src/us/kbase/workspace/gitcommit/GitCommit.java new file mode 100644 index 00000000..cc030f62 --- /dev/null +++ b/src/us/kbase/workspace/gitcommit/GitCommit.java @@ -0,0 +1,38 @@ +package us.kbase.workspace.gitcommit; + +import java.io.InputStream; +import java.util.Scanner; + +/** The Git commit from which the service was built. Expects a file called "gitcommit" in the same + * directory as the class file which contains the commit hash. + * + * If the file is missing, the Git commit will be replaced with an error message. + * @author gaprice@lbl.gov + * + */ +public class GitCommit { + + // can't really test this easily since the file must be baked into the jar or war, + // just test manually + + /** The Git commit from which the service was built. */ + public static final String COMMIT; + + private static final String COMMIT_FILE_NAME = "gitcommit"; + + static { + final InputStream is = GitCommit.class.getResourceAsStream(COMMIT_FILE_NAME); + final String commit; + if (is == null) { + commit = "Missing git commit file " + COMMIT_FILE_NAME + + ", should be in " + GitCommit.class.getPackage().getName(); + } else { + final Scanner s = new Scanner(is); + s.useDelimiter("\\A"); + commit = s.hasNext() ? s.next() : ""; + s.close(); + } + COMMIT = commit.trim(); + } + +} diff --git a/src/us/kbase/workspace/test/kbase/HandleAndBytestreamIntegrationTest.java b/src/us/kbase/workspace/test/kbase/HandleAndBytestreamIntegrationTest.java index 3a201eb3..d59b783d 100644 --- a/src/us/kbase/workspace/test/kbase/HandleAndBytestreamIntegrationTest.java +++ b/src/us/kbase/workspace/test/kbase/HandleAndBytestreamIntegrationTest.java @@ -5,6 +5,7 @@ import static org.hamcrest.CoreMatchers.startsWith; import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; +import static us.kbase.common.test.TestCommon.list; import static us.kbase.common.test.TestCommon.set; import static us.kbase.workspace.test.kbase.JSONRPCLayerTester.administerCommand; @@ -370,32 +371,24 @@ public static void tearDownClass() throws Exception { @Test public void status() throws Exception { + // only test the parts of the status that are relevant for the handle & blobstore services final Map st = CLIENT1.status(); //top level items - assertThat("incorrect state", st.get("state"), is((Object) "OK")); - assertThat("incorrect message", st.get("message"), is((Object) "OK")); - // should throw an error if not a valid semver - Version.valueOf((String) st.get("version")); - assertThat("incorrect git url", st.get("git_url"), - is((Object) "https://github.com/kbase/workspace_deluxe")); - checkMem(st.get("freemem"), "freemem"); - checkMem(st.get("totalmem"), "totalmem"); - checkMem(st.get("maxmem"), "maxmem"); + assertThat("incorrect state", st.get("state"), is("OK")); //deps @SuppressWarnings("unchecked") - final List> deps = - (List>) st.get("dependencies"); + final List> deps = (List>) st.get("dependencies"); assertThat("missing dependencies", deps.size(), is(4)); final Iterator> gotiter = deps.iterator(); - for (final String name: Arrays.asList( + for (final String name: list( "MongoDB", "S3", "Linked Shock for IDs", "Handle service")) { final Map g = gotiter.next(); - assertThat("incorrect name", (String) g.get("name"), is(name)); - assertThat("incorrect state", g.get("state"), is((Object) "OK")); - assertThat("incorrect message", g.get("message"), is((Object) "OK")); + assertThat("incorrect name", g.get("name"), is(name)); + assertThat("incorrect state", g.get("state"), is("OK")); + assertThat("incorrect message", g.get("message"), is("OK")); if (name.equals("S3")) { assertThat("incorrect version", g.get("version"), is("Unknown")); } else { @@ -404,15 +397,6 @@ public void status() throws Exception { } } - private void checkMem(final Object num, final String name) - throws Exception { - if (num instanceof Integer) { - assertThat("bad " + name, (Integer) num > 0, is(true)); - } else { - assertThat("bad " + name, (Long) num > 0, is(true)); - } - } - @SuppressWarnings("deprecation") @Test public void basicHandleTest() throws Exception { diff --git a/src/us/kbase/workspace/test/kbase/JSONRPCLayerTest.java b/src/us/kbase/workspace/test/kbase/JSONRPCLayerTest.java index da0e3f91..b606ba6c 100644 --- a/src/us/kbase/workspace/test/kbase/JSONRPCLayerTest.java +++ b/src/us/kbase/workspace/test/kbase/JSONRPCLayerTest.java @@ -9,6 +9,7 @@ import static org.junit.Assert.fail; import static us.kbase.common.test.TestCommon.assertExceptionCorrect; import static us.kbase.common.test.TestCommon.list; +import static us.kbase.common.test.TestCommon.set; import java.io.InputStream; import java.net.HttpURLConnection; @@ -111,35 +112,40 @@ public void ver() throws Exception { @Test public void status() throws Exception { final Map st = CLIENT1.status(); + + assertThat("incorrect status keys", st.keySet(), is(set( + "state", "message", "dependencies", "version", "git_url", "git_commit", + "freemem", "totalmem", "maxmem"))); //top level items - assertThat("incorrect state", st.get("state"), is((Object) "OK")); - assertThat("incorrect message", st.get("message"), is((Object) "OK")); + assertThat("incorrect state", st.get("state"), is("OK")); + assertThat("incorrect message", st.get("message"), is("OK")); assertThat("incorrect version", st.get("version"), is(VER)); assertThat("incorrect git url", st.get("git_url"), - is((Object) "https://github.com/kbase/workspace_deluxe")); + is("https://github.com/kbase/workspace_deluxe")); + final String gc = (String) st.get("git_commit"); + if (gc.length() == 40) { // it's a git hash, probably running from gradle + assertThat("is SHA1 hash", gc.matches("^[a-fA-F0-9]{40}$"), is(true)); + } else { // probably running from an IDE + assertThat("incorrect git commit", st.get("git_commit"), + is("Missing git commit file gitcommit, " + + "should be in us.kbase.workspace.gitcommit")); + } checkMem(st.get("freemem"), "freemem"); checkMem(st.get("totalmem"), "totalmem"); checkMem(st.get("maxmem"), "maxmem"); //deps @SuppressWarnings("unchecked") - final List> deps = - (List>) st.get("dependencies"); + final List> deps = (List>) st.get("dependencies"); assertThat("missing dependencies", deps.size(), is(2)); - final List exp = new ArrayList(); - exp.add("MongoDB"); - exp.add("GridFS"); - final Iterator expiter = exp.iterator(); final Iterator> gotiter = deps.iterator(); - while (expiter.hasNext()) { + for (final String name: list("MongoDB", "GridFS")) { final Map g = gotiter.next(); - assertThat("incorrect name", (String) g.get("name"), - is(expiter.next())); - assertThat("incorrect state", g.get("state"), is((Object) "OK")); - assertThat("incorrect message", g.get("message"), - is((Object) "OK")); + assertThat("incorrect name", g.get("name"), is(name)); + assertThat("incorrect state", g.get("state"), is("OK")); + assertThat("incorrect message", g.get("message"), is("OK")); Version.valueOf((String) g.get("version")); } } diff --git a/src/us/kbase/workspace/test/kbase/SampleServiceIntegrationTest.java b/src/us/kbase/workspace/test/kbase/SampleServiceIntegrationTest.java index aa3f27ea..0706b22a 100644 --- a/src/us/kbase/workspace/test/kbase/SampleServiceIntegrationTest.java +++ b/src/us/kbase/workspace/test/kbase/SampleServiceIntegrationTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; import static us.kbase.common.test.controllers.ControllerCommon.findFreePort; +import static us.kbase.common.test.TestCommon.set; import static us.kbase.workspace.test.kbase.JSONRPCLayerTester.administerCommand; import java.io.File; @@ -13,7 +14,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.HashSet; import java.util.List; import java.util.Map; @@ -338,11 +338,9 @@ public void status() throws Exception { // only test the parts of the status that are relevant for the sample service final Map status = CLIENT1.status(); -// System.out.println(status); - - assertThat("incorrect status keys", status.keySet(), is(new HashSet<>(Arrays.asList( - "state", "message", "dependencies", "version", "git_url", "freemem", "totalmem", - "maxmem")))); + assertThat("incorrect status keys", status.keySet(), is(set( + "state", "message", "dependencies", "version", "git_url", "git_commit", + "freemem", "totalmem", "maxmem"))); assertThat("incorrect state", status.get("state"), is("OK"));