@@ -27,109 +27,27 @@ import (
2727 "github.com/gruntwork-io/terratest/modules/logger"
2828 "github.com/stretchr/testify/assert"
2929 "github.com/terraform-google-modules/terraform-google-bootstrap/test/integration/utils"
30- "github.com/xanzy/go-gitlab"
3130)
3231
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+ )
10435
10536func TestCloudBuildWorkspaceSimpleGitLab (t * testing.T ) {
106- repoName := fmt .Sprintf ("cb-bp-gl-%s" , utils .GetRandomStringFromSetup (t ))
10737 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 ()
11540
11641 vars := map [string ]interface {}{
11742 "gitlab_authorizer_credential" : gitlabPAT ,
11843 "gitlab_read_authorizer_credential" : gitlabPAT ,
119- "repository_uri" : client .project .HTTPURLToRepo ,
44+ "repository_uri" : client .Project .HTTPURLToRepo ,
12045 }
12146 bpt := tft .NewTFBlueprintTest (t , tft .WithVars (vars ))
12247
12348 bpt .DefineVerify (func (assert * assert.Assertions ) {
12449 bpt .DefaultVerify (assert )
12550
126- t .Cleanup (func () {
127- // Delete the repository if we hit a failed state
128- if t .Failed () {
129- client .DeleteProject ()
130- }
131- })
132-
13351 location := bpt .GetStringOutput ("location" )
13452 projectID := bpt .GetStringOutput ("project_id" )
13553
@@ -138,8 +56,8 @@ func TestCloudBuildWorkspaceSimpleGitLab(t *testing.T) {
13856 for _ , trigger := range triggers {
13957 triggerOP := utils .LastElement (bpt .GetStringOutput (fmt .Sprintf ("cloudbuild_%s_trigger_id" , trigger )), "/" )
14058 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" )
14361 }
14462
14563 // artifacts, state and log buckets
@@ -174,7 +92,7 @@ func TestCloudBuildWorkspaceSimpleGitLab(t *testing.T) {
17492 }
17593 }
17694
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 )
17896 gitRun (
"config" ,
"user.email" ,
"[email protected] " )
17997 gitRun ("config" , "user.name" , "TF Robot" )
18098
@@ -188,7 +106,7 @@ func TestCloudBuildWorkspaceSimpleGitLab(t *testing.T) {
188106 git .RunCmdE ("checkout" , "-b" , branch )
189107 }
190108 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" )
192110 lastCommit := git .GetLatestCommit ()
193111 // filter builds triggered based on pushed commit sha
194112 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) {
229147 bpt .DefineTeardown (func (assert * assert.Assertions ) {
230148 // Guarantee clean up even if the normal gcloud/teardown run into errors
231149 t .Cleanup (func () {
232- client .DeleteProject ()
233150 bpt .DefaultTeardown (assert )
234151 })
235152 })
0 commit comments