Skip to content

Commit 6d54d11

Browse files
committed
Add NFR tests logging
1 parent a23a8f5 commit 6d54d11

19 files changed

+777
-202
lines changed

tests/framework/crossplane.go

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"time"
1010

11+
. "github.com/onsi/ginkgo/v2"
1112
core "k8s.io/api/core/v1"
1213
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314
"k8s.io/client-go/kubernetes"
@@ -42,10 +43,13 @@ const crossplaneImageName = "nginx-crossplane:latest"
4243

4344
// ValidateNginxFieldExists accepts the nginx config and the configuration for the expected field,
4445
// and returns whether or not that field exists where it should.
45-
func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) error {
46+
func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField, opts ...Option) error {
4647
b, err := json.Marshal(conf)
4748
if err != nil {
48-
return fmt.Errorf("error marshaling nginx config: %w", err)
49+
marshalErr := fmt.Errorf("error marshaling nginx config: %w", err)
50+
GinkgoWriter.Printf("%v\n", marshalErr)
51+
52+
return marshalErr
4953
}
5054

5155
for _, config := range conf.Config {
@@ -55,7 +59,7 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
5559

5660
for _, directive := range config.Parsed {
5761
if expFieldCfg.Server == "" && expFieldCfg.Upstream == "" {
58-
if expFieldCfg.fieldFound(directive) {
62+
if expFieldCfg.fieldFound(directive, opts...) {
5963
return nil
6064
}
6165
continue
@@ -70,8 +74,10 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
7074
}
7175
}
7276
}
77+
directiveErr := fmt.Errorf("directive %s not found in: nginx config %s", expFieldCfg.Directive, string(b))
78+
GinkgoWriter.Printf("ERROR: %v\n", directiveErr)
7379

74-
return fmt.Errorf("directive %s not found in: nginx config %s", expFieldCfg.Directive, string(b))
80+
return directiveErr
7581
}
7682

7783
func fieldExistsInServer(
@@ -95,6 +101,12 @@ func fieldExistsInUpstream(
95101
expFieldCfg ExpectedNginxField,
96102
directive Directive,
97103
) bool {
104+
GinkgoWriter.Printf(
105+
"Checking upstream %q for directive %q with value %q\n",
106+
directive.Args[0],
107+
expFieldCfg.Directive,
108+
expFieldCfg.Value,
109+
)
98110
if directive.Directive == "upstream" && directive.Args[0] == expFieldCfg.Upstream {
99111
for _, directive := range directive.Block {
100112
if expFieldCfg.fieldFound(directive) {
@@ -115,15 +127,32 @@ func getServerName(serverBlock Directives) string {
115127
return ""
116128
}
117129

118-
func (e ExpectedNginxField) fieldFound(directive *Directive) bool {
130+
func (e ExpectedNginxField) fieldFound(directive *Directive, opts ...Option) bool {
131+
options := &Options{logEnabled: true}
132+
for _, opt := range opts {
133+
opt(options)
134+
}
119135
arg := strings.Join(directive.Args, " ")
120136

121137
valueMatch := arg == e.Value
122138
if e.ValueSubstringAllowed {
123139
valueMatch = strings.Contains(arg, e.Value)
124140
}
125141

126-
return directive.Directive == e.Directive && valueMatch
142+
if directive.Directive == e.Directive && valueMatch {
143+
if options.logEnabled {
144+
GinkgoWriter.Printf(
145+
"Found field %q with value %q in field %q with value %q\n",
146+
e.Directive,
147+
e.Value,
148+
directive.Directive,
149+
arg,
150+
)
151+
}
152+
return true
153+
}
154+
155+
return false
127156
}
128157

129158
func fieldExistsInLocation(locationDirective *Directive, expFieldCfg ExpectedNginxField) bool {
@@ -201,7 +230,10 @@ func injectCrossplaneContainer(
201230

202231
podClient := k8sClient.CoreV1().Pods(namespace)
203232
if _, err := podClient.UpdateEphemeralContainers(ctx, ngfPodName, pod, metav1.UpdateOptions{}); err != nil {
204-
return fmt.Errorf("error adding ephemeral container: %w", err)
233+
containerErr := fmt.Errorf("error adding ephemeral container: %w", err)
234+
GinkgoWriter.Printf("%v\n", containerErr)
235+
236+
return containerErr
205237
}
206238

207239
return nil
@@ -231,7 +263,10 @@ func createCrossplaneExecutor(
231263

232264
exec, err := remotecommand.NewSPDYExecutor(k8sConfig, http.MethodPost, req.URL())
233265
if err != nil {
234-
return nil, fmt.Errorf("error creating executor: %w", err)
266+
executorErr := fmt.Errorf("error creating executor: %w", err)
267+
GinkgoWriter.Printf("%v\n", executorErr)
268+
269+
return nil, executorErr
235270
}
236271

237272
return exec, nil

tests/framework/info.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import (
1111

1212
// GetLogs returns the logs for all containers in all pods for a release.
1313
func GetLogs(rm ResourceManager, namespace string, releaseName string) string {
14+
GinkgoWriter.Printf(
15+
"Getting logs for all containers in all pods for release %q in namespace %q\n",
16+
releaseName,
17+
namespace,
18+
)
1419
var returnLogs string
1520
pods, err := rm.GetPods(namespace, client.MatchingLabels{
1621
"app.kubernetes.io/instance": releaseName,
@@ -26,6 +31,13 @@ func GetLogs(rm ResourceManager, namespace string, releaseName string) string {
2631
Container: container.Name,
2732
})
2833
if err != nil {
34+
GinkgoWriter.Printf(
35+
"ERROR occurred during getting logs for container %q in pod %q in namespace %q: %v\n",
36+
container.Name,
37+
pod.Name,
38+
pod.Namespace,
39+
err,
40+
)
2941
returnLogs += fmt.Sprintf(" failed to get logs: %v\n", err)
3042
continue
3143
}
@@ -40,6 +52,8 @@ func GetEvents(rm ResourceManager, namespace string) string {
4052
var returnEvents string
4153
events, err := rm.GetEvents(namespace)
4254
if err != nil {
55+
GinkgoWriter.Printf("ERROR occurred during getting events in namespace %q: %v\n", namespace, err)
56+
4357
return fmt.Sprintf("failed to get events: %v", err)
4458
}
4559

@@ -60,6 +74,7 @@ func GetEvents(rm ResourceManager, namespace string) string {
6074

6175
// GetBuildInfo returns the build information.
6276
func GetBuildInfo() (commitHash string, commitTime string, dirtyBuild string) {
77+
GinkgoWriter.Printf("Getting build info\n")
6378
commitHash = "unknown"
6479
commitTime = "unknown"
6580
dirtyBuild = "unknown"
@@ -84,11 +99,12 @@ func GetBuildInfo() (commitHash string, commitTime string, dirtyBuild string) {
8499
}
85100

86101
// AddNginxLogsAndEventsToReport adds nginx logs and events from the namespace to the report if the spec failed.
87-
func AddNginxLogsAndEventsToReport(rm ResourceManager, namespace string) {
102+
func AddNginxLogsAndEventsToReport(rm ResourceManager, namespace string, opts ...Option) {
88103
if CurrentSpecReport().Failed() {
104+
GinkgoWriter.Printf("Current spec failed. Adding Nginx logs and events to report for namespace %q\n", namespace)
89105
var returnLogs string
90106

91-
nginxPodNames, _ := GetReadyNginxPodNames(rm.K8sClient, namespace, rm.TimeoutConfig.GetStatusTimeout)
107+
nginxPodNames, _ := GetReadyNginxPodNames(rm.K8sClient, namespace, rm.TimeoutConfig.GetStatusTimeout, opts...)
92108

93109
for _, nginxPodName := range nginxPodNames {
94110
returnLogs += fmt.Sprintf("Logs for Nginx Pod %s:\n", nginxPodName)

tests/framework/load.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"time"
99

10+
. "github.com/onsi/ginkgo/v2"
1011
vegeta "github.com/tsenart/vegeta/v12/lib"
1112
)
1213

@@ -49,6 +50,7 @@ type Metrics struct {
4950
// RunLoadTest uses Vegeta to send traffic to the provided Targets at the given rate for the given duration and writes
5051
// the results to the provided file.
5152
func RunLoadTest(cfg LoadTestConfig) (vegeta.Results, Metrics) {
53+
GinkgoWriter.Printf("Running load test: %s\n", cfg.Description)
5254
vegTargets := convertTargetToVegetaTarget(cfg.Targets)
5355
targeter := vegeta.NewStaticTargeter(vegTargets...)
5456

@@ -61,7 +63,12 @@ func RunLoadTest(cfg LoadTestConfig) (vegeta.Results, Metrics) {
6163
Timeout: vegeta.DefaultTimeout,
6264
Transport: &http.Transport{
6365
DialContext: func(ctx context.Context, network, _ string) (net.Conn, error) {
64-
return dialer.DialContext(ctx, network, cfg.Proxy)
66+
conn, err := dialer.DialContext(ctx, network, cfg.Proxy)
67+
if err != nil {
68+
GinkgoWriter.Printf("ERROR occurred during dialing %q in %q network, error: %s\n", cfg.Proxy, network, err)
69+
}
70+
71+
return conn, err
6572
},
6673
TLSClientConfig: &tls.Config{
6774
InsecureSkipVerify: true, //nolint:gosec // self-signed cert for testing

0 commit comments

Comments
 (0)