6
6
"context"
7
7
"errors"
8
8
"fmt"
9
+ "regexp"
9
10
"sort"
10
11
"strconv"
11
12
"testing"
@@ -126,13 +127,47 @@ var testBuildDefinition = build.BuildDefinition{
126
127
VariableGroups : & []build.VariableGroup {},
127
128
}
128
129
130
+ // This definition matches the overall structure of what a configured Bitbucket git repository would
131
+ // look like.
132
+ func testBuildDefinitionBitbucket () build.BuildDefinition {
133
+ return build.BuildDefinition {
134
+ Id : converter .Int (100 ),
135
+ Revision : converter .Int (1 ),
136
+ Name : converter .String ("Name" ),
137
+ Path : converter .String ("\\ " ),
138
+ Repository : & build.BuildRepository {
139
+ Url : converter .String ("https://bitbucket.com/RepoId.git" ),
140
+ Id : converter .String ("RepoId" ),
141
+ Name : converter .String ("RepoId" ),
142
+ DefaultBranch : converter .String ("RepoBranchName" ),
143
+ Type : converter .String ("Bitbucket" ),
144
+ Properties : & map [string ]string {
145
+ "connectedServiceId" : "ServiceConnectionID" ,
146
+ },
147
+ },
148
+ Process : & build.YamlProcess {
149
+ YamlFilename : converter .String ("YamlFilename" ),
150
+ },
151
+ Queue : & build.AgentPoolQueue {
152
+ Name : converter .String ("BuildPoolName" ),
153
+ Pool : & build.TaskAgentPoolReference {
154
+ Name : converter .String ("BuildPoolName" ),
155
+ },
156
+ },
157
+ QueueStatus : & build .DefinitionQueueStatusValues .Enabled ,
158
+ Type : & build .DefinitionTypeValues .Build ,
159
+ Quality : & build .DefinitionQualityValues .Definition ,
160
+ VariableGroups : & []build.VariableGroup {},
161
+ }
162
+ }
163
+
129
164
/**
130
165
* Begin unit tests
131
166
*/
132
167
133
168
// validates that all supported repo types are allowed by the schema
134
169
func TestAzureDevOpsBuildDefinition_RepoTypeListIsCorrect (t * testing.T ) {
135
- expectedRepoTypes := []string {"GitHub" , "TfsGit" }
170
+ expectedRepoTypes := []string {"GitHub" , "TfsGit" , "Bitbucket" }
136
171
repoSchema := resourceBuildDefinition ().Schema ["repository" ]
137
172
repoTypeSchema := repoSchema .Elem .(* schema.Resource ).Schema ["repo_type" ]
138
173
@@ -160,6 +195,50 @@ func TestAzureDevOpsBuildDefinition_PathInvalidStartingSlashIsError(t *testing.T
160
195
require .Equal (t , "path must start with backslash" , errors [0 ].Error ())
161
196
}
162
197
198
+ // verifies that GitHub repo urls are expanded to URLs Azure DevOps expects
199
+ func TestAzureDevOpsBuildDefinition_Expand_RepoUrl_Github (t * testing.T ) {
200
+ resourceData := schema .TestResourceDataRaw (t , resourceBuildDefinition ().Schema , nil )
201
+ flattenBuildDefinition (resourceData , & testBuildDefinition , testProjectID )
202
+ buildDefinitionAfterRoundTrip , projectID , err := expandBuildDefinition (resourceData )
203
+
204
+ require .Nil (t , err )
205
+ require .Equal (t , * buildDefinitionAfterRoundTrip .Repository .Url , "https://github.com/RepoId.git" )
206
+ require .Equal (t , testProjectID , projectID )
207
+ }
208
+
209
+ // verifies that Bitbucket repo urls are expanded to URLs Azure DevOps expects
210
+ func TestAzureDevOpsBuildDefinition_Expand_RepoUrl_Bitbucket (t * testing.T ) {
211
+ resourceData := schema .TestResourceDataRaw (t , resourceBuildDefinition ().Schema , nil )
212
+ bitBucketBuildDef := testBuildDefinitionBitbucket ()
213
+ flattenBuildDefinition (resourceData , & bitBucketBuildDef , testProjectID )
214
+ buildDefinitionAfterRoundTrip , projectID , err := expandBuildDefinition (resourceData )
215
+
216
+ require .Nil (t , err )
217
+ require .Equal (t , * buildDefinitionAfterRoundTrip .Repository .Url , "https://bitbucket.org/RepoId.git" )
218
+ require .Equal (t , testProjectID , projectID )
219
+ }
220
+
221
+ // verifies that a service connection is required for bitbucket repos
222
+ func TestAzureDevOpsBuildDefinition_ValidatesServiceConnection_Bitbucket (t * testing.T ) {
223
+ resourceData := schema .TestResourceDataRaw (t , resourceBuildDefinition ().Schema , nil )
224
+ bitBucketBuildDef := testBuildDefinitionBitbucket ()
225
+ (* bitBucketBuildDef .Repository .Properties )["connectedServiceId" ] = ""
226
+ flattenBuildDefinition (resourceData , & bitBucketBuildDef , testProjectID )
227
+
228
+ ctrl := gomock .NewController (t )
229
+ defer ctrl .Finish ()
230
+ buildClient := azdosdkmocks .NewMockBuildClient (ctrl )
231
+ clients := & config.AggregatedClient {BuildClient : buildClient , Ctx : context .Background ()}
232
+
233
+ err := resourceBuildDefinitionCreate (resourceData , clients )
234
+ require .NotNil (t , err )
235
+ require .Contains (t , err .Error (), "bitbucket repositories need a referenced service connection ID" )
236
+
237
+ err = resourceBuildDefinitionUpdate (resourceData , clients )
238
+ require .NotNil (t , err )
239
+ require .Contains (t , err .Error (), "bitbucket repositories need a referenced service connection ID" )
240
+ }
241
+
163
242
// verifies that the flatten/expand round trip yields the same build definition
164
243
func TestAzureDevOpsBuildDefinition_ExpandFlatten_Roundtrip (t * testing.T ) {
165
244
resourceData := schema .TestResourceDataRaw (t , resourceBuildDefinition ().Schema , nil )
@@ -281,7 +360,7 @@ func TestAzureDevOpsBuildDefinition_Update_DoesNotSwallowError(t *testing.T) {
281
360
282
361
// validates that an apply followed by another apply (i.e., resource update) will be reflected in AzDO and the
283
362
// underlying terraform state.
284
- func TestAccAzureDevOpsBuildDefinition_CreateAndUpdate (t * testing.T ) {
363
+ func TestAccAzureDevOpsBuildDefinition_Create_Update_Import (t * testing.T ) {
285
364
projectName := testhelper .TestAccResourcePrefix + acctest .RandStringFromCharSet (10 , acctest .CharSetAlphaNum )
286
365
buildDefinitionPathEmpty := `\`
287
366
buildDefinitionNameFirst := testhelper .TestAccResourcePrefix + acctest .RandStringFromCharSet (10 , acctest .CharSetAlphaNum )
@@ -300,7 +379,7 @@ func TestAccAzureDevOpsBuildDefinition_CreateAndUpdate(t *testing.T) {
300
379
CheckDestroy : testAccBuildDefinitionCheckDestroy ,
301
380
Steps : []resource.TestStep {
302
381
{
303
- Config : testhelper .TestAccBuildDefinitionResource (projectName , buildDefinitionNameFirst , buildDefinitionPathEmpty ),
382
+ Config : testhelper .TestAccBuildDefinitionResourceGitHub (projectName , buildDefinitionNameFirst , buildDefinitionPathEmpty ),
304
383
Check : resource .ComposeTestCheckFunc (
305
384
testAccCheckBuildDefinitionResourceExists (buildDefinitionNameFirst ),
306
385
resource .TestCheckResourceAttrSet (tfBuildDefNode , "project_id" ),
@@ -309,7 +388,7 @@ func TestAccAzureDevOpsBuildDefinition_CreateAndUpdate(t *testing.T) {
309
388
resource .TestCheckResourceAttr (tfBuildDefNode , "path" , buildDefinitionPathEmpty ),
310
389
),
311
390
}, {
312
- Config : testhelper .TestAccBuildDefinitionResource (projectName , buildDefinitionNameSecond , buildDefinitionPathEmpty ),
391
+ Config : testhelper .TestAccBuildDefinitionResourceGitHub (projectName , buildDefinitionNameSecond , buildDefinitionPathEmpty ),
313
392
Check : resource .ComposeTestCheckFunc (
314
393
testAccCheckBuildDefinitionResourceExists (buildDefinitionNameSecond ),
315
394
resource .TestCheckResourceAttrSet (tfBuildDefNode , "project_id" ),
@@ -318,7 +397,7 @@ func TestAccAzureDevOpsBuildDefinition_CreateAndUpdate(t *testing.T) {
318
397
resource .TestCheckResourceAttr (tfBuildDefNode , "path" , buildDefinitionPathEmpty ),
319
398
),
320
399
}, {
321
- Config : testhelper .TestAccBuildDefinitionResource (projectName , buildDefinitionNameFirst , buildDefinitionPathFirst ),
400
+ Config : testhelper .TestAccBuildDefinitionResourceGitHub (projectName , buildDefinitionNameFirst , buildDefinitionPathFirst ),
322
401
Check : resource .ComposeTestCheckFunc (
323
402
testAccCheckBuildDefinitionResourceExists (buildDefinitionNameFirst ),
324
403
resource .TestCheckResourceAttrSet (tfBuildDefNode , "project_id" ),
@@ -327,7 +406,7 @@ func TestAccAzureDevOpsBuildDefinition_CreateAndUpdate(t *testing.T) {
327
406
resource .TestCheckResourceAttr (tfBuildDefNode , "path" , buildDefinitionPathFirst ),
328
407
),
329
408
}, {
330
- Config : testhelper .TestAccBuildDefinitionResource (projectName , buildDefinitionNameFirst ,
409
+ Config : testhelper .TestAccBuildDefinitionResourceGitHub (projectName , buildDefinitionNameFirst ,
331
410
buildDefinitionPathSecond ),
332
411
Check : resource .ComposeTestCheckFunc (
333
412
testAccCheckBuildDefinitionResourceExists (buildDefinitionNameFirst ),
@@ -337,7 +416,7 @@ func TestAccAzureDevOpsBuildDefinition_CreateAndUpdate(t *testing.T) {
337
416
resource .TestCheckResourceAttr (tfBuildDefNode , "path" , buildDefinitionPathSecond ),
338
417
),
339
418
}, {
340
- Config : testhelper .TestAccBuildDefinitionResource (projectName , buildDefinitionNameFirst , buildDefinitionPathThird ),
419
+ Config : testhelper .TestAccBuildDefinitionResourceGitHub (projectName , buildDefinitionNameFirst , buildDefinitionPathThird ),
341
420
Check : resource .ComposeTestCheckFunc (
342
421
testAccCheckBuildDefinitionResourceExists (buildDefinitionNameFirst ),
343
422
resource .TestCheckResourceAttrSet (tfBuildDefNode , "project_id" ),
@@ -346,7 +425,7 @@ func TestAccAzureDevOpsBuildDefinition_CreateAndUpdate(t *testing.T) {
346
425
resource .TestCheckResourceAttr (tfBuildDefNode , "path" , buildDefinitionPathThird ),
347
426
),
348
427
}, {
349
- Config : testhelper .TestAccBuildDefinitionResource (projectName , buildDefinitionNameFirst , buildDefinitionPathFourth ),
428
+ Config : testhelper .TestAccBuildDefinitionResourceGitHub (projectName , buildDefinitionNameFirst , buildDefinitionPathFourth ),
350
429
Check : resource .ComposeTestCheckFunc (
351
430
testAccCheckBuildDefinitionResourceExists (buildDefinitionNameFirst ),
352
431
resource .TestCheckResourceAttrSet (tfBuildDefNode , "project_id" ),
@@ -365,6 +444,25 @@ func TestAccAzureDevOpsBuildDefinition_CreateAndUpdate(t *testing.T) {
365
444
})
366
445
}
367
446
447
+ // Verifies a build for Bitbucket can happen. Note: the update/import logic is tested in other tests
448
+ func TestAccAzureDevOpsBuildDefinitionBitbucket_Create (t * testing.T ) {
449
+ projectName := testhelper .TestAccResourcePrefix + acctest .RandStringFromCharSet (10 , acctest .CharSetAlphaNum )
450
+ resource .Test (t , resource.TestCase {
451
+ PreCheck : func () { testhelper .TestAccPreCheck (t , nil ) },
452
+ Providers : testAccProviders ,
453
+ CheckDestroy : testAccBuildDefinitionCheckDestroy ,
454
+ Steps : []resource.TestStep {
455
+ {
456
+ Config : testhelper .TestAccBuildDefinitionResourceBitbucket (projectName , "build-def-name" , "\\ " , "" ),
457
+ ExpectError : regexp .MustCompile ("bitbucket repositories need a referenced service connection ID" ),
458
+ }, {
459
+ Config : testhelper .TestAccBuildDefinitionResourceBitbucket (projectName , "build-def-name" , "\\ " , "some-service-connection" ),
460
+ Check : testAccCheckBuildDefinitionResourceExists ("build-def-name" ),
461
+ },
462
+ },
463
+ })
464
+ }
465
+
368
466
// Given the name of an AzDO build definition, this will return a function that will check whether
369
467
// or not the definition (1) exists in the state and (2) exist in AzDO and (3) has the correct name
370
468
func testAccCheckBuildDefinitionResourceExists (expectedName string ) resource.TestCheckFunc {
0 commit comments