Skip to content

Commit

Permalink
Merge pull request #733 from kbase/dev-gradle2_upstream
Browse files Browse the repository at this point in the history
Add git commit to status() method & build
  • Loading branch information
MrCreosote authored Apr 23, 2024
2 parents 661daf7 + 9b494eb commit 26742b9
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 46 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,28 @@ 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

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 {
Expand Down
2 changes: 2 additions & 0 deletions src/us/kbase/workspace/WorkspaceServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1766,6 +1767,7 @@ public Map<String, Object> 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());
Expand Down
38 changes: 38 additions & 0 deletions src/us/kbase/workspace/gitcommit/GitCommit.java
Original file line number Diff line number Diff line change
@@ -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 [email protected]
*
*/
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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String, Object> 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<Map<String, String>> deps =
(List<Map<String, String>>) st.get("dependencies");
final List<Map<String, String>> deps = (List<Map<String, String>>) st.get("dependencies");
assertThat("missing dependencies", deps.size(), is(4));

final Iterator<Map<String, String>> gotiter = deps.iterator();
for (final String name: Arrays.asList(
for (final String name: list(
"MongoDB", "S3", "Linked Shock for IDs", "Handle service")) {
final Map<String, String> 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 {
Expand All @@ -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 {
Expand Down
36 changes: 21 additions & 15 deletions src/us/kbase/workspace/test/kbase/JSONRPCLayerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -111,35 +112,40 @@ public void ver() throws Exception {
@Test
public void status() throws Exception {
final Map<String, Object> 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<Map<String, String>> deps =
(List<Map<String, String>>) st.get("dependencies");
final List<Map<String, String>> deps = (List<Map<String, String>>) st.get("dependencies");
assertThat("missing dependencies", deps.size(), is(2));

final List<String> exp = new ArrayList<String>();
exp.add("MongoDB");
exp.add("GridFS");
final Iterator<String> expiter = exp.iterator();
final Iterator<Map<String, String>> gotiter = deps.iterator();
while (expiter.hasNext()) {
for (final String name: list("MongoDB", "GridFS")) {
final Map<String, String> 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"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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<String, Object> 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"));

Expand Down

0 comments on commit 26742b9

Please sign in to comment.