Skip to content

Commit

Permalink
Merge pull request #4 from zanata/master
Browse files Browse the repository at this point in the history
Added Jenkinsfile for building
  • Loading branch information
Patrick Huang authored Mar 3, 2017
2 parents 9ac740d + 930eb38 commit 3d52c2a
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 4 deletions.
67 changes: 65 additions & 2 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,67 @@
#!/usr/bin/env groovy
@Library('github.com/zanata/zanata-pipeline-library@master')

/* `buildPlugin` step provided by: https://github.com/jenkins-infra/pipeline-library */
buildPlugin()
/* Only keep the 10 most recent builds. */
def projectProperties = [
[
$class: 'BuildDiscarderProperty',
strategy: [$class: 'LogRotator', numToKeepStr: '10']
],
]

properties(projectProperties)

/* Upto stash stage should fail fast:
* Failed and stop the build
* Yet able to create report
*/
try {
timestamps {
node {
ansicolor {
stage('Checkout') {
info.printNode()
notify.started()

// Shallow Clone does not work with RHEL7, which use git-1.8.3
// https://issues.jenkins-ci.org/browse/JENKINS-37229
checkout scm
}

// Build and Unit Tests
// The result is archived
stage('Build') {
info.printNode()
info.printEnv()
def testReports = 'target/surefire-reports/TEST-*.xml'
def hpiFiles = 'target/*.hpi'

withEnv(["MVN_HOME=${ tool name: 'mvn', type: 'hudson.tasks.Maven$MavenInstallation' }"]) {
sh '$MVN_HOME/bin/mvn clean verify'
}

junit allowEmptyResults: true,
keepLongStdio: false,
testDataPublishers: [[$class: 'StabilityTestDataPublisher']],
testResults: "**/${testReports}"

// notify if compile+unit test successful
notify.testResults(null)
archive "**/${hpiFiles},**/target/site/jacoco/**"
}

stage('Report') {
// this is not working properly yet
// step([$class: 'JacocoPublisher'])
}
}
}
}
} catch (e) {
notify.failed()
junit allowEmptyResults: true,
keepLongStdio: false,
testDataPublishers: [[$class: 'StabilityTestDataPublisher']],
testResults: "**/${testReports}"
throw e
}
4 changes: 4 additions & 0 deletions Jenkinsfile_jenkinsci
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env groovy

/* `buildPlugin` step provided by: https://github.com/jenkins-infra/pipeline-library */
buildPlugin()
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ of this software and associated documentation files (the "Software"), to deal
import hudson.FilePath;
import hudson.Util;
import hudson.model.Computer;
import hudson.model.Executor;
import hudson.model.Item;
import hudson.model.Job;
import hudson.model.Queue;
Expand Down Expand Up @@ -148,8 +149,9 @@ public void perform(Run<?,?> build, FilePath workspace, Launcher launcher, TaskL

// build.getEnvironment(listener) doesn't seem to get current node's environment.
// This will work and inherit the global JAVA_HOME env
if (build.getExecutor() != null) {
Computer owner = build.getExecutor().getOwner();
Executor executor = build.getExecutor();
if (executor != null) {
Computer owner = executor.getOwner();
EnvVars envForNode = owner.buildEnvironment(listener);
envs.putAll(envForNode);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package org.jenkinsci.plugins.zanata.zanatareposync;

import static hudson.model.Item.EXTENDED_READ;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;
import org.jvnet.hudson.test.WithoutJenkins;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import hudson.model.AbstractProject;
import hudson.model.Item;
import hudson.security.Permission;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;

Expand Down Expand Up @@ -79,4 +84,36 @@ public void testPushTypeOptions() {
assertThat(options.stream().map(option -> option.value))
.contains("source", "trans", "both");
}

@Test
@WithoutJenkins
public void credentialIdIsOkWhenContextIsNotNullAndDoesNotHaveExtendedReadPermission() {
ZanataSyncStep.DescriptorImpl descriptor =
new ZanataSyncStep.DescriptorImpl();
AbstractProject context = Mockito.mock(AbstractProject.class);
when(context.hasPermission(EXTENDED_READ)).thenReturn(false);
String url = "http://localhost";
String credentialId = "some_id";
assertThat(descriptor.doCheckZanataCredentialsId(
context, url, credentialId).kind)
.isEqualTo(FormValidation.Kind.OK);
}

@Test
@WithoutJenkins
public void credentialIdIsOkWhenContextHasExtendedReadPermissionButURLOrValueIsNull() {
ZanataSyncStep.DescriptorImpl descriptor =
new ZanataSyncStep.DescriptorImpl();
AbstractProject context = Mockito.mock(AbstractProject.class);
when(context.hasPermission(EXTENDED_READ)).thenReturn(true);
assertThat(descriptor.doCheckZanataCredentialsId(
context, null, null).kind)
.isEqualTo(FormValidation.Kind.OK);
assertThat(descriptor.doCheckZanataCredentialsId(
context, "http://localhost",
null).kind).isEqualTo(FormValidation.Kind.OK);
assertThat(descriptor.doCheckZanataCredentialsId(
context, null,
"some_id").kind).isEqualTo(FormValidation.Kind.OK);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package org.jenkinsci.plugins.zanata.zanatareposync;

import static hudson.model.Item.EXTENDED_READ;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.List;

import org.junit.Test;
import org.mockito.Mockito;

import hudson.model.AbstractProject;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;

/**
Expand All @@ -24,6 +29,30 @@
*/
public class ZanataSyncStepTest extends WithJenkins {

@Test
public void testCheckCredentialId() throws IOException {
ZanataSyncStep.DescriptorImpl descriptor =
new ZanataSyncStep.DescriptorImpl();
AbstractProject context = Mockito.mock(AbstractProject.class);
when(context.hasPermission(EXTENDED_READ)).thenReturn(true);

String url = "http://localhost";
String credentialId = "some_id";
assertThat(descriptor.doCheckZanataCredentialsId(
context, url, credentialId).kind)
.isEqualTo(FormValidation.Kind.WARNING);

// now we insert a credential with that id
String username = "user";
String password = "s3cr3t";
addUsernamePasswordCredential(username, password, credentialId,
"");

assertThat(descriptor.doCheckZanataCredentialsId(
context, url, credentialId).kind)
.isEqualTo(FormValidation.Kind.OK);
}

@Test
public void testCredentialsOptions() throws IOException {
String username = "user";
Expand Down

0 comments on commit 3d52c2a

Please sign in to comment.