Skip to content

Commit 232a997

Browse files
authored
Merge pull request #287 from justinsb/normalize_requestlog
tests: normalize http request order
2 parents 173ddfe + 3453443 commit 232a997

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed

pkg/patterns/declarative/pkg/applier/applylib_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,16 @@ func runApplierGoldenTests(t *testing.T, testDir string, interceptHTTPServer boo
9494
t.Logf("replacing old url prefix %q", "http://"+restConfig.Host)
9595
requestLog.ReplaceURLPrefix("http://"+restConfig.Host, "http://kube-apiserver")
9696
requestLog.RemoveUserAgent()
97+
requestLog.SortGETs()
98+
9799
requests := requestLog.FormatHTTP()
98100
h.CompareGoldenFile(filepath.Join(testdir, "expected.yaml"), requests)
99101

100102
if interceptHTTPServer {
101103
t.Logf("replacing old url prefix %q", "http://"+restConfig.Host)
102104
apiserverRequestLog.ReplaceURLPrefix("http://"+restConfig.Host, "http://kube-apiserver")
103105
apiserverRequestLog.RemoveUserAgent()
106+
apiserverRequestLog.SortGETs()
104107
apiserverRequestLog.RemoveHeader("Kubectl-Session")
105108
apiserverRequests := apiserverRequestLog.FormatHTTP()
106109
h.CompareGoldenFile(filepath.Join(testdir, "expected-apiserver.yaml"), apiserverRequests)

pkg/patterns/declarative/pkg/applier/testdata/kubectl/simple1/expected-apiserver.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Kubectl-Command: kubectl apply
1818

1919
---
2020

21-
GET /apis?timeout=32s
21+
GET /api/v1?timeout=32s
2222
Accept: application/json, */*
2323
Accept-Encoding: gzip
2424
Kubectl-Command: kubectl apply
@@ -38,7 +38,7 @@ Kubectl-Command: kubectl apply
3838

3939
---
4040

41-
GET /api/v1?timeout=32s
41+
GET /apis?timeout=32s
4242
Accept: application/json, */*
4343
Accept-Encoding: gzip
4444
Kubectl-Command: kubectl apply

pkg/patterns/declarative/pkg/applier/testdata/kubectl/simple2/expected-apiserver.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Kubectl-Command: kubectl apply
1818

1919
---
2020

21-
GET /apis?timeout=32s
21+
GET /api/v1?timeout=32s
2222
Accept: application/json, */*
2323
Accept-Encoding: gzip
2424
Kubectl-Command: kubectl apply
@@ -38,7 +38,7 @@ Kubectl-Command: kubectl apply
3838

3939
---
4040

41-
GET /api/v1?timeout=32s
41+
GET /apis?timeout=32s
4242
Accept: application/json, */*
4343
Accept-Encoding: gzip
4444
Kubectl-Command: kubectl apply

pkg/patterns/declarative/pkg/applier/testdata/kubectl/simple3/expected-apiserver.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Kubectl-Command: kubectl apply
1818

1919
---
2020

21-
GET /apis?timeout=32s
21+
GET /api/v1?timeout=32s
2222
Accept: application/json, */*
2323
Accept-Encoding: gzip
2424
Kubectl-Command: kubectl apply
@@ -38,7 +38,7 @@ Kubectl-Command: kubectl apply
3838

3939
---
4040

41-
GET /api/v1?timeout=32s
41+
GET /apis?timeout=32s
4242
Accept: application/json, */*
4343
Accept-Encoding: gzip
4444
Kubectl-Command: kubectl apply

pkg/test/httprecorder/request_log.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package httprecorder
33
import (
44
"fmt"
55
"net/http"
6+
"net/url"
67
"regexp"
78
"sort"
89
"strings"
@@ -108,6 +109,47 @@ func (l *RequestLog) RemoveHeader(k string) {
108109
}
109110
}
110111

112+
// SortGETs attempts to normalize parallel requests.
113+
// Consecutive GET requests are sorted alphabetically.
114+
func (l *RequestLog) SortGETs() {
115+
116+
isSwappable := func(urlString string) (string, bool) {
117+
u, err := url.Parse(urlString)
118+
if err != nil {
119+
klog.Warningf("unable to parse url %q", urlString)
120+
return "", false
121+
}
122+
123+
switch u.Path {
124+
case "/apis", "/api/v1", "/apis/v1":
125+
return u.Path, true
126+
}
127+
return "", false
128+
}
129+
130+
doAgain:
131+
changed := false
132+
for i := 0; i < len(l.Entries)-1; i++ {
133+
a := l.Entries[i]
134+
b := l.Entries[i+1]
135+
136+
if a.Request.Method == "GET" && b.Request.Method == "GET" {
137+
aKey, aSwappable := isSwappable(a.Request.URL)
138+
bKey, bSwappable := isSwappable(b.Request.URL)
139+
if aSwappable && bSwappable {
140+
if aKey > bKey {
141+
l.Entries[i+1] = a
142+
l.Entries[i] = b
143+
changed = true
144+
}
145+
}
146+
}
147+
}
148+
if changed {
149+
goto doAgain
150+
}
151+
}
152+
111153
func (l *RequestLog) RemoveUserAgent() {
112154
l.RemoveHeader("user-agent")
113155
}

0 commit comments

Comments
 (0)