Skip to content

Commit

Permalink
Add integration tests for BuildAgeRangeCondition (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
damianszczepanik authored Jan 1, 2020
1 parent f394b1b commit ea56ceb
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>Action</code> or <code>Condition</code>
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
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@
<format>html</format>
<format>xml</format>
</formats>
<check />
<instrumentation>
<excludes>
<!-- generated class -->
<exclude>pl/damianszczepanik/jenkins/buildhistorymanager/Messages.class</exclude>
</excludes>
</instrumentation>
<check/>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ <h3>Use cases</h3>
Allow to match several builds at once based on build age.

<h3>Warning!</h3>
Provided values are always relative to <i>today</i> so most likely they interact with different builds every day<br>
Time of the build completed is reset to zero before comparison so only days are compared.
<code>maxDaysAge = 0</code> refers to builds which were completed today
<code>1-2</code> means builds which were completed yesterday or the day before yesterday.<br>
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <a href="https://github.com/jenkinsci/build-history-manager-plugin/wiki/Build-age-range-condition">documentation</a>
*/
@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<Rule> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.io.File;
import java.io.IOException;
import java.util.Date;

import hudson.model.Job;
import hudson.model.Result;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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()) : "");
}
}

0 comments on commit ea56ceb

Please sign in to comment.