Skip to content

Commit

Permalink
[JENKINS-17604] Added test suite for Git
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfgarnet committed Apr 14, 2013
1 parent 3612e72 commit 7ed417b
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 6 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Expand Up @@ -292,7 +292,7 @@
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>2.0.0.201206130900-r</version>
<version>2.3.1.201302201838-r</version>
</dependency>


Expand All @@ -309,6 +309,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>


</dependencies>

Expand Down
Expand Up @@ -71,11 +71,17 @@ public enum ResultType {
private static String VERSION = "Unresolved";

static {
if( Jenkins.getInstance() != null ) {
FEED_PATH = new File( Jenkins.getInstance().getRootDir(), FEED_DIR );
VERSION = Jenkins.getInstance().getPlugin( "config-rotator" ).getWrapper().getVersion();
} else {
try {
if( Jenkins.getInstance() != null ) {
System.out.println( "JENKINS" + Jenkins.getInstance() );
System.out.println( "JENKINS" + Jenkins.getInstance().getRootDir() );
FEED_PATH = new File( Jenkins.getInstance().getRootDir(), FEED_DIR );
VERSION = Jenkins.getInstance().getPlugin( "config-rotator" ).getWrapper().getVersion();
} else {

}
} catch ( Exception e ) {
/* No op */
}
}

Expand Down
Expand Up @@ -5,7 +5,6 @@
import hudson.model.Project;
import hudson.model.TopLevelItem;
import hudson.scm.SCM;
import net.praqma.jenkins.configrotator.scm.clearcaseucm.ClearCaseUCMTarget;

import java.io.IOException;
import java.util.ArrayList;
Expand Down
75 changes: 75 additions & 0 deletions src/test/java/net/praqma/jenkins/configrotator/GitRule.java
@@ -0,0 +1,75 @@
package net.praqma.jenkins.configrotator;

import hudson.FilePath;
import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

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

/**
* @author cwolfgang
*/
public class GitRule implements TestRule {

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

private File gitPath;
private Repository gitRepo;
private Git git;

public void initialize( File gitPath ) {
this.gitPath = gitPath;

FileRepositoryBuilder builder = new FileRepositoryBuilder();
try {
gitRepo = builder.setGitDir( new File( gitPath, ".git" ) ).build();
gitRepo.create();
} catch( IOException e ) {
e.printStackTrace();
}
git = new Git( gitRepo );
}

public RevCommit createCommit( String filename, String content ) throws IOException, GitAPIException {

File file = new File( gitPath, filename );
boolean create = !file.exists();
FileUtils.write( file, content, false );

git.add().addFilepattern( filename ).call();
return git.commit().setMessage( ( create ? "Creating " : "Updating" ) + " " + filename ).setAll( true ).call();
}

public String getRepo() {
return gitRepo.getDirectory().getAbsolutePath();
}


@Override
public Statement apply( final Statement base, Description description ) {
return new Statement() {

@Override
public void evaluate() throws Throwable {
base.evaluate();
}
};
}

protected void listPath( File path ) {
logger.info( "Listing " + path + "(" + path.exists() + ")" );
for( String f : path.list() ) {
logger.info( " * " + f );
}
}
}
Expand Up @@ -4,6 +4,7 @@
import hudson.model.AbstractBuild;
import hudson.model.Result;
import hudson.scm.SCM;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -114,6 +115,15 @@ public void validate() {
}
}

if( this.checkContent ) {
logger.info( "Checking content of " + contentFile );
try {
doCheckContent();
} catch ( Exception e ) {
fail( e.getMessage() );
}
}

logger.info( "Successfully validated system" );

logger.info( "-----= Successfully validated system =-----" );
Expand Down Expand Up @@ -222,4 +232,23 @@ private void doCheckPaths() throws IOException, InterruptedException {
}
}
}

private boolean checkContent = false;
private File contentFile;
private String content = "";

public SystemValidator checkContent( File file, String content ) {
this.checkContent = true;
this.contentFile = file;
this.content = content;

return this;
}

private void doCheckContent() throws IOException {
String oc = FileUtils.readFileToString( contentFile );

logger.info( "Content of " + contentFile + " must be " + content + "(" + oc + ")" );
assertThat( oc, is( content ) );
}
}
@@ -1,5 +1,15 @@
package net.praqma.jenkins.configrotator.functional.scm.git;

import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import net.praqma.jenkins.configrotator.*;
import net.praqma.jenkins.configrotator.scm.clearcaseucm.ClearCaseUCMTarget;
import net.praqma.jenkins.configrotator.scm.git.Git;
import net.praqma.jenkins.configrotator.scm.git.GitTarget;
import net.praqma.logging.PraqmaticLogFormatter;
import net.praqma.util.test.junit.LoggingRule;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.lib.ObjectId;
Expand All @@ -8,14 +18,114 @@
import org.eclipse.jgit.revwalk.RevSort;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestRule;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;

public class GitTest {

@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Rule
public GitRule git = new GitRule();

@ClassRule
public static LoggingRule lrule = new LoggingRule( Level.ALL, "net.praqma" ).setFormat( PraqmaticLogFormatter.NORMAL_FORMAT );

@ClassRule
public static ConfigRotatorRule2 crRule = new ConfigRotatorRule2( GitTest.class );

@Test
public void basic() throws IOException, GitAPIException, InterruptedException {
git.initialize( folder.newFolder() );
RevCommit commit1 = git.createCommit( "text.txt", "1" );

ProjectBuilder builder = new ProjectBuilder( new Git() ).setName( "git-test-01" );
ConfigRotatorProject project = builder.getProject();
project.addTarget( new GitTarget( "test", git.getRepo(), "master", commit1.getName(), false ) );

AbstractBuild<?, ?> build = crRule.buildProject( project.getJenkinsProject(), false, null );

FilePath path = new FilePath( project.getJenkinsProject().getLastBuiltOn().getWorkspaceFor( (FreeStyleProject)project.getJenkinsProject() ), "test" );
File filePath = new File( path.toURI() );

SystemValidator<ClearCaseUCMTarget> val = new SystemValidator<ClearCaseUCMTarget>( build );
val.checkExpectedResult( Result.SUCCESS ).
checkAction( true ).
checkCompatability( true ).
checkTargets( new GitTarget( "test", git.getRepo(), "master", commit1.getName(), false ) ).
checkContent( new File( filePath, "text.txt" ), "1" ).
validate();
}

@Test
public void basicNothingToDo() throws IOException, GitAPIException, InterruptedException {
git.initialize( folder.newFolder() );
RevCommit commit1 = git.createCommit( "text.txt", "1" );

ProjectBuilder builder = new ProjectBuilder( new Git() ).setName( "git-test-02" );
ConfigRotatorProject project = builder.getProject();
project.addTarget( new GitTarget( "test", git.getRepo(), "master", commit1.getName(), false ) );

AbstractBuild<?, ?> build = crRule.buildProject( project.getJenkinsProject(), false, null );

FilePath path = new FilePath( project.getJenkinsProject().getLastBuiltOn().getWorkspaceFor( (FreeStyleProject)project.getJenkinsProject() ), "test" );
File filePath = new File( path.toURI() );

SystemValidator<ClearCaseUCMTarget> val = new SystemValidator<ClearCaseUCMTarget>( build );
val.checkExpectedResult( Result.SUCCESS ).
checkAction( true ).
checkCompatability( true ).
checkTargets( new GitTarget( "test", git.getRepo(), "master", commit1.getName(), false ) ).
checkContent( new File( filePath, "text.txt" ), "1" ).
validate();

AbstractBuild<?, ?> build2 = crRule.buildProject( project.getJenkinsProject(), false, null );

SystemValidator<ClearCaseUCMTarget> val2 = new SystemValidator<ClearCaseUCMTarget>( build2 );
val2.checkExpectedResult( Result.NOT_BUILT ).checkAction( false ).validate();
}


@Test
public void basicMultiple() throws IOException, GitAPIException, InterruptedException {
git.initialize( folder.newFolder() );
RevCommit commit1 = git.createCommit( "text.txt", "1" );
RevCommit commit2 = git.createCommit( "text.txt", "2" );

ProjectBuilder builder = new ProjectBuilder( new Git() ).setName( "git-test-03" );
ConfigRotatorProject project = builder.getProject();
project.addTarget( new GitTarget( "test", git.getRepo(), "master", commit1.getName(), false ) );

AbstractBuild<?, ?> build1 = crRule.buildProject( project.getJenkinsProject(), false, null );
AbstractBuild<?, ?> build2 = crRule.buildProject( project.getJenkinsProject(), false, null );

FilePath path = new FilePath( project.getJenkinsProject().getLastBuiltOn().getWorkspaceFor( (FreeStyleProject)project.getJenkinsProject() ), "test" );
File filePath = new File( path.toURI() );

SystemValidator<ClearCaseUCMTarget> val2 = new SystemValidator<ClearCaseUCMTarget>( build2 );
val2.checkExpectedResult( Result.SUCCESS ).
checkAction( true ).
checkCompatability( true ).
checkTargets( new GitTarget( "test", git.getRepo(), "master", commit2.getName() , false ) ).
checkContent( new File( filePath, "text.txt" ), "2" ).
validate();

AbstractBuild<?, ?> build3 = crRule.buildProject( project.getJenkinsProject(), false, null );

SystemValidator<ClearCaseUCMTarget> val3 = new SystemValidator<ClearCaseUCMTarget>( build3 );
val3.checkExpectedResult( Result.NOT_BUILT ).validate();
}

//@Test
public void testing() throws IOException, GitAPIException {

String commitId = "HEAD";
Expand Down

0 comments on commit 7ed417b

Please sign in to comment.