tests: Use bash like patterns in TestAuthority instead of string formating

Signed-off-by: Marek Siarkowicz <siarkowicz@google.com>
This commit is contained in:
Marek Siarkowicz
2022-10-13 16:05:20 +02:00
parent cd9764a99f
commit fdce1b38fb
2 changed files with 42 additions and 73 deletions

View File

@ -35,49 +35,44 @@ func TestAuthority(t *testing.T) {
name string name string
useTLS bool useTLS bool
useInsecureTLS bool useInsecureTLS bool
// Pattern used to generate endpoints for client. Fields filled
// %d - will be filled with member grpc port
clientURLPattern string clientURLPattern string
// Pattern used to validate authority received by server. Fields filled:
// %d - will be filled with first member grpc port
expectAuthorityPattern string expectAuthorityPattern string
}{ }{
{ {
name: "http://domain[:port]", name: "http://domain[:port]",
clientURLPattern: "http://localhost:%d", clientURLPattern: "http://localhost:${MEMBER_PORT}",
expectAuthorityPattern: "localhost:%d", expectAuthorityPattern: "localhost:${MEMBER_PORT}",
}, },
{ {
name: "http://address[:port]", name: "http://address[:port]",
clientURLPattern: "http://127.0.0.1:%d", clientURLPattern: "http://127.0.0.1:${MEMBER_PORT}",
expectAuthorityPattern: "127.0.0.1:%d", expectAuthorityPattern: "127.0.0.1:${MEMBER_PORT}",
}, },
{ {
name: "https://domain[:port] insecure", name: "https://domain[:port] insecure",
useTLS: true, useTLS: true,
useInsecureTLS: true, useInsecureTLS: true,
clientURLPattern: "https://localhost:%d", clientURLPattern: "https://localhost:${MEMBER_PORT}",
expectAuthorityPattern: "localhost:%d", expectAuthorityPattern: "localhost:${MEMBER_PORT}",
}, },
{ {
name: "https://address[:port] insecure", name: "https://address[:port] insecure",
useTLS: true, useTLS: true,
useInsecureTLS: true, useInsecureTLS: true,
clientURLPattern: "https://127.0.0.1:%d", clientURLPattern: "https://127.0.0.1:${MEMBER_PORT}",
expectAuthorityPattern: "127.0.0.1:%d", expectAuthorityPattern: "127.0.0.1:${MEMBER_PORT}",
}, },
{ {
name: "https://domain[:port]", name: "https://domain[:port]",
useTLS: true, useTLS: true,
clientURLPattern: "https://localhost:%d", clientURLPattern: "https://localhost:${MEMBER_PORT}",
expectAuthorityPattern: "localhost:%d", expectAuthorityPattern: "localhost:${MEMBER_PORT}",
}, },
{ {
name: "https://address[:port]", name: "https://address[:port]",
useTLS: true, useTLS: true,
clientURLPattern: "https://127.0.0.1:%d", clientURLPattern: "https://127.0.0.1:${MEMBER_PORT}",
expectAuthorityPattern: "127.0.0.1:%d", expectAuthorityPattern: "127.0.0.1:${MEMBER_PORT}",
}, },
} }
for _, tc := range tcs { for _, tc := range tcs {
@ -110,7 +105,7 @@ func TestAuthority(t *testing.T) {
} }
testutils.ExecuteWithTimeout(t, 5*time.Second, func() { testutils.ExecuteWithTimeout(t, 5*time.Second, func() {
assertAuthority(t, fmt.Sprintf(tc.expectAuthorityPattern, 20000), epc) assertAuthority(t, strings.Replace(tc.expectAuthorityPattern, "${MEMBER_PORT}", "20000", -1), epc)
}) })
}) })
@ -123,12 +118,7 @@ func templateEndpoints(t *testing.T, pattern string, clus *e2e.EtcdProcessCluste
var endpoints []string var endpoints []string
for i := 0; i < clus.Cfg.ClusterSize; i++ { for i := 0; i < clus.Cfg.ClusterSize; i++ {
ent := pattern ent := pattern
if strings.Contains(ent, "%d") { ent = strings.Replace(ent, "${MEMBER_PORT}", fmt.Sprintf("%d", e2e.EtcdProcessBasePort+i*5), -1)
ent = fmt.Sprintf(ent, e2e.EtcdProcessBasePort+i*5)
}
if strings.Contains(ent, "%") {
t.Fatalf("Failed to template pattern, %% symbol left %q", ent)
}
endpoints = append(endpoints, ent) endpoints = append(endpoints, ent)
} }
return endpoints return endpoints

View File

@ -32,64 +32,57 @@ func TestAuthority(t *testing.T) {
name string name string
useTCP bool useTCP bool
useTLS bool useTLS bool
// Pattern used to generate endpoints for client. Fields filled
// %d - will be filled with member grpc port
// %s - will be filled with member name
clientURLPattern string clientURLPattern string
// Pattern used to validate authority received by server. Fields filled:
// %d - will be filled with first member grpc port
// %s - will be filled with first member name
expectAuthorityPattern string expectAuthorityPattern string
}{ }{
{ {
name: "unix:path", name: "unix:path",
clientURLPattern: "unix:localhost:%s", clientURLPattern: "unix:localhost:${MEMBER_NAME}",
expectAuthorityPattern: "localhost:%s", expectAuthorityPattern: "localhost:${MEMBER_NAME}",
}, },
{ {
name: "unix://absolute_path", name: "unix://absolute_path",
clientURLPattern: "unix://localhost:%s", clientURLPattern: "unix://localhost:${MEMBER_NAME}",
expectAuthorityPattern: "localhost:%s", expectAuthorityPattern: "localhost:${MEMBER_NAME}",
}, },
// "unixs" is not standard schema supported by etcd // "unixs" is not standard schema supported by etcd
{ {
name: "unixs:absolute_path", name: "unixs:absolute_path",
useTLS: true, useTLS: true,
clientURLPattern: "unixs:localhost:%s", clientURLPattern: "unixs:localhost:${MEMBER_NAME}",
expectAuthorityPattern: "localhost:%s", expectAuthorityPattern: "localhost:${MEMBER_NAME}",
}, },
{ {
name: "unixs://absolute_path", name: "unixs://absolute_path",
useTLS: true, useTLS: true,
clientURLPattern: "unixs://localhost:%s", clientURLPattern: "unixs://localhost:${MEMBER_NAME}",
expectAuthorityPattern: "localhost:%s", expectAuthorityPattern: "localhost:${MEMBER_NAME}",
}, },
{ {
name: "http://domain[:port]", name: "http://domain[:port]",
useTCP: true, useTCP: true,
clientURLPattern: "http://localhost:%d", clientURLPattern: "http://localhost:${MEMBER_PORT}",
expectAuthorityPattern: "localhost:%d", expectAuthorityPattern: "localhost:${MEMBER_PORT}",
}, },
{ {
name: "https://domain[:port]", name: "https://domain[:port]",
useTLS: true, useTLS: true,
useTCP: true, useTCP: true,
clientURLPattern: "https://localhost:%d", clientURLPattern: "https://localhost:${MEMBER_PORT}",
expectAuthorityPattern: "localhost:%d", expectAuthorityPattern: "localhost:${MEMBER_PORT}",
}, },
{ {
name: "http://address[:port]", name: "http://address[:port]",
useTCP: true, useTCP: true,
clientURLPattern: "http://127.0.0.1:%d", clientURLPattern: "http://127.0.0.1:${MEMBER_PORT}",
expectAuthorityPattern: "127.0.0.1:%d", expectAuthorityPattern: "127.0.0.1:${MEMBER_PORT}",
}, },
{ {
name: "https://address[:port]", name: "https://address[:port]",
useTCP: true, useTCP: true,
useTLS: true, useTLS: true,
clientURLPattern: "https://127.0.0.1:%d", clientURLPattern: "https://127.0.0.1:${MEMBER_PORT}",
expectAuthorityPattern: "127.0.0.1:%d", expectAuthorityPattern: "127.0.0.1:${MEMBER_PORT}",
}, },
} }
for _, tc := range tcs { for _, tc := range tcs {
@ -153,15 +146,8 @@ func templateEndpoints(t *testing.T, pattern string, clus *integration.Cluster)
var endpoints []string var endpoints []string
for _, m := range clus.Members { for _, m := range clus.Members {
ent := pattern ent := pattern
if strings.Contains(ent, "%d") { ent = strings.Replace(ent, "${MEMBER_PORT}", fmt.Sprintf("%d", integration.GrpcPortNumber(m.UniqNumber, m.MemberNumber)), -1)
ent = fmt.Sprintf(ent, integration.GrpcPortNumber(m.UniqNumber, m.MemberNumber)) ent = strings.Replace(ent, "${MEMBER_NAME}", m.Name, -1)
}
if strings.Contains(ent, "%s") {
ent = fmt.Sprintf(ent, m.Name)
}
if strings.Contains(ent, "%") {
t.Fatalf("Failed to template pattern, %% symbol left %q", ent)
}
endpoints = append(endpoints, ent) endpoints = append(endpoints, ent)
} }
return endpoints return endpoints
@ -170,15 +156,8 @@ func templateEndpoints(t *testing.T, pattern string, clus *integration.Cluster)
func templateAuthority(t *testing.T, pattern string, m *integration.Member) string { func templateAuthority(t *testing.T, pattern string, m *integration.Member) string {
t.Helper() t.Helper()
authority := pattern authority := pattern
if strings.Contains(authority, "%d") { authority = strings.Replace(authority, "${MEMBER_PORT}", fmt.Sprintf("%d", integration.GrpcPortNumber(m.UniqNumber, m.MemberNumber)), -1)
authority = fmt.Sprintf(authority, integration.GrpcPortNumber(m.UniqNumber, m.MemberNumber)) authority = strings.Replace(authority, "${MEMBER_NAME}", m.Name, -1)
}
if strings.Contains(authority, "%s") {
authority = fmt.Sprintf(authority, m.Name)
}
if strings.Contains(authority, "%") {
t.Fatalf("Failed to template pattern, %% symbol left %q", authority)
}
return authority return authority
} }