diff --git a/hack/ci/test.sh b/hack/ci/test.sh index 39017b9c..cdcf7477 100755 --- a/hack/ci/test.sh +++ b/hack/ci/test.sh @@ -41,6 +41,7 @@ echo "kubectl version is $(kubectl version --client)" rm -f go.work go.work.sum go work init . go work use applylib +go work use ktest go work use mockkubeapiserver go work use examples/guestbook-operator diff --git a/ktest/go.mod b/ktest/go.mod new file mode 100644 index 00000000..0289757b --- /dev/null +++ b/ktest/go.mod @@ -0,0 +1,30 @@ +module sigs.k8s.io/kubebuilder-declarative-pattern/ktest + +go 1.21 + +toolchain go1.22.0 + +require ( + github.com/google/go-cmp v0.6.0 + k8s.io/apimachinery v0.29.1 + k8s.io/klog/v2 v2.110.1 + sigs.k8s.io/yaml v1.4.0 +) + +require ( + github.com/go-logr/logr v1.4.1 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/google/gofuzz v1.2.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + golang.org/x/net v0.19.0 // indirect + golang.org/x/text v0.14.0 // indirect + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect + gopkg.in/inf.v0 v0.9.1 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect + sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect + sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect +) diff --git a/ktest/go.sum b/ktest/go.sum new file mode 100644 index 00000000..4f5c8885 --- /dev/null +++ b/ktest/go.sum @@ -0,0 +1,94 @@ +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= +github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= +github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= +golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= +gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +k8s.io/apimachinery v0.29.1 h1:KY4/E6km/wLBguvCZv8cKTeOwwOBqFNjwJIdMkMbbRc= +k8s.io/apimachinery v0.29.1/go.mod h1:6HVkd1FwxIagpYrHSwJlQqZI3G9LfYWRPAkUvLnXTKU= +k8s.io/klog/v2 v2.110.1 h1:U/Af64HJf7FcwMcXyKm2RPM22WZzyR7OSpYj5tg3cL0= +k8s.io/klog/v2 v2.110.1/go.mod h1:YGtd1984u+GgbuZ7e08/yBuAfKLSO0+uR1Fhi6ExXjo= +k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI= +k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= +sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo= +sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0= +sigs.k8s.io/structured-merge-diff/v4 v4.4.1 h1:150L+0vs/8DA78h1u02ooW1/fFq/Lwr+sGiqlzvrtq4= +sigs.k8s.io/structured-merge-diff/v4 v4.4.1/go.mod h1:N8hJocpFajUSSeSJ9bOZ77VzejKZaXsTtZo4/u7Io08= +sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E= +sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY= diff --git a/ktest/httprecorder/http_recorder.go b/ktest/httprecorder/http_recorder.go new file mode 100644 index 00000000..cc68db7a --- /dev/null +++ b/ktest/httprecorder/http_recorder.go @@ -0,0 +1,78 @@ +package httprecorder + +import ( + "bytes" + "fmt" + "io" + "net/http" + "strings" +) + +type HTTPRecorder struct { + inner http.RoundTripper + log *RequestLog +} + +func NewRecorder(inner http.RoundTripper, log *RequestLog) *HTTPRecorder { + rt := &HTTPRecorder{inner: inner, log: log} + return rt +} + +func (m *HTTPRecorder) RoundTrip(request *http.Request) (*http.Response, error) { + entry := &LogEntry{} + entry.Request = Request{ + Method: request.Method, + URL: request.URL.String(), + Header: request.Header, + } + + if request.Body != nil { + requestBody, err := io.ReadAll(request.Body) + if err != nil { + return nil, fmt.Errorf("HTTPRecorder failed to read request body") + } + entry.Request.Body = string(requestBody) + request.Body = io.NopCloser(bytes.NewReader(requestBody)) + } + + streaming := false + if request.URL.Query().Get("watch") == "true" { + streaming = true + } + + // We log the request here, because otherwise we miss long-running requests (watches) + m.log.AddEntry(entry) + + response, err := m.inner.RoundTrip(request) + + if response != nil { + entry.Response.Status = response.Status + entry.Response.StatusCode = response.StatusCode + + entry.Response.Header = make(http.Header) + for k, values := range response.Header { + switch strings.ToLower(k) { + case "authorization": + entry.Response.Header[k] = []string{"(redacted)"} + case "date": + entry.Response.Header[k] = []string{"(removed)"} + default: + entry.Response.Header[k] = values + } + } + + if streaming { + entry.Response.Body = "" + } else if response.Body != nil { + responseBody, err := io.ReadAll(response.Body) + if err != nil { + entry.Response.Body = fmt.Sprintf("", err) + } else { + entry.Response.Body = string(responseBody) + response.Body = io.NopCloser(bytes.NewReader(responseBody)) + } + } + } + + return response, err +} diff --git a/ktest/httprecorder/request_log.go b/ktest/httprecorder/request_log.go new file mode 100644 index 00000000..2cbcd340 --- /dev/null +++ b/ktest/httprecorder/request_log.go @@ -0,0 +1,242 @@ +package httprecorder + +import ( + "encoding/json" + "fmt" + "net/http" + "net/url" + "regexp" + "sort" + "strings" + "sync" + "testing" + "time" + + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/klog/v2" + "sigs.k8s.io/yaml" +) + +type LogEntry struct { + Request Request `json:"request,omitempty"` + Response Response `json:"response,omitempty"` + Error string `json:"error,omitempty"` +} + +type Request struct { + Method string `json:"method,omitempty"` + URL string `json:"url,omitempty"` + Header http.Header `json:"header,omitempty"` + Body string `json:"body,omitempty"` +} + +type Response struct { + Status string `json:"status,omitempty"` + StatusCode int `json:"statusCode,omitempty"` + Header http.Header `json:"header,omitempty"` + Body string `json:"body,omitempty"` +} + +func (e *LogEntry) FormatHTTP() string { + var b strings.Builder + b.WriteString(e.Request.FormatHTTP()) + b.WriteString(e.Response.FormatHTTP()) + return b.String() +} + +func (r *Request) FormatHTTP() string { + var b strings.Builder + b.WriteString(fmt.Sprintf("%s %s\n", r.Method, r.URL)) + var keys []string + for k := range r.Header { + keys = append(keys, k) + } + sort.Strings(keys) + for _, k := range keys { + for _, v := range r.Header[k] { + b.WriteString(fmt.Sprintf("%s: %s\n", k, v)) + } + } + b.WriteString("\n") + if r.Body != "" { + b.WriteString(r.Body) + b.WriteString("\n\n") + } + return b.String() +} + +func (l *RequestLog) ReplaceTimestamp() { + l.mutex.Lock() + defer l.mutex.Unlock() + + for _, entry := range l.entries { + entry.Request.Body = resetTimestamp(entry.Request.Body) + entry.Response.Body = resetTimestamp(entry.Response.Body) + } +} + +func resetTimestamp(body string) string { + if body == "" { + return body + } + var u *unstructured.Unstructured + if err := yaml.Unmarshal([]byte(body), &u); err != nil { + return body + } + + if u.Object["status"] == nil { + return body + } + status := u.Object["status"].(map[string]interface{}) + if status["conditions"] == nil { + return body + } + conditions := status["conditions"].([]interface{}) + for _, condition := range conditions { + cond := condition.(map[string]interface{}) + // Use a fixed timestamp for golden tests. + t := time.Date(2022, time.January, 1, 0, 0, 0, 0, time.UTC) + cond["lastTransitionTime"] = t.Format("2006-01-02T15:04:05Z07:00") + } + b, _ := json.Marshal(u) + return string(b) +} + +func (r *Response) FormatHTTP() string { + var b strings.Builder + b.WriteString(fmt.Sprintf("%s\n", r.Status)) + var keys []string + for k := range r.Header { + keys = append(keys, k) + } + sort.Strings(keys) + for _, k := range keys { + for _, v := range r.Header[k] { + b.WriteString(fmt.Sprintf("%s: %s\n", k, v)) + } + } + b.WriteString("\n") + if r.Body != "" { + b.WriteString(r.Body) + b.WriteString("\n") + } + return b.String() +} + +type RequestLog struct { + // We have seen dropped logs from concurrent requests + mutex sync.Mutex + + entries []*LogEntry +} + +func (l *RequestLog) AddEntry(entry *LogEntry) { + l.mutex.Lock() + defer l.mutex.Unlock() + + l.entries = append(l.entries, entry) +} + +func (l *RequestLog) FormatHTTP() string { + l.mutex.Lock() + defer l.mutex.Unlock() + + var actual []string + for _, entry := range l.entries { + s := entry.FormatHTTP() + actual = append(actual, s) + } + return strings.Join(actual, "\n---\n\n") +} + +func (l *RequestLog) ReplaceURLPrefix(old, new string) { + l.mutex.Lock() + defer l.mutex.Unlock() + + for i := range l.entries { + r := &l.entries[i].Request + if strings.HasPrefix(r.URL, old) { + r.URL = new + strings.TrimPrefix(r.URL, old) + } + } +} + +func (l *RequestLog) RemoveHeader(k string) { + l.mutex.Lock() + defer l.mutex.Unlock() + + for i := range l.entries { + r := &l.entries[i].Request + r.Header.Del(k) + } +} + +// SortGETs attempts to normalize parallel requests. +// Consecutive GET requests are sorted alphabetically. +func (l *RequestLog) SortGETs() { + l.mutex.Lock() + defer l.mutex.Unlock() + + isSwappable := func(urlString string) (string, bool) { + u, err := url.Parse(urlString) + if err != nil { + klog.Warningf("unable to parse url %q", urlString) + return "", false + } + + switch u.Path { + case "/apis", "/api/v1", "/apis/v1": + return u.Path, true + default: + tokens := strings.Split(strings.TrimPrefix(u.Path, "/"), "/") + if len(tokens) == 3 && tokens[0] == "apis" { + return u.Path, true + } + } + return "", false + } + +doAgain: + changed := false + for i := 0; i < len(l.entries)-1; i++ { + a := l.entries[i] + b := l.entries[i+1] + + if a.Request.Method == "GET" && b.Request.Method == "GET" { + aKey, aSwappable := isSwappable(a.Request.URL) + bKey, bSwappable := isSwappable(b.Request.URL) + if aSwappable && bSwappable { + if aKey > bKey { + l.entries[i+1] = a + l.entries[i] = b + changed = true + } + } + } + } + if changed { + goto doAgain + } +} + +func (l *RequestLog) RemoveUserAgent() { + l.RemoveHeader("user-agent") +} + +func (l *RequestLog) RegexReplaceURL(t *testing.T, find string, replace string) { + l.mutex.Lock() + defer l.mutex.Unlock() + + r, err := regexp.Compile(find) + if err != nil { + t.Fatalf("failed to compile regex %q: %v", find, err) + } + + for i := range l.entries { + request := &l.entries[i].Request + u := request.URL + + u = r.ReplaceAllString(u, replace) + request.URL = u + } +} diff --git a/ktest/testharness/golden.go b/ktest/testharness/golden.go new file mode 100644 index 00000000..44ec757b --- /dev/null +++ b/ktest/testharness/golden.go @@ -0,0 +1,60 @@ +package testharness + +import ( + "bytes" + "io/fs" + "os" + "path/filepath" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func RunGoldenTests(t *testing.T, basedir string, fn func(h *Harness, dir string)) { + entries, err := os.ReadDir(basedir) + if err != nil { + t.Fatalf("ReadDir(%q) failed: %v", basedir, err) + } + files := make([]fs.FileInfo, 0, len(entries)) + for _, entry := range entries { + info, err := entry.Info() + if err != nil { + t.Fatalf("failed to get FileInfo %v: %v", info, err) + } + files = append(files, info) + } + count := 0 + for _, file := range files { + name := file.Name() + absPath := filepath.Join(basedir, name) + count++ + t.Run(name, func(t *testing.T) { + h := New(t) + fn(h, absPath) + }) + } + // Likely a typo in basedir (?) + if count == 0 { + t.Errorf("no golden tests found in %q", basedir) + } +} + +func (h *Harness) CompareGoldenFile(p string, got string) { + if os.Getenv("WRITE_GOLDEN_OUTPUT") != "" { + // Short-circuit when the output is correct + b, err := os.ReadFile(p) + if err == nil && bytes.Equal(b, []byte(got)) { + return + } + + if err := os.WriteFile(p, []byte(got), 0644); err != nil { + h.Fatalf("failed to write golden output %s: %v", p, err) + } + h.Errorf("wrote output to %s", p) + } else { + want := string(h.MustReadFile(p)) + if diff := cmp.Diff(want, got); diff != "" { + h.Errorf("unexpected diff in %s: %s", p, diff) + } + } +} diff --git a/ktest/testharness/harness.go b/ktest/testharness/harness.go new file mode 100644 index 00000000..3e49ac3e --- /dev/null +++ b/ktest/testharness/harness.go @@ -0,0 +1,52 @@ +package testharness + +import ( + "os" + "testing" +) + +type Harness struct { + *testing.T +} + +func New(t *testing.T) *Harness { + h := &Harness{T: t} + t.Cleanup(h.Cleanup) + return h +} + +func (h *Harness) Cleanup() { + +} + +func (h *Harness) TempDir() string { + tmpdir, err := os.MkdirTemp("", "test") + if err != nil { + h.Fatalf("failed to make temp directory: %v", err) + } + h.T.Cleanup(func() { + if err := os.RemoveAll(tmpdir); err != nil { + h.Errorf("error cleaning up temp directory %q: %v", tmpdir, err) + } + }) + return tmpdir +} + +func (h *Harness) MustReadFile(p string) []byte { + b, err := os.ReadFile(p) + if err != nil { + h.Fatalf("error from ReadFile(%q): %v", p, err) + } + return b +} + +func (h *Harness) FileExists(p string) bool { + _, err := os.Stat(p) + if err == nil { + return true + } + if !os.IsNotExist(err) { + h.Fatalf("error from os.Stat(%q): %v", p, err) + } + return false +} diff --git a/ktest/testharness/manifest.go b/ktest/testharness/manifest.go new file mode 100644 index 00000000..06734e5f --- /dev/null +++ b/ktest/testharness/manifest.go @@ -0,0 +1,52 @@ +/* +Copyright 2024 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package testharness + +import ( + "bufio" + "context" + "fmt" + "io" + "strings" + + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + k8syaml "k8s.io/apimachinery/pkg/util/yaml" +) + +func ParseObjects(ctx context.Context, manifest string) ([]*unstructured.Unstructured, error) { + var objects []*unstructured.Unstructured + reader := k8syaml.NewYAMLReader(bufio.NewReader(strings.NewReader(manifest))) + for { + raw, err := reader.Read() + if err != nil { + if err == io.EOF { + return objects, nil + } + + return nil, fmt.Errorf("reading YAML doc: %w", err) + } + + u := &unstructured.Unstructured{} + if err := k8syaml.Unmarshal(raw, &u); err != nil { + return nil, fmt.Errorf("parsing object to unstructured: %w", err) + } + + objects = append(objects, u) + } + + return objects, nil +}