-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_test.go
120 lines (111 loc) · 2.78 KB
/
build_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package jenkins
import (
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func setupBuild(t *testing.T) *Build {
var build *Build
build, err := pipeline.GetLastCompleteBuild()
assert.Nil(t, err)
if build != nil {
return build
}
qitem, err := pipeline.Build(nil)
assert.Nil(t, err)
for {
time.Sleep(1 * time.Second)
build, err = qitem.GetBuild()
assert.Nil(t, err)
if build != nil {
break
}
}
var output []string
err = build.LoopProgressiveLog("text", func(line string) error {
output = append(output, string(line))
time.Sleep(1 * time.Second)
return nil
})
assert.Nil(t, err)
assert.Contains(t, strings.Join(output, ""), os.Getenv("JENKINS_VERSION"))
return build
}
func TestBuildItemIsBuilding(t *testing.T) {
build := setupBuild(t)
building, err := build.IsBuilding()
assert.Nil(t, err)
assert.False(t, building)
}
func TestBuildItemGetJob(t *testing.T) {
build := setupBuild(t)
job, err := build.GetJob()
assert.Nil(t, err)
assert.Equal(t, job.URL, pipeline.URL)
}
func TestBuildItemGetDescription(t *testing.T) {
build := setupBuild(t)
discription, err := build.GetDescription()
assert.Nil(t, err)
assert.Empty(t, discription)
_, err = build.SetDescription("test")
assert.Nil(t, err)
discription, err = build.GetDescription()
assert.Nil(t, err)
assert.Equal(t, discription, "test")
}
func TestBuildItemDelete(t *testing.T) {
build := setupBuild(t)
assert.NotNil(t, build)
_, err := build.Delete()
assert.Nil(t, err)
build, err = pipeline.GetBuild(build.Number)
assert.NotNil(t, err)
assert.Nil(t, build)
}
func TestStopBuildItem(t *testing.T) {
// change config
conf := `<?xml version='1.1' encoding='UTF-8'?>
<flow-definition plugin="workflow-job">
<definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps">
<script>sleep(20)</script>
<sandbox>true</sandbox>
</definition>
<disabled>false</disabled>
</flow-definition>`
_, err := pipeline.SetConfigure(strings.NewReader(conf))
assert.Nil(t, err)
// start build to sleep 20s
qitem, err := pipeline.Build(nil)
assert.Nil(t, err)
job, err := qitem.GetJob()
assert.Nil(t, err)
assert.Equal(t, job.FullName, pipeline.FullName)
var build *Build
for {
time.Sleep(1 * time.Second)
build, err = qitem.GetBuild()
assert.Nil(t, err)
if build != nil {
break
}
}
building, err := build.IsBuilding()
assert.Nil(t, err)
assert.True(t, building)
_, err = build.Stop()
assert.Nil(t, err)
building, err = build.IsBuilding()
assert.Nil(t, err)
assert.False(t, building)
result, err := build.GetResult()
assert.Nil(t, err)
assert.Equal(t, result, "ABORTED")
// delete build and revert configure
_, err = build.Delete()
assert.Nil(t, err)
_, err = pipeline.SetConfigure(strings.NewReader(jobConf))
assert.Nil(t, err)
}