Skip to content

Commit

Permalink
Updated to use current version of Horreum 0.15.x
Browse files Browse the repository at this point in the history
  • Loading branch information
whitingjr committed Sep 19, 2024
1 parent 9052b35 commit 0f9c64b
Show file tree
Hide file tree
Showing 15 changed files with 580 additions and 328 deletions.
288 changes: 122 additions & 166 deletions pom.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ public BaseExecutionContext(String url, String credentials, PrintStream logger)
this.remoteLogger = new RemoteOutputStream(new CloseProofOutputStream(logger));
this.localLogger = logger;

//Retrieve Credentials
//TODO:: pass in DomainRequirement
List<StandardCredentials> credentialsList = CredentialsProvider.lookupCredentials(
StandardCredentials.class, // (1)
Jenkins.get(), // (1)
StandardCredentials.class,
Jenkins.get(),
ACL.SYSTEM,
Collections.emptyList()
) ;
Expand Down Expand Up @@ -143,9 +141,6 @@ private static ByteArrayOutputStream toByteArrayOutputStream(InputStream stream)
protected HorreumClient createClient() {
HorreumClient.Builder clientBuilder = new HorreumClient.Builder()
.horreumUrl(url)
.keycloakUrl(keycloak.getBaseUrl())
.keycloakRealm(keycloak.getRealm())
.clientId(keycloak.getClientId())
.horreumUser(usernameCredentials.getUsername())
.horreumPassword(usernameCredentials.getPassword().getPlainText());
return clientBuilder.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
package jenkins.plugins.horreum.auth;

import java.io.Serializable;
import java.util.List;

import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials;

import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.security.ACL;
import jenkins.model.Jenkins;

public class KeycloakAuthentication extends AbstractDescribableImpl<KeycloakAuthentication> implements Serializable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.function.Supplier;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -25,7 +24,8 @@
import hudson.model.Run;
import hudson.model.TaskListener;
import io.hyperfoil.tools.HorreumClient;
import io.hyperfoil.tools.horreum.entity.json.Access;
import io.hyperfoil.tools.horreum.api.data.Access;
import jakarta.ws.rs.core.Response;
import jenkins.model.Jenkins;
import jenkins.plugins.horreum.BaseExecutionContext;
import jenkins.plugins.horreum.HorreumGlobalConfig;
Expand Down
33 changes: 17 additions & 16 deletions src/test/java/jenkins/plugins/horreum/HorreumExpectStepTest.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
package jenkins.plugins.horreum;

import static io.hyperfoil.tools.HorreumTestClientExtension.dummyTest;
import static io.hyperfoil.tools.HorreumTestClientExtension.horreumClient;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static jenkins.plugins.horreum.junit.HorreumTestExtension.*;
import static jenkins.plugins.horreum.junit.HorreumTestClientExtension.getHorreumClient;

import java.util.List;
import java.util.stream.Collectors;

import io.hyperfoil.tools.horreum.api.alerting.RunExpectation;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.jupiter.api.Test;

import io.hyperfoil.tools.horreum.entity.alerting.RunExpectation;
import org.junit.jupiter.api.TestInfo;

public class HorreumExpectStepTest extends HorreumPluginTestBase {
@Test
public void testExpect() throws Exception {
public void testExpect(TestInfo info) throws Exception {
WorkflowJob proj = j.jenkins.createProject(WorkflowJob.class, "Horreum-Expect-Pipeline");
io.hyperfoil.tools.horreum.api.data.Test dummyTest = createTest(info.getTestClass() + "-expect-step", "dev-team");
proj.setDefinition(new CpsFlowDefinition(
"node {\n" +
"horreumExpect(\n" +
"credentials: '" + HorreumPluginTestBase.HORREUM_UPLOAD_CREDENTIALS + "',\n" +
"test: '" + dummyTest.name + "',\n" +
"timeout: 60,\n" +
"expectedBy: 'Jenkins CI',\n" +
"backlink: \"${env.BUILD_URL}\",\n" +
")\n" +
"}\n",
true));
"node {\n" +
"horreumExpect(\n" +
"credentials: '" + HORREUM_UPLOAD_CREDENTIALS + "',\n" +
"test: '" + dummyTest.name + "',\n" +
"timeout: 60,\n" +
"expectedBy: 'Jenkins CI',\n" +
"backlink: \"${env.BUILD_URL}\",\n" +
")\n" +
"}\n",
true));

WorkflowRun run = proj.scheduleBuild2(0).get();

j.assertBuildStatusSuccess(run);

List<RunExpectation> expectations = horreumClient.alertingService.expectations();
List<RunExpectation> expectations = getHorreumClient().alertingService.expectations();
expectations = expectations.stream().filter(e -> e.testId == dummyTest.id).collect(Collectors.toList());
assertEquals(1, expectations.size());
RunExpectation runExpectation = expectations.get(0);
Expand Down
16 changes: 9 additions & 7 deletions src/test/java/jenkins/plugins/horreum/HorreumExpectTest.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
package jenkins.plugins.horreum;

import static io.hyperfoil.tools.HorreumTestClientExtension.dummyTest;
import static io.hyperfoil.tools.HorreumTestClientExtension.horreumClient;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static jenkins.plugins.horreum.junit.HorreumTestExtension.*;
import static jenkins.plugins.horreum.junit.HorreumTestClientExtension.getHorreumClient;

import java.util.List;
import java.util.stream.Collectors;

import io.hyperfoil.tools.horreum.api.alerting.RunExpectation;
import org.junit.jupiter.api.Test;

import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import io.hyperfoil.tools.horreum.entity.alerting.RunExpectation;
import jenkins.plugins.horreum.expect.HorreumExpect;
import org.junit.jupiter.api.TestInfo;

public class HorreumExpectTest extends HorreumPluginTestBase {
@Test
public void testExpectRun() throws Exception {
public void testExpectRun(TestInfo info) throws Exception {
io.hyperfoil.tools.horreum.api.data.Test dummyTest = createTest(info.getTestClass() + "-expect", "dev-team");
HorreumExpect horreumExpect = new HorreumExpect(
HORREUM_UPLOAD_CREDENTIALS, dummyTest.name, 60, "Jenkins CI", "$BUILD_URL"
HORREUM_UPLOAD_CREDENTIALS, dummyTest.name, 60L, "Jenkins CI", "$BUILD_URL"
);

// Run build
FreeStyleProject project = this.j.createFreeStyleProject("Horreum-Expect-Freestyle");
FreeStyleProject project = j.createFreeStyleProject("Horreum-Expect-Freestyle");
project.getBuildersList().add(horreumExpect);
FreeStyleBuild build = project.scheduleBuild2(0).get();

// Check expectations
j.assertBuildStatusSuccess(build);

List<RunExpectation> expectations = horreumClient.alertingService.expectations();
List<RunExpectation> expectations = getHorreumClient().alertingService.expectations();
expectations = expectations.stream().filter(e -> e.testId == dummyTest.id).collect(Collectors.toList());
assertEquals(1, expectations.size());
RunExpectation runExpectation = expectations.get(0);
Expand Down
63 changes: 24 additions & 39 deletions src/test/java/jenkins/plugins/horreum/HorreumPluginTestBase.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,37 @@
package jenkins.plugins.horreum;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.BeforeEach;
import java.util.logging.Logger;

import io.hyperfoil.tools.horreum.api.data.Test;
import jenkins.plugins.horreum.junit.HorreumTestClientExtension;
import jenkins.plugins.horreum.junit.HorreumTestExtension;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import org.junit.jupiter.api.extension.ExtensionContext;

import io.hyperfoil.tools.HorreumTestClientExtension;
import io.hyperfoil.tools.HorreumTestExtension;
import static jenkins.plugins.horreum.junit.HorreumTestClientExtension.getHorreumClient;

@ExtendWith(HorreumTestClientExtension.class)
public class HorreumPluginTestBase {
public static final String HORREUM_UPLOAD_CREDENTIALS = "horreum-creds";
private static final Logger LOGGER = Logger.getLogger(HorreumPluginTestBase.class.getName());

@RegisterExtension
public JenkinsExtension j = new JenkinsExtension();
private Map<Domain, List<Credentials>> credentials;

void registerBasicCredential(String id, String username, String password) {
credentials.get(Domain.global()).add(
new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL,
id, "", username, password));
SystemCredentialsProvider.getInstance().setDomainCredentialsMap(credentials);
Test createTest(String name, String owner) {
Test test = new Test();
test.name = name;
test.owner = owner;
return getHorreumClient().testService.add(test);
}

@BeforeEach
public void init() {
credentials = new HashMap<>();
credentials.put(Domain.global(), new ArrayList<Credentials>());
this.registerBasicCredential(HORREUM_UPLOAD_CREDENTIALS, "user", "secret");
@BeforeAll
public static void before() throws Exception {
HorreumTestExtension.beforeAll();
}

HorreumGlobalConfig globalConfig = HorreumGlobalConfig.get();
if (globalConfig != null) {
globalConfig.setKeycloakRealm("horreum");
globalConfig.setClientId("horreum-ui");
globalConfig.setKeycloakBaseUrl(HorreumTestExtension.HORREUM_KEYCLOAK_BASE_URL);
globalConfig.setBaseUrl(HorreumTestExtension.HORREUM_BASE_URL);
} else {
System.out.println("Can not find Horreum Global Config");
}
@AfterAll
public static void after() throws Exception {
// HorreumTestExtension.afterAll();
}
}
90 changes: 47 additions & 43 deletions src/test/java/jenkins/plugins/horreum/HorreumUploadStepTest.java
Original file line number Diff line number Diff line change
@@ -1,84 +1,88 @@
package jenkins.plugins.horreum;

import static io.hyperfoil.tools.HorreumTestClientExtension.dummyTest;
import static io.hyperfoil.tools.HorreumTestClientExtension.horreumClient;
import static jenkins.plugins.horreum.junit.HorreumTestExtension.*;
import static org.junit.jupiter.api.Assertions.*;
import static jenkins.plugins.horreum.junit.HorreumTestClientExtension.getHorreumClient;

import java.net.URL;
import java.util.Map;

import com.fasterxml.jackson.databind.node.ObjectNode;
import hudson.FilePath;
import io.hyperfoil.tools.horreum.api.services.RunService;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.jupiter.api.Test;

import io.hyperfoil.tools.horreum.api.RunService;
import org.junit.jupiter.api.TestInfo;

public class HorreumUploadStepTest extends HorreumPluginTestBase {
@Test
public void testUpload() throws Exception {
URL jsonResource = Thread.currentThread().getContextClassLoader().getResource("data/config-quickstart.jvm.json");
WorkflowJob proj = j.jenkins.createProject(WorkflowJob.class, "Horreum-Upload-Pipeline");
WorkflowJob proj = j.jenkins.createProject(WorkflowJob.class, "Horreum-Upload-Pipeline-testUpload");
io.hyperfoil.tools.horreum.api.data.Test dummyTest = createTest("upload-single", "dev-team");
proj.setDefinition(new CpsFlowDefinition(
"node {\n" +
"def id = horreumUpload(\n" +
"credentials: '" + HorreumPluginTestBase.HORREUM_UPLOAD_CREDENTIALS + "',\n" +
"test: '" + dummyTest.name + "',\n" +
"owner: '" + dummyTest.owner + "',\n" +
"access: 'PUBLIC',\n" +
"start: '$.build-timestamp',\n" +
"stop: '$.build-timestamp',\n" +
"jsonFile: '" + jsonResource.getPath() + "',\n" +
"addBuildInfo: true\n" +
")\n" +
"println(id)\n" +
"}\n",
true));
"node {\n" +
"def id = horreumUpload(\n" +
"credentials: '" + HORREUM_UPLOAD_CREDENTIALS + "',\n" +
"test: '" + dummyTest.name + "',\n" +
"owner: '" + dummyTest.owner + "',\n" +
"access: 'PUBLIC',\n" +
"start: '$.build-timestamp',\n" +
"stop: '$.build-timestamp',\n" +
"jsonFile: '" + jsonResource.getPath() + "',\n" +
"addBuildInfo: true\n" +
")\n" +
"println(id)\n" +
"}\n",
true));

WorkflowRun run = proj.scheduleBuild2(0).get();

j.assertBuildStatusSuccess(run);

RunService.RunsSummary summary = horreumClient.runService.listTestRuns(dummyTest.id, false, null, null, "", null);
RunService.RunsSummary summary = getHorreumClient().runService.listTestRuns(dummyTest.id, false, null, null, "", null);
assertEquals(1, summary.total);
assertEquals(1, summary.runs.size());
}

@Test
public void testUploadMultiple() throws Exception {
public void testUploadMultiple(TestInfo info) throws Exception {
URL jsonResource1 = Thread.currentThread().getContextClassLoader().getResource("data/config-quickstart.jvm.json");
URL jsonResource2 = Thread.currentThread().getContextClassLoader().getResource("data/another-file.json");
WorkflowJob proj = j.jenkins.createProject(WorkflowJob.class, "Horreum-Upload-Pipeline");
assertNotNull(j);
assertNotNull(j.jenkins);
WorkflowJob proj = j.jenkins.createProject(WorkflowJob.class, "Horreum-Upload-Pipeline-testUploadMultiple");
FilePath folder = j.jenkins.getWorkspaceFor(proj).child("run");
folder.child("config-quickstart.jvm.json").copyFrom(jsonResource1);
folder.child("another-file.json").copyFrom(jsonResource2);
io.hyperfoil.tools.horreum.api.data.Test dummyTest = createTest(info.getTestClass() + "-upload-multiple", "dev-team");
proj.setDefinition(new CpsFlowDefinition(
"node {\n" +
"def id = horreumUpload(\n" +
"credentials: '" + HorreumPluginTestBase.HORREUM_UPLOAD_CREDENTIALS + "',\n" +
"test: '" + dummyTest.name + "',\n" +
"owner: '" + dummyTest.owner + "',\n" +
"access: 'PUBLIC',\n" +
"start: '1970-01-01T00:00:00.00Z',\n" +
"stop: '1970-01-01T00:00:01.00Z',\n" +
"files: '**/*.json',\n"+
"addBuildInfo: true\n" +
")\n" +
"println(id)\n" +
"}\n",
true));
"node {\n" +
"def id = horreumUpload(\n" +
"credentials: '" + HORREUM_UPLOAD_CREDENTIALS + "',\n" +
"test: '" + dummyTest.name + "',\n" +
"owner: '" + dummyTest.owner + "',\n" +
"access: 'PUBLIC',\n" +
"start: '1970-01-01T00:00:00.00Z',\n" +
"stop: '1970-01-01T00:00:01.00Z',\n" +
"files: '**/*.json',\n"+
"addBuildInfo: true\n" +
")\n" +
"println(id)\n" +
"}\n",
true));
WorkflowRun run = proj.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(run);
RunService.RunsSummary summary = horreumClient.runService.listTestRuns(dummyTest.id, false, null, null, "", null);
RunService.RunsSummary summary = getHorreumClient().runService.listTestRuns(dummyTest.id, false, null, null, "", null);
assertEquals(1, summary.total);
assertEquals(1, summary.runs.size());
Object runObject = horreumClient.runService.getRun(summary.runs.get(0).id,summary.runs.get(0).token);
RunService.RunExtended runObject = getHorreumClient().runService.getRun(summary.runs.get(0).id,summary.runs.get(0).token);
assertNotNull(runObject);
assertTrue(runObject instanceof Map,"run should return a map");
Object data = ((Map)runObject).get("data");
assertInstanceOf(RunService.RunExtended.class, runObject,"run should return a RunService.RunExtended");
Object data = runObject.data;
assertNotNull(data);
assertTrue(data instanceof Map,"data should be a map");
assertEquals(2,((Map<?, ?>) data).size(),"data should have an entry for each file");
assertInstanceOf(ObjectNode.class, data,"data should be a ObjectNode");
assertEquals(2,((ObjectNode) data).size(),"data should have an entry for each file");
}
}
Loading

0 comments on commit 0f9c64b

Please sign in to comment.