Skip to content

Commit 46c3ffc

Browse files
committed
test: extract CmdEnv to be reusable
Signed-off-by: Peter Wilcsinszky <[email protected]>
1 parent 4a989b6 commit 46c3ffc

File tree

3 files changed

+18
-45
lines changed

3 files changed

+18
-45
lines changed

e2e/common/cluster.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package common
1616

1717
import (
1818
"context"
19+
"fmt"
1920
"os"
2021
"os/exec"
2122
"strings"
@@ -109,6 +110,14 @@ func DeleteTestCluster(clusterName string) error {
109110
}), "deleting kind cluster", "clusterName", clusterName)
110111
}
111112

113+
func CmdEnv(cmd *exec.Cmd, c Cluster) *exec.Cmd {
114+
cmd.Env = []string{
115+
fmt.Sprintf("KUBECONFIG=%s", c.KubeConfigFilePath()),
116+
}
117+
cmd.Stderr = os.Stderr
118+
return cmd
119+
}
120+
112121
type kindCluster struct {
113122
cluster.Cluster
114123
kubeconfigFilePath string

e2e/syslog-ng-aggregator/syslog_ng_aggregator_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,7 @@ func TestSyslogNGIsRunningAndForwardingLogs(t *testing.T) {
165165
}))
166166

167167
require.Eventually(t, func() bool {
168-
cmd := exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "logs", consumer.PodKey.Name)
169-
cmd.Env = []string{
170-
fmt.Sprintf("KUBECONFIG=%s", c.KubeConfigFilePath()),
171-
}
172-
cmd.Stderr = os.Stderr
168+
cmd := common.CmdEnv(exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "logs", consumer.PodKey.Name), c)
173169
rawOut, err := cmd.Output()
174170
if err != nil {
175171
t.Logf("failed to get log consumer logs: %+v %s", err, rawOut)

e2e/volumedrain/volumedrain_test.go

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,7 @@ func TestVolumeDrain_Downscale(t *testing.T) {
158158
}))
159159

160160
require.Eventually(t, func() bool {
161-
cmd := exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "logs", consumer.PodKey.Name)
162-
cmd.Env = []string{
163-
fmt.Sprintf("KUBECONFIG=%s", c.KubeConfigFilePath()),
164-
}
165-
cmd.Stderr = os.Stderr
161+
cmd := common.CmdEnv(exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "logs", consumer.PodKey.Name), c)
166162
rawOut, err := cmd.Output()
167163
if err != nil {
168164
t.Logf("failed to get log consumer logs: %v", err)
@@ -172,19 +168,11 @@ func TestVolumeDrain_Downscale(t *testing.T) {
172168
return strings.Contains(string(rawOut), "got request")
173169
}, 5*time.Minute, 2*time.Second)
174170

175-
cmd := exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "exec", consumer.PodKey.Name, "--", "curl", "-sS", "http://localhost:8082/off")
176-
cmd.Env = []string{
177-
fmt.Sprintf("KUBECONFIG=%s", c.KubeConfigFilePath()),
178-
}
179-
cmd.Stderr = os.Stderr
171+
cmd := common.CmdEnv(exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "exec", consumer.PodKey.Name, "--", "curl", "-sS", "http://localhost:8082/off"), c)
180172
require.NoError(t, cmd.Run())
181173

182174
require.Eventually(t, func() bool {
183-
cmd := exec.Command("kubectl", "-n", ns, "exec", fluentdReplicaName, "-c", "fluentd", "--", "ls", "-1", "/buffers")
184-
cmd.Env = []string{
185-
fmt.Sprintf("KUBECONFIG=%s", c.KubeConfigFilePath()),
186-
}
187-
cmd.Stderr = os.Stderr
175+
cmd := common.CmdEnv(exec.Command("kubectl", "-n", ns, "exec", fluentdReplicaName, "-c", "fluentd", "--", "ls", "-1", "/buffers"), c)
188176
rawOut, err := cmd.Output()
189177
if err != nil {
190178
t.Logf("failed to list buffer directory: %v", err)
@@ -206,11 +194,7 @@ func TestVolumeDrain_Downscale(t *testing.T) {
206194

207195
require.Eventually(t, cond.PodShouldBeRunning(t, c.GetClient(), client.ObjectKey{Namespace: ns, Name: fluentdReplicaName}), 30*time.Second, time.Second/2)
208196

209-
cmd = exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "exec", consumer.PodKey.Name, "--", "curl", "-sS", "http://localhost:8082/on")
210-
cmd.Env = []string{
211-
fmt.Sprintf("KUBECONFIG=%s", c.KubeConfigFilePath()),
212-
}
213-
cmd.Stderr = os.Stderr
197+
cmd = common.CmdEnv(exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "exec", consumer.PodKey.Name, "--", "curl", "-sS", "http://localhost:8082/on"), c)
214198
require.NoError(t, cmd.Run())
215199

216200
require.Eventually(t, cond.ResourceShouldBeAbsent(t, c.GetClient(), common.Resource(new(batchv1.Job), ns, drainerJobName)), 5*time.Minute, 30*time.Second)
@@ -339,11 +323,7 @@ func TestVolumeDrain_Downscale_DeleteVolume(t *testing.T) {
339323
}))
340324

341325
require.Eventually(t, func() bool {
342-
cmd := exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "logs", consumer.PodKey.Name)
343-
cmd.Env = []string{
344-
fmt.Sprintf("KUBECONFIG=%s", c.KubeConfigFilePath()),
345-
}
346-
cmd.Stderr = os.Stderr
326+
cmd := common.CmdEnv(exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "logs", consumer.PodKey.Name), c)
347327
rawOut, err := cmd.Output()
348328
if err != nil {
349329
t.Logf("failed to get log consumer logs: %v", err)
@@ -353,19 +333,11 @@ func TestVolumeDrain_Downscale_DeleteVolume(t *testing.T) {
353333
return strings.Contains(string(rawOut), "got request")
354334
}, 5*time.Minute, 2*time.Second)
355335

356-
cmd := exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "exec", consumer.PodKey.Name, "--", "curl", "-sS", "http://localhost:8082/off")
357-
cmd.Env = []string{
358-
fmt.Sprintf("KUBECONFIG=%s", c.KubeConfigFilePath()),
359-
}
360-
cmd.Stderr = os.Stderr
336+
cmd := common.CmdEnv(exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "exec", consumer.PodKey.Name, "--", "curl", "-sS", "http://localhost:8082/off"), c)
361337
require.NoError(t, cmd.Run())
362338

363339
require.Eventually(t, func() bool {
364-
cmd := exec.Command("kubectl", "-n", ns, "exec", fluentdReplicaName, "-c", "fluentd", "--", "ls", "-1", "/buffers")
365-
cmd.Env = []string{
366-
fmt.Sprintf("KUBECONFIG=%s", c.KubeConfigFilePath()),
367-
}
368-
cmd.Stderr = os.Stderr
340+
cmd := common.CmdEnv(exec.Command("kubectl", "-n", ns, "exec", fluentdReplicaName, "-c", "fluentd", "--", "ls", "-1", "/buffers"), c)
369341
rawOut, err := cmd.Output()
370342
if err != nil {
371343
t.Logf("failed to list buffer directory: %v", err)
@@ -387,11 +359,7 @@ func TestVolumeDrain_Downscale_DeleteVolume(t *testing.T) {
387359

388360
require.Eventually(t, cond.PodShouldBeRunning(t, c.GetClient(), client.ObjectKey{Namespace: ns, Name: fluentdReplicaName}), 30*time.Second, time.Second/2)
389361

390-
cmd = exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "exec", consumer.PodKey.Name, "--", "curl", "-sS", "http://localhost:8082/on")
391-
cmd.Env = []string{
392-
fmt.Sprintf("KUBECONFIG=%s", c.KubeConfigFilePath()),
393-
}
394-
cmd.Stderr = os.Stderr
362+
cmd = common.CmdEnv(exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "exec", consumer.PodKey.Name, "--", "curl", "-sS", "http://localhost:8082/on"), c)
395363
require.NoError(t, cmd.Run())
396364

397365
require.Eventually(t, cond.ResourceShouldBeAbsent(t, c.GetClient(), common.Resource(new(batchv1.Job), ns, drainerJobName)), 5*time.Minute, 30*time.Second)

0 commit comments

Comments
 (0)