From 92fd06c78a00f8cfecfee9f914b84675cefacead Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 15 Oct 2016 23:44:11 +0800 Subject: [PATCH] Add plugin testing. Signed-off-by: Bo-Yi Wu --- .gitignore | 2 +- Makefile | 2 +- README.md | 1 + jenkins.go | 1 - plugin_test.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 plugin_test.go diff --git a/.gitignore b/.gitignore index 19ad47f..d713321 100644 --- a/.gitignore +++ b/.gitignore @@ -23,5 +23,5 @@ _testmain.go *.test *.prof vendor -drone-telegram +drone-jenkins coverage.txt diff --git a/Makefile b/Makefile index 0f8c9d0..7733c51 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: install build test html update docker_build docker_image docker_deploy clean +.PHONY: install build test html update docker_build docker_image docker docker_deploy clean VERSION := $(shell git describe --tags || git rev-parse --short HEAD) DEPLOY_ACCOUNT := "appleboy" diff --git a/README.md b/README.md index 5f09ba5..0ad7ab4 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ Execute from the working directory: ``` docker run --rm \ + -e PLUGIN_BASE_URL=http://example.com \ -e PLUGIN_USERNAME=xxxxxxx \ -e PLUGIN_TOKEN=xxxxxxx \ -e PLUGIN_JOB=xxxxxxx \ diff --git a/jenkins.go b/jenkins.go index 6cf577e..f5cf660 100644 --- a/jenkins.go +++ b/jenkins.go @@ -34,7 +34,6 @@ func NewJenkins(auth *Auth, url string) *Jenkins { func (jenkins *Jenkins) buildURL(path string, params url.Values) (requestURL string) { requestURL = jenkins.BaseURL + path - fmt.Println(requestURL) if params != nil { queryString := params.Encode() if queryString != "" { diff --git a/plugin_test.go b/plugin_test.go new file mode 100644 index 0000000..b04eb9d --- /dev/null +++ b/plugin_test.go @@ -0,0 +1,70 @@ +package main + +import ( + "github.com/stretchr/testify/assert" + + "testing" +) + +func TestMissingConfig(t *testing.T) { + var plugin Plugin + + err := plugin.Exec() + + assert.NotNil(t, err) +} + +func TestMissingJenkinsConfig(t *testing.T) { + plugin := Plugin{ + Config: Config{ + BaseURL: "http://example.com", + }, + } + + err := plugin.Exec() + + assert.NotNil(t, err) +} + +func TestPluginTriggerBuild(t *testing.T) { + plugin := Plugin{ + Repo: Repo{ + Name: "go-hello", + Owner: "appleboy", + }, + Build: Build{ + Number: 101, + Status: "success", + Link: "https://github.com/appleboy/go-hello", + Author: "Bo-Yi Wu", + Branch: "master", + Message: "update by drone line plugin.", + Commit: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2", + }, + + Config: Config{ + BaseURL: "http://example.com", + Username: "foo", + Token: "bar", + Job: []string{"drone-jenkins"}, + }, + } + + err := plugin.Exec() + + assert.Nil(t, err) +} + +func TestTrimElement(t *testing.T) { + var input, result []string + + input = []string{"1", " ", "3"} + result = []string{"1", "3"} + + assert.Equal(t, result, trimElement(input)) + + input = []string{"1", "2"} + result = []string{"1", "2"} + + assert.Equal(t, result, trimElement(input)) +}