@@ -24,10 +24,15 @@ import (
2424 "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
2525 cftutils "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
2626 "github.com/stretchr/testify/assert"
27- "github.com/terraform-google-modules/terraform-google-bootstrap/test/integration/utils"
2827 "github.com/xanzy/go-gitlab"
2928)
3029
30+ const (
31+ gitlabProjectName = "cb-repo-conn-gl"
32+ gitlabGroup = "infrastructure-manager"
33+ gitlabGroupID = 84326276
34+ )
35+
3136type GitLabClient struct {
3237 t * testing.T
3338 client * gitlab.Client
@@ -37,7 +42,7 @@ type GitLabClient struct {
3742 project * gitlab.Project
3843}
3944
40- func NewGitLabClient (t * testing.T , token , owner , repo string ) * GitLabClient {
45+ func NewGitLabClient (t * testing.T , token string ) * GitLabClient {
4146 t .Helper ()
4247 client , err := gitlab .NewClient (token )
4348 if err != nil {
@@ -46,9 +51,9 @@ func NewGitLabClient(t *testing.T, token, owner, repo string) *GitLabClient {
4651 return & GitLabClient {
4752 t : t ,
4853 client : client ,
49- group : "infrastructure-manager" ,
50- namespace : 84326276 ,
51- repo : repo ,
54+ group : gitlabGroup ,
55+ namespace : gitlabGroupID ,
56+ repo : gitlabProjectName ,
5257 }
5358}
5459
@@ -65,71 +70,23 @@ func (gl *GitLabClient) GetProject() *gitlab.Project {
6570 return proj
6671}
6772
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-
10373func TestCloudBuildRepoConnectionGitLab (t * testing.T ) {
104- repoName := fmt .Sprintf ("conn-gl-%s" , utils .GetRandomStringFromSetup (t ))
10574 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 ()
11377
11478 resourcesLocation := "us-central1"
11579 vars := map [string ]interface {}{
11680 "gitlab_read_authorizer_credential" : gitlabPAT ,
11781 "gitlab_authorizer_credential" : gitlabPAT ,
118- "repository_name" : repoName ,
82+ "repository_name" : gitlabProjectName ,
11983 "repository_url" : client .project .HTTPURLToRepo ,
12084 }
12185 bpt := tft .NewTFBlueprintTest (t , tft .WithVars (vars ))
12286
12387 bpt .DefineVerify (func (assert * assert.Assertions ) {
12488 bpt .DefaultVerify (assert )
12589
126- t .Cleanup (func () {
127- // Delete the repository if we hit a failed state
128- if t .Failed () {
129- client .DeleteProject ()
130- }
131- })
132-
13390 // validate if repository was created using the connection
13491 projectId := bpt .GetTFSetupStringOutput ("project_id" )
13592 connectionId := bpt .GetStringOutput ("cloud_build_repositories_2nd_gen_connection" )
@@ -148,15 +105,14 @@ func TestCloudBuildRepoConnectionGitLab(t *testing.T) {
148105 assert .Equal (projectId , connectionProjectId , "Connection project id should be the same as input project id." )
149106 assert .Equal (resourcesLocation , connectionLocation , fmt .Sprintf ("Connection location should be '%s'." , resourcesLocation ))
150107
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 )
152109
153110 assert .Equal (client .project .HTTPURLToRepo , repository .Get ("remoteUri" ).String (), "Git clone URL must be the same on the created resource." )
154111 })
155112
156113 bpt .DefineTeardown (func (assert * assert.Assertions ) {
157114 // Guarantee clean up even if the normal gcloud/teardown run into errors
158115 t .Cleanup (func () {
159- client .DeleteProject ()
160116 bpt .DefaultTeardown (assert )
161117 })
162118 })
0 commit comments