Skip to content

Commit 802463f

Browse files
committed
Add NFR tests logging
1 parent 5e8513d commit 802463f

24 files changed

+946
-472
lines changed

tests/framework/crossplane.go

Lines changed: 44 additions & 9 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
@@ -65,13 +69,15 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
6569
return nil
6670
}
6771

68-
if expFieldCfg.Upstream != "" && fieldExistsInUpstream(expFieldCfg, *directive) {
72+
if expFieldCfg.Upstream != "" && fieldExistsInUpstream(expFieldCfg, *directive, opts...) {
6973
return nil
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(
@@ -94,7 +100,16 @@ func fieldExistsInServer(
94100
func fieldExistsInUpstream(
95101
expFieldCfg ExpectedNginxField,
96102
directive Directive,
103+
opts ...Option,
97104
) bool {
105+
options := LogOptions(opts...)
106+
if options.logEnabled {
107+
GinkgoWriter.Printf(
108+
"Checking upstream for directive %q with value %q\n",
109+
expFieldCfg.Directive,
110+
expFieldCfg.Value,
111+
)
112+
}
98113
if directive.Directive == "upstream" && directive.Args[0] == expFieldCfg.Upstream {
99114
for _, directive := range directive.Block {
100115
if expFieldCfg.fieldFound(directive) {
@@ -115,15 +130,29 @@ func getServerName(serverBlock Directives) string {
115130
return ""
116131
}
117132

118-
func (e ExpectedNginxField) fieldFound(directive *Directive) bool {
133+
func (e ExpectedNginxField) fieldFound(directive *Directive, opts ...Option) bool {
134+
options := LogOptions(opts...)
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/generate_manifests.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"io"
88
"text/template"
99

10-
. "github.com/onsi/ginkgo/v2"
1110
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1211
"k8s.io/apimachinery/pkg/util/yaml"
1312
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -222,7 +221,6 @@ func GenerateScaleListenerObjects(numListeners int, tls bool) (ScaleObjects, err
222221
}
223222

224223
func generateSecrets(secrets []string) ([]client.Object, error) {
225-
GinkgoWriter.Printf("Generating secrets\n")
226224
objects := make([]client.Object, 0, len(secrets))
227225

228226
for _, secret := range secrets {
@@ -239,7 +237,6 @@ func generateSecrets(secrets []string) ([]client.Object, error) {
239237

240238
objects = append(objects, objs...)
241239
}
242-
GinkgoWriter.Printf("Generated %d secrets\n", len(objects))
243240

244241
return objects, nil
245242
}

tests/framework/info.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ func GetBuildInfo() (commitHash string, commitTime string, dirtyBuild string) {
8484
}
8585

8686
// AddNginxLogsAndEventsToReport adds nginx logs and events from the namespace to the report if the spec failed.
87-
func AddNginxLogsAndEventsToReport(rm ResourceManager, namespace string) {
87+
func AddNginxLogsAndEventsToReport(rm ResourceManager, namespace string, opts ...Option) {
8888
if CurrentSpecReport().Failed() {
8989
var returnLogs string
9090

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

9393
for _, nginxPodName := range nginxPodNames {
9494
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

tests/framework/logging.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ func WithLoggingDisabled() Option {
1111
opts.logEnabled = false
1212
}
1313
}
14+
15+
func LogOptions(opts ...Option) *Options {
16+
options := &Options{logEnabled: true}
17+
for _, opt := range opts {
18+
opt(options)
19+
}
20+
21+
return options
22+
}

tests/framework/ngf.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1515
apierrors "k8s.io/apimachinery/pkg/api/errors"
1616
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17-
"sigs.k8s.io/controller-runtime/pkg/client"
1817
)
1918

2019
const (
@@ -101,7 +100,7 @@ func InstallNGF(cfg InstallationConfig, extraArgs ...string) ([]byte, error) {
101100
}
102101

103102
// CreateLicenseSecret creates the NGINX Plus JWT secret.
104-
func CreateLicenseSecret(k8sClient client.Client, namespace, filename string) error {
103+
func CreateLicenseSecret(rm ResourceManager, namespace, filename string) error {
105104
GinkgoWriter.Printf("Creating NGINX Plus license secret in namespace %q from file %q\n", namespace, filename)
106105

107106
conf, err := os.ReadFile(filename)
@@ -121,11 +120,8 @@ func CreateLicenseSecret(k8sClient client.Client, namespace, filename string) er
121120
},
122121
}
123122

124-
if err := k8sClient.Create(ctx, ns); err != nil && !apierrors.IsAlreadyExists(err) {
125-
createNSErr := fmt.Errorf("error creating namespace: %w", err)
126-
GinkgoWriter.Printf("%v\n", createNSErr)
127-
128-
return createNSErr
123+
if err := rm.Create(ctx, ns); err != nil && !apierrors.IsAlreadyExists(err) {
124+
return fmt.Errorf("error creating namespace: %w", err)
129125
}
130126

131127
secret := &core.Secret{
@@ -138,7 +134,7 @@ func CreateLicenseSecret(k8sClient client.Client, namespace, filename string) er
138134
},
139135
}
140136

141-
if err := k8sClient.Create(ctx, secret); err != nil && !apierrors.IsAlreadyExists(err) {
137+
if err := rm.Create(ctx, secret); err != nil && !apierrors.IsAlreadyExists(err) {
142138
createSecretErr := fmt.Errorf("error creating secret: %w", err)
143139
GinkgoWriter.Printf("%v\n", createSecretErr)
144140

@@ -185,7 +181,7 @@ func UpgradeNGF(cfg InstallationConfig, extraArgs ...string) ([]byte, error) {
185181
}
186182

187183
// UninstallNGF uninstalls NGF.
188-
func UninstallNGF(cfg InstallationConfig, k8sClient client.Client) ([]byte, error) {
184+
func UninstallNGF(cfg InstallationConfig, rm ResourceManager) ([]byte, error) {
189185
args := []string{
190186
"uninstall", cfg.ReleaseName, "--namespace", cfg.Namespace,
191187
}
@@ -199,20 +195,20 @@ func UninstallNGF(cfg InstallationConfig, k8sClient client.Client) ([]byte, erro
199195
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
200196
defer cancel()
201197

202-
err = k8sClient.Delete(ctx, &core.Namespace{ObjectMeta: metav1.ObjectMeta{Name: cfg.Namespace}})
198+
err = rm.Delete(ctx, &core.Namespace{ObjectMeta: metav1.ObjectMeta{Name: cfg.Namespace}}, nil)
203199
if err != nil && !apierrors.IsNotFound(err) {
204200
return nil, err
205201
}
206202

207203
var crList apiext.CustomResourceDefinitionList
208-
if err := k8sClient.List(ctx, &crList); err != nil {
204+
if err := rm.List(ctx, &crList); err != nil {
209205
return nil, err
210206
}
211207

212208
for _, cr := range crList.Items {
213209
if strings.Contains(cr.Spec.Group, "gateway.nginx.org") {
214210
cr := cr
215-
if err := k8sClient.Delete(ctx, &cr); err != nil && !apierrors.IsNotFound(err) {
211+
if err := rm.Delete(ctx, &cr, nil); err != nil && !apierrors.IsNotFound(err) {
216212
return nil, err
217213
}
218214
}

0 commit comments

Comments
 (0)