Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojingchen committed Mar 15, 2019
1 parent 7c30239 commit 81c31ed
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
4 changes: 2 additions & 2 deletions tests/cmd/e2e/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ func main() {
LogLevel: "2",
}
if err := oa.CleanOperator(operatorInfo); err != nil {
oa.DumpAllLogs(operatorInfo, []*tests.TidbClusterInfo{})
oa.DumpAllLogs(operatorInfo, nil)
glog.Fatal(err)
}
if err := oa.DeployOperator(operatorInfo); err != nil {
oa.DumpAllLogs(operatorInfo, []*tests.TidbClusterInfo{})
oa.DumpAllLogs(operatorInfo, nil)
glog.Fatal(err)
}

Expand Down
34 changes: 24 additions & 10 deletions tests/log_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ func (oa *operatorActions) DumpAllLogs(operatorInfo *OperatorInfo, testClusters
dumpLog(fmt.Sprintf("kubectl get po -owide -n %s", operatorInfo.Namespace), resourceWriter)
dumpLog("kubectl get pv", resourceWriter)
dumpLog("kubectl get pv -oyaml", resourceWriter)
dumpedNamespace := map[string]bool{}
for _, testCluster := range testClusters {
dumpLog(fmt.Sprintf("kubectl get po,pvc,svc,cm,cronjobs,jobs,statefulsets,tidbclusters -owide -n %s", testCluster.Namespace), resourceWriter)
dumpLog(fmt.Sprintf("kubectl get po,pvc,svc,cm,cronjobs,jobs,statefulsets,tidbclusters -n %s -oyaml", testCluster.Namespace), resourceWriter)
if _, exist := dumpedNamespace[testCluster.Namespace]; !exist {
dumpLog(fmt.Sprintf("kubectl get po,pvc,svc,cm,cronjobs,jobs,statefulsets,tidbclusters -owide -n %s", testCluster.Namespace), resourceWriter)
dumpLog(fmt.Sprintf("kubectl get po,pvc,svc,cm,cronjobs,jobs,statefulsets,tidbclusters -n %s -oyaml", testCluster.Namespace), resourceWriter)
dumpedNamespace[testCluster.Namespace] = true
}
}

// dump operator components's log
Expand All @@ -49,16 +53,20 @@ func (oa *operatorActions) DumpAllLogs(operatorInfo *OperatorInfo, testClusters
}

// dump all test clusters's logs
dumpedNamespace = map[string]bool{}
for _, testCluster := range testClusters {
clusterPodList, err := oa.kubeCli.CoreV1().Pods(testCluster.Namespace).List(metav1.ListOptions{})
if err != nil {
return err
}
for _, pod := range clusterPodList.Items {
err := dumpPod(logPath, &pod)
if _, exist := dumpedNamespace[testCluster.Namespace]; !exist {
clusterPodList, err := oa.kubeCli.CoreV1().Pods(testCluster.Namespace).List(metav1.ListOptions{})
if err != nil {
return err
}
for _, pod := range clusterPodList.Items {
err := dumpPod(logPath, &pod)
if err != nil {
return err
}
}
dumpedNamespace[testCluster.Namespace] = true
}
}

Expand All @@ -76,8 +84,12 @@ func dumpPod(logPath string, pod *corev1.Pod) error {
return err
}
defer plogFile.Close()

logWriter := bufio.NewWriter(logFile)
plogWriter := bufio.NewWriter(plogFile)
defer logWriter.Flush()
defer plogWriter.Flush()

for _, c := range pod.Spec.Containers {
dumpLog(fmt.Sprintf("kubectl logs -n %s %s -c %s", pod.Namespace, pod.GetName(), c.Name), logWriter)
dumpLog(fmt.Sprintf("kubectl logs -n %s %s -c %s -p", pod.Namespace, pod.GetName(), c.Name), plogWriter)
Expand All @@ -88,9 +100,11 @@ func dumpPod(logPath string, pod *corev1.Pod) error {

func dumpLog(cmdStr string, writer *bufio.Writer) {
writer.WriteString(fmt.Sprintf("$ %s\n", cmdStr))
data, err := exec.Command("/bin/sh", "-c", "/usr/local/bin/"+cmdStr).CombinedOutput()
cmd := exec.Command("/bin/sh", "-c", "/usr/local/bin/"+cmdStr)
cmd.Stderr = writer
cmd.Stdout = writer
err := cmd.Run()
if err != nil {
writer.WriteString(err.Error())
}
writer.WriteString(string(data))
}

0 comments on commit 81c31ed

Please sign in to comment.