-
Notifications
You must be signed in to change notification settings - Fork 618
draft: fast e2e tests #12993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
howardjohn
wants to merge
6
commits into
kgateway-dev:main
Choose a base branch
from
howardjohn:agw/fast-e2e
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
draft: fast e2e tests #12993
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,257 @@ | ||
| package curl | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "crypto/tls" | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| // ExecuteRequest accepts a set of Option and executes a native Go HTTP request | ||
| // If multiple Option modify the same parameter, the last defined one will win | ||
| // | ||
| // Example: | ||
| // | ||
| // resp, err := ExecuteRequest(WithMethod("GET"), WithMethod("POST")) | ||
| // will executeNative a POST request | ||
| // | ||
| // A notable exception is the WithHeader option, which accumulates headers | ||
| func ExecuteRequest(options ...Option) (*http.Response, error) { | ||
| config := &requestConfig{ | ||
| verbose: false, | ||
| ignoreServerCert: false, | ||
| connectionTimeout: 0, | ||
| headersOnly: false, | ||
| method: "GET", | ||
| host: "127.0.0.1", | ||
| port: 80, | ||
| headers: make(map[string][]string), | ||
| scheme: "http", | ||
| sni: "", | ||
| caFile: "", | ||
| path: "", | ||
| retry: 0, | ||
| retryDelay: -1, | ||
| retryMaxTime: 0, | ||
| ipv4Only: false, | ||
| ipv6Only: false, | ||
| cookie: "", | ||
| queryParameters: make(map[string]string), | ||
| } | ||
|
|
||
| for _, opt := range options { | ||
| opt(config) | ||
| } | ||
|
|
||
| return config.executeNative() | ||
| } | ||
|
|
||
| func (c *requestConfig) executeNative() (*http.Response, error) { | ||
| // Build URL | ||
| fullURL := c.buildURL() | ||
|
|
||
| // Create HTTP client with custom transport | ||
| client := c.buildHTTPClient() | ||
|
|
||
| // Prepare request body | ||
| var bodyReader io.Reader | ||
| if c.body != "" { | ||
| bodyReader = bytes.NewBufferString(c.body) | ||
| } | ||
|
|
||
| // Create context with timeout | ||
| ctx := context.Background() | ||
| if c.connectionTimeout > 0 { | ||
| var cancel context.CancelFunc | ||
| ctx, cancel = context.WithTimeout(ctx, time.Duration(c.connectionTimeout)*time.Second) | ||
| defer cancel() | ||
| } | ||
|
|
||
| // Create request | ||
| req, err := http.NewRequestWithContext(ctx, c.method, fullURL, bodyReader) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create request: %w", err) | ||
| } | ||
|
|
||
| // Add headers | ||
| for key, values := range c.headers { | ||
| for _, value := range values { | ||
| // Host header must be set on req.Host, not in req.Header | ||
| if strings.EqualFold(key, "Host") { | ||
| req.Host = value | ||
| } else { | ||
| req.Header.Add(key, value) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Add cookies | ||
| if c.cookie != "" { | ||
| req.Header.Add("Cookie", c.cookie) | ||
| } | ||
|
|
||
| // Handle HEAD-only requests | ||
| if c.headersOnly { | ||
| req.Method = "HEAD" | ||
| } | ||
|
|
||
| // Execute request | ||
| if c.verbose { | ||
| fmt.Printf("> %s %s\n", req.Method, req.URL.String()) | ||
| fmt.Printf("> Host: %s\n", req.Host) | ||
| for k, v := range req.Header { | ||
| fmt.Printf("> %s: %s\n", k, strings.Join(v, ", ")) | ||
| } | ||
| } | ||
|
|
||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| if c.verbose { | ||
| fmt.Printf("Request failed: %v\n", err) | ||
| } | ||
| return nil, err | ||
| } | ||
|
|
||
| if c.verbose { | ||
| fmt.Printf("< HTTP %s\n", resp.Status) | ||
| for k, v := range resp.Header { | ||
| fmt.Printf("< %s: %s\n", k, strings.Join(v, ", ")) | ||
| } | ||
| } | ||
|
|
||
| return resp, nil | ||
| } | ||
|
|
||
| func (c *requestConfig) buildURL() string { | ||
| path := c.path | ||
| if path != "" && !strings.HasPrefix(path, "/") { | ||
| path = "/" + path | ||
| } | ||
|
|
||
| baseURL := fmt.Sprintf("%s://%s:%d%s", c.scheme, c.host, c.port, path) | ||
|
|
||
| if len(c.queryParameters) > 0 { | ||
| values := url.Values{} | ||
| for k, v := range c.queryParameters { | ||
| values.Add(k, v) | ||
| } | ||
| return fmt.Sprintf("%s?%s", baseURL, values.Encode()) | ||
| } | ||
|
|
||
| return baseURL | ||
| } | ||
|
|
||
| func (c *requestConfig) buildHTTPClient() *http.Client { | ||
| transport := &http.Transport{ | ||
| DialContext: c.buildDialer(), | ||
| } | ||
|
|
||
| // Configure TLS | ||
| if c.scheme == "https" || c.ignoreServerCert || c.sni != "" { | ||
| tlsConfig := &tls.Config{ | ||
| InsecureSkipVerify: c.ignoreServerCert, | ||
| } | ||
|
|
||
| if c.sni != "" { | ||
| tlsConfig.ServerName = c.sni | ||
| } | ||
|
|
||
| // Configure TLS version | ||
| if c.tlsVersion != "" { | ||
| tlsConfig.MinVersion = parseTLSVersion(c.tlsVersion) | ||
| } | ||
| if c.tlsMaxVersion != "" { | ||
| tlsConfig.MaxVersion = parseTLSVersion(c.tlsMaxVersion) | ||
| } | ||
|
|
||
| // Configure cipher suites (simplified) | ||
| if c.ciphers != "" { | ||
| // Note: Go's TLS implementation uses predefined cipher suites | ||
| // This would require parsing the cipher string and mapping to Go's constants | ||
| // For simplicity, this is left as a placeholder | ||
| } | ||
|
|
||
| // Configure curves (simplified) | ||
| if c.curves != "" { | ||
| // Similar to ciphers, this would require parsing and mapping | ||
| } | ||
|
|
||
| transport.TLSClientConfig = tlsConfig | ||
| } | ||
|
|
||
| // Configure HTTP version | ||
| if c.http2 { | ||
| // HTTP/2 is enabled by default in Go's transport | ||
| transport.ForceAttemptHTTP2 = true | ||
| } else if c.http11 { | ||
| // Disable HTTP/2 to force HTTP/1.1 | ||
| transport.ForceAttemptHTTP2 = false | ||
| transport.TLSNextProto = make(map[string]func(string, *tls.Conn) http.RoundTripper) | ||
| } | ||
|
|
||
| client := &http.Client{ | ||
| Transport: transport, | ||
| } | ||
|
|
||
| // Set timeout (client-level timeout) | ||
| if c.connectionTimeout > 0 { | ||
| client.Timeout = time.Duration(c.connectionTimeout) * time.Second | ||
| } | ||
|
|
||
| client.CheckRedirect = func(req *http.Request, via []*http.Request) error { | ||
| // Disable redirects | ||
| return http.ErrUseLastResponse | ||
| } | ||
| return client | ||
| } | ||
|
|
||
| func (c *requestConfig) buildDialer() func(context.Context, string, string) (net.Conn, error) { | ||
| dialer := &net.Dialer{ | ||
| Timeout: 30 * time.Second, | ||
| } | ||
|
|
||
| if c.connectionTimeout > 0 { | ||
| dialer.Timeout = time.Duration(c.connectionTimeout) * time.Second | ||
| } | ||
|
|
||
| // Handle IPv4/IPv6 restrictions | ||
| if c.ipv4Only { | ||
| return func(ctx context.Context, network, addr string) (net.Conn, error) { | ||
| return dialer.DialContext(ctx, "tcp4", addr) | ||
| } | ||
| } | ||
| if c.ipv6Only { | ||
| return func(ctx context.Context, network, addr string) (net.Conn, error) { | ||
| return dialer.DialContext(ctx, "tcp6", addr) | ||
| } | ||
| } | ||
|
|
||
| // Handle SNI with custom host resolution | ||
| // TODO | ||
| if c.sni != "" { | ||
| panic("sni is not implemented") | ||
| } | ||
|
|
||
| return dialer.DialContext | ||
| } | ||
|
|
||
| func parseTLSVersion(version string) uint16 { | ||
| switch version { | ||
| case "1.0": | ||
| return tls.VersionTLS10 | ||
| case "1.1": | ||
| return tls.VersionTLS11 | ||
| case "1.2": | ||
| return tls.VersionTLS12 | ||
| case "1.3": | ||
| return tls.VersionTLS13 | ||
| default: | ||
| return tls.VersionTLS12 // default | ||
| } | ||
| } | ||
This file contains hidden or 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,70 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "istio.io/istio/pkg/log" | ||
| "istio.io/istio/pkg/test/util/assert" | ||
| "istio.io/istio/pkg/test/util/retry" | ||
| "k8s.io/apimachinery/pkg/types" | ||
|
|
||
| "github.com/kgateway-dev/kgateway/v2/pkg/utils/requestutils/curl" | ||
| "github.com/kgateway-dev/kgateway/v2/test/e2e" | ||
| "github.com/kgateway-dev/kgateway/v2/test/gomega/matchers" | ||
| testmatchers "github.com/kgateway-dev/kgateway/v2/test/gomega/matchers" | ||
| ) | ||
|
|
||
| func SetupBaseConfig(ctx context.Context, t *testing.T, installation *e2e.TestInstallation, manifests ...string) { | ||
| for _, s := range log.Scopes() { | ||
| s.SetOutputLevel(log.DebugLevel) | ||
| } | ||
| err := installation.ClusterContext.IstioClient.ApplyYAMLFiles("", manifests...) | ||
| assert.NoError(t, err) | ||
| //for _, manifest := range manifests { | ||
| //err := installation.Actions.Kubectl().ApplyFile(ctx, manifest) | ||
| //} | ||
| } | ||
|
|
||
| func SetupBaseGateway(ctx context.Context, installation *e2e.TestInstallation, name types.NamespacedName) { | ||
| address := installation.Assertions.EventuallyGatewayAddress( | ||
| ctx, | ||
| name.Name, | ||
| name.Namespace, | ||
| ) | ||
| BaseGateway = Gateway{ | ||
| NamespacedName: name, | ||
| Address: address, | ||
| } | ||
| } | ||
|
|
||
| type Gateway struct { | ||
| types.NamespacedName | ||
| Address string | ||
| } | ||
|
|
||
| var BaseGateway Gateway | ||
|
|
||
| func (g *Gateway) Send(t *testing.T, match *testmatchers.HttpResponse, opts ...curl.Option) *http.Response { | ||
| fullOpts := append([]curl.Option{curl.WithHost(g.Address)}, opts...) | ||
| var resp *http.Response | ||
| retry.UntilSuccessOrFail(t, func() error { | ||
| r, err := curl.ExecuteRequest(fullOpts...) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| resp = r | ||
| mm := matchers.HaveHttpResponse(match) | ||
| success, err := mm.Match(resp) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if !success { | ||
| return fmt.Errorf("match failed: %v", mm.FailureMessage(resp)) | ||
| } | ||
| return nil | ||
| }) | ||
| return resp | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SNI support is not implemented and will panic at runtime if used. Either implement this feature or remove SNI as an option until it can be properly supported.