Skip to content

Commit

Permalink
[FIXED JENKINS-40828] Provide a way for tests using MockSCMController…
Browse files Browse the repository at this point in the history
… to inject failures
  • Loading branch information
stephenc committed Jan 5, 2017
1 parent 8aa91ab commit ba0fca0
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/test/java/jenkins/scm/impl/mock/MockFailure.java
@@ -0,0 +1,46 @@
/*
* The MIT License
*
* Copyright (c) 2016 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package jenkins.scm.impl.mock;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import java.io.IOException;

/**
* Represents a failure in {@link MockSCMNavigator} and/or {@link MockSCMSource}
*/
public abstract class MockFailure {
/**
* Checks whether this combination should fail.
*
* @param repository the repository.
* @param branchOrCR the branch, tag or change request
* @param revision the revision.
* @param actions
* @throws IOException if you want a fault.
*/
public abstract void check(@CheckForNull String repository, @CheckForNull String branchOrCR,
@CheckForNull String revision, boolean actions) throws IOException;
}
18 changes: 18 additions & 0 deletions src/test/java/jenkins/scm/impl/mock/MockSCMController.java
Expand Up @@ -25,6 +25,7 @@

package jenkins.scm.impl.mock;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.FilePath;
import java.io.ByteArrayInputStream;
import java.io.Closeable;
Expand Down Expand Up @@ -55,6 +56,7 @@ public class MockSCMController implements Closeable {
private String id = UUID.randomUUID().toString();

private Map<String, Repository> repositories = new TreeMap<String, Repository>();
private List<MockFailure> faults = new ArrayList<MockFailure>();
private String displayName;
private String description;
private String url;
Expand Down Expand Up @@ -136,6 +138,22 @@ public void setUrl(String url) throws IOException {
this.url = url;
}

public synchronized void addFault(MockFailure fault) {
this.faults.add(fault);
}

public synchronized void clearFaults() {
this.faults.clear();
}

public synchronized void checkFaults(@CheckForNull String repository, @CheckForNull String branch,
@CheckForNull String revision, boolean actions)
throws IOException {
for (MockFailure fault: faults) {
fault.check(repository, branch, revision, actions);
}
}

public synchronized void createRepository(String name) throws IOException {
repositories.put(name, new Repository());
createBranch(name, "master");
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/jenkins/scm/impl/mock/MockSCMNavigator.java
Expand Up @@ -99,11 +99,13 @@ protected String id() {

@Override
public void visitSources(@NonNull SCMSourceObserver observer) throws IOException, InterruptedException {
controller.checkFaults(null, null, null, false);
for (String name : controller().listRepositories()) {
if (!observer.isObserving()) {
return;
}
checkInterrupt();
controller.checkFaults(name, null, null, false);
SCMSourceObserver.ProjectObserver po = observer.observe(name);
po.addSource(new MockSCMSource(getId() + "::" + name,
controller, name, includeBranches, includeTags, includeChangeRequests));
Expand All @@ -117,6 +119,7 @@ public List<Action> retrieveActions(@NonNull SCMNavigatorOwner owner,
@CheckForNull SCMNavigatorEvent event,
@NonNull TaskListener listener)
throws IOException, InterruptedException {
controller.checkFaults(null, null, null, true);
List<Action> result = new ArrayList<Action>();
result.add(new MockSCMLink("organization"));
String description = controller().getDescription();
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/jenkins/scm/impl/mock/MockSCMSource.java
Expand Up @@ -118,16 +118,19 @@ public boolean isIncludeChangeRequests() {
protected void retrieve(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHeadObserver observer,
@CheckForNull SCMHeadEvent<?> event, @NonNull TaskListener listener)
throws IOException, InterruptedException {
controller.checkFaults(repository, null, null, false);
Set<SCMHead> includes = observer.getIncludes();
if (includeBranches) {
for (final String branch : controller().listBranches(repository)) {
checkInterrupt();
String revision = controller().getRevision(repository, branch);
MockSCMHead head = new MockSCMHead(branch);
controller.checkFaults(repository, head.getName(), null, false);
if (includes != null && !includes.contains(head)) {
continue;
}
if (criteria == null || criteria.isHead(new MockSCMProbe(head, revision), listener)) {
controller.checkFaults(repository, head.getName(), revision, false);
observer.observe(head, new MockSCMRevision(head, revision));
}
}
Expand All @@ -137,10 +140,12 @@ protected void retrieve(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHe
checkInterrupt();
String revision = controller().getRevision(repository, tag);
MockSCMHead head = new MockTagSCMHead(tag);
controller.checkFaults(repository, head.getName(), null, false);
if (includes != null && !includes.contains(head)) {
continue;
}
if (criteria == null || criteria.isHead(new MockSCMProbe(head, revision), listener)) {
controller.checkFaults(repository, head.getName(), revision, false);
observer.observe(head, new MockSCMRevision(head, revision));
}
}
Expand All @@ -151,10 +156,12 @@ protected void retrieve(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHe
String revision = controller().getRevision(repository, "change-request/" + number);
String target = controller().getTarget(repository, number);
MockChangeRequestSCMHead head = new MockChangeRequestSCMHead(number, target);
controller.checkFaults(repository, head.getName(), null, false);
if (includes != null && !includes.contains(head)) {
continue;
}
if (criteria == null || criteria.isHead(new MockSCMProbe(head, revision), listener)) {
controller.checkFaults(repository, head.getName(), revision, false);
observer.observe(head, new MockSCMRevision(head, revision));
}
}
Expand All @@ -172,6 +179,7 @@ public SCM build(@NonNull SCMHead head, @CheckForNull SCMRevision revision) {
@Override
protected List<Action> retrieveActions(@CheckForNull SCMSourceEvent event, @NonNull TaskListener listener)
throws IOException, InterruptedException {
controller.checkFaults(repository, null, null, true);
List<Action> result = new ArrayList<Action>();
result.add(new MockSCMLink("source"));
String description = controller().getDescription(repository);
Expand All @@ -193,6 +201,7 @@ protected List<Action> retrieveActions(@NonNull SCMRevision revision,
@CheckForNull SCMHeadEvent event,
@NonNull TaskListener listener)
throws IOException, InterruptedException {
controller.checkFaults(repository, revision.getHead().getName(), ((MockSCMRevision)revision).getHash(), true);
return Collections.<Action>singletonList(new MockSCMLink("revision"));
}

Expand All @@ -202,6 +211,7 @@ protected List<Action> retrieveActions(@NonNull SCMHead head,
@CheckForNull SCMHeadEvent event,
@NonNull TaskListener listener)
throws IOException, InterruptedException {
controller.checkFaults(repository, head.getName(), null, true);
List<Action> result = new ArrayList<Action>();
if (head instanceof MockChangeRequestSCMHead) {
result.add(new ContributorMetadataAction(
Expand Down

0 comments on commit ba0fca0

Please sign in to comment.