Skip to content

Commit a0d455b

Browse files
committed
xds/bootstrap: add testing support to generate config
1 parent 07078c4 commit a0d455b

26 files changed

+487
-399
lines changed

admin/test/utils.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@ import (
2626
"testing"
2727
"time"
2828

29-
"github.com/google/uuid"
3029
"google.golang.org/grpc"
3130
"google.golang.org/grpc/admin"
3231
"google.golang.org/grpc/codes"
3332
"google.golang.org/grpc/credentials/insecure"
34-
"google.golang.org/grpc/internal/testutils/xds/bootstrap"
3533
"google.golang.org/grpc/status"
3634

3735
v3statusgrpc "github.com/envoyproxy/go-control-plane/envoy/service/status/v3"
@@ -54,16 +52,6 @@ type ExpectedStatusCodes struct {
5452
// RunRegisterTests makes a client, runs the RPCs, and compares the status
5553
// codes.
5654
func RunRegisterTests(t *testing.T, ec ExpectedStatusCodes) {
57-
nodeID := uuid.New().String()
58-
bootstrapCleanup, err := bootstrap.CreateFile(bootstrap.Options{
59-
NodeID: nodeID,
60-
ServerURI: "no.need.for.a.server",
61-
})
62-
if err != nil {
63-
t.Fatal(err)
64-
}
65-
defer bootstrapCleanup()
66-
6755
lis, err := net.Listen("tcp", "localhost:0")
6856
if err != nil {
6957
t.Fatalf("cannot create listener: %v", err)

internal/testutils/xds/e2e/setup_certs.go renamed to internal/testutils/tls_creds.go

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright 2022 gRPC authors.
3+
* Copyright 2024 gRPC authors.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -13,65 +13,20 @@
1313
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
16-
*
1716
*/
1817

19-
package e2e
18+
package testutils
2019

2120
import (
2221
"crypto/tls"
2322
"crypto/x509"
24-
"fmt"
2523
"os"
26-
"path"
2724
"testing"
2825

2926
"google.golang.org/grpc/credentials"
3027
"google.golang.org/grpc/testdata"
3128
)
3229

33-
const (
34-
// Names of files inside tempdir, for certprovider plugin to watch.
35-
certFile = "cert.pem"
36-
keyFile = "key.pem"
37-
rootFile = "ca.pem"
38-
)
39-
40-
func createTmpFile(src, dst string) error {
41-
data, err := os.ReadFile(src)
42-
if err != nil {
43-
return fmt.Errorf("os.ReadFile(%q) failed: %v", src, err)
44-
}
45-
if err := os.WriteFile(dst, data, os.ModePerm); err != nil {
46-
return fmt.Errorf("os.WriteFile(%q) failed: %v", dst, err)
47-
}
48-
return nil
49-
}
50-
51-
// createTempDirWithFiles creates a temporary directory under the system default
52-
// tempDir with the given dirSuffix. It also reads from certSrc, keySrc and
53-
// rootSrc files are creates appropriate files under the newly create tempDir.
54-
// Returns the name of the created tempDir.
55-
func createTmpDirWithFiles(dirSuffix, certSrc, keySrc, rootSrc string) (string, error) {
56-
// Create a temp directory. Passing an empty string for the first argument
57-
// uses the system temp directory.
58-
dir, err := os.MkdirTemp("", dirSuffix)
59-
if err != nil {
60-
return "", fmt.Errorf("os.MkdirTemp() failed: %v", err)
61-
}
62-
63-
if err := createTmpFile(testdata.Path(certSrc), path.Join(dir, certFile)); err != nil {
64-
return "", err
65-
}
66-
if err := createTmpFile(testdata.Path(keySrc), path.Join(dir, keyFile)); err != nil {
67-
return "", err
68-
}
69-
if err := createTmpFile(testdata.Path(rootSrc), path.Join(dir, rootFile)); err != nil {
70-
return "", err
71-
}
72-
return dir, nil
73-
}
74-
7530
// CreateClientTLSCredentials creates client-side TLS transport credentials
7631
// using certificate and key files from testdata/x509 directory.
7732
func CreateClientTLSCredentials(t *testing.T) credentials.TransportCredentials {

internal/testutils/xds/bootstrap/bootstrap.go

Lines changed: 0 additions & 169 deletions
This file was deleted.

internal/testutils/xds/e2e/setup_management_server.go

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ package e2e
2121
import (
2222
"encoding/json"
2323
"fmt"
24+
"os"
2425
"path"
2526
"testing"
2627

2728
"github.com/google/uuid"
2829
"google.golang.org/grpc/internal"
29-
"google.golang.org/grpc/internal/testutils/xds/bootstrap"
30+
"google.golang.org/grpc/internal/xds/bootstrap"
3031
"google.golang.org/grpc/resolver"
32+
"google.golang.org/grpc/testdata"
3133
)
3234

3335
// SetupManagementServer performs the following:
@@ -99,14 +101,69 @@ func DefaultBootstrapContents(nodeID, serverURI string) ([]byte, error) {
99101
}
100102

101103
// Create the bootstrap configuration.
102-
bs, err := bootstrap.Contents(bootstrap.Options{
104+
bs, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
105+
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
106+
"server_uri": %q,
107+
"channel_creds": [{"type": "insecure"}]
108+
}`, serverURI))},
103109
NodeID: nodeID,
104-
ServerURI: serverURI,
105110
CertificateProviders: cpc,
106111
ServerListenerResourceNameTemplate: ServerListenerResourceNameTemplate,
112+
Authorities: map[string]json.RawMessage{
113+
// Most tests that use new style xdstp resource names do not specify
114+
// an authority. These end up looking up an entry with the empty key
115+
// in the authorities map. Having an entry with an empty key and
116+
// empty configuration, results in these resources also using the
117+
// top-level configuration, which is what we want mostly for our
118+
// tests, unless explicitly specified by tests that use multiple
119+
// authorities etc.
120+
"": []byte(`{}`),
121+
},
107122
})
108123
if err != nil {
109124
return nil, fmt.Errorf("failed to create bootstrap configuration: %v", err)
110125
}
111126
return bs, nil
112127
}
128+
129+
const (
130+
// Names of files inside tempdir, for certprovider plugin to watch.
131+
certFile = "cert.pem"
132+
keyFile = "key.pem"
133+
rootFile = "ca.pem"
134+
)
135+
136+
func createTmpFile(src, dst string) error {
137+
data, err := os.ReadFile(src)
138+
if err != nil {
139+
return fmt.Errorf("os.ReadFile(%q) failed: %v", src, err)
140+
}
141+
if err := os.WriteFile(dst, data, os.ModePerm); err != nil {
142+
return fmt.Errorf("os.WriteFile(%q) failed: %v", dst, err)
143+
}
144+
return nil
145+
}
146+
147+
// createTempDirWithFiles creates a temporary directory under the system default
148+
// tempDir with the given dirSuffix. It also reads from certSrc, keySrc and
149+
// rootSrc files are creates appropriate files under the newly create tempDir.
150+
// Returns the name of the created tempDir.
151+
func createTmpDirWithFiles(dirSuffix, certSrc, keySrc, rootSrc string) (string, error) {
152+
// Create a temp directory. Passing an empty string for the first argument
153+
// uses the system temp directory.
154+
dir, err := os.MkdirTemp("", dirSuffix)
155+
if err != nil {
156+
return "", fmt.Errorf("os.MkdirTemp() failed: %v", err)
157+
}
158+
159+
if err := createTmpFile(testdata.Path(certSrc), path.Join(dir, certFile)); err != nil {
160+
return "", err
161+
}
162+
if err := createTmpFile(testdata.Path(keySrc), path.Join(dir, keyFile)); err != nil {
163+
return "", err
164+
}
165+
if err := createTmpFile(testdata.Path(rootSrc), path.Join(dir, rootFile)); err != nil {
166+
return "", err
167+
}
168+
return dir, nil
169+
}

0 commit comments

Comments
 (0)