@@ -24,10 +24,15 @@ import (
24
24
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
25
25
cftutils "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
26
26
"github.com/stretchr/testify/assert"
27
- "github.com/terraform-google-modules/terraform-google-bootstrap/test/integration/utils"
28
27
"github.com/xanzy/go-gitlab"
29
28
)
30
29
30
+ const (
31
+ gitlabProjectName = "cb-repo-conn-gl"
32
+ gitlabGroup = "infrastructure-manager"
33
+ gitlabGroupID = 84326276
34
+ )
35
+
31
36
type GitLabClient struct {
32
37
t * testing.T
33
38
client * gitlab.Client
@@ -37,7 +42,7 @@ type GitLabClient struct {
37
42
project * gitlab.Project
38
43
}
39
44
40
- func NewGitLabClient (t * testing.T , token , owner , repo string ) * GitLabClient {
45
+ func NewGitLabClient (t * testing.T , token string ) * GitLabClient {
41
46
t .Helper ()
42
47
client , err := gitlab .NewClient (token )
43
48
if err != nil {
@@ -46,9 +51,9 @@ func NewGitLabClient(t *testing.T, token, owner, repo string) *GitLabClient {
46
51
return & GitLabClient {
47
52
t : t ,
48
53
client : client ,
49
- group : "infrastructure-manager" ,
50
- namespace : 84326276 ,
51
- repo : repo ,
54
+ group : gitlabGroup ,
55
+ namespace : gitlabGroupID ,
56
+ repo : gitlabProjectName ,
52
57
}
53
58
}
54
59
@@ -65,71 +70,23 @@ func (gl *GitLabClient) GetProject() *gitlab.Project {
65
70
return proj
66
71
}
67
72
68
- func (gl * GitLabClient ) CreateProject () {
69
- opts := & gitlab.CreateProjectOptions {
70
- Name : gitlab .Ptr (gl .repo ),
71
- // ID of the the Infrastructure Manager group (gitlab.com/infrastructure-manager)
72
- NamespaceID : gitlab .Ptr (gl .namespace ),
73
- // Required otherwise Cloud Build errors on creating the connection
74
- InitializeWithReadme : gitlab .Ptr (true ),
75
- }
76
- proj , _ , err := gl .client .Projects .CreateProject (opts )
77
- if err != nil {
78
- gl .t .Fatal (err .Error ())
79
- }
80
- gl .project = proj
81
- }
82
-
83
- func (gl * GitLabClient ) AddFileToProject (file []byte ) {
84
- opts := & gitlab.CreateFileOptions {
85
- Branch : gitlab .Ptr ("main" ),
86
- CommitMessage : gitlab .Ptr ("Initial config commit" ),
87
- Content : gitlab .Ptr (string (file )),
88
- }
89
- _ , _ , err := gl .client .RepositoryFiles .CreateFile (gl .ProjectName (), "main.tf" , opts )
90
- if err != nil {
91
- gl .t .Fatal (err .Error ())
92
- }
93
- }
94
-
95
- func (gl * GitLabClient ) DeleteProject () {
96
- resp , err := gl .client .Projects .DeleteProject (gl .ProjectName (), utils .GetDeleteProjectOptions ())
97
- if resp .StatusCode != 404 && err != nil {
98
- gl .t .Errorf ("error deleting project with status %s and error %s" , resp .Status , err .Error ())
99
- }
100
- gl .project = nil
101
- }
102
-
103
73
func TestCloudBuildRepoConnectionGitLab (t * testing.T ) {
104
- repoName := fmt .Sprintf ("conn-gl-%s" , utils .GetRandomStringFromSetup (t ))
105
74
gitlabPAT := cftutils .ValFromEnv (t , "IM_GITLAB_PAT" )
106
- owner := "infrastructure-manager"
107
-
108
- client := NewGitLabClient (t , gitlabPAT , owner , repoName )
109
- proj := client .GetProject ()
110
- if proj == nil {
111
- client .CreateProject ()
112
- }
75
+ client := NewGitLabClient (t , gitlabPAT )
76
+ client .GetProject ()
113
77
114
78
resourcesLocation := "us-central1"
115
79
vars := map [string ]interface {}{
116
80
"gitlab_read_authorizer_credential" : gitlabPAT ,
117
81
"gitlab_authorizer_credential" : gitlabPAT ,
118
- "repository_name" : repoName ,
82
+ "repository_name" : gitlabProjectName ,
119
83
"repository_url" : client .project .HTTPURLToRepo ,
120
84
}
121
85
bpt := tft .NewTFBlueprintTest (t , tft .WithVars (vars ))
122
86
123
87
bpt .DefineVerify (func (assert * assert.Assertions ) {
124
88
bpt .DefaultVerify (assert )
125
89
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
90
// validate if repository was created using the connection
134
91
projectId := bpt .GetTFSetupStringOutput ("project_id" )
135
92
connectionId := bpt .GetStringOutput ("cloud_build_repositories_2nd_gen_connection" )
@@ -148,15 +105,14 @@ func TestCloudBuildRepoConnectionGitLab(t *testing.T) {
148
105
assert .Equal (projectId , connectionProjectId , "Connection project id should be the same as input project id." )
149
106
assert .Equal (resourcesLocation , connectionLocation , fmt .Sprintf ("Connection location should be '%s'." , resourcesLocation ))
150
107
151
- repository := gcloud .Runf (t , "builds repositories describe %s --project %s --region %s --connection %s" , repoName , projectId , resourcesLocation , connectionName )
108
+ repository := gcloud .Runf (t , "builds repositories describe %s --project %s --region %s --connection %s" , gitlabProjectName , projectId , resourcesLocation , connectionName )
152
109
153
110
assert .Equal (client .project .HTTPURLToRepo , repository .Get ("remoteUri" ).String (), "Git clone URL must be the same on the created resource." )
154
111
})
155
112
156
113
bpt .DefineTeardown (func (assert * assert.Assertions ) {
157
114
// Guarantee clean up even if the normal gcloud/teardown run into errors
158
115
t .Cleanup (func () {
159
- client .DeleteProject ()
160
116
bpt .DefaultTeardown (assert )
161
117
})
162
118
})
0 commit comments