Skip to content

Commit 4a989b6

Browse files
committed
test: explicitly set kubeconfig for kubectl commands in e2e tests
Signed-off-by: Peter Wilcsinszky <[email protected]>
1 parent d91fe03 commit 4a989b6

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

e2e/common/cluster.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type Cluster interface {
3939
LoadImages(images ...string) error
4040
Cleanup() error
4141
PrintLogs(config PrintLogConfig) error
42+
KubeConfigFilePath() string
4243
}
4344

4445
type PrintLogConfig struct {
@@ -137,3 +138,7 @@ func (c kindCluster) LoadImages(images ...string) error {
137138
Name: c.clusterName,
138139
})
139140
}
141+
142+
func (c kindCluster) KubeConfigFilePath() string {
143+
return c.kubeconfigFilePath
144+
}

e2e/syslog-ng-aggregator/syslog_ng_aggregator_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ func TestSyslogNGIsRunningAndForwardingLogs(t *testing.T) {
166166

167167
require.Eventually(t, func() bool {
168168
cmd := exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "logs", consumer.PodKey.Name)
169+
cmd.Env = []string{
170+
fmt.Sprintf("KUBECONFIG=%s", c.KubeConfigFilePath()),
171+
}
169172
cmd.Stderr = os.Stderr
170173
rawOut, err := cmd.Output()
171174
if err != nil {

e2e/volumedrain/volumedrain_test.go

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

160160
require.Eventually(t, func() bool {
161-
rawOut, err := exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "logs", consumer.PodKey.Name).Output()
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
166+
rawOut, err := cmd.Output()
162167
if err != nil {
163168
t.Logf("failed to get log consumer logs: %v", err)
164169
return false
@@ -167,10 +172,20 @@ func TestVolumeDrain_Downscale(t *testing.T) {
167172
return strings.Contains(string(rawOut), "got request")
168173
}, 5*time.Minute, 2*time.Second)
169174

170-
require.NoError(t, exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "exec", consumer.PodKey.Name, "--", "curl", "-sS", "http://localhost:8082/off").Run())
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
180+
require.NoError(t, cmd.Run())
171181

172182
require.Eventually(t, func() bool {
173-
rawOut, err := exec.Command("kubectl", "-n", ns, "exec", fluentdReplicaName, "-c", "fluentd", "--", "ls", "-1", "/buffers").Output()
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
188+
rawOut, err := cmd.Output()
174189
if err != nil {
175190
t.Logf("failed to list buffer directory: %v", err)
176191
return false
@@ -191,7 +206,12 @@ func TestVolumeDrain_Downscale(t *testing.T) {
191206

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

194-
require.NoError(t, exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "exec", consumer.PodKey.Name, "--", "curl", "-sS", "http://localhost:8082/on").Run())
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
214+
require.NoError(t, cmd.Run())
195215

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

@@ -319,7 +339,12 @@ func TestVolumeDrain_Downscale_DeleteVolume(t *testing.T) {
319339
}))
320340

321341
require.Eventually(t, func() bool {
322-
rawOut, err := exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "logs", consumer.PodKey.Name).Output()
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
347+
rawOut, err := cmd.Output()
323348
if err != nil {
324349
t.Logf("failed to get log consumer logs: %v", err)
325350
return false
@@ -328,10 +353,20 @@ func TestVolumeDrain_Downscale_DeleteVolume(t *testing.T) {
328353
return strings.Contains(string(rawOut), "got request")
329354
}, 5*time.Minute, 2*time.Second)
330355

331-
require.NoError(t, exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "exec", consumer.PodKey.Name, "--", "curl", "-sS", "http://localhost:8082/off").Run())
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
361+
require.NoError(t, cmd.Run())
332362

333363
require.Eventually(t, func() bool {
334-
rawOut, err := exec.Command("kubectl", "-n", ns, "exec", fluentdReplicaName, "-c", "fluentd", "--", "ls", "-1", "/buffers").Output()
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
369+
rawOut, err := cmd.Output()
335370
if err != nil {
336371
t.Logf("failed to list buffer directory: %v", err)
337372
return false
@@ -352,7 +387,12 @@ func TestVolumeDrain_Downscale_DeleteVolume(t *testing.T) {
352387

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

355-
require.NoError(t, exec.Command("kubectl", "-n", consumer.PodKey.Namespace, "exec", consumer.PodKey.Name, "--", "curl", "-sS", "http://localhost:8082/on").Run())
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
395+
require.NoError(t, cmd.Run())
356396

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

0 commit comments

Comments
 (0)