Skip to content

Commit e9e62d0

Browse files
authored
Merge pull request #5367 from sbueringer/pr-e2e-timestamps
🌱 E2E: Enable logging to a file with prefixed timestamps
2 parents 55e92ea + 59b35a6 commit e9e62d0

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

test/e2e/e2e_suite_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"sigs.k8s.io/cluster-api/test/framework"
3737
"sigs.k8s.io/cluster-api/test/framework/bootstrap"
3838
"sigs.k8s.io/cluster-api/test/framework/clusterctl"
39+
"sigs.k8s.io/cluster-api/test/framework/ginkgoextensions"
3940
)
4041

4142
// Test suite flags
@@ -49,6 +50,10 @@ var (
4950
// artifactFolder is the folder to store e2e test artifacts.
5051
artifactFolder string
5152

53+
// alsoLogToFile enables additional logging to the 'ginkgo-log.txt' file in the artifact folder.
54+
// These logs also contain timestamps.
55+
alsoLogToFile bool
56+
5257
// skipCleanup prevents cleanup of test resources e.g. for debug purposes.
5358
skipCleanup bool
5459
)
@@ -75,6 +80,7 @@ var (
7580
func init() {
7681
flag.StringVar(&configPath, "e2e.config", "", "path to the e2e config file")
7782
flag.StringVar(&artifactFolder, "e2e.artifacts-folder", "", "folder where e2e test artifact should be stored")
83+
flag.BoolVar(&alsoLogToFile, "e2e.also-log-to-file", true, "if true, ginkgo logs are additionally written to the `ginkgo-log.txt` file in the artifacts folder (including timestamps)")
7884
flag.BoolVar(&skipCleanup, "e2e.skip-resource-cleanup", false, "if true, the resource cleanup after tests will be skipped")
7985
flag.BoolVar(&useExistingCluster, "e2e.use-existing-cluster", false, "if true, the test uses the current cluster instead of creating a new one (default discovery rules apply)")
8086
}
@@ -87,6 +93,12 @@ func TestE2E(t *testing.T) {
8793

8894
RegisterFailHandler(Fail)
8995

96+
if alsoLogToFile {
97+
w, err := ginkgoextensions.EnableFileLogging(filepath.Join(artifactFolder, "ginkgo-log.txt"))
98+
Expect(err).ToNot(HaveOccurred())
99+
defer w.Close()
100+
}
101+
90102
junitReporter := framework.CreateJUnitReporterForProw(artifactFolder)
91103
RunSpecsWithDefaultAndCustomReporters(t, "capi-e2e", []Reporter{junitReporter})
92104
}

test/framework/ginkgoextensions/output.go

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,67 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
// Package ginkgoextensions extends ginko.
17+
// Package ginkgoextensions extends ginkgo.
1818
package ginkgoextensions
1919

2020
import (
2121
"fmt"
22+
"io"
23+
"os"
24+
"time"
2225

2326
"github.com/onsi/ginkgo"
27+
"github.com/pkg/errors"
2428
)
2529

2630
// TestOutput can be used for writing testing output.
2731
var TestOutput = ginkgo.GinkgoWriter
2832

29-
// Byf provides formatted output to the GinkoWriter.
33+
// Byf provides formatted output to the GinkgoWriter.
3034
func Byf(format string, a ...interface{}) {
3135
ginkgo.By(fmt.Sprintf(format, a...))
3236
}
37+
38+
type writerRedirecter interface {
39+
AndRedirectTo(writer io.Writer)
40+
}
41+
42+
// EnableFileLogging enables additional file logging.
43+
// Logs are written to the given path with timestamps.
44+
func EnableFileLogging(path string) (io.WriteCloser, error) {
45+
w, err := newFileWriter(path)
46+
if err != nil {
47+
return nil, errors.Wrapf(err, "failed to create fileWriter")
48+
}
49+
50+
ginkgoWriter, ok := ginkgo.GinkgoWriter.(writerRedirecter)
51+
if !ok {
52+
return nil, errors.Errorf("GinkgoWriter does not have an AndRedirectTo method")
53+
}
54+
55+
ginkgoWriter.AndRedirectTo(w)
56+
57+
return w, nil
58+
}
59+
60+
func newFileWriter(path string) (io.WriteCloser, error) {
61+
f, err := os.Create(path)
62+
if err != nil {
63+
return nil, errors.Wrap(err, "failed to create file")
64+
}
65+
return &fileWriter{
66+
file: f,
67+
}, nil
68+
}
69+
70+
type fileWriter struct {
71+
file *os.File
72+
}
73+
74+
func (w *fileWriter) Write(data []byte) (n int, err error) {
75+
return w.file.Write([]byte("[" + time.Now().Format(time.RFC3339) + "] " + string(data)))
76+
}
77+
78+
func (w *fileWriter) Close() error {
79+
return w.file.Close()
80+
}

0 commit comments

Comments
 (0)