From 94132603d9baec11b98dad1acce1892d392d976a Mon Sep 17 00:00:00 2001 From: Yuxiang Zhu Date: Mar 28 2019 15:02:45 +0000 Subject: Add unit tests for PagureClient This also demonstrates how to use Jenkins Pipeline Unit Testing Framework for a Jenkins shared library. --- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..08b1a6f --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.* +!.gitignore +gradle/ +out/ +build/ diff --git a/README.md b/README.md index 9295f56..ee4be65 100644 --- a/README.md +++ b/README.md @@ -17,3 +17,14 @@ More complete documentation is in progress. ## Usage See the [Jenkins documentation](https://jenkins.io/doc/book/pipeline/shared-libraries/#using-libraries) for information on how to use this library from your pipelines. + +## Run unit tests + +C3I Library uses [Jenkins Pipeline Unit testing framework](https://github.com/jenkinsci/JenkinsPipelineUnit) +to simulate Jenkins pipeline engine and [Gradle](https://gradle.org/) for compiling and running tests. + +You can run the unit test suite with the following command: + +``` +gradle test # or `gradle test --info` to be more verbose +``` diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..46b631d --- /dev/null +++ b/build.gradle @@ -0,0 +1,28 @@ +plugins { + id "groovy" +} + +repositories { + jcenter() +} + +dependencies { + implementation "org.codehaus.groovy:groovy-all:2.5.0" + testImplementation "junit:junit:4.12" + // use Jenkins Pipeline Unit testing framework (https://github.com/jenkinsci/JenkinsPipelineUnit) + testImplementation group:'com.lesfurets', name:'jenkins-pipeline-unit', version:'1.1' +} + +sourceSets { + main { + groovy { + srcDirs = ['src', 'vars'] + } + } + + test { + groovy { + srcDirs = ['test'] + } + } +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..e46f7fb --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'c3i-library' diff --git a/test/PagureTest.groovy b/test/PagureTest.groovy new file mode 100644 index 0000000..b2b6db8 --- /dev/null +++ b/test/PagureTest.groovy @@ -0,0 +1,122 @@ +import org.junit.* +import com.lesfurets.jenkins.unit.* +import static groovy.test.GroovyAssert.* +import groovy.json.* +import java.net.URLEncoder + +class PagureTest extends BasePipelineTest { + def pagure + def client + + @Before + void setUp() { + super.setUp() + pagure = loadScript("vars/pagure.groovy") + binding.setVariable('steps', pagure) + client = pagure.client(apiUrl: 'https://pagure.io', repo: 'c3i-library') + helper.registerAllowedMethod('readJSON', [Map.class], { + def jsonSlurper = new JsonSlurper() + jsonSlurper.parseText(it.text) + }) + } + + @Test + void testGetPR() { + def pr = 123 + def response = [ + "assignee" : null, + "branch" : "master", + "branch_from" : "master", + "closed_at" : null, + "closed_by" : null, + "comments" : [], + "commit_start": null, + "commit_stop" : null, + "date_created": "1431414800", + "id" : pr, + "project" : [ + "close_status": [], + "custom_keys" : [], + "date_created": "1431414800", + "description" : "test project #1", + "id" : 1, + "name" : "test", + "parent" : null, + "user" : [ + "fullname": "PY C", + "name" : "pingou" + ] + ], + "repo_from" : [ + "date_created": "1431414800", + "description" : "test project #1", + "id" : 1, + "name" : "test", + "parent" : null, + "user" : [ + "fullname": "PY C", + "name" : "pingou" + ] + ], + "status" : "Open", + "title" : "test pull-request", + "uid" : "1431414800", + "updated_on" : "1431414800", + "user" : [ + "fullname": "PY C", + "name" : "pingou" + ] + ] + helper.registerAllowedMethod("httpRequest", [Map.class], { c -> + assert c.httpMode == 'GET' + assert c.url == "https://pagure.io/c3i-library/pull-request/${pr}" + return [content: JsonOutput.toJson(response)] + }) + assert client.getPR(pr).id == pr + } + + @Test + void testCommentOnPR() { + def args = [pr: 123, comment: 'Hello, world!'] + def response = [message: 'Comment added'] + helper.registerAllowedMethod("httpRequest", [Map.class], { c -> + assert c.httpMode == 'POST' + assert c.url == "https://pagure.io/c3i-library/pull-request/${args.pr}/comment" + assert c.requestBody == 'comment=' + URLEncoder.encode(args.comment) + return [content: JsonOutput.toJson(response)] + }) + assert client.commentOnPR(args) == response + } + + @Test + void testUpdatePRStatus() { + def args = [pr : 123, username: 'c3i-jenkins', uid: 'c3i-premerge', percent: 100, + comment: 'Test succeeded.', url: 'http://example.com/'] + def response = [ + "flag" : [ + "comment" : args.comment, + "date_created" : "1510742565", + "percent" : args.percent, + "pull_request_uid": "62b49f00d489452994de5010565fab81", + "status" : "success", + "url" : args.url, + "user" : [ + "default_email": "yuxzhu@redhat.com", + "emails" : ["yuxzhu@redhat.com"], + "fullname" : "Yuxiang Zhu", + "name" : "rayson"], + "username" : args.username + ], + "message": "Flag updated", + "uid" : "jenkins_build_pagure_100+seed" + ] + helper.registerAllowedMethod("httpRequest", [Map.class], { c -> + assert c.httpMode == 'POST' + assert c.url == "https://pagure.io/c3i-library/pull-request/${args.pr}/flag" + assert c.requestBody == "username=${URLEncoder.encode(args.username)}&uid=${URLEncoder.encode(args.uid)}" + + "&percent=${args.percent}&comment=${URLEncoder.encode(args.comment)}&url=${URLEncoder.encode(args.url)}" + return [content: JsonOutput.toJson(response)] + }) + assert client.updatePRStatus(args) == response + } +}