Skip to content

Commit f8ee6d1

Browse files
authored
Run functional and NFR tests with --race (#3614)
Problem: The functional and NFR tests do not run with --race enabled, so race conditions within the tests may be present and unknown. Solution: Enable --race and fix any issues.
1 parent e88fbeb commit f8ee6d1

File tree

5 files changed

+46
-17
lines changed

5 files changed

+46
-17
lines changed

tests/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ stop-longevity-test: nfr-test ## Stop the longevity test and collects results
120120

121121
.PHONY: .vm-nfr-test
122122
.vm-nfr-test: ## Runs the NFR tests on the GCP VM (called by `nfr-test`)
123-
go run github.com/onsi/ginkgo/v2/ginkgo --randomize-all --randomize-suites --keep-going --fail-on-pending \
124-
--trace -r -v --buildvcs --force-newlines $(GITHUB_OUTPUT) \
123+
CGO_ENABLED=1 go run github.com/onsi/ginkgo/v2/ginkgo --race --randomize-all --randomize-suites --keep-going --fail-on-pending \
124+
--trace -r -v --buildvcs --force-newlines $(GITHUB_OUTPUT) --flake-attempts=2 \
125125
--label-filter "nfr" $(GINKGO_FLAGS) --timeout 5h ./suite -- --gateway-api-version=$(GW_API_VERSION) \
126126
--gateway-api-prev-version=$(GW_API_PREV_VERSION) --image-tag=$(TAG) --version-under-test=$(NGF_VERSION) \
127127
--ngf-image-repo=$(PREFIX) --nginx-image-repo=$(NGINX_PREFIX) --nginx-plus-image-repo=$(NGINX_PLUS_PREFIX) \
@@ -132,7 +132,7 @@ stop-longevity-test: nfr-test ## Stop the longevity test and collects results
132132
.PHONY: test
133133
test: build-crossplane-image ## Runs the functional tests on your kind k8s cluster
134134
kind load docker-image nginx-crossplane:latest --name $(CLUSTER_NAME)
135-
go run github.com/onsi/ginkgo/v2/ginkgo --randomize-all --randomize-suites --keep-going --fail-on-pending \
135+
go run github.com/onsi/ginkgo/v2/ginkgo --race --randomize-all --randomize-suites --keep-going --fail-on-pending \
136136
--trace -r -v --buildvcs --force-newlines $(GITHUB_OUTPUT) \
137137
--label-filter "functional" $(GINKGO_FLAGS) ./suite -- \
138138
--gateway-api-version=$(GW_API_VERSION) --gateway-api-prev-version=$(GW_API_PREV_VERSION) \

tests/framework/portforward.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"net/url"
99
"path"
10+
"sync"
1011
"time"
1112

1213
"k8s.io/client-go/rest"
@@ -35,12 +36,10 @@ func PortForward(config *rest.Config, namespace, podName string, ports []string,
3536

3637
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: roundTripper}, http.MethodPost, serverURL)
3738

38-
out, errOut := new(bytes.Buffer), new(bytes.Buffer)
39-
4039
forward := func() error {
4140
readyCh := make(chan struct{}, 1)
4241

43-
forwarder, err := portforward.New(dialer, ports, stopCh, readyCh, out, errOut)
42+
forwarder, err := portforward.New(dialer, ports, stopCh, readyCh, newSafeBuffer(), newSafeBuffer())
4443
if err != nil {
4544
return fmt.Errorf("error creating port forwarder: %w", err)
4645
}
@@ -66,3 +65,31 @@ func PortForward(config *rest.Config, namespace, podName string, ports []string,
6665

6766
return nil
6867
}
68+
69+
// safeBuffer is a goroutine safe bytes.Buffer.
70+
type safeBuffer struct {
71+
buffer bytes.Buffer
72+
mutex sync.Mutex
73+
}
74+
75+
func newSafeBuffer() *safeBuffer {
76+
return &safeBuffer{}
77+
}
78+
79+
// Write appends the contents of p to the buffer, growing the buffer as needed. It returns
80+
// the number of bytes written.
81+
func (s *safeBuffer) Write(p []byte) (n int, err error) {
82+
s.mutex.Lock()
83+
defer s.mutex.Unlock()
84+
85+
return s.buffer.Write(p)
86+
}
87+
88+
// String returns the contents of the unread portion of the buffer
89+
// as a string. If the Buffer is a nil pointer, it returns "<nil>".
90+
func (s *safeBuffer) String() string {
91+
s.mutex.Lock()
92+
defer s.mutex.Unlock()
93+
94+
return s.buffer.String()
95+
}

tests/framework/request.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ func makeRequest(
6161
return nil, errors.New("transport is not of type *http.Transport")
6262
}
6363

64-
transport.DialContext = func(
64+
customTransport := transport.Clone()
65+
customTransport.DialContext = func(
6566
ctx context.Context,
6667
network,
6768
addr string,
@@ -93,21 +94,13 @@ func makeRequest(
9394

9495
var resp *http.Response
9596
if strings.HasPrefix(url, "https") {
96-
transport, ok := http.DefaultTransport.(*http.Transport)
97-
if !ok {
98-
return nil, errors.New("transport is not of type *http.Transport")
99-
}
100-
101-
customTransport := transport.Clone()
10297
// similar to how in our examples with https requests we run our curl command
10398
// we turn off verification of the certificate, we do the same here
10499
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} //nolint:gosec // for https test traffic
105-
client := &http.Client{Transport: customTransport}
106-
resp, err = client.Do(req)
107-
} else {
108-
resp, err = http.DefaultClient.Do(req)
109100
}
110101

102+
client := &http.Client{Transport: customTransport}
103+
resp, err = client.Do(req)
111104
if err != nil {
112105
return nil, err
113106
}

tests/scripts/create-and-setup-gcp-vm.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ done
4848

4949
gcloud compute scp --zone "${GKE_CLUSTER_ZONE}" --project="${GKE_PROJECT}" "${SCRIPT_DIR}"/vars.env username@"${RESOURCE_NAME}":~
5050

51+
gcloud compute ssh --zone "${GKE_CLUSTER_ZONE}" --project="${GKE_PROJECT}" username@"${RESOURCE_NAME}" --command="bash -s" <"${SCRIPT_DIR}"/remote-scripts/install-deps.sh
52+
5153
if [ -n "${NGF_REPO}" ] && [ "${NGF_REPO}" != "nginx" ]; then
5254
gcloud compute ssh --zone "${GKE_CLUSTER_ZONE}" --project="${GKE_PROJECT}" username@"${RESOURCE_NAME}" \
5355
--command="bash -i <<EOF
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
source ~/vars.env
6+
7+
sudo apt-get -y update && sudo apt-get -y install gcc

0 commit comments

Comments
 (0)