This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 9a40fd7  ci: chaos test could not directly read error.log (#4320)
9a40fd7 is described below

commit 9a40fd7cb497974f9d7bd75f128eed5dd238cdc6
Author: Shuyang Wu <[email protected]>
AuthorDate: Thu May 27 08:09:31 2021 -0400

    ci: chaos test could not directly read error.log (#4320)
    
    Signed-off-by: yiyiyimu <[email protected]>
---
 t/chaos/go.mod            |  1 +
 t/chaos/kill-etcd_test.go |  6 ++++--
 t/chaos/kube_utils.go     | 20 ++++++++++++++++++++
 3 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/t/chaos/go.mod b/t/chaos/go.mod
index 2db9fac..336be32 100644
--- a/t/chaos/go.mod
+++ b/t/chaos/go.mod
@@ -4,6 +4,7 @@ require (
        github.com/chaos-mesh/chaos-mesh v1.1.1
        github.com/gavv/httpexpect/v2 v2.1.0
        github.com/onsi/gomega v1.9.0
+       github.com/pkg/errors v0.9.1
        k8s.io/api v0.17.0
        k8s.io/apimachinery v0.17.0
        k8s.io/client-go v0.17.0
diff --git a/t/chaos/kill-etcd_test.go b/t/chaos/kill-etcd_test.go
index f001b86..62a4578 100644
--- a/t/chaos/kill-etcd_test.go
+++ b/t/chaos/kill-etcd_test.go
@@ -93,7 +93,8 @@ func TestGetSuccessWhenEtcdKilled(t *testing.T) {
        apisixPod := getPod(g, cliSet.ctrlCli, metav1.NamespaceDefault, 
listOption)
 
        t.Run("error log not contains etcd error", func(t *testing.T) {
-               errorLog := execInPod(g, cliSet.kubeCli, apisixPod, "cat 
logs/error.log")
+               errorLog, err := log(apisixPod, cliSet.kubeCli)
+               g.Expect(err).To(BeNil())
                g.Expect(strings.Contains(errorLog, "failed to fetch data from 
etcd")).To(BeFalse())
        })
 
@@ -110,7 +111,8 @@ func TestGetSuccessWhenEtcdKilled(t *testing.T) {
        testPrometheusEtcdMetric(e, 0)
 
        t.Run("error log contains etcd error", func(t *testing.T) {
-               errorLog := execInPod(g, cliSet.kubeCli, apisixPod, "cat 
logs/error.log")
+               errorLog, err := log(apisixPod, cliSet.kubeCli)
+               g.Expect(err).To(BeNil())
                g.Expect(strings.Contains(errorLog, "failed to fetch data from 
etcd")).To(BeTrue())
        })
 
diff --git a/t/chaos/kube_utils.go b/t/chaos/kube_utils.go
index f672f25..13e5b8a 100644
--- a/t/chaos/kube_utils.go
+++ b/t/chaos/kube_utils.go
@@ -21,10 +21,12 @@ import (
        "bytes"
        "context"
        "fmt"
+       "io"
        "strings"
 
        "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
        . "github.com/onsi/gomega"
+       "github.com/pkg/errors"
        corev1 "k8s.io/api/core/v1"
        "k8s.io/apimachinery/pkg/runtime"
        "k8s.io/client-go/kubernetes"
@@ -100,3 +102,21 @@ func execInPod(g *WithT, cli *kubernetes.Clientset, pod 
*corev1.Pod, cmd string)
        }
        return stdout.String()
 }
+
+func log(pod *corev1.Pod, c *kubernetes.Clientset) (string, error) {
+       podLogOpts := corev1.PodLogOptions{}
+
+       req := c.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &podLogOpts)
+       podLogs, err := req.Stream()
+       if err != nil {
+               return "", errors.Wrapf(err, "failed to open log stream for pod 
%s/%s", pod.GetNamespace(), pod.GetName())
+       }
+       defer podLogs.Close()
+
+       buf := new(bytes.Buffer)
+       _, err = io.Copy(buf, podLogs)
+       if err != nil {
+               return "", errors.Wrapf(err, "failed to copy information from 
podLogs to buf")
+       }
+       return buf.String(), nil
+}

Reply via email to