From baf51b119ef3984890593552401dd75476417905 Mon Sep 17 00:00:00 2001 From: Daniel F B Morinigo Date: Fri, 7 Feb 2025 14:39:11 +0800 Subject: [PATCH] fix: Issue in eventlisteners e2e when kubernetes host has a path Previously having a prefix into the server url would generate an error during testing due to url parsing of host with embeded path: parse "https://k8s.example.com%2Fkubernetes%2Fpath/api/v1/namespaces/arrakis-t7ss8/pods/el-my-eventlistener-848756bd88-sg8fv/portforward": invalid URL escape "%2F" Previously having a prefix into the server url would generate an error during testing due to url parsing of host with embeded path: Add more configuration descriptions and cases in test/README --- test/README.md | 69 ++++++++++++++++++++++++++++++++++++++ test/eventlistener_test.go | 16 ++++++--- 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/test/README.md b/test/README.md index ade812b8f..c989caa6f 100644 --- a/test/README.md +++ b/test/README.md @@ -31,6 +31,73 @@ which need `-tags=e2e` to be enabled. Environment variables used by end to end tests: - `KO_DOCKER_REPO` - Set this to an image registry your tests can push images to +**kubeconfig**: + +- Use `--kubeconfig` flag pointing to a kubeconfig file, defaults to `~/.kube/config`. +or +- `KUBECONFIG` - Set this environment variable with the path to a kubeconfig file that will be used when [running](#running) tests + +Setting up the configuration file may differ between environments. Here is an example using a Kubernetes cluster behind a path `/k/cluster`: + +```yaml +apiVersion: v1 +clusters: +- cluster: + certificate-authority-data: [REDACTED] + server: https://k8s.example.com/k/cluster + name: proxy +contexts: +- context: + cluster: proxy + namespace: default + user: kubeconfig-user + name: proxy +current-context: proxy +kind: Config +preferences: {} +users: +- name: kubeconfig-user + user: + token: [REDACTED] +``` + +And another example using [minikube](https://minikube.sigs.k8s.io/): + +```yaml +apiVersion: v1 +clusters: +- cluster: + certificate-authority: "[REDACTED]" + extensions: + - extension: + provider: minikube.sigs.k8s.io + version: v1.35.0 + name: cluster_info + server: https://127.0.0.1:32771 + name: minikube +contexts: +- context: + cluster: minikube + extensions: + - extension: + provider: minikube.sigs.k8s.io + version: v1.35.0 + name: context_info + namespace: default + user: minikube + name: minikube +current-context: minikube +kind: Config +preferences: {} +users: +- name: minikube + user: {} # redacted +``` + +**Configuration issues**: + +- Having multiple `config` files in `KUBECONFIG` (i.e., `KUBECONFIG=$HOME/.kube/config:$HOME/.kube/config-2`) will cause test cases to fail. You can either assign `KUBECONFIG` to a single file or use the `--kubeconfig` flag when [running](#running) tests. + ### Running @@ -95,6 +162,8 @@ To run the YAML e2e tests, run the following command: ./test/e2e-tests-yaml.sh ``` +### Configuration + ### Adding integration tests In the [`test`](/test/) dir you will find several libraries in the `test` diff --git a/test/eventlistener_test.go b/test/eventlistener_test.go index c6f4db263..f326d90ba 100644 --- a/test/eventlistener_test.go +++ b/test/eventlistener_test.go @@ -29,7 +29,6 @@ import ( "os" "path/filepath" "strconv" - "strings" "testing" "time" @@ -441,6 +440,7 @@ func TestEventListenerCreate(t *testing.T) { defer func() { close(stopChan) }() + go func(stopChan chan struct{}, errChan chan error) { config, err := clientcmd.BuildConfigFromFlags("", knativetest.Flags.Kubeconfig) if err != nil { @@ -453,9 +453,17 @@ func TestEventListenerCreate(t *testing.T) { return } - path := fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/portforward", namespace, podName) - hostIP := strings.TrimPrefix(config.Host, "https://") - serverURL := url.URL{Scheme: "https", Path: path, Host: hostIP} + // parsing the host to split host and path for + // cases where the host has a path prefix like https://host:8433/some/path + hostURL, err := url.Parse(config.Host) + if err != nil { + errChan <- err + return + } + + // adds back the already present path if it exists + path := fmt.Sprintf("%s/api/v1/namespaces/%s/pods/%s/portforward", hostURL.Path, namespace, podName) + serverURL := url.URL{Scheme: "https", Path: path, Host: hostURL.Host} dialer := spdy.NewDialer(upgrader, &http.Client{Transport: roundTripper}, http.MethodPost, &serverURL) out, errOut := new(Buffer), new(Buffer) readyChan := make(chan struct{}, 1)