Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 50 additions & 9 deletions tests/framework/crossplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

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

// ValidateNginxFieldExists accepts the nginx config and the configuration for the expected field,
// and returns whether or not that field exists where it should.
func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) error {
func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField, opts ...Option) error {
b, err := json.Marshal(conf)
if err != nil {
return fmt.Errorf("error marshaling nginx config: %w", err)
marshalErr := fmt.Errorf("error marshaling nginx config: %w", err)
GinkgoWriter.Printf("%v\n", marshalErr)

return marshalErr
}

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

for _, directive := range config.Parsed {
if expFieldCfg.Server == "" && expFieldCfg.Upstream == "" {
if expFieldCfg.fieldFound(directive) {
if expFieldCfg.fieldFound(directive, opts...) {
return nil
}
continue
Expand All @@ -65,13 +69,15 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
return nil
}

if expFieldCfg.Upstream != "" && fieldExistsInUpstream(expFieldCfg, *directive) {
if expFieldCfg.Upstream != "" && fieldExistsInUpstream(expFieldCfg, *directive, opts...) {
return nil
}
}
}
directiveErr := fmt.Errorf("directive %s not found in: nginx config %s", expFieldCfg.Directive, string(b))
GinkgoWriter.Printf("ERROR: %v\n", directiveErr)

return fmt.Errorf("directive %s not found in: nginx config %s", expFieldCfg.Directive, string(b))
return directiveErr
}

func fieldExistsInServer(
Expand All @@ -94,7 +100,19 @@ func fieldExistsInServer(
func fieldExistsInUpstream(
expFieldCfg ExpectedNginxField,
directive Directive,
opts ...Option,
) bool {
options := &Options{logEnabled: true}
for _, opt := range opts {
opt(options)
}
if options.logEnabled {
GinkgoWriter.Printf(
"Checking upstream for directive %q with value %q\n",
expFieldCfg.Directive,
expFieldCfg.Value,
)
}
if directive.Directive == "upstream" && directive.Args[0] == expFieldCfg.Upstream {
for _, directive := range directive.Block {
if expFieldCfg.fieldFound(directive) {
Expand All @@ -115,15 +133,32 @@ func getServerName(serverBlock Directives) string {
return ""
}

func (e ExpectedNginxField) fieldFound(directive *Directive) bool {
func (e ExpectedNginxField) fieldFound(directive *Directive, opts ...Option) bool {
options := &Options{logEnabled: true}
for _, opt := range opts {
opt(options)
}
arg := strings.Join(directive.Args, " ")

valueMatch := arg == e.Value
if e.ValueSubstringAllowed {
valueMatch = strings.Contains(arg, e.Value)
}

return directive.Directive == e.Directive && valueMatch
if directive.Directive == e.Directive && valueMatch {
if options.logEnabled {
GinkgoWriter.Printf(
"Found field %q with value %q in field %q with value %q\n",
e.Directive,
e.Value,
directive.Directive,
arg,
)
}
return true
}

return false
}

func fieldExistsInLocation(locationDirective *Directive, expFieldCfg ExpectedNginxField) bool {
Expand Down Expand Up @@ -201,7 +236,10 @@ func injectCrossplaneContainer(

podClient := k8sClient.CoreV1().Pods(namespace)
if _, err := podClient.UpdateEphemeralContainers(ctx, ngfPodName, pod, metav1.UpdateOptions{}); err != nil {
return fmt.Errorf("error adding ephemeral container: %w", err)
containerErr := fmt.Errorf("error adding ephemeral container: %w", err)
GinkgoWriter.Printf("%v\n", containerErr)

return containerErr
}

return nil
Expand Down Expand Up @@ -231,7 +269,10 @@ func createCrossplaneExecutor(

exec, err := remotecommand.NewSPDYExecutor(k8sConfig, http.MethodPost, req.URL())
if err != nil {
return nil, fmt.Errorf("error creating executor: %w", err)
executorErr := fmt.Errorf("error creating executor: %w", err)
GinkgoWriter.Printf("%v\n", executorErr)

return nil, executorErr
}

return exec, nil
Expand Down
18 changes: 17 additions & 1 deletion tests/framework/generate_manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"text/template"

. "github.com/onsi/ginkgo/v2"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -150,7 +151,11 @@ func decodeObjects(reader io.Reader) ([]client.Object, error) {
}

// GenerateScaleListenerObjects generates objects for a given number of listeners for the scale test.
func GenerateScaleListenerObjects(numListeners int, tls bool) (ScaleObjects, error) {
func GenerateScaleListenerObjects(
numListeners int,
tls bool,
opts ...Option,
) (ScaleObjects, error) {
var result ScaleObjects

listeners := make([]listener, 0)
Expand Down Expand Up @@ -183,8 +188,17 @@ func GenerateScaleListenerObjects(numListeners int, tls bool) (ScaleObjects, err

backends = append(backends, backendName)

options := &Options{logEnabled: true}
for _, opt := range opts {
opt(options)
}
if !options.logEnabled {
GinkgoWriter.Printf("Generating manifests for listeners %v and route %v\n", listeners, r)
}
objects, err := generateManifests(listeners, []route{r})
if err != nil {
GinkgoWriter.Printf("Error generating manifests: %v\n", err)

return ScaleObjects{}, err
}

Expand All @@ -209,6 +223,7 @@ func GenerateScaleListenerObjects(numListeners int, tls bool) (ScaleObjects, err
}

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

for _, secret := range secrets {
Expand All @@ -225,6 +240,7 @@ func generateSecrets(secrets []string) ([]client.Object, error) {

objects = append(objects, objs...)
}
GinkgoWriter.Printf("Generated %d secrets\n", len(objects))

return objects, nil
}
Expand Down
20 changes: 18 additions & 2 deletions tests/framework/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (

// GetLogs returns the logs for all containers in all pods for a release.
func GetLogs(rm ResourceManager, namespace string, releaseName string) string {
GinkgoWriter.Printf(
"Getting logs for all containers in all pods for release %q in namespace %q\n",
releaseName,
namespace,
)
var returnLogs string
pods, err := rm.GetPods(namespace, client.MatchingLabels{
"app.kubernetes.io/instance": releaseName,
Expand All @@ -26,6 +31,13 @@ func GetLogs(rm ResourceManager, namespace string, releaseName string) string {
Container: container.Name,
})
if err != nil {
GinkgoWriter.Printf(
"ERROR occurred during getting logs for container %q in pod %q in namespace %q: %v\n",
container.Name,
pod.Name,
pod.Namespace,
err,
)
returnLogs += fmt.Sprintf(" failed to get logs: %v\n", err)
continue
}
Expand All @@ -40,6 +52,8 @@ func GetEvents(rm ResourceManager, namespace string) string {
var returnEvents string
events, err := rm.GetEvents(namespace)
if err != nil {
GinkgoWriter.Printf("ERROR occurred during getting events in namespace %q: %v\n", namespace, err)

return fmt.Sprintf("failed to get events: %v", err)
}

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

// GetBuildInfo returns the build information.
func GetBuildInfo() (commitHash string, commitTime string, dirtyBuild string) {
GinkgoWriter.Printf("Getting build info\n")
commitHash = "unknown"
commitTime = "unknown"
dirtyBuild = "unknown"
Expand All @@ -84,11 +99,12 @@ func GetBuildInfo() (commitHash string, commitTime string, dirtyBuild string) {
}

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

nginxPodNames, _ := GetReadyNginxPodNames(rm.K8sClient, namespace, rm.TimeoutConfig.GetStatusTimeout)
nginxPodNames, _ := GetReadyNginxPodNames(rm.K8sClient, namespace, rm.TimeoutConfig.GetStatusTimeout, opts...)

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