Skip to content

Commit b4bae26

Browse files
committed
Check when env var been set to run integration tests.
All tests require Jenkins instance to succeed. Signed-off-by: Hui Luo <[email protected]>
1 parent d70ffde commit b4bae26

File tree

2 files changed

+96
-7
lines changed

2 files changed

+96
-7
lines changed

credentials_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ package gojenkins
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67
"testing"
78

89
"github.com/stretchr/testify/assert"
910
)
1011

12+
const integration_test string = "INTEGRATION"
13+
1114
var (
12-
cm CredentialsManager
15+
cm *CredentialsManager
1316
domain = "_"
1417
dockerID = "dockerIDCred"
1518
sshID = "sshIdCred"
@@ -19,7 +22,9 @@ var (
1922
)
2023

2124
func TestCreateUsernameCredentials(t *testing.T) {
22-
25+
if _, ok := os.LookupEnv(integration_test); !ok {
26+
return
27+
}
2328
cred := UsernameCredentials{
2429
ID: usernameID,
2530
Scope: scope,
@@ -41,7 +46,9 @@ func TestCreateUsernameCredentials(t *testing.T) {
4146
}
4247

4348
func TestCreateFileCredentials(t *testing.T) {
44-
49+
if _, ok := os.LookupEnv(integration_test); !ok {
50+
return
51+
}
4552
cred := FileCredentials{
4653
ID: fileID,
4754
Scope: scope,
@@ -63,7 +70,9 @@ func TestCreateFileCredentials(t *testing.T) {
6370
}
6471

6572
func TestCreateDockerCredentials(t *testing.T) {
66-
73+
if _, ok := os.LookupEnv(integration_test); !ok {
74+
return
75+
}
6776
cred := DockerServerCredentials{
6877
Scope: scope,
6978
ID: dockerID,
@@ -89,6 +98,9 @@ func TestCreateDockerCredentials(t *testing.T) {
8998
}
9099

91100
func TestCreateSSHCredentialsFullFlow(t *testing.T) {
101+
if _, ok := os.LookupEnv(integration_test); !ok {
102+
return
103+
}
92104
sshCred := SSHCredentials{
93105
Scope: scope,
94106
ID: sshID,
@@ -128,8 +140,8 @@ func TestMain(m *testing.M) {
128140
jenkins := CreateJenkins(nil, "http://localhost:8080", "admin", "admin")
129141
jenkins.Init(ctx)
130142

131-
cm = CredentialsManager{J: jenkins}
132-
143+
cm = &CredentialsManager{J: jenkins}
144+
fmt.Printf("Debug, from TestMain\n")
133145
//execute tests
134146
os.Exit(m.Run())
135147
}

jenkins_test.go

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@ var (
1616
)
1717

1818
func TestInit(t *testing.T) {
19+
if _, ok := os.LookupEnv(integration_test); !ok {
20+
return
21+
}
1922
ctx := context.Background()
2023
jenkins = CreateJenkins(nil, "http://localhost:8080", "admin", "admin")
2124
_, err := jenkins.Init(ctx)
2225
assert.Nil(t, err, "Jenkins Initialization should not fail")
2326
}
2427

2528
func TestCreateJobs(t *testing.T) {
29+
if _, ok := os.LookupEnv(integration_test); !ok {
30+
return
31+
}
2632
job1ID := "Job1_test"
2733
job2ID := "job2_test"
2834
job_data := getFileAsString("job.xml")
@@ -41,7 +47,9 @@ func TestCreateJobs(t *testing.T) {
4147
}
4248

4349
func TestCreateNodes(t *testing.T) {
44-
50+
if _, ok := os.LookupEnv(integration_test); !ok {
51+
return
52+
}
4553
id1 := "node1_test"
4654
//id2 := "node2_test"
4755
id3 := "node3_test"
@@ -64,6 +72,9 @@ func TestCreateNodes(t *testing.T) {
6472
}
6573

6674
func TestDeleteNodes(t *testing.T) {
75+
if _, ok := os.LookupEnv(integration_test); !ok {
76+
return
77+
}
6778
id := "node4_test"
6879

6980
ctx := context.Background()
@@ -72,6 +83,9 @@ func TestDeleteNodes(t *testing.T) {
7283
}
7384

7485
func TestCreateBuilds(t *testing.T) {
86+
if _, ok := os.LookupEnv(integration_test); !ok {
87+
return
88+
}
7589
ctx := context.Background()
7690
jobs, _ := jenkins.GetAllJobs(ctx)
7791
for _, item := range jobs {
@@ -88,6 +102,9 @@ func TestCreateBuilds(t *testing.T) {
88102
}
89103

90104
func TestGetQueueItem(t *testing.T) {
105+
if _, ok := os.LookupEnv(integration_test); !ok {
106+
return
107+
}
91108
ctx := context.Background()
92109
task, err := jenkins.GetQueueItem(ctx, queueID)
93110
if err != nil {
@@ -99,6 +116,9 @@ func TestGetQueueItem(t *testing.T) {
99116
}
100117

101118
func TestParseBuildHistory(t *testing.T) {
119+
if _, ok := os.LookupEnv(integration_test); !ok {
120+
return
121+
}
102122
r, err := os.Open("_tests/build_history.txt")
103123
if err != nil {
104124
panic(err)
@@ -108,6 +128,9 @@ func TestParseBuildHistory(t *testing.T) {
108128
}
109129

110130
func TestCreateViews(t *testing.T) {
131+
if _, ok := os.LookupEnv(integration_test); !ok {
132+
return
133+
}
111134
ctx := context.Background()
112135
list_view, err := jenkins.CreateView(ctx, "test_list_view", LIST_VIEW)
113136
assert.Nil(t, err)
@@ -124,20 +147,29 @@ func TestCreateViews(t *testing.T) {
124147
}
125148

126149
func TestGetAllJobs(t *testing.T) {
150+
if _, ok := os.LookupEnv(integration_test); !ok {
151+
return
152+
}
127153
ctx := context.Background()
128154
jobs, _ := jenkins.GetAllJobs(ctx)
129155
assert.Equal(t, 2, len(jobs))
130156
assert.Equal(t, jobs[0].Raw.Color, "blue")
131157
}
132158

133159
func TestGetAllNodes(t *testing.T) {
160+
if _, ok := os.LookupEnv(integration_test); !ok {
161+
return
162+
}
134163
ctx := context.Background()
135164
nodes, _ := jenkins.GetAllNodes(ctx)
136165
assert.Equal(t, 3, len(nodes))
137166
assert.Equal(t, nodes[0].GetName(), "master")
138167
}
139168

140169
func TestGetAllBuilds(t *testing.T) {
170+
if _, ok := os.LookupEnv(integration_test); !ok {
171+
return
172+
}
141173
ctx := context.Background()
142174
builds, _ := jenkins.GetAllBuildIds(ctx, "Job1_test")
143175
for _, b := range builds {
@@ -148,6 +180,9 @@ func TestGetAllBuilds(t *testing.T) {
148180
}
149181

150182
func TestGetLabel(t *testing.T) {
183+
if _, ok := os.LookupEnv(integration_test); !ok {
184+
return
185+
}
151186
ctx := context.Background()
152187
label, err := jenkins.GetLabel(ctx, "test_label")
153188
assert.Nil(t, err)
@@ -174,6 +209,12 @@ func TestGetLabel(t *testing.T) {
174209
}
175210

176211
func TestBuildMethods(t *testing.T) {
212+
if _, ok := os.LookupEnv(integration_test); !ok {
213+
return
214+
}
215+
if _, ok := os.LookupEnv(integration_test); !ok {
216+
return
217+
}
177218
ctx := context.Background()
178219
job, _ := jenkins.GetJob(ctx, "Job1_test")
179220
build, _ := job.GetLastBuild(ctx)
@@ -182,6 +223,12 @@ func TestBuildMethods(t *testing.T) {
182223
}
183224

184225
func TestGetSingleJob(t *testing.T) {
226+
if _, ok := os.LookupEnv(integration_test); !ok {
227+
return
228+
}
229+
if _, ok := os.LookupEnv(integration_test); !ok {
230+
return
231+
}
185232
ctx := context.Background()
186233
job, _ := jenkins.GetJob(ctx, "Job1_test")
187234
isRunning, _ := job.IsRunning(ctx)
@@ -192,6 +239,9 @@ func TestGetSingleJob(t *testing.T) {
192239
}
193240

194241
func TestEnableDisableJob(t *testing.T) {
242+
if _, ok := os.LookupEnv(integration_test); !ok {
243+
return
244+
}
195245
ctx := context.Background()
196246
job, _ := jenkins.GetJob(ctx, "Job1_test")
197247
result, _ := job.Disable(ctx)
@@ -201,6 +251,9 @@ func TestEnableDisableJob(t *testing.T) {
201251
}
202252

203253
func TestCopyDeleteJob(t *testing.T) {
254+
if _, ok := os.LookupEnv(integration_test); !ok {
255+
return
256+
}
204257
ctx := context.Background()
205258
job, _ := jenkins.GetJob(ctx, "Job1_test")
206259
jobCopy, _ := job.Copy(ctx, "Job1_test_copy")
@@ -210,19 +263,28 @@ func TestCopyDeleteJob(t *testing.T) {
210263
}
211264

212265
func TestGetPlugins(t *testing.T) {
266+
if _, ok := os.LookupEnv(integration_test); !ok {
267+
return
268+
}
213269
ctx := context.Background()
214270
plugins, _ := jenkins.GetPlugins(ctx, 3)
215271
assert.Equal(t, 10, plugins.Count())
216272
}
217273

218274
func TestGetViews(t *testing.T) {
275+
if _, ok := os.LookupEnv(integration_test); !ok {
276+
return
277+
}
219278
ctx := context.Background()
220279
views, _ := jenkins.GetAllViews(ctx)
221280
assert.Equal(t, len(views), 3)
222281
assert.Equal(t, len(views[0].Raw.Jobs), 2)
223282
}
224283

225284
func TestGetSingleView(t *testing.T) {
285+
if _, ok := os.LookupEnv(integration_test); !ok {
286+
return
287+
}
226288
ctx := context.Background()
227289
view, _ := jenkins.GetView(ctx, "All")
228290
view2, _ := jenkins.GetView(ctx, "test_list_view")
@@ -232,6 +294,9 @@ func TestGetSingleView(t *testing.T) {
232294
}
233295

234296
func TestCreateFolder(t *testing.T) {
297+
if _, ok := os.LookupEnv(integration_test); !ok {
298+
return
299+
}
235300
ctx := context.Background()
236301
folder1ID := "folder1_test"
237302
folder2ID := "folder2_test"
@@ -248,6 +313,9 @@ func TestCreateFolder(t *testing.T) {
248313
}
249314

250315
func TestCreateJobInFolder(t *testing.T) {
316+
if _, ok := os.LookupEnv(integration_test); !ok {
317+
return
318+
}
251319
ctx := context.Background()
252320
jobName := "Job_test"
253321
job_data := getFileAsString("job.xml")
@@ -266,6 +334,9 @@ func TestCreateJobInFolder(t *testing.T) {
266334
}
267335

268336
func TestGetFolder(t *testing.T) {
337+
if _, ok := os.LookupEnv(integration_test); !ok {
338+
return
339+
}
269340
ctx := context.Background()
270341
folder1ID := "folder1_test"
271342
folder2ID := "folder2_test"
@@ -281,6 +352,9 @@ func TestGetFolder(t *testing.T) {
281352
assert.Equal(t, folder2ID, folder2.GetName())
282353
}
283354
func TestInstallPlugin(t *testing.T) {
355+
if _, ok := os.LookupEnv(integration_test); !ok {
356+
return
357+
}
284358
ctx := context.Background()
285359

286360
err := jenkins.InstallPlugin(ctx, "packer", "1.4")
@@ -289,6 +363,9 @@ func TestInstallPlugin(t *testing.T) {
289363
}
290364

291365
func TestConcurrentRequests(t *testing.T) {
366+
if _, ok := os.LookupEnv(integration_test); !ok {
367+
return
368+
}
292369
ctx := context.Background()
293370
for i := 0; i <= 16; i++ {
294371
go func() {

0 commit comments

Comments
 (0)