-
Notifications
You must be signed in to change notification settings - Fork 94
/
dump.go
81 lines (66 loc) · 2.03 KB
/
dump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
Copyright 2021 Banzai Cloud.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package clusterstate
import (
"fmt"
"os"
"path/filepath"
"github.com/go-logr/logr"
"github.com/onsi/ginkgo"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"github.com/banzaicloud/istio-operator/test/e2e/util"
)
type Dumper struct {
log logr.Logger
dumpDir string
}
const defaultLogDir = "logs"
func NewDumper(log logr.Logger) *Dumper {
return &Dumper{
log: log,
dumpDir: getDumpDir(log),
}
}
func (d Dumper) Dump(currentTest ginkgo.GinkgoTestDescription) {
testDumpDir := filepath.Join(append([]string{d.dumpDir}, currentTest.ComponentTexts...)...)
d.log.Info("Dumping cluster state and logs", "dir", testDumpDir)
err := dump(d.log, testDumpDir)
if err != nil {
panic(err)
}
}
func getDumpDir(log logr.Logger) string {
dumpDir := os.Getenv("E2E_LOG_DIR")
if dumpDir == "" {
logf.Log.Info(fmt.Sprintf("Env variable E2E_LOG_DIR is not set. Using \"%s\" as log dir", defaultLogDir))
dumpDir = defaultLogDir
}
dumpDir, err := filepath.Abs(dumpDir)
if err != nil {
panic(err)
}
log.Info(fmt.Sprintf("Log dir: %s", dumpDir))
return dumpDir
}
func dump(log logr.Logger, dir string) error {
script := os.Getenv("E2E_TEST_DUMP_SCRIPT")
command := fmt.Sprintf("%s \"%s\"", script, dir)
stdout, stderr, err := util.RunInShell(command)
if err != nil {
log.Error(err, "Failed to dump cluster state and logs. Stderr and stdout follows", "command", command)
log.Info("stderr:\n" + stderr.String())
log.Info("stdout:\n" + stdout.String())
return err
}
return nil
}