Skip to content

Commit

Permalink
ci: chaos test could not directly read error.log (#4320)
Browse files Browse the repository at this point in the history
Signed-off-by: yiyiyimu <[email protected]>
  • Loading branch information
Yiyiyimu authored May 27, 2021
1 parent 0746f81 commit 9a40fd7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions t/chaos/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions t/chaos/kill-etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})

Expand All @@ -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())
})

Expand Down
20 changes: 20 additions & 0 deletions t/chaos/kube_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 9a40fd7

Please sign in to comment.