Skip to content

Commit d6a1497

Browse files
committed
test: update SanitizeRepoName
* Replaces repoPrefix with repoSuffix which now includes the GCP project name
1 parent 822e075 commit d6a1497

File tree

5 files changed

+40
-30
lines changed

5 files changed

+40
-30
lines changed

e2e/nomostest/gitproviders/cloud_source_repository.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@ func CSRReaderEmail() string {
3434
type CSRClient struct {
3535
// project in which to store the source repo
3636
project string
37-
// repoPrefix is used to avoid overlap
38-
repoPrefix string
37+
// repoSuffix is used to avoid overlap
38+
repoSuffix string
3939
// shell used for invoking CLI tools
4040
shell *testshell.TestShell
4141
}
4242

4343
// newCSRClient instantiates a new CSR client.
44-
func newCSRClient(repoPrefix string, shell *testshell.TestShell) *CSRClient {
44+
func newCSRClient(repoSuffix string, shell *testshell.TestShell) *CSRClient {
4545
return &CSRClient{
4646
project: *e2e.GCPProject,
47-
repoPrefix: repoPrefix,
47+
repoSuffix: repoSuffix,
4848
shell: shell,
4949
}
5050
}
5151

5252
func (c *CSRClient) fullName(name string) string {
53-
return util.SanitizeRepoName("cs-e2e-"+c.repoPrefix, name)
53+
return util.SanitizeRepoName(c.repoSuffix, name)
5454
}
5555

5656
// Type returns the provider type.

e2e/nomostest/gitproviders/git-provider.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,18 @@ func NewGitProvider(t testing.NTB, provider, clusterName string, logger *testlog
6262
t.Fatal(err)
6363
}
6464
return client
65-
case e2e.CSR:
66-
return newCSRClient(clusterName, shell)
67-
case e2e.SSM:
65+
case e2e.CSR, e2e.SSM:
66+
repoSuffix := *e2e.GCPProject + "/" + clusterName
67+
if provider == e2e.CSR {
68+
return newCSRClient(repoSuffix, shell)
69+
}
70+
// case e2e.SSM
6871
out, err := shell.ExecWithDebug("gcloud", "projects", "describe", *e2e.GCPProject, "--format", "value(projectNumber)")
6972
if err != nil {
7073
t.Fatalf("getting project number: %w", err)
7174
}
72-
7375
projectNumber := strings.Split(string(out), "\n")[0]
74-
75-
return newSSMClient(clusterName, shell, projectNumber)
76+
return newSSMClient(repoSuffix, shell, projectNumber)
7677
default:
7778
return &LocalProvider{}
7879
}

e2e/nomostest/gitproviders/secure_source_manager.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,28 @@ type SSMClient struct {
4040
instanceID string
4141
// region of the SSM instance in which to store the source repo
4242
region string
43-
// repoPrefix is used to avoid overlap
44-
repoPrefix string
43+
// repoSuffix is used to avoid overlap
44+
repoSuffix string
4545
// shell used for invoking CLI tools
4646
shell *testshell.TestShell
4747
}
4848

4949
var _ GitProvider = &SSMClient{}
5050

5151
// newSSMClient instantiates a new SSM client.
52-
func newSSMClient(repoPrefix string, shell *testshell.TestShell, projectNumber string) *SSMClient {
52+
func newSSMClient(repoSuffix string, shell *testshell.TestShell, projectNumber string) *SSMClient {
5353
return &SSMClient{
5454
project: *e2e.GCPProject,
5555
instanceID: testing.SSMInstanceID,
5656
region: *e2e.SSMInstanceRegion,
57-
repoPrefix: repoPrefix,
57+
repoSuffix: repoSuffix,
5858
shell: shell,
5959
projectNumber: projectNumber,
6060
}
6161
}
6262

6363
func (c *SSMClient) fullName(name string) string {
64-
return util.SanitizeRepoName(c.repoPrefix, name)
64+
return util.SanitizeRepoName(c.repoSuffix, name)
6565
}
6666

6767
// Type returns the provider type.

e2e/nomostest/gitproviders/util/reponame.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ const (
2828

2929
// SanitizeRepoName replaces all slashes with hyphens, and truncate the name.
3030
// repo name may contain between 3 and 63 lowercase letters, digits and hyphens.
31-
func SanitizeRepoName(repoPrefix, name string) string {
32-
fullName := "cs-e2e-" + repoPrefix + "-" + name
31+
func SanitizeRepoName(repoSuffix, name string) string {
32+
fullName := name + "-" + repoSuffix
3333
hashBytes := sha1.Sum([]byte(fullName))
3434
hashStr := hex.EncodeToString(hashBytes[:])[:repoNameHashLen]
3535

3636
if len(fullName) > repoNameMaxLen-1-repoNameHashLen {
3737
fullName = fullName[:repoNameMaxLen-1-repoNameHashLen]
3838
}
39-
return fmt.Sprintf("%s-%s", strings.ReplaceAll(fullName, "/", "-"), hashStr)
39+
sanitizedName := strings.ReplaceAll(fullName, "/", "-")
40+
sanitizedName = strings.TrimRight(sanitizedName, "-") // Avoids double dash before the hash.
41+
42+
return fmt.Sprintf("%s-%s", sanitizedName, hashStr)
4043
}

e2e/nomostest/gitproviders/util/reponame_test.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,45 @@ import (
2323
func TestSanitizeRepoName(t *testing.T) {
2424
testCases := []struct {
2525
testName string
26-
repoPrefix string
26+
repoSuffix string
2727
repoName string
2828
expectedName string
2929
}{
3030
{
3131
testName: "RepoSync test-ns/repo-sync",
32-
repoPrefix: "test",
32+
repoSuffix: "project/cluster",
3333
repoName: "test-ns/repo-sync",
34-
expectedName: "cs-e2e-test-test-ns-repo-sync-19dcbc51",
34+
expectedName: "test-ns-repo-sync-project-cluster-b96b1396",
3535
},
3636
{
3737
testName: "RepoSync test/ns-repo-sync should not collide with RepoSync test-ns/repo-sync",
38-
repoPrefix: "test",
38+
repoSuffix: "project/cluster",
3939
repoName: "test/ns-repo-sync",
40-
expectedName: "cs-e2e-test-test-ns-repo-sync-f98ca740",
40+
expectedName: "test-ns-repo-sync-project-cluster-d98dee7d",
4141
},
4242
{
43-
testName: "A very long repoPrefix should be truncated",
44-
repoPrefix: "autopilot-rapid-latest-10",
43+
testName: "A very long repoSuffix should be truncated",
44+
repoSuffix: "kpt-config-sync-ci-main/autopilot-rapid-latest-10",
4545
repoName: "config-management-system/root-sync",
46-
expectedName: "cs-e2e-autopilot-rapid-latest-10-config-management-sys-0aab99c5",
46+
expectedName: "config-management-system-root-sync-kpt-config-sync-ci-6485bfa0",
47+
},
48+
{
49+
testName: "A similar very long repoSuffix should be truncated and not collide",
50+
repoSuffix: "kpt-config-sync-ci-release/autopilot-rapid-latest-10",
51+
repoName: "config-management-system/root-sync",
52+
expectedName: "config-management-system-root-sync-kpt-config-sync-ci-8b9c3b0d",
4753
},
4854
{
4955
testName: "A very long repoName should be truncated",
50-
repoPrefix: "test",
56+
repoSuffix: "test",
5157
repoName: "config-management-system/root-sync-with-a-very-long-name",
52-
expectedName: "cs-e2e-test-config-management-system-root-sync-with-a--0d0af6c0",
58+
expectedName: "config-management-system-root-sync-with-a-very-long-na-3b0dae1c",
5359
},
5460
}
5561

5662
for _, tc := range testCases {
5763
t.Run(tc.testName, func(t *testing.T) {
58-
gotName := SanitizeRepoName(tc.repoPrefix, tc.repoName)
64+
gotName := SanitizeRepoName(tc.repoSuffix, tc.repoName)
5965
assert.Equal(t, tc.expectedName, gotName)
6066
})
6167
}

0 commit comments

Comments
 (0)