Skip to content

Commit

Permalink
[fix for JENKINS-18280] Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfgarnet committed Aug 6, 2013
1 parent f1ed86b commit fc1694d
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 24 deletions.
10 changes: 10 additions & 0 deletions src/main/java/net/praqma/hudson/scm/CCUCMScm.java
Expand Up @@ -104,6 +104,16 @@ public class CCUCMScm extends SCM {
public CCUCMScm() {
}

/**
* To support backwards compatibility.
* @since 1.4.0
*/
public CCUCMScm( String component, String levelToPoll, String loadModule, boolean newest, String polling, String stream, String treatUnstable,
boolean createBaseline, String nameTemplate, boolean forceDeliver, boolean recommend, boolean makeTag, boolean setDescription, String buildProject ) {

this( component, levelToPoll, loadModule, newest, polling, stream, treatUnstable, createBaseline, nameTemplate, forceDeliver, recommend, makeTag, setDescription, buildProject, true );
}

@DataBoundConstructor
public CCUCMScm( String component, String levelToPoll, String loadModule, boolean newest, String polling, String stream, String treatUnstable,
boolean createBaseline, String nameTemplate, boolean forceDeliver, boolean recommend, boolean makeTag, boolean setDescription, String buildProject, boolean removeViewPrivateFiles ) {
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/net/praqma/hudson/test/BaseTestClass.java
@@ -1,15 +1,21 @@
package net.praqma.hudson.test;

import hudson.FilePath;
import net.praqma.util.test.junit.LoggingRule;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;

import java.io.IOException;
import java.util.logging.Logger;

/**
* User: cwolfgang
*/
public class BaseTestClass {

private static Logger logger = Logger.getLogger( BaseTestClass.class.getName() );

@ClassRule
public static CCUCMRule jenkins = new CCUCMRule();

Expand All @@ -25,4 +31,12 @@ public void before() {
public String getName() {
return this.getClass().getName();
}


protected void listPath( FilePath path ) throws IOException, InterruptedException {
logger.info( "Listing " + path + "(" + path.exists() + ")" );
for( FilePath f : path.list() ) {
logger.info( " * " + f );
}
}
}
53 changes: 38 additions & 15 deletions src/test/java/net/praqma/hudson/test/CCUCMRule.java
Expand Up @@ -78,6 +78,7 @@ public enum Type {
boolean description = false;
boolean createBaseline = false;
boolean forceDeliver = false;
boolean swipe = true;

String template = "[project]_build_[number]";
PromotionLevel promotionLevel = PromotionLevel.INITIAL;
Expand Down Expand Up @@ -131,6 +132,12 @@ public ProjectCreator setForceDeliver( boolean forceDeliver ) {
return this;
}

public ProjectCreator setSwipe( boolean swipe ) {
this.swipe = swipe;

return this;
}

public Project getProject() throws IOException {
System.out.println( "==== [Setting up ClearCase UCM project] ====" );
System.out.println( " * Stream : " + stream );
Expand All @@ -143,13 +150,14 @@ public Project getProject() throws IOException {
System.out.println( " * Create baseline: " + createBaseline );
System.out.println( " * Template : " + template );
System.out.println( " * Force deliver : " + forceDeliver );
System.out.println( " * Swipe : " + swipe );
System.out.println( "============================================" );

Project project = (Project) Hudson.getInstance().createProject( projectClass, name );

// boolean createBaseline, String nameTemplate, boolean forceDeliver, boolean recommend, boolean makeTag, boolean setDescription
//CCUCMScm scm = new CCUCMScm( component, "INITIAL", "ALL", false, type, stream, "successful", createBaseline, "[project]_build_[number]", forceDeliver, recommend, tag, description, "jenkins" );
CCUCMScm scm = new CCUCMScm( component, ( promotionLevel != null ? promotionLevel.name() : "ANY" ), "ALL", false, type.name(), stream, "successful", createBaseline, template, forceDeliver, recommend, tag, description, "" );
CCUCMScm scm = new CCUCMScm( component, ( promotionLevel != null ? promotionLevel.name() : "ANY" ), "ALL", false, type.name(), stream, "successful", createBaseline, template, forceDeliver, recommend, tag, description, "", swipe );
project.setScm( scm );

return project;
Expand Down Expand Up @@ -189,7 +197,7 @@ public FreeStyleProject setupProject( String projectName, String type, String co

// boolean createBaseline, String nameTemplate, boolean forceDeliver, boolean recommend, boolean makeTag, boolean setDescription
//CCUCMScm scm = new CCUCMScm( component, "INITIAL", "ALL", false, type, stream, "successful", createBaseline, "[project]_build_[number]", forceDeliver, recommend, tag, description, "jenkins" );
CCUCMScm scm = new CCUCMScm( component, promotionLevel, "ALL", false, type, stream, "successful", createBaseline, template, forceDeliver, recommend, tag, description, "" );
CCUCMScm scm = new CCUCMScm( component, promotionLevel, "ALL", false, type, stream, "successful", createBaseline, template, forceDeliver, recommend, tag, description, "", true );
this.scm = scm;
project.setScm( scm );

Expand All @@ -210,7 +218,7 @@ public CCUCMScm getCCUCM( String type, String component, String stream, String p
System.out.println( " * Force deliver : " + forceDeliver );
System.out.println( "============================================" );

CCUCMScm scm = new CCUCMScm( component, promotionLevel, "ALL", false, type, stream, "successful", createBaseline, template, forceDeliver, recommend, tag, description, "" );
CCUCMScm scm = new CCUCMScm( component, promotionLevel, "ALL", false, type, stream, "successful", createBaseline, template, forceDeliver, recommend, tag, description, "", true );

return scm;
}
Expand Down Expand Up @@ -255,7 +263,11 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
return buildProject( project, fail );
}

public static class ProjectBuilder {
public ProjectBuilder getProjectBuilder( Project<?, ?> project ) {
return new ProjectBuilder( project );
}

public class ProjectBuilder {
Project<?, ?> project;

boolean fail = false;
Expand All @@ -272,6 +284,7 @@ public ProjectBuilder failBuild( boolean cancel ) {
}

public AbstractBuild build() throws ExecutionException, InterruptedException, IOException {

if( fail ) {
logger.info( "Failing " + project );
project.getBuildersList().add(new Failer() );
Expand All @@ -280,20 +293,30 @@ public AbstractBuild build() throws ExecutionException, InterruptedException, IO
project.getBuildersList().remove( Failer.class );
}

Future<? extends Build<?, ?>> futureBuild = project.scheduleBuild2( 0, new Cause.UserCause() );
EnableLoggerAction action = null;
if( outputDir != null ) {
logger.info( "Enabling logging" );
action = new EnableLoggerAction( outputDir );
}

AbstractBuild build = futureBuild.get();
Future<? extends Build<?, ?>> futureBuild = project.scheduleBuild2( 0, new Cause.UserCause(), action );

if( displayOutput ) {
logger.info( "Build info for: " + build );
logger.info( "Workspace: " + build.getWorkspace() );
logger.info( "Logfile: " + build.getLogFile() );
logger.info( "DESCRIPTION: " + build.getDescription() );
AbstractBuild build = futureBuild.get();

logger.info( "-------------------------------------------------\nJENKINS LOG: " );
logger.info( getLog( build ) );
logger.info( "\n-------------------------------------------------\n" );
}
PrintStream out = new PrintStream( new File( outputDir, "jenkins." + getSafeName( project.getDisplayName() ) + "." + build.getNumber() + ".log" ) );

out.println( "Build : " + build );
out.println( "Workspace : " + build.getWorkspace() );
out.println( "Logfile : " + build.getLogFile() );
out.println( "Description: " + build.getDescription() );
out.println();
out.println( "-------------------------------------------------" );
out.println( " JENKINS LOG: " );
out.println( "-------------------------------------------------" );
out.println( getLog( build ) );
out.println( "-------------------------------------------------" );
out.println( "-------------------------------------------------" );
out.println();

return build;
}
Expand Down
71 changes: 71 additions & 0 deletions src/test/java/net/praqma/hudson/test/SystemValidator.java
@@ -1,7 +1,13 @@
package net.praqma.hudson.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import hudson.FilePath;
import org.hamcrest.CoreMatchers;
import org.hamcrest.core.IsNot;
import org.hamcrest.core.IsNull;
Expand All @@ -27,7 +33,13 @@

public class SystemValidator {

private static Logger logger = Logger.getLogger( SystemValidator.class.getName() );

private AbstractBuild<?, ?> build;

/* Validate path elements */
private boolean checkPathElements = false;
private Map<FilePath, List<Element>> pathsToCheck = new HashMap<FilePath, List<Element>>();

public SystemValidator( AbstractBuild<?, ?> build ) {
this.build = build;
Expand Down Expand Up @@ -66,6 +78,16 @@ public SystemValidator validate() throws ClearCaseException {
System.out.println( "[Validating created baseline]" );
checkCreatedBaseline();
}

/* Check the path elements */
if( checkPathElements ) {
logger.info( "Checking path elements" );
try {
doCheckPaths();
} catch( Exception e ) {
fail( e.getMessage() );
}
}

/**/

Expand Down Expand Up @@ -210,6 +232,55 @@ private void checkCreatedBaseline() throws ClearCaseException {
}

}


/* Path checks */

public static class Element {
private boolean mustExist;
private String element;

public Element( String element, boolean mustExist ) {
this.element = element;
this.mustExist = mustExist;
}

@Override
public String toString() {
return element;
}
}

public SystemValidator addElementToPathCheck( FilePath path, Element element ) {
this.checkPathElements = true;

if( pathsToCheck.containsKey( path ) ) {
pathsToCheck.get( path ).add( element );
} else {
List<Element> e = new ArrayList<Element>();
e.add( element );
pathsToCheck.put( path, e );
}

return this;
}

private void doCheckPaths() throws IOException, InterruptedException {
for( FilePath path : pathsToCheck.keySet() ) {
List<Element> elements = pathsToCheck.get( path );
logger.info( "Checking " + path );

for( Element element : elements ) {
if( element.mustExist ) {
logger.info( "Path must have " + element );
assertTrue( "The path " + path + " does not have " + element, new FilePath( path, element.element ).exists() );
} else {
logger.info( "Path must NOT have " + element );
assertFalse( "The path " + path + " does have " + element, new FilePath( path, element.element ).exists() );
}
}
}
}

/* Helpers */
private CCUCMBuildAction action;
Expand Down
Expand Up @@ -34,11 +34,11 @@ public void jenkins16487Any() throws Exception {

/* First build must be a success, because there is a valid baseline.
* This build is done because we need a previous action object */
AbstractBuild build1 = new CCUCMRule.ProjectBuilder( project ).build();
AbstractBuild build1 = jenkins.getProjectBuilder( project ).build();
new SystemValidator( build1 ).validateBuild( Result.SUCCESS ).validate();

/* Because we have ANY promotion level, the build must NOT fail */
AbstractBuild build2 = new CCUCMRule.ProjectBuilder( project ).build();
AbstractBuild build2 = jenkins.getProjectBuilder( project ).build();
new SystemValidator( build2 ).validateBuild( Result.SUCCESS ).validate();
}

Expand Down
Expand Up @@ -36,13 +36,13 @@ public class JENKINS16620 extends BaseTestClass {
public void jenkins16620() throws Exception {
Project project = new CCUCMRule.ProjectCreator( "JENKINS-16620", "_System@" + ccenv.getPVob(), "one_int@" + ccenv.getPVob() ).getProject();

AbstractBuild build1 = new CCUCMRule.ProjectBuilder( project ).failBuild( true ).build();
AbstractBuild build1 = jenkins.getProjectBuilder( project ).failBuild( true ).build();

Baseline bl = ccenv.context.baselines.get( "model-1" ).load();

bl.setPromotionLevel( net.praqma.clearcase.ucm.entities.Project.PromotionLevel.INITIAL );

AbstractBuild build2 = new CCUCMRule.ProjectBuilder( project ).build();
AbstractBuild build2 = jenkins.getProjectBuilder( project ).build();

new SystemValidator( build2 ).validateBuild( Result.SUCCESS ).validate();
}
Expand All @@ -53,7 +53,7 @@ public void jenkins16620() throws Exception {
public void jenkins16620Any() throws Exception {
Project project = new CCUCMRule.ProjectCreator( "JENKINS-16620-ANY", "_System@" + ccenv.getPVob(), "one_int@" + ccenv.getPVob() ).setPromotionLevel( null ).getProject();

AbstractBuild build1 = new CCUCMRule.ProjectBuilder( project ).failBuild( false ).build();
AbstractBuild build1 = jenkins.getProjectBuilder( project ).failBuild( false ).build();

new SystemValidator( build1 ).validateBuild( Result.SUCCESS ).validate();

Expand Down
Expand Up @@ -38,11 +38,11 @@ public void jenkins16636() throws Exception {

/* First build must be a success, because there is a valid baseline.
* This build is done because we need a previous action object */
AbstractBuild build1 = new CCUCMRule.ProjectBuilder( project ).build();
AbstractBuild build1 = jenkins.getProjectBuilder( project ).build();
new SystemValidator( build1 ).validateBuild( Result.SUCCESS ).validate();

/* Because there are no new baselines, the build must fail */
AbstractBuild build2 = new CCUCMRule.ProjectBuilder( project ).build();
AbstractBuild build2 = jenkins.getProjectBuilder( project ).build();
new SystemValidator( build2 ).validateBuild( Result.NOT_BUILT ).validate();
}

Expand All @@ -54,11 +54,11 @@ public void jenkins16636Any() throws Exception {

/* First build must be a success, because there is a valid baseline.
* This build is done because we need a previous action object */
AbstractBuild build1 = new CCUCMRule.ProjectBuilder( project ).build();
AbstractBuild build1 = jenkins.getProjectBuilder( project ).build();
new SystemValidator( build1 ).validateBuild( Result.SUCCESS ).validate();

/* Because we have ANY promotion level, the build must NOT fail */
AbstractBuild build2 = new CCUCMRule.ProjectBuilder( project ).build();
AbstractBuild build2 = jenkins.getProjectBuilder( project ).build();
new SystemValidator( build2 ).validateBuild( Result.SUCCESS ).validate();
}

Expand Down

0 comments on commit fc1694d

Please sign in to comment.