Skip to content

Commit b9752af

Browse files
committed
e2e: add step at the end of the e2e tests to print all logs (with a high hard limit)
Signed-off-by: Peter Wilcsinszky <[email protected]>
1 parent 3d13f3d commit b9752af

File tree

6 files changed

+70
-20
lines changed

6 files changed

+70
-20
lines changed

.github/workflows/e2e.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ jobs:
5858
- name: Checkout code
5959
uses: actions/checkout@v3
6060

61+
- name: Stern
62+
run: make stern && test -x bin/stern
63+
6164
- name: Build controller image
6265
run: make docker-build IMG='controller:local'
6366

@@ -88,3 +91,6 @@ jobs:
8891
8992
- name: Test
9093
run: hack/test.sh
94+
95+
- name: Print last 10k kubernetes logs from default and logging namespaces
96+
run: bin/stern -n default,logging ".*" --tail 10000 --no-follow

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ test: generate fmt vet manifests ${ENVTEST_BINARY_ASSETS} ${KUBEBUILDER} ## Run
156156
ENVTEST_BINARY_ASSETS=${ENVTEST_BINARY_ASSETS} go test ./controllers/extensions/... ./pkg/... -coverprofile cover.out
157157

158158
.PHONY: test-e2e
159-
test-e2e: ${KIND} docker-build generate fmt vet manifests ## Run E2E tests
160-
cd e2e && LOGGING_OPERATOR_IMAGE="${IMG}" go test -timeout ${E2E_TEST_TIMEOUT} ./...
159+
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} ./...
161161

162162
.PHONY: tidy
163163
tidy: ## Tidy Go modules
@@ -229,6 +229,9 @@ ${SETUP_ENVTEST}: VERSION := latest
229229
${SETUP_ENVTEST}: | ${BIN}
230230
GOBIN=${BIN} go install ${IMPORT_PATH}@${VERSION}
231231

232+
stern: | ${BIN}
233+
GOBIN=${BIN} go install github.com/stern/stern@latest
234+
232235
${ENVTEST_BIN_DIR}: | ${BIN}
233236
mkdir -p $@
234237

e2e/common/cluster.go

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

1717
import (
1818
"context"
19+
"os"
20+
"os/exec"
21+
"strings"
1922
"testing"
2023

2124
"emperror.dev/errors"
25+
"github.com/spf13/cast"
26+
"github.com/stretchr/testify/assert"
2227
"github.com/stretchr/testify/require"
28+
"k8s.io/client-go/rest"
2329
"k8s.io/client-go/tools/clientcmd"
2430
"sigs.k8s.io/controller-runtime/pkg/cluster"
2531

@@ -28,9 +34,13 @@ import (
2834

2935
const defaultClusterName = "e2e-test"
3036

37+
var NamespacesUsed []string
38+
3139
type Cluster interface {
3240
cluster.Cluster
3341
LoadImages(images ...string) error
42+
Cleanup() error
43+
PrintLogs(int) error
3444
}
3545

3646
func WithCluster(t *testing.T, fn func(*testing.T, Cluster), opts ...cluster.Option) {
@@ -43,6 +53,10 @@ func WithCluster(t *testing.T, fn func(*testing.T, Cluster), opts ...cluster.Opt
4353
}()
4454

4555
defer func() {
56+
t.Log("CLUSTER LOGS START")
57+
assert.NoError(t, cluster.PrintLogs(10000))
58+
t.Log("CLUSTER LOGS END")
59+
assert.NoError(t, cluster.Cleanup())
4660
cancel()
4761
require.NoError(t, DeleteTestCluster(defaultClusterName))
4862
}()
@@ -51,22 +65,38 @@ func WithCluster(t *testing.T, fn func(*testing.T, Cluster), opts ...cluster.Opt
5165
}
5266

5367
func GetTestCluster(clusterName string, opts ...cluster.Option) (Cluster, error) {
54-
kubeconfig, err := KindClusterKubeconfig(clusterName)
68+
var err error
69+
var kubeconfig []byte
70+
var kubeconfigFile *os.File
71+
var clientCfg clientcmd.ClientConfig
72+
var restCfg *rest.Config
73+
var c cluster.Cluster
74+
75+
kubeconfig, err = KindClusterKubeconfig(clusterName)
5576
if err != nil {
5677
return nil, errors.WrapIfWithDetails(err, "getting kubeconfig of kind cluster", "clusterName", clusterName)
5778
}
58-
clientCfg, err := clientcmd.NewClientConfigFromBytes(kubeconfig)
79+
kubeconfigFile, err = os.CreateTemp("", "kind-kind-kubeconfig")
80+
if err != nil {
81+
return nil, errors.WrapIfWithDetails(err, "unable to create temp file for kubeconfig", "clusterName", clusterName)
82+
}
83+
err = os.WriteFile(kubeconfigFile.Name(), kubeconfig, os.FileMode(0600))
84+
if err != nil {
85+
return nil, errors.WrapIfWithDetails(err, "failed to write kubeconfig", "clusterName", clusterName, "path", kubeconfigFile.Name())
86+
}
87+
clientCfg, err = clientcmd.NewClientConfigFromBytes(kubeconfig)
5988
if err != nil {
6089
return nil, errors.WrapIfWithDetails(err, "creating client config from kubeconfig bytes", "kubeconfig", kubeconfig)
6190
}
62-
restCfg, err := clientCfg.ClientConfig()
91+
restCfg, err = clientCfg.ClientConfig()
6392
if err != nil {
6493
return nil, errors.WrapIfWithDetails(err, "creating rest config from client config", "cfg", clientCfg)
6594
}
66-
c, err := cluster.New(restCfg, opts...)
95+
c, err = cluster.New(restCfg, opts...)
6796
return &kindCluster{
68-
Cluster: c,
69-
clusterName: clusterName,
97+
Cluster: c,
98+
kubeconfigFilePath: kubeconfigFile.Name(),
99+
clusterName: clusterName,
70100
}, errors.WrapIfWithDetails(err, "creating cluster with rest config", "cfg", restCfg)
71101
}
72102

@@ -78,7 +108,19 @@ func DeleteTestCluster(clusterName string) error {
78108

79109
type kindCluster struct {
80110
cluster.Cluster
81-
clusterName string
111+
kubeconfigFilePath string
112+
clusterName string
113+
}
114+
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
118+
cmd.Stderr = os.Stderr
119+
return cmd.Run()
120+
}
121+
122+
func (c kindCluster) Cleanup() error {
123+
return os.Remove(c.kubeconfigFilePath)
82124
}
83125

84126
func (c kindCluster) LoadImages(images ...string) error {

e2e/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/cisco-open/operator-tools v0.29.0
88
github.com/go-logr/logr v1.2.4
99
github.com/kube-logging/logging-operator/pkg/sdk v0.9.1
10+
github.com/spf13/cast v1.5.0
1011
github.com/stretchr/testify v1.8.2
1112
k8s.io/api v0.26.4
1213
k8s.io/apiextensions-apiserver v0.26.4
@@ -56,7 +57,6 @@ require (
5657
github.com/prometheus/client_model v0.3.0 // indirect
5758
github.com/prometheus/common v0.37.0 // indirect
5859
github.com/prometheus/procfs v0.8.0 // indirect
59-
github.com/spf13/cast v1.5.0 // indirect
6060
github.com/spf13/pflag v1.0.5 // indirect
6161
github.com/wayneashleyberry/terminal-dimensions v1.0.0 // indirect
6262
go.uber.org/atomic v1.7.0 // indirect

e2e/volumedrain/volumedrain_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import (
2222
"time"
2323

2424
"github.com/cisco-open/operator-tools/pkg/utils"
25-
"github.com/kube-logging/logging-operator/pkg/sdk/logging/api/v1beta1"
26-
"github.com/kube-logging/logging-operator/pkg/sdk/logging/model/output"
2725
"github.com/stretchr/testify/assert"
2826
"github.com/stretchr/testify/require"
2927
appsv1 "k8s.io/api/apps/v1"
@@ -37,15 +35,18 @@ import (
3735
"sigs.k8s.io/controller-runtime/pkg/client"
3836
"sigs.k8s.io/controller-runtime/pkg/cluster"
3937

38+
"github.com/kube-logging/logging-operator/pkg/sdk/logging/api/v1beta1"
39+
"github.com/kube-logging/logging-operator/pkg/sdk/logging/model/output"
40+
4041
"github.com/kube-logging/logging-operator/e2e/common"
4142
"github.com/kube-logging/logging-operator/e2e/common/cond"
4243
"github.com/kube-logging/logging-operator/e2e/common/setup"
4344
)
4445

4546
func TestVolumeDrain_Downscale(t *testing.T) {
47+
ns := "testing-1"
48+
common.NamespacesUsed = []string{ns, "default"}
4649
common.WithCluster(t, func(t *testing.T, c common.Cluster) {
47-
ns := "testing-1"
48-
4950
setup.LoggingOperator(t, c, setup.LoggingOperatorOptionFunc(func(options *setup.LoggingOperatorOptions) {
5051
options.Config.DisableWebhook = true
5152
options.Config.Namespace = ns
@@ -196,9 +197,9 @@ func TestVolumeDrain_Downscale(t *testing.T) {
196197
}
197198

198199
func TestVolumeDrain_Downscale_DeleteVolume(t *testing.T) {
200+
ns := "testing-2"
201+
common.NamespacesUsed = []string{ns, "default"}
199202
common.WithCluster(t, func(t *testing.T, c common.Cluster) {
200-
ns := "testing-2"
201-
202203
setup.LoggingOperator(t, c, setup.LoggingOperatorOptionFunc(func(options *setup.LoggingOperatorOptions) {
203204
options.Config.DisableWebhook = true
204205
options.Config.Namespace = ns

hack/test.sh

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ function main()
1616
helm_deploy_logging_operator
1717
configure_logging
1818

19-
20-
2119
wait_for_log_files "${mc_pod}" 300
2220
print_logs "${mc_pod}"
2321
}
@@ -53,7 +51,7 @@ function helm_add_repo()
5351
function helm_deploy_logging_operator()
5452
{
5553
# TODO: remove version once there is a semver stable version in the repo
56-
helm install \
54+
helm upgrade --install \
5755
--debug \
5856
--wait \
5957
--set image.tag='local' \
@@ -66,7 +64,7 @@ function helm_deploy_logging_operator()
6664
function configure_logging()
6765
{
6866
# TODO: remove version once there is a semver stable version in the repo
69-
helm install \
67+
helm upgrade --install \
7068
--debug \
7169
--wait \
7270
--create-namespace \

0 commit comments

Comments
 (0)