33package e2e
44
55import (
6+ "crypto/sha256"
67 "crypto/tls"
78 "crypto/x509"
89 "encoding/json"
@@ -21,28 +22,53 @@ import (
2122 "github.com/observatorium/api/test/testtls"
2223)
2324
25+ // uniqueE2ENetworkName returns a Docker-valid e2e network name (≤16 chars, [-a-zA-Z0-9])
26+ // that is distinct for each Go test. efficientgo/e2e's default name hashes runtime.Caller(3),
27+ // which resolves to testing.tRunner for every test, so bare e2e.New() would assign the same
28+ // network to all parallel tests and reproduce Docker network races.
29+ func uniqueE2ENetworkName (t * testing.T ) string {
30+ t .Helper ()
31+ sum := sha256 .Sum256 ([]byte (t .Name ()))
32+ return fmt .Sprintf ("%x" , sum [:8 ]) // 16 hex digits
33+ }
34+
2435// Generates certificates and copies static configuration to the shared directory.
25- func prepareConfigsAndCerts (t * testing.T , tt testType , e e2e.Environment ) {
36+ func prepareConfigsAndCerts (t * testing.T , e e2e.Environment ) {
2637 testutil .Ok (
2738 t ,
2839 testtls .GenerateCerts (
2940 filepath .Join (e .SharedDir (), certsSharedDir ),
30- getContainerName (t , tt , "observatorium-api" ),
31- []string {getContainerName (t , tt , "observatorium-api" ), "127.0.0.1" },
32- getContainerName (t , tt , "dex" ),
33- []string {getContainerName (t , tt , "dex" ), "127.0.0.1" },
41+ getContainerName (e , "observatorium-api" ),
42+ []string {
43+ getContainerName (e , "observatorium-api" ),
44+ "127.0.0.1" ,
45+ "host.docker.internal" ,
46+ },
47+ getContainerName (e , "dex" ),
48+ []string {
49+ getContainerName (e , "dex" ),
50+ "127.0.0.1" ,
51+ "host.docker.internal" ,
52+ },
3453 ),
3554 )
3655
3756 testutil .Ok (t , exec .Command ("cp" , "-r" , "../config" , filepath .Join (e .SharedDir (), configSharedDir )).Run ())
3857}
3958
4059// obtainToken obtains a bearer token needed for communication with the API.
41- func obtainToken (endpoint string , tlsConf * tls.Config ) (string , error ) {
60+ // dexTLSHost is the Dex DNS name from the test CA (e.g. {network}-dex); set it so TLS verifies the
61+ // server cert when the TCP dial target is 127.0.0.1 or host.docker.internal (e2e.Endpoint).
62+ func obtainToken (endpoint , dexTLSHost string , tlsConf * tls.Config ) (string , error ) {
4263 type token struct {
4364 IDToken string `json:"id_token"`
4465 }
4566
67+ tlsClient := tlsConf .Clone ()
68+ if dexTLSHost != "" {
69+ tlsClient .ServerName = dexTLSHost
70+ }
71+
4672 data := url.Values {}
4773 data .Add ("grant_type" , "password" )
4874 data .Add ("username" , "admin@example.com" )
@@ -59,7 +85,7 @@ func obtainToken(endpoint string, tlsConf *tls.Config) (string, error) {
5985
6086 c := & http.Client {
6187 Transport : & http.Transport {
62- TLSClientConfig : tlsConf ,
88+ TLSClientConfig : tlsClient ,
6389 },
6490 }
6591
@@ -82,40 +108,20 @@ func obtainToken(endpoint string, tlsConf *tls.Config) (string, error) {
82108 return t .IDToken , nil
83109}
84110
85- func getContainerName (t * testing.T , tt testType , serviceName string ) string {
86- switch tt {
87- case logs :
88- return envLogsName + "-" + serviceName
89- case metrics :
90- return envMetricsName + "-" + serviceName
91- case rules :
92- return envRulesAPIName + "-" + serviceName
93- case alerts :
94- return envAlertmanagerName + "-" + serviceName
95- case tenants :
96- return envTenantsName + "-" + serviceName
97- case interactive :
98- return envInteractive + "-" + serviceName
99- case probes :
100- return envProbesName + "-" + serviceName
101- case traces :
102- return envTracesName + "-" + serviceName
103- case tracesTemplate :
104- return envTracesTemplateName + "-" + serviceName
105- case tracesTempo :
106- return envTracesTempoName + "-" + serviceName
107- default :
108- t .Fatal ("invalid test type provided" )
109- return ""
110- }
111+ // getContainerName returns the Docker DNS hostname for a service in this environment.
112+ // It must match e2e's naming ({networkName}-{runnableName}) so TLS SANs and OIDC redirects stay correct.
113+ func getContainerName (e e2e.Environment , serviceName string ) string {
114+ return e .Name () + "-" + serviceName
111115}
112116
113117func getTLSClientConfig (t * testing.T , e e2e.Environment ) * tls.Config {
114118 cert , err := os .ReadFile (filepath .Join (e .SharedDir (), certsSharedDir , "ca.pem" ))
115119 testutil .Ok (t , err )
116120
117121 cp := x509 .NewCertPool ()
118- cp .AppendCertsFromPEM (cert )
122+ if ok := cp .AppendCertsFromPEM (cert ); ! ok {
123+ t .Fatal ("failed to parse CA certificate from ca.pem" )
124+ }
119125
120126 return & tls.Config {
121127 RootCAs : cp ,
0 commit comments