Skip to content

Commit 0acdaf8

Browse files
authored
Merge pull request #2131 from DirectXMan12/docs/book-test-client-advice
📖 Fix advice on clients in tests, ensure that book gets tested
2 parents d7c180d + 1de6c3f commit 0acdaf8

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ golangci-lint:
8888
##@ Tests
8989

9090
.PHONY: test
91-
test: test-unit test-integration test-testdata ## Run the unit and integration tests (used in the CI)
91+
test: test-unit test-integration test-testdata test-book ## Run the unit and integration tests (used in the CI)
9292

9393
.PHONY: test-unit
9494
test-unit: ## Run the unit tests
@@ -119,3 +119,7 @@ test-e2e-local: ## Run the end-to-end tests locally
119119
.PHONY: test-e2e-ci
120120
test-e2e-ci: ## Run the end-to-end tests (used in the CI)`
121121
./test/e2e/ci.sh
122+
123+
.PHONY: test-book
124+
test-book: ## Run the cronjob tutorial's unit tests to make sure we don't break it
125+
cd ./docs/book/src/cronjob-tutorial/testdata/project && make test

docs/book/src/cronjob-tutorial/testdata/project/controllers/suite_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,16 @@ var _ = BeforeSuite(func() {
115115
The only difference is that the manager is started in a separate goroutine so it does not block the cleanup of envtest
116116
when you’re done running your tests.
117117
118-
It is not recommended to use the manager client in tests because it is not strongly consistent. Indeed, the manager
119-
client is designed to do the "right thing" for controllers by default which is to read from caches. The best solution
120-
is to instantiate a new client using client.New for tests (as k8sClient above). It will provide a client that reads
121-
directly from the API meaning that you can write tests expecting read-after-write consistency.
122-
123-
However, keep in mind that you should not do this in the controller's conciliation loop (read an object after you have
124-
written it). Kubernetes favors an approach where you first do some reads, process and then do some writes and return.
125-
This way, you let the queue take care of the next cycle of readings if they are necessary.
118+
Note that we set up both a "live" k8s client, separate from the manager. This is because when making assertions in
119+
tests, you generally want to assert against the live state of the API server. If you used the client from the
120+
manager (`k8sManager.GetClient`), you'd end up asserting against the contents of the cache instead, which is slower
121+
and can introduce flakiness into your tests. We could use the manager's `APIReader` to accomplish the same thing,
122+
but that would leave us with two clients in our test assertions and setup (one for reading, one for writing), and
123+
it'd be easy to make mistakes.
124+
125+
Note that we keep the reconciler running against the manager's cache client, though -- we want our controller to
126+
behave as it would in production, and we use features of the cache (like indicies) in our controller which aren't
127+
available when talking directly to the API server.
126128
*/
127129

128130
k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{

test.sh

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
source test/common.sh
18-
19-
header_text "Running kubebuilder unit tests"
20-
go test -race -v ./pkg/...
21-
22-
./test/integration.sh
23-
24-
./test/testdata/test.sh
17+
# prow calls this file currently, but we can just use `make test` to test
18+
# the set of things we want.
19+
make test

0 commit comments

Comments
 (0)