Skip to content

Commit 0fb002e

Browse files
authored
chore: Update CB workspace GL test to use existing repo (#374)
1 parent 2f01d9e commit 0fb002e

File tree

3 files changed

+24
-97
lines changed

3 files changed

+24
-97
lines changed

examples/tf_cloudbuild_workspace_simple_gitlab/scripts/push-to-repo.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,18 @@ git config user.email "[email protected]"
4040
git config user.name "TF Robot"
4141
git checkout plan || git checkout -b plan
4242
git add -A
43-
git commit -m "init tf configs"
44-
git push origin plan -f
43+
44+
# The '-z' flag checks if the following string is empty.
45+
if [ -z "$(git status --porcelain)" ]; then
46+
# If the output is empty, the working directory is clean.
47+
echo "No changes to commit. Nothing to do."
48+
else
49+
# If there is output, changes exist, so we commit.
50+
echo "Changes detected. Attempting to commit..."
51+
git commit -m "init tf configs"
52+
git push origin plan -f
53+
fi
54+
4555
sleep 60
4656
git checkout main || git checkout -b main
4757
git merge plan

test/integration/tf_cloudbuild_workspace_simple_gitlab/tf_cloudbuild_workspace_simple_gitlab_test.go

Lines changed: 10 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -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

10536
func 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
})

test/integration/utils/gitlab_client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
)
2323

2424
const (
25-
gitlabGroup = "infrastructure-manager"
25+
GitlabGroup = "infrastructure-manager"
2626
gitlabGroupID = 84326276
2727
)
2828

@@ -44,7 +44,7 @@ func NewGitLabClient(t *testing.T, token, projectName string) *GitLabClient {
4444
return &GitLabClient{
4545
t: t,
4646
client: client,
47-
group: gitlabGroup,
47+
group: GitlabGroup,
4848
namespace: gitlabGroupID,
4949
repo: projectName,
5050
}

0 commit comments

Comments
 (0)