Skip to content

Commit 3f7f7e3

Browse files
authored
fix: schematics upgrade refactor to tar file only (#916)
1 parent 8c35ed6 commit 3f7f7e3

File tree

6 files changed

+189
-192
lines changed

6 files changed

+189
-192
lines changed

.secrets.baseline

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": "go.sum|package-lock.json|^.secrets.baseline$",
44
"lines": null
55
},
6-
"generated_at": "2025-01-24T16:21:04Z",
6+
"generated_at": "2025-01-29T15:25:24Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"
@@ -164,7 +164,23 @@
164164
"hashed_secret": "bff8f8143a073833d713e3c1821fe97661bc3cef",
165165
"is_secret": false,
166166
"is_verified": false,
167-
"line_number": 318,
167+
"line_number": 320,
168+
"type": "Secret Keyword",
169+
"verified_result": null
170+
},
171+
{
172+
"hashed_secret": "b4e929aa58c928e3e44d12e6f873f39cd8207a25",
173+
"is_secret": false,
174+
"is_verified": false,
175+
"line_number": 465,
176+
"type": "Secret Keyword",
177+
"verified_result": null
178+
},
179+
{
180+
"hashed_secret": "b994b23302ebc7b46888b0f5c623bfc2bcfa2e3f",
181+
"is_secret": false,
182+
"is_verified": false,
183+
"line_number": 478,
168184
"type": "Secret Keyword",
169185
"verified_result": null
170186
}
@@ -210,24 +226,6 @@
210226
"type": "Secret Keyword",
211227
"verified_result": null
212228
}
213-
],
214-
"testhelper/tests.go": [
215-
{
216-
"hashed_secret": "b4e929aa58c928e3e44d12e6f873f39cd8207a25",
217-
"is_secret": false,
218-
"is_verified": false,
219-
"line_number": 487,
220-
"type": "Secret Keyword",
221-
"verified_result": null
222-
},
223-
{
224-
"hashed_secret": "16282376ddaaaf2bf60be9041a7504280f3f338b",
225-
"is_secret": false,
226-
"is_verified": false,
227-
"line_number": 500,
228-
"type": "Secret Keyword",
229-
"verified_result": null
230-
}
231229
]
232230
},
233231
"version": "0.13.1+ibm.62.dss",

common/git.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"strings"
1212
"testing"
1313

14+
"github.com/go-git/go-git/v5"
15+
"github.com/go-git/go-git/v5/plumbing"
1416
"github.com/go-git/go-git/v5/plumbing/transport"
1517
"github.com/go-git/go-git/v5/plumbing/transport/http"
1618
gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
@@ -427,3 +429,68 @@ func SkipUpgradeTest(testing *testing.T, source_repo string, source_branch strin
427429

428430
return doNotRunUpgradeTest
429431
}
432+
433+
func CloneAndCheckoutBranch(testing *testing.T, repoURL string, branch string, cloneDir string) error {
434+
435+
authMethod, authErr := DetermineAuthMethod(repoURL)
436+
if authErr != nil {
437+
logger.Log(testing, "Failed to determine authentication method, trying without authentication...")
438+
439+
// Convert SSH URL to HTTPS URL
440+
if strings.HasPrefix(repoURL, "git@") {
441+
repoURL = strings.Replace(repoURL, ":", "/", 1)
442+
repoURL = strings.Replace(repoURL, "git@", "https://", 1)
443+
repoURL = strings.TrimSuffix(repoURL, ".git") + ".git"
444+
}
445+
446+
// Try to clone without authentication
447+
_, errUnauth := git.PlainClone(cloneDir, false, &git.CloneOptions{
448+
URL: repoURL,
449+
ReferenceName: plumbing.NewBranchReferenceName(branch),
450+
SingleBranch: true,
451+
})
452+
453+
if errUnauth != nil {
454+
// If unauthenticated clone fails and we cannot determine authentication, return the error from the unauthenticated approach
455+
return fmt.Errorf("failed to determine authentication method and clone base repo and branch without authentication: %v", errUnauth)
456+
} else {
457+
logger.Log(testing, "Cloned base repo and branch without authentication")
458+
}
459+
} else {
460+
// Authentication method determined, try with authentication
461+
_, errAuth := git.PlainClone(cloneDir, false, &git.CloneOptions{
462+
URL: repoURL,
463+
ReferenceName: plumbing.NewBranchReferenceName(branch),
464+
SingleBranch: true,
465+
Auth: authMethod,
466+
})
467+
468+
if errAuth != nil {
469+
logger.Log(testing, "Failed to clone base repo and branch with authentication, trying without authentication...")
470+
// Convert SSH URL to HTTPS URL
471+
if strings.HasPrefix(repoURL, "git@") {
472+
repoURL = strings.Replace(repoURL, ":", "/", 1)
473+
repoURL = strings.Replace(repoURL, "git@", "https://", 1)
474+
repoURL = strings.TrimSuffix(repoURL, ".git") + ".git"
475+
}
476+
477+
// Try to clone without authentication
478+
_, errUnauth := git.PlainClone(cloneDir, false, &git.CloneOptions{
479+
URL: repoURL,
480+
ReferenceName: plumbing.NewBranchReferenceName(branch),
481+
SingleBranch: true,
482+
})
483+
484+
if errUnauth != nil {
485+
// If unauthenticated clone also fails, return the error from the authenticated approach
486+
return fmt.Errorf("failed to clone base repo and branch with authentication: %v", errAuth)
487+
} else {
488+
logger.Log(testing, "Cloned base repo and branch without authentication")
489+
}
490+
} else {
491+
logger.Log(testing, "Cloned base repo and branch with authentication")
492+
}
493+
}
494+
495+
return nil
496+
}

testhelper/tests.go

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ import (
1313

1414
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/cloudinfo"
1515

16-
"github.com/go-git/go-git/v5/plumbing"
1716
"github.com/gruntwork-io/terratest/modules/files"
1817

19-
"github.com/go-git/go-git/v5"
2018
"github.com/gruntwork-io/terratest/modules/logger"
2119
"github.com/gruntwork-io/terratest/modules/terraform"
2220
"github.com/stretchr/testify/assert"
@@ -454,64 +452,9 @@ func (options *TestOptions) RunTestUpgrade() (*terraform.PlanStruct, error) {
454452
}
455453
}
456454

457-
authMethod, err := common.DetermineAuthMethod(baseRepo)
458-
if err != nil {
459-
logger.Log(options.Testing, "Failed to determine authentication method, trying without authentication...")
460-
461-
// Convert SSH URL to HTTPS URL
462-
if strings.HasPrefix(baseRepo, "git@") {
463-
baseRepo = strings.Replace(baseRepo, ":", "/", 1)
464-
baseRepo = strings.Replace(baseRepo, "git@", "https://", 1)
465-
baseRepo = strings.TrimSuffix(baseRepo, ".git") + ".git"
466-
}
467-
468-
// Try to clone without authentication
469-
_, errUnauth := git.PlainClone(baseTempDir, false, &git.CloneOptions{
470-
URL: baseRepo,
471-
ReferenceName: plumbing.NewBranchReferenceName(baseBranch),
472-
SingleBranch: true,
473-
})
474-
475-
if errUnauth != nil {
476-
// If unauthenticated clone fails and we cannot determine authentication, return the error from the unauthenticated approach
477-
return nil, fmt.Errorf("failed to determine authentication method and clone base repo and branch without authentication: %v", errUnauth)
478-
} else {
479-
logger.Log(options.Testing, "Cloned base repo and branch without authentication")
480-
}
481-
} else {
482-
// Authentication method determined, try with authentication
483-
_, errAuth := git.PlainClone(baseTempDir, false, &git.CloneOptions{
484-
URL: baseRepo,
485-
ReferenceName: plumbing.NewBranchReferenceName(baseBranch),
486-
SingleBranch: true,
487-
Auth: authMethod,
488-
})
489-
490-
if errAuth != nil {
491-
logger.Log(options.Testing, "Failed to clone base repo and branch with authentication, trying without authentication...")
492-
// Convert SSH URL to HTTPS URL
493-
if strings.HasPrefix(baseRepo, "git@") {
494-
baseRepo = strings.Replace(baseRepo, ":", "/", 1)
495-
baseRepo = strings.Replace(baseRepo, "git@", "https://", 1)
496-
baseRepo = strings.TrimSuffix(baseRepo, ".git") + ".git"
497-
}
498-
499-
// Try to clone without authentication
500-
_, errUnauth := git.PlainClone(baseTempDir, false, &git.CloneOptions{
501-
URL: baseRepo,
502-
ReferenceName: plumbing.NewBranchReferenceName(baseBranch),
503-
SingleBranch: true,
504-
})
505-
506-
if errUnauth != nil {
507-
// If unauthenticated clone also fails, return the error from the authenticated approach
508-
return nil, fmt.Errorf("failed to clone base repo and branch with authentication: %v", errAuth)
509-
} else {
510-
logger.Log(options.Testing, "Cloned base repo and branch without authentication")
511-
}
512-
} else {
513-
logger.Log(options.Testing, "Cloned base repo and branch with authentication")
514-
}
455+
cloneBaseErr := common.CloneAndCheckoutBranch(options.Testing, baseRepo, baseBranch, baseTempDir)
456+
if cloneBaseErr != nil {
457+
return nil, cloneBaseErr
515458
}
516459

517460
// Set TerraformDir to the appropriate directory within baseTempDir

testschematic/schematics.go

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ type SchematicsTestService struct {
8282
BaseTerraformRepoBranch string // the branch name of the main origin branch of the project (main or master), used for upgrade test
8383
TestTerraformRepo string // the URL of the repo for the pull request, will be either origin or a fork
8484
TestTerraformRepoBranch string // the branch of the test, usually the current checked out branch of the test run
85+
BaseTerraformTempDir string // if upgrade test, will contain the temp directory containing clone of base repo
8586
}
8687

8788
// CreateAuthenticator will accept a valid IBM cloud API key, and
@@ -258,74 +259,6 @@ func (svc *SchematicsTestService) CreateUploadTarFile(projectPath string) (strin
258259
return tarballName, nil
259260
}
260261

261-
// SetTemplateRepoToBranch will call UpdateTestTemplateRepo with the appropriate URLs set at the service level
262-
// for the branch of this test
263-
func (svc *SchematicsTestService) SetTemplateRepoToBranch() error {
264-
return svc.UpdateTestTemplateRepo(svc.TestTerraformRepo, svc.TestTerraformRepoBranch, svc.TestOptions.TemplateGitToken)
265-
}
266-
267-
// SetTemplateRepoToBase will call UpdateTestTemplateRepo with the appropriate URLs set at the service level
268-
// for the base (master or main) branch of this test, used in upgrade tests
269-
func (svc *SchematicsTestService) SetTemplateRepoToBase() error {
270-
return svc.UpdateTestTemplateRepo(svc.BaseTerraformRepo, svc.BaseTerraformRepoBranch, svc.TestOptions.TemplateGitToken)
271-
}
272-
273-
// UpdateTestTemplateRepo will replace the workspace repository with a given github URL and branch
274-
func (svc *SchematicsTestService) UpdateTestTemplateRepo(url string, branch string, token string) error {
275-
svc.TestOptions.Testing.Logf("[SCHEMATICS] Setting template repository to repo %s (%s)", url, branch)
276-
// set up repo update request
277-
repoUpdateRequest := &schematics.TemplateRepoUpdateRequest{
278-
URL: core.StringPtr(url),
279-
Branch: core.StringPtr(branch),
280-
}
281-
282-
// set up overall workspace options
283-
replaceOptions := &schematics.ReplaceWorkspaceOptions{
284-
WID: core.StringPtr(svc.WorkspaceID),
285-
TemplateRepo: repoUpdateRequest,
286-
}
287-
288-
// if supplied set a token for private repo
289-
if len(token) > 0 {
290-
replaceOptions.SetXGithubToken(token)
291-
}
292-
293-
// now update template
294-
var resp *core.DetailedResponse
295-
var updateErr error
296-
retries := 0
297-
for {
298-
_, resp, updateErr = svc.SchematicsApiSvc.ReplaceWorkspace(replaceOptions)
299-
if svc.retryApiCall(updateErr, getDetailedResponseStatusCode(resp), retries) {
300-
retries++
301-
svc.TestOptions.Testing.Logf("[SCHEMATICS] RETRY ReplaceWorkspace, status code: %d", getDetailedResponseStatusCode(resp))
302-
} else {
303-
break
304-
}
305-
}
306-
if updateErr != nil {
307-
return updateErr
308-
}
309-
310-
// -------- SETTING UP WORKSPACE WITH REPO ----------
311-
// find the repository refresh job
312-
replaceJob, replaceJobErr := svc.FindLatestWorkspaceJobByName(SchematicsJobTypeUpdate)
313-
if replaceJobErr != nil {
314-
return fmt.Errorf("error finding the update workspace action: %w - %s", replaceJobErr, svc.WorkspaceNameForLog)
315-
}
316-
// wait for it to finish
317-
replaceJobStatus, replaceJobStatusErr := svc.WaitForFinalJobStatus(*replaceJob.ActionID)
318-
if replaceJobStatusErr != nil {
319-
return fmt.Errorf("error waiting for update of workspace to finish: %w - %s", replaceJobStatusErr, svc.WorkspaceNameForLog)
320-
}
321-
// check if complete
322-
if replaceJobStatus != SchematicsJobStatusCompleted {
323-
return fmt.Errorf("workspace update has failed with status %s - %s", replaceJobStatus, svc.WorkspaceNameForLog)
324-
}
325-
326-
return nil
327-
}
328-
329262
// UpdateTestTemplateVars will update an existing Schematics Workspace terraform template with a
330263
// Variablestore, which will set terraform input variables for test runs.
331264
func (svc *SchematicsTestService) UpdateTestTemplateVars(vars []TestSchematicTerraformVar) error {

testschematic/test_options.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,6 @@ type TestSchematicOptions struct {
8080
// to that value.
8181
TemplateFolder string
8282

83-
// The Git auth token used by schematics to clone initial template source of project.
84-
// If TAR upload option is not used, the default will be to use the Git URL of the test branch for initial schematics workload setup.
85-
// If the test repo is private or protected, set this value to a Git token that is authorized to read/clone the repository.
86-
// NOTE: you may also need to set the same token in a `NetrcSettings` as well for this scenario.
87-
TemplateGitToken string
88-
8983
// Optional list of tags that will be applied to the Schematics Workspace instance
9084
Tags []string
9185

@@ -165,10 +159,14 @@ type TestSchematicOptions struct {
165159
// Set this value to `true` to have all schematics job logs (plan/apply/destroy) printed to the test log.
166160
PrintAllSchematicsLogs bool
167161

168-
// This property will be set to true by the test when an upgrade test was performed.
162+
// These properties will be set to true by the test when an upgrade test was performed.
169163
// You can then inspect this value after the test run, if needed, to make further code decisions.
170164
// NOTE: this is not an option field that is meant to be set from a unit test, it is informational only
171-
IsUpgradeTest bool
165+
IsUpgradeTest bool
166+
UpgradeTestSkipped bool // Informs the calling test that conditions were met to skip the upgrade test
167+
168+
// Set to true if you wish for an Upgrade test to do a final `terraform apply` after the consistency check on the new (not base) branch.
169+
CheckApplyResultForUpgrade bool
172170
}
173171

174172
type TestSchematicTerraformVar struct {

0 commit comments

Comments
 (0)