Skip to content

Commit

Permalink
adapt unit tests for gpg keys
Browse files Browse the repository at this point in the history
  • Loading branch information
mcalmer committed Jan 31, 2025
1 parent fef69fa commit 8804a81
Showing 1 changed file with 75 additions and 30 deletions.
105 changes: 75 additions & 30 deletions java/code/src/com/suse/manager/hub/test/HubManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,50 +108,91 @@ public class HubManagerTest extends JMockBaseTestCaseWithUser {
private String originalProductVersion;

static class MockTaskomaticApi extends TaskomaticApi {
private boolean invokeCalled;
private boolean verify;
private int invoked;
private String invokeName;
private String invokeBunch;
private String invokeRootCaFilename;
private String invokeRootCaContent;
private String invokeGpgKeyContent;
private int expectedInvocations;
private String expectedRootCaFilename;
private String expectedRootCaContent;
private String expectedGpgKeyContent;

MockTaskomaticApi() {
resetTaskomaticCall();
}

public void setVerify(boolean doVerify) {
verify = doVerify;
}

public void resetTaskomaticCall() {
invokeCalled = false;
verify = false;
invoked = 0;
invokeName = "";
invokeBunch = "";
invokeRootCaFilename = null;
invokeRootCaContent = null;
invokeGpgKeyContent = null;
}

public void setExpectations(int expectedInvocationsIn,
String expectedRootCaFilenameIn,
String expectedRootCaContentIn,
String expectedGpgKeyContentIn) {
setVerify(true);
expectedInvocations = expectedInvocationsIn;
expectedRootCaFilename = expectedRootCaFilenameIn;
expectedRootCaContent = expectedRootCaContentIn;
expectedGpgKeyContent = expectedGpgKeyContentIn;
}

public void verifyTaskoRootCaCertUpdateCall(String expectedRootCaFilename,
String expectedRootCaContent) {
assertTrue(invokeCalled);
public void verifyTaskoCall() {
if (!verify) {
return;
}
assertEquals("tasko.scheduleSingleSatBunchRun", invokeName);
assertEquals("root-ca-cert-update-bunch", invokeBunch);
assertEquals(expectedRootCaFilename, invokeRootCaFilename);
assertEquals(expectedRootCaContent, invokeRootCaContent);
resetTaskomaticCall();
if (invokeBunch.equals("root-ca-cert-update-bunch")) {
assertEquals(expectedRootCaFilename, invokeRootCaFilename);
assertEquals(expectedRootCaContent, invokeRootCaContent);
}
else if (invokeBunch.equals("custom-gpg-key-import-bunch")) {
assertEquals(expectedGpgKeyContent, invokeGpgKeyContent);
}
else {
fail("Unexpected bunch called: " + invokeBunch);
}
}

public void verifyInvokations() {
assertEquals(expectedInvocations, invoked);
}

@Override
protected Object invoke(String name, Object... args) throws TaskomaticApiException {
invokeCalled = true;
invoked += 1;
invokeName = name;
invokeBunch = (String) args[0];
Map<String, Object> paramList = (Map<String, Object>) args[1];
Map<String, String> fileToCaCertMap = (Map<String, String>) paramList.get("filename_to_root_ca_cert_map");
Optional<Map.Entry<String, String>> firstKeyVal = fileToCaCertMap.entrySet().stream().findFirst();
if (firstKeyVal.isPresent()) {
invokeRootCaFilename = firstKeyVal.get().getKey();
invokeRootCaContent = firstKeyVal.get().getValue();
if (invokeBunch.equals("root-ca-cert-update-bunch")) {
Map<String, String> fileToCaCertMap =
(Map<String, String>) paramList.get("filename_to_root_ca_cert_map");
Optional<Map.Entry<String, String>> firstKeyVal = fileToCaCertMap.entrySet().stream().findFirst();
if (firstKeyVal.isPresent()) {
invokeRootCaFilename = firstKeyVal.get().getKey();
invokeRootCaContent = firstKeyVal.get().getValue();
}
else {
invokeRootCaFilename = null;
invokeRootCaContent = null;
}
}
else {
invokeRootCaFilename = null;
invokeRootCaContent = null;
else if (invokeBunch.equals("custom-gpg-key-import-bunch")) {
invokeGpgKeyContent = (String) paramList.get("gpg-key");
}
verifyTaskoCall();
return null;
}
}
Expand Down Expand Up @@ -459,36 +500,40 @@ public void canUpdateServer() {
}

@Test
public void canSaveRootCa() throws TaskomaticApiException {
public void canSaveRootCaAndGpg() throws TaskomaticApiException {
mockTaskomaticApi.resetTaskomaticCall();
mockTaskomaticApi.setExpectations(2, "hub_dummy.hub.fqdn_root_ca.pem",
"dummy-hub-certificate-data", "dummy-gpg-key");
hubManager.saveNewServer(getValidToken("dummy.hub.fqdn"), IssRole.HUB, "dummy-hub-certificate-data",
"");
mockTaskomaticApi.verifyTaskoRootCaCertUpdateCall("hub_dummy.hub.fqdn_root_ca.pem",
"dummy-hub-certificate-data");
"dummy-gpg-key");
mockTaskomaticApi.verifyInvokations();

mockTaskomaticApi.resetTaskomaticCall();
mockTaskomaticApi.setExpectations(1, "hub_dummy2.hub.fqdn_root_ca.pem", "", "");
hubManager.saveNewServer(getValidToken("dummy2.hub.fqdn"), IssRole.HUB, "", "");
mockTaskomaticApi.verifyTaskoRootCaCertUpdateCall("hub_dummy2.hub.fqdn_root_ca.pem", "");
mockTaskomaticApi.verifyInvokations();

mockTaskomaticApi.resetTaskomaticCall();
mockTaskomaticApi.setExpectations(1, "hub_dummy3.hub.fqdn_root_ca.pem", "", "");
hubManager.saveNewServer(getValidToken("dummy3.hub.fqdn"), IssRole.HUB, null, null);
mockTaskomaticApi.verifyTaskoRootCaCertUpdateCall("hub_dummy3.hub.fqdn_root_ca.pem", "");
mockTaskomaticApi.verifyInvokations();

mockTaskomaticApi.resetTaskomaticCall();
mockTaskomaticApi.setExpectations(1, "peripheral_dummy.periph.fqdn_root_ca.pem",
"dummy-periph-certificate-data", null);
hubManager.saveNewServer(getValidToken("dummy.periph.fqdn"), IssRole.PERIPHERAL,
"dummy-periph-certificate-data", null);
mockTaskomaticApi.verifyTaskoRootCaCertUpdateCall("peripheral_dummy.periph.fqdn_root_ca.pem",
"dummy-periph-certificate-data");
mockTaskomaticApi.verifyInvokations();

mockTaskomaticApi.resetTaskomaticCall();
mockTaskomaticApi.setExpectations(1, "peripheral_dummy2.periph.fqdn_root_ca.pem", "", "");
hubManager.saveNewServer(getValidToken("dummy2.periph.fqdn"), IssRole.PERIPHERAL, "", "");
mockTaskomaticApi.verifyTaskoRootCaCertUpdateCall("peripheral_dummy2.periph.fqdn_root_ca.pem",
"");
mockTaskomaticApi.verifyInvokations();

mockTaskomaticApi.resetTaskomaticCall();
mockTaskomaticApi.setExpectations(1, "peripheral_dummy3.periph.fqdn_root_ca.pem", "", "");
hubManager.saveNewServer(getValidToken("dummy3.periph.fqdn"), IssRole.PERIPHERAL, null, null);
mockTaskomaticApi.verifyTaskoRootCaCertUpdateCall("peripheral_dummy3.periph.fqdn_root_ca.pem",
"");
mockTaskomaticApi.verifyInvokations();
}

@Test
Expand Down

0 comments on commit 8804a81

Please sign in to comment.