diff --git a/README.md b/README.md index d7ed79ef..fa729615 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,8 @@ Check [release notes](https://github.com/jenkinsci/build-history-manager-plugin/ ## Contribution If you find the issue you can send pull request to fix it or file the bug. -The same about missing Action or Condition +The same about missing `Action` or `Condition` Remember about: - doing tests on your local Jenkins instance - adding new unit tests according to [given -> when -> then](https://pl.wikipedia.org/wiki/Behavior-driven_development) approach. +- remember about integration tests and wiki update diff --git a/pom.xml b/pom.xml index cb27a21c..f582911f 100644 --- a/pom.xml +++ b/pom.xml @@ -75,7 +75,13 @@ html xml - + + + + pl/damianszczepanik/jenkins/buildhistorymanager/Messages.class + + + diff --git a/src/main/resources/pl/damianszczepanik/jenkins/buildhistorymanager/model/conditions/BuildAgeRangeCondition/help.html b/src/main/resources/pl/damianszczepanik/jenkins/buildhistorymanager/model/conditions/BuildAgeRangeCondition/help.html index d2ca37d8..2628f1c2 100644 --- a/src/main/resources/pl/damianszczepanik/jenkins/buildhistorymanager/model/conditions/BuildAgeRangeCondition/help.html +++ b/src/main/resources/pl/damianszczepanik/jenkins/buildhistorymanager/model/conditions/BuildAgeRangeCondition/help.html @@ -5,6 +5,7 @@

Use cases

Allow to match several builds at once based on build age.

Warning!

+Provided values are always relative to today so most likely they interact with different builds every day
Time of the build completed is reset to zero before comparison so only days are compared. maxDaysAge = 0 refers to builds which were completed today 1-2 means builds which were completed yesterday or the day before yesterday.
diff --git a/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/conditions/BuildAgeRangeConditionIT.java b/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/conditions/BuildAgeRangeConditionIT.java new file mode 100644 index 00000000..894467cf --- /dev/null +++ b/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/conditions/BuildAgeRangeConditionIT.java @@ -0,0 +1,93 @@ +package pl.damianszczepanik.jenkins.buildhistorymanager.model.conditions; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Calendar; +import java.util.List; + +import hudson.model.Job; +import jenkins.model.Jenkins; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import pl.damianszczepanik.jenkins.buildhistorymanager.BuildHistoryManager; +import pl.damianszczepanik.jenkins.buildhistorymanager.descriptors.actions.DeleteBuildActionDescriptor; +import pl.damianszczepanik.jenkins.buildhistorymanager.model.Rule; +import pl.damianszczepanik.jenkins.buildhistorymanager.model.actions.DeleteArtifactsAction; +import pl.damianszczepanik.jenkins.buildhistorymanager.utils.DescriptorMocker; +import pl.damianszczepanik.jenkins.buildhistorymanager.utils.JobBuilder; +import pl.damianszczepanik.jenkins.buildhistorymanager.utils.RunStub; + +/** + * @author Damian Szczepanik (damianszczepanik@github) + * @see documentation + */ +@PrepareForTest(Jenkins.class) +@RunWith(PowerMockRunner.class) +@PowerMockIgnore("javax.xml.*") +public class BuildAgeRangeConditionIT { + + @Before + public void setUp() { + new DescriptorMocker(new DeleteBuildActionDescriptor()); + } + + @Test + public void testBuildNumberRangeCondition() throws IOException, InterruptedException { + + // given + Calendar midnight = createMidnight(); + RunStub run80 = new RunStub(80, midnight.getTimeInMillis()); + + RunStub run76 = new RunStub(76, midnight.getTimeInMillis()); + run80.setPreviousBuild(run76); + + Calendar yesterday = createMidnight(); + yesterday.add(Calendar.DAY_OF_MONTH, -1); + RunStub run75 = new RunStub(75, yesterday.getTimeInMillis()); + run76.setPreviousBuild(run75); + + Calendar twoDaysPast = createMidnight(); + twoDaysPast.add(Calendar.DAY_OF_MONTH, -2); + RunStub run73 = new RunStub(73, twoDaysPast.getTimeInMillis()); + run75.setPreviousBuild(run73); + + Calendar threeDaysPast = createMidnight(); + threeDaysPast.add(Calendar.DAY_OF_MONTH, -3); + RunStub run71 = new RunStub(71, threeDaysPast.getTimeInMillis()); + run73.setPreviousBuild(run71); + + + BuildAgeRangeCondition condition = new BuildAgeRangeCondition(); + condition.setMinDaysAge(1); + condition.setMaxDaysAge(2); + Rule rule = new Rule(Arrays.asList(condition), Arrays.asList(new DeleteArtifactsAction())); + + + List rules = Arrays.asList(rule); + BuildHistoryManager buildHistoryManager = new BuildHistoryManager(rules); + Job job = JobBuilder.buildSampleJob(run80); + + // when + buildHistoryManager.perform(job); + + // then + run80.assertArtifactsAreAvailable(); + run76.assertArtifactsAreAvailable(); + run75.assertArtifactsWereDeleted(); + run73.assertArtifactsWereDeleted(); + run71.assertArtifactsAreAvailable(); + } + + private Calendar createMidnight() { + Calendar midnight = Calendar.getInstance(); + midnight.set(Calendar.HOUR_OF_DAY, 0); + midnight.set(Calendar.MINUTE, 0); + midnight.set(Calendar.SECOND, 0); + midnight.set(Calendar.MILLISECOND, 0); + return midnight; + } +} diff --git a/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/conditions/BuildAgeRangeConditionTest.java b/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/conditions/BuildAgeRangeConditionTest.java index 6dc82121..e04610c3 100644 --- a/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/conditions/BuildAgeRangeConditionTest.java +++ b/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/conditions/BuildAgeRangeConditionTest.java @@ -69,7 +69,7 @@ public void matches_OnBuildTimeBelowMax_ReturnsFalse() throws IOException { long buildTimeMinus3Days = System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 15; - Run run = new RunStub(buildTimeMinus3Days); + Run run = new RunStub(1, buildTimeMinus3Days); // when boolean matches = condition.matches(run, null); @@ -87,7 +87,7 @@ public void matches_OnBuildTimeAboveMin_ReturnsFalse() throws IOException { long buildTimeMinus3Days = System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 3; - Run run = new RunStub(buildTimeMinus3Days); + Run run = new RunStub(1, buildTimeMinus3Days); // when boolean matches = condition.matches(run, null); @@ -104,7 +104,7 @@ public void matches_OnBuildTimeInRange_ReturnsTrue() throws IOException { condition.setMinDaysAge(5); long buildTimeMinus3Days = System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 6; - Run run = new RunStub(buildTimeMinus3Days); + Run run = new RunStub(1, buildTimeMinus3Days); // when boolean matches = condition.matches(run, null); diff --git a/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/utils/RunStub.java b/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/utils/RunStub.java index 3ba9ac3e..2a00c450 100644 --- a/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/utils/RunStub.java +++ b/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/utils/RunStub.java @@ -6,6 +6,7 @@ import java.io.File; import java.io.IOException; +import java.util.Date; import hudson.model.Job; import hudson.model.Result; @@ -39,8 +40,8 @@ public RunStub(int buildNumber, Result result) throws IOException { this.result = result; } - public RunStub(long startTime) throws IOException { - this(); + public RunStub(int buildNumber, long startTime) throws IOException { + this(buildNumber); setStartTime(startTime); } @@ -127,4 +128,12 @@ public long getDuration() { public boolean isBuilding() { return false; } + + @Override + public String toString() { + return "RunStub:" + + (number != 0 ? " number=" + number : "") + + (result != null ? " result=" + result : "") + + (getStartTimeInMillis() != 0 ? " startTime=" + new Date(getStartTimeInMillis()) : ""); + } }