-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow access to pods using port forwarder (#249)
* Allow access to pods using port forwarder Signed-off-by: Pablo Chacin <pablochacin@gmail.com>
- Loading branch information
1 parent
49d65e8
commit 796b721
Showing
2 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,101 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
package e2e | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/grafana/xk6-disruptor/pkg/kubernetes" | ||
"github.com/grafana/xk6-disruptor/pkg/testutils/e2e/cluster" | ||
"github.com/grafana/xk6-disruptor/pkg/testutils/e2e/deploy" | ||
"github.com/grafana/xk6-disruptor/pkg/testutils/e2e/kubectl" | ||
"github.com/grafana/xk6-disruptor/pkg/testutils/e2e/kubernetes/namespace" | ||
"github.com/grafana/xk6-disruptor/pkg/testutils/kubernetes/builders" | ||
) | ||
|
||
func Test_Kubectl(t *testing.T) { | ||
cluster, err := cluster.BuildE2eCluster( | ||
t, | ||
cluster.DefaultE2eClusterConfig(), | ||
cluster.WithName("e2e-kubectl"), | ||
cluster.WithIngressPort(30087), | ||
) | ||
if err != nil { | ||
t.Errorf("failed to create cluster: %v", err) | ||
return | ||
} | ||
|
||
k8s, err := kubernetes.NewFromKubeconfig(cluster.Kubeconfig()) | ||
if err != nil { | ||
t.Errorf("error creating kubernetes client: %v", err) | ||
return | ||
} | ||
|
||
// Test Wait Pod Running | ||
t.Run("Test local random port", func(t *testing.T) { | ||
namespace, err := namespace.CreateTestNamespace(context.TODO(), t, k8s.Client()) | ||
if err != nil { | ||
t.Errorf("failed to create test namespace: %v", err) | ||
return | ||
} | ||
|
||
// Deploy nginx | ||
nginx := builders.NewPodBuilder("nginx"). | ||
WithContainer( | ||
*builders.NewContainerBuilder("nginx"). | ||
WithImage("nginx"). | ||
WithPort("http", 80). | ||
Build(), | ||
). | ||
Build() | ||
|
||
err = deploy.RunPod(k8s, namespace, nginx, 20 * time.Second) | ||
if err != nil { | ||
t.Errorf("failed to create test pod: %v", err) | ||
return | ||
} | ||
|
||
client, err := kubectl.NewFromKubeconfig(context.TODO(), cluster.Kubeconfig() ) | ||
if err != nil { | ||
t.Errorf("failed to create kubectl client: %v", err) | ||
return | ||
} | ||
|
||
ctx, stopper := context.WithCancel(context.TODO()) | ||
// ensure por forwarder is cancelled | ||
defer stopper() | ||
|
||
port, err := client.ForwardPodPort(ctx, namespace, nginx.GetName(), 80) | ||
if err != nil { | ||
t.Errorf("failed to forward local port: %v", err) | ||
return | ||
} | ||
|
||
url := fmt.Sprintf("http://localhost:%d", port) | ||
request, err := http.NewRequest("GET", url, bytes.NewReader([]byte{})) | ||
if err != nil { | ||
t.Errorf("failed to create request: %v", err) | ||
return | ||
} | ||
|
||
resp, err := http.DefaultClient.Do(request) | ||
if err != nil { | ||
t.Errorf("failed make request: %v", err) | ||
return | ||
} | ||
defer func() { | ||
_ = resp.Body.Close() | ||
}() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
t.Errorf("expected status code %d but %d received", http.StatusOK, resp.StatusCode) | ||
return | ||
} | ||
}) | ||
} |
This file contains 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