Skip to content

Commit c1bfbd2

Browse files
committed
test: print and save logs in github actions
Signed-off-by: Peter Wilcsinszky <[email protected]>
1 parent b9752af commit c1bfbd2

File tree

4 files changed

+74
-16
lines changed

4 files changed

+74
-16
lines changed

.github/workflows/e2e.yaml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ env:
1414

1515
jobs:
1616
build:
17-
name: Build
17+
name: Go end2end tests
1818
runs-on: ubuntu-latest
1919

2020
steps:
@@ -34,8 +34,16 @@ jobs:
3434
- name: Run e2e tests
3535
run: make test-e2e
3636

37+
- name: Archive Test Results
38+
if: always()
39+
uses: actions/upload-artifact@v3
40+
with:
41+
name: go-e2e-test-cluster-logs
42+
path: build/_test
43+
retention-days: 5
44+
3745
basic_flow:
38-
name: Basic flow
46+
name: Shell script tests with chart install
3947
runs-on: ubuntu-latest
4048

4149
steps:
@@ -93,4 +101,15 @@ jobs:
93101
run: hack/test.sh
94102

95103
- name: Print last 10k kubernetes logs from default and logging namespaces
96-
run: bin/stern -n default,logging ".*" --tail 10000 --no-follow
104+
if: always()
105+
run: |
106+
mkdir -p build/_test
107+
bin/stern -n default,logging ".*" --tail 100000 --no-follow > build/_test/cluster.logs
108+
109+
- name: Archive Test Results
110+
if: always()
111+
uses: actions/upload-artifact@v3
112+
with:
113+
name: script-e2e-test-cluster-logs
114+
path: build/_test
115+
retention-days: 5

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ test: generate fmt vet manifests ${ENVTEST_BINARY_ASSETS} ${KUBEBUILDER} ## Run
157157

158158
.PHONY: test-e2e
159159
test-e2e: ${KIND} docker-build generate fmt vet manifests stern ## Run E2E tests
160-
cd e2e && LOGGING_OPERATOR_IMAGE="${IMG}" go test -v -timeout ${E2E_TEST_TIMEOUT} ./...
160+
cd e2e && LOGGING_OPERATOR_IMAGE="${IMG}" PROJECT_DIR="$(PWD)" go test -v -timeout ${E2E_TEST_TIMEOUT} ./...
161161

162162
.PHONY: tidy
163163
tidy: ## Tidy Go modules

e2e/common/cluster.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,20 @@ import (
3434

3535
const defaultClusterName = "e2e-test"
3636

37-
var NamespacesUsed []string
38-
3937
type Cluster interface {
4038
cluster.Cluster
4139
LoadImages(images ...string) error
4240
Cleanup() error
43-
PrintLogs(int) error
41+
PrintLogs(config PrintLogConfig) error
42+
}
43+
44+
type PrintLogConfig struct {
45+
Namespaces []string
46+
FilePath string
47+
Limit int
4448
}
4549

46-
func WithCluster(t *testing.T, fn func(*testing.T, Cluster), opts ...cluster.Option) {
50+
func WithCluster(t *testing.T, fn func(*testing.T, Cluster), beforeCleanup func(*testing.T, Cluster) error, opts ...cluster.Option) {
4751
cluster, err := GetTestCluster(defaultClusterName, opts...)
4852
require.NoError(t, err)
4953

@@ -53,9 +57,7 @@ func WithCluster(t *testing.T, fn func(*testing.T, Cluster), opts ...cluster.Opt
5357
}()
5458

5559
defer func() {
56-
t.Log("CLUSTER LOGS START")
57-
assert.NoError(t, cluster.PrintLogs(10000))
58-
t.Log("CLUSTER LOGS END")
60+
assert.NoError(t, beforeCleanup(t, cluster))
5961
assert.NoError(t, cluster.Cleanup())
6062
cancel()
6163
require.NoError(t, DeleteTestCluster(defaultClusterName))
@@ -112,10 +114,16 @@ type kindCluster struct {
112114
clusterName string
113115
}
114116

115-
func (c kindCluster) PrintLogs(tail int) error {
116-
cmd := exec.Command("stern", "-n", strings.Join(NamespacesUsed, ","), ".*", "--no-follow", "--tail", cast.ToString(tail), "--kubeconfig", c.kubeconfigFilePath)
117-
cmd.Stdout = os.Stdout
117+
func (c kindCluster) PrintLogs(config PrintLogConfig) error {
118+
cmd := exec.Command("stern", "-n", strings.Join(config.Namespaces, ","), ".*", "--no-follow", "--tail", cast.ToString(config.Limit), "--kubeconfig", c.kubeconfigFilePath)
119+
f, err := os.Create(config.FilePath)
120+
if err != nil {
121+
return err
122+
}
123+
cmd.Stdout = f
118124
cmd.Stderr = os.Stderr
125+
126+
defer f.Close()
119127
return cmd.Run()
120128
}
121129

e2e/volumedrain/volumedrain_test.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ package volumedrain
1616

1717
import (
1818
"context"
19+
"fmt"
20+
"os"
1921
"os/exec"
22+
"path"
2023
"strings"
2124
"testing"
2225
"time"
@@ -43,9 +46,22 @@ import (
4346
"github.com/kube-logging/logging-operator/e2e/common/setup"
4447
)
4548

49+
var TestTempDir string
50+
51+
func init() {
52+
var ok bool
53+
TestTempDir, ok = os.LookupEnv("PROJECT_DIR")
54+
if !ok {
55+
TestTempDir = "../.."
56+
}
57+
TestTempDir = path.Join(TestTempDir, "build/_test")
58+
err := os.MkdirAll(TestTempDir, os.FileMode(0755))
59+
if err != nil {
60+
panic(err)
61+
}
62+
}
4663
func TestVolumeDrain_Downscale(t *testing.T) {
4764
ns := "testing-1"
48-
common.NamespacesUsed = []string{ns, "default"}
4965
common.WithCluster(t, func(t *testing.T, c common.Cluster) {
5066
setup.LoggingOperator(t, c, setup.LoggingOperatorOptionFunc(func(options *setup.LoggingOperatorOptions) {
5167
options.Config.DisableWebhook = true
@@ -183,6 +199,14 @@ func TestVolumeDrain_Downscale(t *testing.T) {
183199
pvc := common.Resource(new(corev1.PersistentVolumeClaim), ns, logging.Name+"-fluentd-buffer-"+fluentdReplicaName)
184200
require.NoError(t, c.GetClient().Get(ctx, client.ObjectKeyFromObject(pvc), pvc))
185201
assert.Equal(t, "drained", pvc.GetLabels()["logging.banzaicloud.io/drain-status"])
202+
}, func(t *testing.T, c common.Cluster) error {
203+
path := fmt.Sprintf("%s/cluster-%s.log", TestTempDir, t.Name())
204+
t.Logf("Printing cluster logs to %s", path)
205+
return c.PrintLogs(common.PrintLogConfig{
206+
Namespaces: []string{ns, "default"},
207+
FilePath: path,
208+
Limit: 100 * 1000,
209+
})
186210
}, func(o *cluster.Options) {
187211
if o.Scheme == nil {
188212
o.Scheme = runtime.NewScheme()
@@ -198,7 +222,6 @@ func TestVolumeDrain_Downscale(t *testing.T) {
198222

199223
func TestVolumeDrain_Downscale_DeleteVolume(t *testing.T) {
200224
ns := "testing-2"
201-
common.NamespacesUsed = []string{ns, "default"}
202225
common.WithCluster(t, func(t *testing.T, c common.Cluster) {
203226
setup.LoggingOperator(t, c, setup.LoggingOperatorOptionFunc(func(options *setup.LoggingOperatorOptions) {
204227
options.Config.DisableWebhook = true
@@ -335,6 +358,14 @@ func TestVolumeDrain_Downscale_DeleteVolume(t *testing.T) {
335358
require.Eventually(t, cond.ResourceShouldBeAbsent(t, c.GetClient(), common.Resource(new(corev1.Pod), ns, fluentdReplicaName)), 30*time.Second, time.Second/2)
336359

337360
require.Eventually(t, cond.ResourceShouldBeAbsent(t, c.GetClient(), common.Resource(new(corev1.PersistentVolumeClaim), ns, logging.Name+"-fluentd-buffer-"+fluentdReplicaName)), 30*time.Second, time.Second/2)
361+
}, func(t *testing.T, c common.Cluster) error {
362+
path := fmt.Sprintf("%s/cluster-%s.log", TestTempDir, t.Name())
363+
t.Logf("Printing cluster logs to %s", path)
364+
return c.PrintLogs(common.PrintLogConfig{
365+
Namespaces: []string{ns, "default"},
366+
FilePath: path,
367+
Limit: 100 * 1000,
368+
})
338369
}, func(o *cluster.Options) {
339370
if o.Scheme == nil {
340371
o.Scheme = runtime.NewScheme()

0 commit comments

Comments
 (0)