@@ -27,109 +27,27 @@ import (
27
27
"github.com/gruntwork-io/terratest/modules/logger"
28
28
"github.com/stretchr/testify/assert"
29
29
"github.com/terraform-google-modules/terraform-google-bootstrap/test/integration/utils"
30
- "github.com/xanzy/go-gitlab"
31
30
)
32
31
33
- type GitLabClient struct {
34
- t * testing.T
35
- client * gitlab.Client
36
- group string
37
- namespace int
38
- repo string
39
- project * gitlab.Project
40
- }
41
-
42
- func NewGitLabClient (t * testing.T , token , owner , repo string ) * GitLabClient {
43
- t .Helper ()
44
- client , err := gitlab .NewClient (token )
45
- if err != nil {
46
- t .Fatal (err .Error ())
47
- }
48
- return & GitLabClient {
49
- t : t ,
50
- client : client ,
51
- group : "infrastructure-manager" ,
52
- namespace : 84326276 ,
53
- repo : repo ,
54
- }
55
- }
56
-
57
- func (gl * GitLabClient ) ProjectName () string {
58
- return fmt .Sprintf ("%s/%s" , gl .group , gl .repo )
59
- }
60
-
61
- func (gl * GitLabClient ) GetProject () * gitlab.Project {
62
- proj , resp , err := gl .client .Projects .GetProject (gl .ProjectName (), nil )
63
- if resp .StatusCode != 404 && err != nil {
64
- gl .t .Fatalf ("got status code %d, error %s" , resp .StatusCode , err .Error ())
65
- }
66
- gl .project = proj
67
- return proj
68
- }
69
-
70
- func (gl * GitLabClient ) CreateProject () {
71
- opts := & gitlab.CreateProjectOptions {
72
- Name : gitlab .Ptr (gl .repo ),
73
- // ID of the the Infrastructure Manager group (gitlab.com/infrastructure-manager)
74
- NamespaceID : gitlab .Ptr (gl .namespace ),
75
- // Required otherwise Cloud Build errors on creating the connection
76
- InitializeWithReadme : gitlab .Ptr (true ),
77
- }
78
- proj , _ , err := gl .client .Projects .CreateProject (opts )
79
- if err != nil {
80
- gl .t .Fatal (err .Error ())
81
- }
82
- gl .project = proj
83
- }
84
-
85
- func (gl * GitLabClient ) AddFileToProject (file []byte ) {
86
- opts := & gitlab.CreateFileOptions {
87
- Branch : gitlab .Ptr ("main" ),
88
- CommitMessage : gitlab .Ptr ("Initial config commit" ),
89
- Content : gitlab .Ptr (string (file )),
90
- }
91
- _ , _ , err := gl .client .RepositoryFiles .CreateFile (gl .ProjectName (), "main.tf" , opts )
92
- if err != nil {
93
- gl .t .Fatal (err .Error ())
94
- }
95
- }
96
-
97
- func (gl * GitLabClient ) DeleteProject () {
98
- resp , err := gl .client .Projects .DeleteProject (gl .ProjectName (), utils .GetDeleteProjectOptions ())
99
- if resp .StatusCode != 404 && err != nil {
100
- gl .t .Errorf ("error deleting project with status %s and error %s" , resp .Status , err .Error ())
101
- }
102
- gl .project = nil
103
- }
32
+ const (
33
+ gitlabProjectName = "cb-bp-gl"
34
+ )
104
35
105
36
func TestCloudBuildWorkspaceSimpleGitLab (t * testing.T ) {
106
- repoName := fmt .Sprintf ("cb-bp-gl-%s" , utils .GetRandomStringFromSetup (t ))
107
37
gitlabPAT := cftutils .ValFromEnv (t , "IM_GITLAB_PAT" )
108
- owner := "infrastructure-manager"
109
-
110
- client := NewGitLabClient (t , gitlabPAT , owner , repoName )
111
- proj := client .GetProject ()
112
- if proj == nil {
113
- client .CreateProject ()
114
- }
38
+ client := utils .NewGitLabClient (t , gitlabPAT , gitlabProjectName )
39
+ client .GetProject ()
115
40
116
41
vars := map [string ]interface {}{
117
42
"gitlab_authorizer_credential" : gitlabPAT ,
118
43
"gitlab_read_authorizer_credential" : gitlabPAT ,
119
- "repository_uri" : client .project .HTTPURLToRepo ,
44
+ "repository_uri" : client .Project .HTTPURLToRepo ,
120
45
}
121
46
bpt := tft .NewTFBlueprintTest (t , tft .WithVars (vars ))
122
47
123
48
bpt .DefineVerify (func (assert * assert.Assertions ) {
124
49
bpt .DefaultVerify (assert )
125
50
126
- t .Cleanup (func () {
127
- // Delete the repository if we hit a failed state
128
- if t .Failed () {
129
- client .DeleteProject ()
130
- }
131
- })
132
-
133
51
location := bpt .GetStringOutput ("location" )
134
52
projectID := bpt .GetStringOutput ("project_id" )
135
53
@@ -138,8 +56,8 @@ func TestCloudBuildWorkspaceSimpleGitLab(t *testing.T) {
138
56
for _ , trigger := range triggers {
139
57
triggerOP := utils .LastElement (bpt .GetStringOutput (fmt .Sprintf ("cloudbuild_%s_trigger_id" , trigger )), "/" )
140
58
cloudBuildOP := gcloud .Runf (t , "beta builds triggers describe %s --region %s --project %s" , triggerOP , location , projectID )
141
- assert .Equal (fmt .Sprintf ("%s-%s" , repoName , trigger ), cloudBuildOP .Get ("name" ).String (), "should have the correct name" )
142
- assert .Equal (fmt .Sprintf ("projects/%s/serviceAccounts/tf-cb-%s@%s.iam.gserviceaccount.com" , projectID , repoName , projectID ), cloudBuildOP .Get ("serviceAccount" ).String (), "uses expected SA" )
59
+ assert .Equal (fmt .Sprintf ("%s-%s" , gitlabProjectName , trigger ), cloudBuildOP .Get ("name" ).String (), "should have the correct name" )
60
+ assert .Equal (fmt .Sprintf ("projects/%s/serviceAccounts/tf-cb-%s@%s.iam.gserviceaccount.com" , projectID , gitlabProjectName , projectID ), cloudBuildOP .Get ("serviceAccount" ).String (), "uses expected SA" )
143
61
}
144
62
145
63
// artifacts, state and log buckets
@@ -174,7 +92,7 @@ func TestCloudBuildWorkspaceSimpleGitLab(t *testing.T) {
174
92
}
175
93
}
176
94
177
- gitRun (
"clone" ,
fmt .
Sprintf (
"https://gitlab-bot:%[email protected] /%s/%s" ,
gitlabPAT ,
owner , repoName ),
tmpDir )
95
+ gitRun (
"clone" ,
fmt .
Sprintf (
"https://gitlab-bot:%[email protected] /%s/%s" ,
gitlabPAT ,
utils . GitlabGroup , gitlabProjectName ),
tmpDir )
178
96
gitRun (
"config" ,
"user.email" ,
"[email protected] " )
179
97
gitRun ("config" , "user.name" , "TF Robot" )
180
98
@@ -188,7 +106,7 @@ func TestCloudBuildWorkspaceSimpleGitLab(t *testing.T) {
188
106
git .RunCmdE ("checkout" , "-b" , branch )
189
107
}
190
108
git .CommitWithMsg (fmt .Sprintf ("%s commit" , branch ), []string {"--allow-empty" })
191
- gitRun (
"push" ,
"-u" ,
fmt .
Sprintf (
"https://gitlab-bot:%[email protected] /%s/%s.git" ,
gitlabPAT ,
owner , repoName ),
branch ,
"-f" )
109
+ gitRun (
"push" ,
"-u" ,
fmt .
Sprintf (
"https://gitlab-bot:%[email protected] /%s/%s.git" ,
gitlabPAT ,
utils . GitlabGroup , gitlabProjectName ),
branch ,
"-f" )
192
110
lastCommit := git .GetLatestCommit ()
193
111
// filter builds triggered based on pushed commit sha
194
112
buildListCmd := fmt .Sprintf ("builds list --filter substitutions.COMMIT_SHA='%s' --project %s --region %s --limit 1 --sort-by ~createTime" , lastCommit , projectID , location )
@@ -229,7 +147,6 @@ func TestCloudBuildWorkspaceSimpleGitLab(t *testing.T) {
229
147
bpt .DefineTeardown (func (assert * assert.Assertions ) {
230
148
// Guarantee clean up even if the normal gcloud/teardown run into errors
231
149
t .Cleanup (func () {
232
- client .DeleteProject ()
233
150
bpt .DefaultTeardown (assert )
234
151
})
235
152
})
0 commit comments