Skip to content

Commit

Permalink
Add tests and test on local machine
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalDelarea committed Sep 9, 2024
1 parent f57a192 commit b4cd09f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/main/java/io/jenkins/plugins/jfrog/JfStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private boolean shouldConfig(FilePath jfrogHomeTempDir) throws IOException, Inte
private void configAllServers(Launcher.ProcStarter launcher, String jfrogBinaryPath, boolean isWindows, Job<?, ?> job) throws IOException, InterruptedException {
// Config all servers using the 'jf c add' command.
List<JFrogPlatformInstance> jfrogInstances = JFrogPlatformBuilder.getJFrogPlatformInstances();
if (jfrogInstances != null && jfrogInstances.size() > 0) {
if (jfrogInstances != null && !jfrogInstances.isEmpty()) {
for (JFrogPlatformInstance jfrogPlatformInstance : jfrogInstances) {
// Build 'jf' command
ArgumentListBuilder builder = new ArgumentListBuilder();
Expand All @@ -222,10 +222,11 @@ private void addConfigArguments(ArgumentListBuilder builder, JFrogPlatformInstan
} else {
Credentials credentials = PluginsUtils.credentialsLookup(credentialsId, job);
builder.add("--user=" + credentials.getUsername());
String cliVersion = getJfrogCliVersion(launcher, launcher.pwd());
String cliVersion = getJfrogCliVersion(launcher);

// Use password-stdin if available
if (isCliVersionGreaterThan(cliVersion, MIN_CLI_VERSION_PASSWORD_STDIN)) {
builder.add("--password-stdin=");
if (isCliVersionGreaterThanOrEqual(cliVersion, MIN_CLI_VERSION_PASSWORD_STDIN)) {
builder.add("--password-stdin");
ByteArrayInputStream inputStream = new ByteArrayInputStream(credentials.getPassword().getPlainText().getBytes());
launcher.stdin(inputStream);
} else {
Expand Down Expand Up @@ -305,20 +306,25 @@ public boolean isApplicable(Class<? extends AbstractProject> jobType) {
}
}

private String getJfrogCliVersion(Launcher.ProcStarter launcher, FilePath workspace) throws IOException, InterruptedException {
String getJfrogCliVersion(Launcher.ProcStarter launcher) throws IOException, InterruptedException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int exitCode = launcher.cmds("jf", "--version")
.pwd(workspace)
.pwd(launcher.pwd())
.stdout(outputStream)
.join();
if (exitCode != 0) {
throw new IOException("Failed to get JFrog CLI version");
}
String versionOutput = outputStream.toString(StandardCharsets.UTF_8).trim();
return versionOutput.split(" ")[2]; // Assuming the version is the third word in the output
String[] versionParts = versionOutput.split(" ");
if (versionOutput.isEmpty() ||(versionParts.length < 3) ) {
throw new IOException(String.format("Failed to parse JFrog CLI version. CLI Output: %s", versionOutput));
}
// Based on the output, the version should be located at the second index
return versionParts[2];
}

private boolean isCliVersionGreaterThan(String currentVersion, String targetVersion) {
boolean isCliVersionGreaterThanOrEqual(String currentVersion, String targetVersion) {
String[] currentParts = currentVersion.split("\\.");
String[] targetParts = targetVersion.split("\\.");
for (int i = 0; i < Math.min(currentParts.length, targetParts.length); i++) {
Expand All @@ -330,6 +336,6 @@ private boolean isCliVersionGreaterThan(String currentVersion, String targetVers
return false;
}
}
return currentParts.length > targetParts.length;
return currentParts.length >= targetParts.length;
}
}
52 changes: 52 additions & 0 deletions src/test/java/io/jenkins/plugins/jfrog/JfStepTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package io.jenkins.plugins.jfrog;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.stream.Stream;

import static io.jenkins.plugins.jfrog.JfStep.getJFrogCLIPath;
import static io.jenkins.plugins.jfrog.JfrogInstallation.JFROG_BINARY_PATH;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* @author yahavi
Expand All @@ -22,6 +31,49 @@ void getJFrogCLIPathTest(EnvVars inputEnvVars, boolean isWindows, String expecte
Assertions.assertEquals(expectedOutput, getJFrogCLIPath(inputEnvVars, isWindows));
}

@Test
void isCliVersionGreaterThanTest() {
JfStep jfStep = new JfStep("--version");

// Test cases where the current version is greater
assertTrue(jfStep.isCliVersionGreaterThanOrEqual("2.32.0", "2.31.0"));
assertTrue(jfStep.isCliVersionGreaterThanOrEqual("3.0.0", "2.31.0"));
assertTrue(jfStep.isCliVersionGreaterThanOrEqual("2.31.1", "2.31.0"));

// Test cases where the current version is equal
assertTrue(jfStep.isCliVersionGreaterThanOrEqual("2.31.0", "2.31.0"));

// Test cases where the current version is less
assertFalse(jfStep.isCliVersionGreaterThanOrEqual("2.30.0", "2.31.0"));
assertFalse(jfStep.isCliVersionGreaterThanOrEqual("2.31.0", "2.31.1"));
assertFalse(jfStep.isCliVersionGreaterThanOrEqual("1.31.0", "2.31.0"));
}

@Test
void getJfrogCliVersionTest() throws IOException, InterruptedException {
// Mock the Launcher.ProcStarter
Launcher.ProcStarter procStarter = mock(Launcher.ProcStarter.class);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// Mocks the return value of --version command
outputStream.write("jf version 2.31.0 ".getBytes());
// Mock the behavior of the ProcStarter
when(procStarter.cmds("jf", "--version")).thenReturn(procStarter);
when(procStarter.pwd((FilePath) any())).thenReturn(procStarter);
when(procStarter.stdout(any(ByteArrayOutputStream.class))).thenAnswer(invocation -> {
ByteArrayOutputStream out = invocation.getArgument(0);
out.write(outputStream.toByteArray());
return procStarter;
});
when(procStarter.join()).thenReturn(0);

// Create an instance of JfStep and call the method
JfStep jfStep = new JfStep("--version");
String version = jfStep.getJfrogCliVersion(procStarter);

// Verify the result
assertEquals("2.31.0", version);
}

private static Stream<Arguments> jfrogCLIPathProvider() {
return Stream.of(
// Unix agent
Expand Down

0 comments on commit b4cd09f

Please sign in to comment.