Skip to content

Commit dfb0a3e

Browse files
committed
tests/kubectl: Add Port Forwarding functions
Port forwarding is needed in order to fetch the alert. This helper will be used in future commits where the alert test will be introduced Signed-off-by: Ram Lavi <ralavi@redhat.com>
1 parent 1b387a8 commit dfb0a3e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

tests/kubectl/kubectl.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package kubectl
22

33
import (
44
"bytes"
5+
"fmt"
56
"os"
67
"os/exec"
78
)
@@ -25,3 +26,24 @@ func getClusterRootDirectory() string {
2526
}
2627
return dir
2728
}
29+
30+
// StartPortForwardCommand starts a port-forward command in the background and returns the process
31+
func StartPortForwardCommand(namespace, podName string, sourcePort, targetPort int) (*exec.Cmd, error) {
32+
// #nosec G204 -- test code with controlled inputs
33+
cmd := exec.Command("./cluster/kubectl.sh", "port-forward", "-n", namespace,
34+
fmt.Sprintf("pod/%s", podName), fmt.Sprintf("%d:%d", sourcePort, targetPort))
35+
cmd.Dir = getClusterRootDirectory()
36+
37+
if err := cmd.Start(); err != nil {
38+
return nil, fmt.Errorf("failed to start port-forward: %w", err)
39+
}
40+
return cmd, nil
41+
}
42+
43+
// KillPortForwardCommand kills the port-forward process
44+
func KillPortForwardCommand(cmd *exec.Cmd) error {
45+
if cmd == nil || cmd.Process == nil {
46+
return nil
47+
}
48+
return cmd.Process.Kill()
49+
}

0 commit comments

Comments
 (0)