Skip to content

Commit 1cd405e

Browse files
committed
chore: Refactor test helpers into reusable ktest library
This avoids a circular (module) dependency between mockkubeapiserver and kdp itself.
1 parent ba3e4bb commit 1cd405e

File tree

14 files changed

+205
-25
lines changed

14 files changed

+205
-25
lines changed

examples/guestbook-operator/controllers/guestbook_controller_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
api "sigs.k8s.io/kubebuilder-declarative-pattern/examples/guestbook-operator/api/v1alpha1"
2323

24-
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/test/golden"
24+
"sigs.k8s.io/kubebuilder-declarative-pattern/ktest/golden"
2525
)
2626

2727
func TestGuestbook(t *testing.T) {

go.work

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ use (
44
.
55
./applylib
66
./examples/guestbook-operator
7+
./ktest
78
./mockkubeapiserver
89
)

ktest/go.mod

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module sigs.k8s.io/kubebuilder-declarative-pattern/ktest
2+
3+
go 1.21
4+
5+
toolchain go1.22.0
6+
7+
require (
8+
github.com/google/go-cmp v0.6.0
9+
k8s.io/apimachinery v0.29.1
10+
k8s.io/klog/v2 v2.110.1
11+
sigs.k8s.io/yaml v1.4.0
12+
)
13+
14+
require (
15+
github.com/go-logr/logr v1.4.1 // indirect
16+
github.com/gogo/protobuf v1.3.2 // indirect
17+
github.com/google/gofuzz v1.2.0 // indirect
18+
github.com/json-iterator/go v1.1.12 // indirect
19+
github.com/kr/text v0.2.0 // indirect
20+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
21+
github.com/modern-go/reflect2 v1.0.2 // indirect
22+
golang.org/x/net v0.19.0 // indirect
23+
golang.org/x/text v0.14.0 // indirect
24+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
25+
gopkg.in/inf.v0 v0.9.1 // indirect
26+
gopkg.in/yaml.v2 v2.4.0 // indirect
27+
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
28+
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
29+
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
30+
)

ktest/go.sum

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
6+
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
7+
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
8+
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
9+
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
10+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
11+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
12+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
13+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
14+
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
15+
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
16+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
17+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
18+
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
19+
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
20+
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
21+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
22+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
23+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
24+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
25+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
26+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
27+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
28+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
29+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
30+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
31+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
32+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
33+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
34+
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
35+
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
36+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
37+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
38+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
39+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
40+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
41+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
42+
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
43+
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
44+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
45+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
46+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
47+
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
48+
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
49+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
50+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
51+
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
52+
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
53+
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
54+
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
55+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
56+
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
57+
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
58+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
59+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
60+
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
61+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
62+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
63+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
64+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
65+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
66+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
67+
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
68+
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
69+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
70+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
71+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
72+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
73+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
74+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
75+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
76+
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
77+
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
78+
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
79+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
80+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
81+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
82+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
83+
k8s.io/apimachinery v0.29.1 h1:KY4/E6km/wLBguvCZv8cKTeOwwOBqFNjwJIdMkMbbRc=
84+
k8s.io/apimachinery v0.29.1/go.mod h1:6HVkd1FwxIagpYrHSwJlQqZI3G9LfYWRPAkUvLnXTKU=
85+
k8s.io/klog/v2 v2.110.1 h1:U/Af64HJf7FcwMcXyKm2RPM22WZzyR7OSpYj5tg3cL0=
86+
k8s.io/klog/v2 v2.110.1/go.mod h1:YGtd1984u+GgbuZ7e08/yBuAfKLSO0+uR1Fhi6ExXjo=
87+
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
88+
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
89+
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
90+
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
91+
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 h1:150L+0vs/8DA78h1u02ooW1/fFq/Lwr+sGiqlzvrtq4=
92+
sigs.k8s.io/structured-merge-diff/v4 v4.4.1/go.mod h1:N8hJocpFajUSSeSJ9bOZ77VzejKZaXsTtZo4/u7Io08=
93+
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
94+
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=

pkg/test/httprecorder/http_recorder.go renamed to ktest/httprecorder/http_recorder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (m *HTTPRecorder) RoundTrip(request *http.Request) (*http.Response, error)
2929
if request.Body != nil {
3030
requestBody, err := io.ReadAll(request.Body)
3131
if err != nil {
32-
panic("failed to read request body")
32+
return nil, fmt.Errorf("HTTPRecorder failed to read request body")
3333
}
3434
entry.Request.Body = string(requestBody)
3535
request.Body = io.NopCloser(bytes.NewReader(requestBody))

pkg/test/httprecorder/request_log.go renamed to ktest/httprecorder/request_log.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"sort"
1111
"strings"
1212
"sync"
13+
"testing"
1314
"time"
1415

1516
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -242,17 +243,19 @@ func (l *RequestLog) RemoveUserAgent() {
242243
l.RemoveHeader("user-agent")
243244
}
244245

245-
func (l *RequestLog) RegexReplaceURL(find string, replace string) {
246+
func (l *RequestLog) RegexReplaceURL(t *testing.T, find string, replace string) {
246247
l.mutex.Lock()
247248
defer l.mutex.Unlock()
248249

250+
r, err := regexp.Compile(find)
251+
if err != nil {
252+
t.Fatalf("failed to compile regex %q: %v", find, err)
253+
}
254+
249255
for i := range l.entries {
250256
request := &l.entries[i].Request
251257
u := request.URL
252-
r, err := regexp.Compile(find)
253-
if err != nil {
254-
klog.Fatalf("failed to compile regex %q: %v", find, err)
255-
}
258+
256259
u = r.ReplaceAllString(u, replace)
257260
request.URL = u
258261
}
File renamed without changes.
File renamed without changes.

ktest/testharness/manifest.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package testharness
18+
19+
import (
20+
"bufio"
21+
"context"
22+
"fmt"
23+
"io"
24+
"strings"
25+
26+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
27+
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
28+
)
29+
30+
func ParseObjects(ctx context.Context, manifest string) ([]*unstructured.Unstructured, error) {
31+
var objects []*unstructured.Unstructured
32+
reader := k8syaml.NewYAMLReader(bufio.NewReader(strings.NewReader(manifest)))
33+
for {
34+
raw, err := reader.Read()
35+
if err != nil {
36+
if err == io.EOF {
37+
return objects, nil
38+
}
39+
40+
return nil, fmt.Errorf("reading YAML doc: %w", err)
41+
}
42+
43+
u := &unstructured.Unstructured{}
44+
if err := k8syaml.Unmarshal(raw, &u); err != nil {
45+
return nil, fmt.Errorf("parsing object to unstructured: %w", err)
46+
}
47+
48+
objects = append(objects, u)
49+
}
50+
51+
return objects, nil
52+
}

mockkubeapiserver/tests/kubeapiserver_test.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ import (
1010
"k8s.io/client-go/dynamic"
1111
"k8s.io/client-go/rest"
1212
"k8s.io/klog/v2"
13+
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
14+
"sigs.k8s.io/kubebuilder-declarative-pattern/ktest/httprecorder"
15+
"sigs.k8s.io/kubebuilder-declarative-pattern/ktest/testharness"
1316
"sigs.k8s.io/kubebuilder-declarative-pattern/mockkubeapiserver"
14-
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/manifest"
15-
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/restmapper"
16-
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/test/httprecorder"
17-
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/test/testharness"
1817
)
1918

2019
func TestGoldenTests(t *testing.T) {
@@ -47,27 +46,28 @@ func TestGoldenTests(t *testing.T) {
4746
WrapTransport: wrapTransport,
4847
}
4948

50-
httpClient := &http.Client{
51-
Transport: wrapTransport(http.DefaultTransport),
49+
httpClient, err := rest.HTTPClientFor(restConfig)
50+
if err != nil {
51+
t.Fatalf("error from rest.HTTPClientFor: %v", err)
5252
}
5353

5454
p := filepath.Join(testdir, "manifest.yaml")
5555
manifestYAML := string(h.MustReadFile(p))
56-
objects, err := manifest.ParseObjects(ctx, manifestYAML)
56+
objects, err := testharness.ParseObjects(ctx, manifestYAML)
5757
if err != nil {
5858
t.Errorf("error parsing manifest %q: %v", p, err)
5959
}
6060

61-
restMapper, err := restmapper.NewForTest(restConfig)
61+
restMapper, err := apiutil.NewDynamicRESTMapper(restConfig, httpClient)
6262
if err != nil {
63-
t.Fatalf("error from controllerrestmapper.NewForTest: %v", err)
63+
t.Fatalf("error from apiutil.NewDynamicRESTMapper: %v", err)
6464
}
6565

6666
dynamicClient, err := dynamic.NewForConfigAndClient(restConfig, httpClient)
6767
if err != nil {
6868
t.Fatalf("building dynamic client: %v", err)
6969
}
70-
for _, obj := range objects.GetItems() {
70+
for _, obj := range objects {
7171
gvk := obj.GroupVersionKind()
7272
restMapping, err := restMapper.RESTMapping(gvk.GroupKind(), gvk.Version)
7373
if err != nil {
@@ -78,7 +78,7 @@ func TestGoldenTests(t *testing.T) {
7878
applyOptions.FieldManager = "test"
7979
resource := dynamicClient.Resource(restMapping.Resource).Namespace(obj.GetNamespace())
8080

81-
if _, err := resource.Apply(ctx, obj.GetName(), obj.UnstructuredObject(), applyOptions); err != nil {
81+
if _, err := resource.Apply(ctx, obj.GetName(), obj, applyOptions); err != nil {
8282
t.Fatalf("error applying resource %v: %v", gvk, err)
8383
}
8484
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import (
1717
"k8s.io/klog/v2"
1818
"sigs.k8s.io/controller-runtime/pkg/client/fake"
1919
"sigs.k8s.io/kubebuilder-declarative-pattern/applylib/applyset"
20+
"sigs.k8s.io/kubebuilder-declarative-pattern/ktest/httprecorder"
21+
"sigs.k8s.io/kubebuilder-declarative-pattern/ktest/testharness"
2022
"sigs.k8s.io/kubebuilder-declarative-pattern/mockkubeapiserver"
2123
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/manifest"
2224
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/restmapper"
23-
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/test/httprecorder"
24-
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/test/testharness"
2525
)
2626

2727
func fakeParent() runtime.Object {

pkg/test/testreconciler/simpletest/controller_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ import (
1515
ctrl "sigs.k8s.io/controller-runtime"
1616

1717
"sigs.k8s.io/kubebuilder-declarative-pattern/commonclient"
18+
"sigs.k8s.io/kubebuilder-declarative-pattern/ktest/httprecorder"
19+
"sigs.k8s.io/kubebuilder-declarative-pattern/ktest/testharness"
1820
"sigs.k8s.io/kubebuilder-declarative-pattern/mockkubeapiserver"
1921
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/loaders"
2022
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative"
2123
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/applier"
2224
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/restmapper"
23-
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/test/httprecorder"
24-
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/test/testharness"
2525

2626
api "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/test/testreconciler/simpletest/v1alpha1"
2727
)
@@ -125,7 +125,7 @@ func testSimpleReconciler(h *testharness.Harness, testdir string, applier applie
125125
requestLog.RemoveUserAgent()
126126
requestLog.SortGETs()
127127
// Workaround for non-determinism in https://github.com/kubernetes/kubernetes/blob/79a62d62350fb600f97d1f6309c3274515b3587a/staging/src/k8s.io/client-go/tools/cache/reflector.go#L301
128-
requestLog.RegexReplaceURL("&timeoutSeconds=.*&", "&timeoutSeconds=<replaced>&")
128+
requestLog.RegexReplaceURL(h.T, "&timeoutSeconds=.*&", "&timeoutSeconds=<replaced>&")
129129
h.Logf("replacing real timestamp in request and response to a fake value")
130130
requestLog.ReplaceTimestamp()
131131

pkg/test/testreconciler/simpletest/with_kubectl_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ package simpletest
66
import (
77
"testing"
88

9+
"sigs.k8s.io/kubebuilder-declarative-pattern/ktest/testharness"
910
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/status"
1011
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/applier"
11-
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/test/testharness"
1212
)
1313

1414
func TestDirectSimpleReconciler(t *testing.T) {

pkg/test/testreconciler/simpletest/without_kubectl_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"testing"
55

66
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+
"sigs.k8s.io/kubebuilder-declarative-pattern/ktest/testharness"
78
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/status"
89
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/applier"
9-
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/test/testharness"
1010
)
1111

1212
func TestSSASimpleReconciler(t *testing.T) {

0 commit comments

Comments
 (0)