Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 🐛 static code error and mock tests #626

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions pkg/environment/createPR.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,31 @@ import (

func createPR(description, namespace string) func(github.GithubIface, []string) (string, error) {
b := make([]byte, 2)
rand.Read(b)

rand.Read(b) //nolint:errcheck

fourCharUid := hex.EncodeToString(b)
branchName := namespace + "-rds-minor-version-bump-" + fourCharUid

return func(gh github.GithubIface, filenames []string) (string, error) {
checkCmd := exec.Command("/bin/sh", "-c", "git checkout -b "+branchName)
checkCmd.Start()
checkCmd.Wait()
checkCmd.Start() //nolint:errcheck
checkCmd.Wait() //nolint:errcheck

strFiles := strings.Join(filenames, " ")
cmd := exec.Command("/bin/sh", "-c", "git add "+strFiles)
cmd.Dir = "namespaces/live.cloud-platform.service.justice.gov.uk/" + namespace + "/resources"
cmd.Start()
cmd.Wait()
cmd.Start() //nolint:errcheck
cmd.Wait() //nolint:errcheck

commitCmd := exec.Command("/bin/sh", "-c", "git commit -m 'concourse: correcting rds version drift'")
commitCmd.Dir = "namespaces/live.cloud-platform.service.justice.gov.uk/" + namespace + "/resources"
commitCmd.Start()
commitCmd.Wait()
commitCmd.Start() //nolint:errcheck
commitCmd.Wait() //nolint:errcheck

pushCmd := exec.Command("/bin/sh", "-c", "git push --set-upstream origin "+branchName)
pushCmd.Start()
pushCmd.Wait()
pushCmd.Start() //nolint:errcheck
pushCmd.Wait() //nolint:errcheck

return gh.CreatePR(branchName, namespace, description)
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/environment/isRdsVersionMismatched.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func IsRdsVersionMismatched(outputTerraform string) (*RdsVersionResults, error)
match, _ := regexp.MatchString("Error: updating RDS DB Instance .* api error InvalidParameterCombination:.* from .* to .*", outputTerraform)

if !match {
return nil, errors.New("terraform is failing but it doesn't look like a rds version mismatch.")
return nil, errors.New("terraform is failing but it doesn't look like a rds version mismatch")
}

versionRe := regexp.MustCompile(`from (?P<actual_db_version>\d+\.\d+) to (?P<terraform_db_version>\d+\.\d+)`)
Expand All @@ -32,11 +32,11 @@ func IsRdsVersionMismatched(outputTerraform string) (*RdsVersionResults, error)
sanitisedNames := removeInputStr(moduleMatches)

if !checkVersionDowngrade(sanitisedVersions) {
return nil, errors.New("terraform is failing, but it isn't trying to downgrade the RDS versions so it needs more investigation.")
return nil, errors.New("terraform is failing, but it isn't trying to downgrade the RDS versions so it needs more investigation")
}

if len(sanitisedVersions) != len(sanitisedNames) {
return nil, fmt.Errorf("Error: there is an inconistent number of versions vs module names, there should be an even amount but we have %d sets of versions and %d module names", len(sanitisedVersions), len(sanitisedNames))
return nil, fmt.Errorf("error: there is an inconistent number of versions vs module names, there should be an even amount but we have %d sets of versions and %d module names", len(sanitisedVersions), len(sanitisedNames))
}

return &RdsVersionResults{
Expand All @@ -57,12 +57,12 @@ func checkVersionDowngrade(versions [][]string) bool {
adjustedAcc := strings.Join(splitAcc, "")
adjustedTf := strings.Join(splitTf, "")

acc, err := strconv.ParseInt(adjustedAcc, 0, 64)
tf, err := strconv.ParseInt(adjustedTf, 0, 64)
acc, accErr := strconv.ParseInt(adjustedAcc, 0, 64)
tf, tfErr := strconv.ParseInt(adjustedTf, 0, 64)

isUpgrade := tf > acc

if err != nil || isUpgrade {
if accErr != nil || tfErr != nil || isUpgrade {
isValid = false
break
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/environment/isRdsVersionMismatched_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ func TestIsRdsVersionMismatched(t *testing.T) {
},
nil,
},
{"GIVEN an output WITHOUT a version mismatch THEN return nil", nomatchOutput, nil, errors.New("terraform is failing but it doesn't look like a rds version mismatch.")},
{"GIVEN an output WITH a version mismatch BUT the mismatch would cause a db UPGRADE THEN return nil", isVersionUpgradeMismatch, nil, errors.New("terraform is failing, but it isn't trying to downgrade the RDS versions so it needs more investigation.")},
{"GIVEN an output WITHOUT a version mismatch THEN return nil", nomatchOutput, nil, errors.New("terraform is failing but it doesn't look like a rds version mismatch")},
{"GIVEN an output WITH a version mismatch BUT the mismatch would cause a db UPGRADE THEN return nil", isVersionUpgradeMismatch, nil, errors.New("terraform is failing, but it isn't trying to downgrade the RDS versions so it needs more investigation")},
{
"GIVEN an output WITH MULTIPLE version mismatches THEN return the correct string slices", multipleMismatchOutput, &environment.RdsVersionResults{
Versions: [][]string{{"14.12", "14.10"}, {"16.18", "16.16"}},
Expand All @@ -104,7 +104,7 @@ func TestIsRdsVersionMismatched(t *testing.T) {
},
nil,
},
{"GIVEN an output with an inconsistent number of versions and modules THEN return nil and an error", inconsistentOutput, nil, errors.New("Error: there is an inconistent number of versions vs module names, there should be an even amount but we have 1 sets of versions and 3 module names")},
{"GIVEN an output with an inconsistent number of versions and modules THEN return nil and an error", inconsistentOutput, nil, errors.New("error: there is an inconistent number of versions vs module names, there should be an even amount but we have 1 sets of versions and 3 module names")},
}

for _, tt := range tests {
Expand Down
7 changes: 4 additions & 3 deletions pkg/github/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func (m *mockGithub) IsMerged(ctx context.Context, owner string, repo string, nu
return true, nil, nil
}

func (m *mockGithub) Create(ctx context.Context, owner string, repo string, pr *github.NewPullRequest) (*github.PullRequest, *github.Response, error) {
return nil, nil, nil
}

func TestNewGithubClient(t *testing.T) {
type args struct {
config *GithubClientConfig
Expand Down Expand Up @@ -51,7 +55,6 @@ func TestNewGithubClient(t *testing.T) {
}

func TestGithubClient_GetChangedFiles(t *testing.T) {

mc := &mockGithub{
resp: []*github.CommitFile{
{
Expand Down Expand Up @@ -95,13 +98,11 @@ func TestGithubClient_IsMerged(t *testing.T) {
PullRequests: mc,
}
got, err := gh.IsMerged(8344)

if err != nil {
t.Errorf("GithubClient.IsMerged() error = %v, wantErr %v", err, nil)
return
}
if got != mc.merged {
t.Errorf("GithubClient.IsMerged() = %v, want %v", got, true)
}

}
Loading