Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hack for io.jenkins.configuration-as-code:test-harness #344

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public class PluginCompatTester {
private static final String DEFAULT_SOURCE_ID = "default";

/** First version with new parent POM. */
public static final String JENKINS_CORE_FILE_REGEX = "WEB-INF/lib/jenkins-core-([0-9.]+(?:-[0-9a-f.]+)*(?:-(?i)([a-z]+)(-)?([0-9a-f.]+)?)?(?:-(?i)([a-z]+)(-)?([0-9a-f.]+)?)?(?:-SNAPSHOT)?)[.]jar";
public static final String JENKINS_CORE_FILE_REGEX = "WEB-INF/lib/jenkins-core-([0-9.]+(?:-[0-9a-f.]+)*(?:-(?i)([a-z]+)(-)?([0-9a-f.]+)?)?(?:-(?i)([a-z]+)(-)?([0-9a-f_.]+)?)?(?:-SNAPSHOT)?)[.]jar";

private PluginCompatTesterConfig config;
private final ExternalMavenRunner runner;
Expand Down Expand Up @@ -1016,6 +1016,12 @@ private void addSplitPluginDependencies(String thisPlugin, MavenRunner.Config mc
toAddTest = difference(pluginDeps, toAddTest);
toAddTest = difference(toAdd, toAddTest);

if (toReplaceTest.containsKey("configuration-as-code")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this is a special case, wouldn't it be better to use a hook instead of hacking the default logic? TBH I am missing the context so whatever you believe is better. The hook approach means easier code but at the same time is harder to follow what is happening unless you are aware of the PCT hook mechanism. No feeling strongly to push for it if you prefer this way

Copy link
Member Author

@timja timja Jan 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsure, this isn't really for merge it's a continuation of #276

Which just has 7269bed applied on top of it

Integration tests fail I think because the tests need to be updated to use newer configuration as code as it's trying to always add the test harness but the test harness doesn't exist on the old version pinned in the config here.

And can't merge master in because #299 breaks bom quite badly.

A number of InjectedTest fail in bom and we're okay with that (other plugins need updating in the pom file and need to override bom as well for it to work properly I think), but that PR 'correctly' catches that there was an error in test startup

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A number of InjectedTest fail in bom and we're okay with that

jenkinsci/bom#341 you mean?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(note comment in jenkinsci/bom#610)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A number of InjectedTest fail in bom and we're okay with that

jenkinsci/bom#341 you mean?

Yes

VersionNumber versionNumber = toReplaceTest.get("configuration-as-code");
pluginGroupIds.put("test-harness", "io.jenkins.configuration-as-code");
toReplaceTest.put("test-harness", versionNumber);
}

if (!toAdd.isEmpty() || !toReplace.isEmpty() || !toAddTest.isEmpty() || !toReplaceTest.isEmpty()) {
System.out.println("Adding/replacing plugin dependencies for compatibility: " + toAdd + " " + toReplace + "\nFor test: " + toAddTest + " " + toReplaceTest);
pom.addDependencies(toAdd, toReplace, toAddTest, toReplaceTest, coreDep, pluginGroupIds, convertFromTestDep);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.when;

import hudson.model.UpdateSite;
import java.io.File;
Expand All @@ -17,9 +14,6 @@
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.maven.scm.command.checkout.CheckOutScmResult;
import org.apache.maven.scm.manager.ScmManager;
import org.apache.maven.scm.repository.ScmRepository;
import org.jenkins.tools.test.hook.NonStandardTagHook;
import org.jenkins.tools.test.model.MavenCoordinates;
import org.jenkins.tools.test.model.PluginCompatTesterConfig;
Expand All @@ -28,13 +22,8 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.core.io.ClassPathResource;

@RunWith(PowerMockRunner.class)
public class NonStandardTagHookTest {

@Rule
Expand Down Expand Up @@ -86,41 +75,6 @@ public void testCheckMethodWithMinimumVersion() {
}
}

@Test
@PrepareForTest({SCMManagerFactory.class,NonStandardTagHook.class})
public void testActionGeneratesProperInfo() throws Exception {
spy(SCMManagerFactory.class);
SCMManagerFactory mockFactory = mock(SCMManagerFactory.class);
ScmManager mockManager = mock(ScmManager.class);
CheckOutScmResult mockResult = mock(CheckOutScmResult.class);
when(mockResult.isSuccess()).thenReturn(Boolean.TRUE);
when(mockManager.checkOut(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(mockResult);
when(mockManager.makeScmRepository(Mockito.anyString())).thenReturn(mock(ScmRepository.class));
when(mockFactory.createScmManager()).thenReturn(mockManager);
when(SCMManagerFactory.getInstance()).thenReturn(mockFactory);


Map<String, Object> info = new HashMap<>();
PomData data = new PomData("artifactID", "hpi", null, null, null, null);
info.put("pomData", data);
JSONObject pluginData = new JSONObject();
pluginData.put("name","artifactId");
pluginData.put("version","9.9.99");
pluginData.put("url", "example.com");
pluginData.put("dependencies", new JSONArray());
UpdateSite.Plugin plugin = new UpdateSite("fake", "fake").new Plugin("NO Source",pluginData);
info.put("plugin", plugin);
PluginCompatTesterConfig config = new PluginCompatTesterConfig(new File(testFolder.getRoot(), "noexists"), null, null);
info.put("config", config);

NonStandardTagHook hook = new NonStandardTagHook();
Map<String, Object> returnedConfig = hook.action(info);
assertEquals("Checkout dir is not the expected", new File(testFolder.getRoot(), "noexists/artifactID"), returnedConfig.get("checkoutDir"));
assertEquals("Checkout dir is not the same as plugin dir", returnedConfig.get("checkoutDir"), returnedConfig.get("pluginDir"));
assertEquals("RunCheckout should be false", Boolean.FALSE, returnedConfig.get("runCheckout"));

}

@Test
public void testActuallyPerformsTheCheckoutWithVersionGreaterThanMinimum() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*/
package org.jenkins.tools.test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -383,6 +385,15 @@ public void testCustomWarPackagerVersions() {
assertTrue("No matches", m.matches());
}

@Test
@Issue("340")
public void testJEP229WithUnderscore() {
String fileName = "WEB-INF/lib/jenkins-core-2.329-rc31964.3b_29e9d46_038_.jar";
Matcher m = Pattern.compile(PluginCompatTester.JENKINS_CORE_FILE_REGEX).matcher(fileName);
assertThat("No matches", m.matches(), is(true));
assertThat("Invalid group", m.group(1), is("2.329-rc31964.3b_29e9d46_038_"));
}

private static File getSettingsFile() throws IOException {
// Check whether we run in ci.jenkins.io with Azure settings
File ciJenkinsIOSettings = new File(new File("settings-azure.xml").getAbsolutePath()
Expand Down