-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update TeamCity diff checker tool (#12426)
- Loading branch information
1 parent
c5a1e3b
commit a854242
Showing
13 changed files
with
283 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/GoogleCloudPlatform/magic-modules/tools/teamcity-diff-check | ||
|
||
go 1.23.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package main | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func Test_main_happyPaths(t *testing.T) { | ||
testCases := map[string]struct { | ||
providerServiceFile string | ||
teamcityServiceFile string | ||
expectError bool | ||
errorRegex *regexp.Regexp | ||
missingServiceRegex *regexp.Regexp | ||
}{ | ||
"everything matches": { | ||
providerServiceFile: "./test-fixtures/everything-ok/ga-services.txt", | ||
teamcityServiceFile: "./test-fixtures/everything-ok/services_ga.kt", | ||
}, | ||
"something missing in TeamCity config present in provider code": { | ||
providerServiceFile: "./test-fixtures/mismatch-teamcity/ga-services.txt", | ||
teamcityServiceFile: "./test-fixtures/mismatch-teamcity/services_ga.kt", | ||
expectError: true, | ||
errorRegex: regexp.MustCompile("TeamCity service file is missing services present in the provider"), | ||
missingServiceRegex: regexp.MustCompile("[pubsub]"), | ||
}, | ||
"something missing in provider code present in TeamCity config": { | ||
providerServiceFile: "./test-fixtures/mismatch-provider/ga-services.txt", | ||
teamcityServiceFile: "./test-fixtures/mismatch-provider/services_ga.kt", | ||
expectError: true, | ||
errorRegex: regexp.MustCompile("Provider codebase is missing services present in the TeamCity service file"), | ||
missingServiceRegex: regexp.MustCompile("[compute]"), | ||
}, | ||
} | ||
|
||
for tn, tc := range testCases { | ||
t.Run(tn, func(t *testing.T) { | ||
err := compareServices(tc.teamcityServiceFile, tc.providerServiceFile) | ||
if err != nil && !tc.expectError { | ||
t.Fatalf("saw an unexpected error: %s", err) | ||
} | ||
if err == nil && tc.expectError { | ||
t.Fatalf("expected an error but saw none") | ||
} | ||
|
||
if err == nil { | ||
// Stop handling of non-error test cases | ||
return | ||
} | ||
|
||
if !tc.errorRegex.MatchString(err.Error()) { | ||
t.Fatalf("expected error to contain a match for regex `%s`, got error string: `%s`", tc.errorRegex.String(), err) | ||
} | ||
if !tc.missingServiceRegex.MatchString(err.Error()) { | ||
t.Fatalf("expected error to contain a match for regex `%s`, got error string: `%s`", tc.errorRegex.String(), err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_main_unhappyPaths(t *testing.T) { | ||
testCases := map[string]struct { | ||
providerServiceFile string | ||
teamcityServiceFile string | ||
expectError bool | ||
errorRegex *regexp.Regexp | ||
}{ | ||
"cannot find provider service file": { | ||
providerServiceFile: "./test-fixtures/doesnt-exist.txt", | ||
teamcityServiceFile: "./test-fixtures/everything-ok/services_ga.kt", | ||
expectError: true, | ||
errorRegex: regexp.MustCompile("error opening provider service list file: open ./test-fixtures/doesnt-exist.txt"), | ||
}, | ||
"cannot find TeamCity service file": { | ||
providerServiceFile: "./test-fixtures/everything-ok/ga-services.txt", | ||
teamcityServiceFile: "./test-fixtures/everything-ok/doesnt-exist.kt", | ||
expectError: true, | ||
errorRegex: regexp.MustCompile("error opening TeamCity service list file: open ./test-fixtures/everything-ok/doesnt-exist.kt"), | ||
}, | ||
"empty TeamCity service file": { | ||
providerServiceFile: "./test-fixtures/everything-ok/ga-services.txt", | ||
teamcityServiceFile: "./test-fixtures/empty-files/services_ga.kt", | ||
expectError: true, | ||
errorRegex: regexp.MustCompile("could not find any services in the TeamCity service list file ./test-fixtures/empty-files/services_ga.kt"), | ||
}, | ||
"empty provider service file": { | ||
providerServiceFile: "./test-fixtures/empty-files/ga-services.txt", | ||
teamcityServiceFile: "./test-fixtures/everything-ok/services_ga.kt", | ||
expectError: true, | ||
errorRegex: regexp.MustCompile("could not find any services in the provider service list file ./test-fixtures/empty-files/ga-services.txt"), | ||
}, | ||
} | ||
|
||
for tn, tc := range testCases { | ||
t.Run(tn, func(t *testing.T) { | ||
err := compareServices(tc.teamcityServiceFile, tc.providerServiceFile) | ||
if err != nil && !tc.expectError { | ||
t.Fatalf("saw an unexpected error: %s", err) | ||
} | ||
if err == nil && tc.expectError { | ||
t.Fatalf("expected an error but saw none") | ||
} | ||
|
||
if !tc.errorRegex.MatchString(err.Error()) { | ||
t.Fatalf("expected error to contain a match for regex `%s`, got error string: `%s`", tc.errorRegex.String(), err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_listDifference(t *testing.T) { | ||
testCases := map[string]struct { | ||
a []string | ||
b []string | ||
expectDiff bool | ||
errorRegex *regexp.Regexp | ||
}{ | ||
"detects when lists match": { | ||
a: []string{"a", "c", "b"}, | ||
b: []string{"a", "b", "c"}, | ||
}, | ||
"detects when items from list A is missing items present in list B - 1 missing": { | ||
a: []string{"a", "b"}, | ||
b: []string{"a", "c", "b"}, | ||
expectDiff: true, | ||
errorRegex: regexp.MustCompile("[c]"), | ||
}, | ||
"detects when items from list A is missing items present in list B - 2 missing": { | ||
a: []string{"b"}, | ||
b: []string{"a", "c", "b"}, | ||
expectDiff: true, | ||
errorRegex: regexp.MustCompile("[a, c]"), | ||
}, | ||
"doesn't detect differences if list A is a superset of list B": { | ||
a: []string{"a", "b", "c"}, | ||
b: []string{"a", "c"}, | ||
expectDiff: false, | ||
}, | ||
} | ||
|
||
for tn, tc := range testCases { | ||
t.Run(tn, func(t *testing.T) { | ||
err := listDifference(tc.a, tc.b) | ||
if !tc.expectDiff && (err != nil) { | ||
t.Fatalf("saw an unexpected diff error: %s", err) | ||
} | ||
if tc.expectDiff && (err == nil) { | ||
t.Fatalf("expected a diff error but saw none") | ||
} | ||
if !tc.expectDiff && err == nil { | ||
// Stop assertions in no error cases | ||
return | ||
} | ||
if !tc.errorRegex.MatchString(err.Error()) { | ||
t.Fatalf("expected diff error to contain a match for regex %s, error string: %s", tc.errorRegex.String(), err) | ||
} | ||
}) | ||
} | ||
} |
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions
2
tools/teamcity-diff-check/test-fixtures/everything-ok/ga-services.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
compute | ||
pubsub |
12 changes: 12 additions & 0 deletions
12
tools/teamcity-diff-check/test-fixtures/everything-ok/services_ga.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var ServicesListGa = mapOf( | ||
"compute" to mapOf( | ||
"name" to "compute", | ||
"displayName" to "Compute", | ||
"path" to "./google/services/compute" | ||
), | ||
"pubsub" to mapOf( | ||
"name" to "pubsub", | ||
"displayName" to "PubSub", | ||
"path" to "./google/services/pubsub" | ||
), | ||
) |
1 change: 1 addition & 0 deletions
1
tools/teamcity-diff-check/test-fixtures/mismatch-provider/ga-services.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pubsub |
12 changes: 12 additions & 0 deletions
12
tools/teamcity-diff-check/test-fixtures/mismatch-provider/services_ga.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var ServicesListGa = mapOf( | ||
"compute" to mapOf( | ||
"name" to "compute", | ||
"displayName" to "Compute", | ||
"path" to "./google/services/compute" | ||
), | ||
"pubsub" to mapOf( | ||
"name" to "pubsub", | ||
"displayName" to "PubSub", | ||
"path" to "./google/services/pubsub" | ||
), | ||
) |
2 changes: 2 additions & 0 deletions
2
tools/teamcity-diff-check/test-fixtures/mismatch-teamcity/ga-services.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
compute | ||
pubsub |
Oops, something went wrong.